Re: [PATCH v2] kunit: Cover 'assert.c' with tests

kernel test robot
2024-05-09T07:45:09Z

[also build test WARNING on shuah-kselftest/kunit-fixes linus/master v6.9-rc7 next-20240508]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Ivan-Orlov/kunit-Cover-assert-c-with-tests/20240508-212654
base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git kunit
patch link: https://lore.kernel.org/r/20240508132557.599213-1-ivan.orlov0322%40gmail.com
patch subject: [PATCH v2] kunit: Cover 'assert.c' with tests
config: i386-randconfig-004-20240509 (https://download.01.org/0day-ci/archive/20240509/202405091540.XkfmDsAL-lkp@intel.com/config)
compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240509/202405091540.XkfmDsAL-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202405091540.XkfmDsAL-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> lib/kunit/assert.c:35:6: warning: no previous prototype for 'kunit_assert_print_msg' [-Wmissing-prototypes]
35 | void kunit_assert_print_msg(const struct va_format *message,
| ^~~~~~~~~~~~~~~~~~~~~~
>> lib/kunit/assert.c:95:23: warning: no previous prototype for 'is_literal' [-Wmissing-prototypes]
95 | VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
| ^~~~~~~~~~
>> lib/kunit/assert.c:173:23: warning: no previous prototype for 'is_str_literal' [-Wmissing-prototypes]
173 | VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value)
| ^~~~~~~~~~~~~~
>> lib/kunit/assert.c:217:6: warning: no previous prototype for 'kunit_assert_hexdump' [-Wmissing-prototypes]
217 | void kunit_assert_hexdump(struct string_stream *stream,
| ^~~~~~~~~~~~~~~~~~~~

vim +/kunit_assert_print_msg +35 lib/kunit/assert.c

33
34 VISIBLE_IF_KUNIT
> 35 void kunit_assert_print_msg(const struct va_format *message,
36 struct string_stream *stream)
37 {
38 if (message->fmt)
39 string_stream_add(stream, "\n%pV", message);
40 }
41 EXPORT_SYMBOL_IF_KUNIT(kunit_assert_print_msg);
42
43 void kunit_fail_assert_format(const struct kunit_assert *assert,
44 const struct va_format *message,
45 struct string_stream *stream)
46 {
47 string_stream_add(stream, "%pV", message);
48 }
49 EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
50
51 void kunit_unary_assert_format(const struct kunit_assert *assert,
52 const struct va_format *message,
53 struct string_stream *stream)
54 {
55 struct kunit_unary_assert *unary_assert;
56
57 unary_assert = container_of(assert, struct kunit_unary_assert, assert);
58
59 if (unary_assert->expected_true)
60 string_stream_add(stream,
61 KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n",
62 unary_assert->condition);
63 else
64 string_stream_add(stream,
65 KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
66 unary_assert->condition);
67 kunit_assert_print_msg(message, stream);
68 }
69 EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
70
71 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
72 const struct va_format *message,
73 struct string_stream *stream)
74 {
75 struct kunit_ptr_not_err_assert *ptr_assert;
76
77 ptr_assert = container_of(assert, struct kunit_ptr_not_err_assert,
78 assert);
79
80 if (!ptr_assert->value) {
81 string_stream_add(stream,
82 KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n",
83 ptr_assert->text);
84 } else if (IS_ERR(ptr_assert->value)) {
85 string_stream_add(stream,
86 KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
87 ptr_assert->text,
88 PTR_ERR(ptr_assert->value));
89 }
90 kunit_assert_print_msg(message, stream);
91 }
92 EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
93
94 /* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */
> 95 VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
96 {
97 char *buffer;
98 int len;
99 bool ret;
100
101 len = snprintf(NULL, 0, "%lld", value);
102 if (strlen(text) != len)
103 return false;
104
105 buffer = kmalloc(len+1, GFP_KERNEL);
106 if (!buffer)
107 return false;
108
109 snprintf(buffer, len+1, "%lld", value);
110 ret = strncmp(buffer, text, len) == 0;
111
112 kfree(buffer);
113
114 return ret;
115 }
116 EXPORT_SYMBOL_IF_KUNIT(is_literal);
117
118 void kunit_binary_assert_format(const struct kunit_assert *assert,
119 const struct va_format *message,
120 struct string_stream *stream)
121 {
122 struct kunit_binary_assert *binary_assert;
123
124 binary_assert = container_of(assert, struct kunit_binary_assert,
125 assert);
126
127 string_stream_add(stream,
128 KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
129 binary_assert->text->left_text,
130 binary_assert->text->operation,
131 binary_assert->text->right_text);
132 if (!is_literal(binary_assert->text->left_text, binary_assert->left_value))
133 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n",
134 binary_assert->text->left_text,
135 binary_assert->left_value,
136 binary_assert->left_value);
137 if (!is_literal(binary_assert->text->right_text, binary_assert->right_value))
138 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)",
139 binary_assert->text->right_text,
140 binary_assert->right_value,
141 binary_assert->right_value);
142 kunit_assert_print_msg(message, stream);
143 }
144 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
145
146 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
147 const struct va_format *message,
148 struct string_stream *stream)
149 {
150 struct kunit_binary_ptr_assert *binary_assert;
151
152 binary_assert = container_of(assert, struct kunit_binary_ptr_assert,
153 assert);
154
155 string_stream_add(stream,
156 KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
157 binary_assert->text->left_text,
158 binary_assert->text->operation,
159 binary_assert->text->right_text);
160 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
161 binary_assert->text->left_text,
162 binary_assert->left_value);
163 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
164 binary_assert->text->right_text,
165 binary_assert->right_value);
166 kunit_assert_print_msg(message, stream);
167 }
168 EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
169
170 /* Checks if KUNIT_EXPECT_STREQ() args were string literals.
171 * Note: `text` will have ""s where as `value` will not.
172 */
> 173 VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value)
174 {
175 int len;
176
177 len = strlen(text);
178 if (len < 2)
179 return false;
180 if (text[0] != '\"' || text[len - 1] != '\"')
181 return false;
182
183 return strncmp(text + 1, value, len - 2) == 0;
184 }
185 EXPORT_SYMBOL_IF_KUNIT(is_str_literal);
186
187 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
188 const struct va_format *message,
189 struct string_stream *stream)
190 {
191 struct kunit_binary_str_assert *binary_assert;
192
193 binary_assert = container_of(assert, struct kunit_binary_str_assert,
194 assert);
195
196 string_stream_add(stream,
197 KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
198 binary_assert->text->left_text,
199 binary_assert->text->operation,
200 binary_assert->text->right_text);
201 if (!is_str_literal(binary_assert->text->left_text, binary_assert->left_value))
202 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"\n",
203 binary_assert->text->left_text,
204 binary_assert->left_value);
205 if (!is_str_literal(binary_assert->text->right_text, binary_assert->right_value))
206 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"",
207 binary_assert->text->right_text,
208 binary_assert->right_value);
209 kunit_assert_print_msg(message, stream);
210 }
211 EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
212
213 /* Adds a hexdump of a buffer to a string_stream comparing it with
214 * a second buffer. The different bytes are marked with <>.
215 */
216 VISIBLE_IF_KUNIT
> 217 void kunit_assert_hexdump(struct string_stream *stream,
218 const void *buf,
219 const void *compared_buf,
220 const size_t len)
221 {
222 size_t i;
223 const u8 *buf1 = buf;
224 const u8 *buf2 = compared_buf;
225
226 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT);
227
228 for (i = 0; i < len; ++i) {
229 if (!(i % 16) && i)
230 string_stream_add(stream, "\n" KUNIT_SUBSUBTEST_INDENT);
231
232 if (buf1[i] != buf2[i])
233 string_stream_add(stream, "<%02x>", buf1[i]);
234 else
235 string_stream_add(stream, " %02x ", buf1[i]);
236 }
237 }
238 EXPORT_SYMBOL_IF_KUNIT(kunit_assert_hexdump);
239