Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Test cases for printf facility.
  3 */
  4
  5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6
  7#include <linux/init.h>
  8#include <linux/kernel.h>
  9#include <linux/module.h>
 10#include <linux/printk.h>
 11#include <linux/random.h>
 
 12#include <linux/slab.h>
 13#include <linux/string.h>
 14
 15#include <linux/bitmap.h>
 16#include <linux/dcache.h>
 17#include <linux/socket.h>
 18#include <linux/in.h>
 19
 20#include <linux/gfp.h>
 21#include <linux/mm.h>
 22
 
 
 23#define BUF_SIZE 256
 24#define PAD_SIZE 16
 25#define FILL_CHAR '$'
 26
 27#define PTR1 ((void*)0x01234567)
 28#define PTR2 ((void*)(long)(int)0xfedcba98)
 29
 30#if BITS_PER_LONG == 64
 31#define PTR1_ZEROES "000000000"
 32#define PTR1_SPACES "         "
 33#define PTR1_STR "1234567"
 34#define PTR2_STR "fffffffffedcba98"
 35#define PTR_WIDTH 16
 36#else
 37#define PTR1_ZEROES "0"
 38#define PTR1_SPACES " "
 39#define PTR1_STR "1234567"
 40#define PTR2_STR "fedcba98"
 41#define PTR_WIDTH 8
 42#endif
 43#define PTR_WIDTH_STR stringify(PTR_WIDTH)
 44
 45static unsigned total_tests __initdata;
 46static unsigned failed_tests __initdata;
 47static char *test_buffer __initdata;
 48static char *alloced_buffer __initdata;
 49
 50static int __printf(4, 0) __init
 51do_test(int bufsize, const char *expect, int elen,
 52	const char *fmt, va_list ap)
 53{
 54	va_list aq;
 55	int ret, written;
 56
 57	total_tests++;
 58
 59	memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
 60	va_copy(aq, ap);
 61	ret = vsnprintf(test_buffer, bufsize, fmt, aq);
 62	va_end(aq);
 63
 64	if (ret != elen) {
 65		pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
 66			bufsize, fmt, ret, elen);
 67		return 1;
 68	}
 69
 70	if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
 71		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
 72		return 1;
 73	}
 74
 75	if (!bufsize) {
 76		if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
 77			pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
 78				fmt);
 79			return 1;
 80		}
 81		return 0;
 82	}
 83
 84	written = min(bufsize-1, elen);
 85	if (test_buffer[written]) {
 86		pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
 87			bufsize, fmt);
 88		return 1;
 89	}
 90
 91	if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
 92		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
 93			bufsize, fmt);
 94		return 1;
 95	}
 96
 97	if (memcmp(test_buffer, expect, written)) {
 98		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
 99			bufsize, fmt, test_buffer, written, expect);
100		return 1;
101	}
102	return 0;
103}
104
105static void __printf(3, 4) __init
106__test(const char *expect, int elen, const char *fmt, ...)
107{
108	va_list ap;
109	int rand;
110	char *p;
111
112	if (elen >= BUF_SIZE) {
113		pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
114		       elen, fmt);
115		failed_tests++;
116		return;
117	}
118
119	va_start(ap, fmt);
120
121	/*
122	 * Every fmt+args is subjected to four tests: Three where we
123	 * tell vsnprintf varying buffer sizes (plenty, not quite
124	 * enough and 0), and then we also test that kvasprintf would
125	 * be able to print it as expected.
126	 */
127	failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
128	rand = 1 + prandom_u32_max(elen+1);
129	/* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
130	failed_tests += do_test(rand, expect, elen, fmt, ap);
131	failed_tests += do_test(0, expect, elen, fmt, ap);
132
133	p = kvasprintf(GFP_KERNEL, fmt, ap);
134	if (p) {
135		total_tests++;
136		if (memcmp(p, expect, elen+1)) {
137			pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
138				fmt, p, expect);
139			failed_tests++;
140		}
141		kfree(p);
142	}
143	va_end(ap);
144}
145
146#define test(expect, fmt, ...)					\
147	__test(expect, strlen(expect), fmt, ##__VA_ARGS__)
148
149static void __init
150test_basic(void)
151{
152	/* Work around annoying "warning: zero-length gnu_printf format string". */
153	char nul = '\0';
154
155	test("", &nul);
156	test("100%", "100%%");
157	test("xxx%yyy", "xxx%cyyy", '%');
158	__test("xxx\0yyy", 7, "xxx%cyyy", '\0');
159}
160
161static void __init
162test_number(void)
163{
164	test("0x1234abcd  ", "%#-12x", 0x1234abcd);
165	test("  0x1234abcd", "%#12x", 0x1234abcd);
166	test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
167	test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
168	test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
169	test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
170	/*
171	 * POSIX/C99: »The result of converting zero with an explicit
172	 * precision of zero shall be no characters.« Hence the output
173	 * from the below test should really be "00|0||| ". However,
174	 * the kernel's printf also produces a single 0 in that
175	 * case. This test case simply documents the current
176	 * behaviour.
177	 */
178	test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
179#ifndef __CHAR_UNSIGNED__
180	{
181		/*
182		 * Passing a 'char' to a %02x specifier doesn't do
183		 * what was presumably the intention when char is
184		 * signed and the value is negative. One must either &
185		 * with 0xff or cast to u8.
186		 */
187		char val = -16;
188		test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
189	}
190#endif
191}
192
193static void __init
194test_string(void)
195{
196	test("", "%s%.0s", "", "123");
197	test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
198	test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
199	test("1234      ", "%-10.4s", "123456");
200	test("      1234", "%10.4s", "123456");
201	/*
202	 * POSIX and C99 say that a negative precision (which is only
203	 * possible to pass via a * argument) should be treated as if
204	 * the precision wasn't present, and that if the precision is
205	 * omitted (as in %.s), the precision should be taken to be
206	 * 0. However, the kernel's printf behave exactly opposite,
207	 * treating a negative precision as 0 and treating an omitted
208	 * precision specifier as if no precision was given.
209	 *
210	 * These test cases document the current behaviour; should
211	 * anyone ever feel the need to follow the standards more
212	 * closely, this can be revisited.
213	 */
214	test("    ", "%4.*s", -5, "123456");
215	test("123456", "%.s", "123456");
216	test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
217	test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
218}
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220static void __init
221plain(void)
222{
223	test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
224	/*
225	 * The field width is overloaded for some %p extensions to
226	 * pass another piece of information. For plain pointers, the
227	 * behaviour is slightly odd: One cannot pass either the 0
228	 * flag nor a precision to %p without gcc complaining, and if
229	 * one explicitly gives a field width, the number is no longer
230	 * zero-padded.
231	 */
232	test("|" PTR1_STR PTR1_SPACES "  |  " PTR1_SPACES PTR1_STR "|",
233	     "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
234	test("|" PTR2_STR "  |  " PTR2_STR "|",
235	     "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
 
 
 
 
 
 
 
 
236
237	/*
238	 * Unrecognized %p extensions are treated as plain %p, but the
239	 * alphanumeric suffix is ignored (that is, does not occur in
240	 * the output.)
241	 */
242	test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
243	test("|"PTR2_STR"|", "|%p0y|", PTR2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244}
245
246static void __init
247symbol_ptr(void)
248{
249}
250
251static void __init
252kernel_ptr(void)
253{
 
254}
255
256static void __init
257struct_resource(void)
258{
259}
260
261static void __init
262addr(void)
263{
264}
265
266static void __init
267escaped_str(void)
268{
269}
270
271static void __init
272hex_string(void)
273{
274	const char buf[3] = {0xc0, 0xff, 0xee};
275
276	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
277	     "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
278	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
279	     "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
280}
281
282static void __init
283mac(void)
284{
285	const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
286
287	test("2d:48:d6:fc:7a:05", "%pM", addr);
288	test("05:7a:fc:d6:48:2d", "%pMR", addr);
289	test("2d-48-d6-fc-7a-05", "%pMF", addr);
290	test("2d48d6fc7a05", "%pm", addr);
291	test("057afcd6482d", "%pmR", addr);
292}
293
294static void __init
295ip4(void)
296{
297	struct sockaddr_in sa;
298
299	sa.sin_family = AF_INET;
300	sa.sin_port = cpu_to_be16(12345);
301	sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
302
303	test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
304	test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
305	sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
306	test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
307}
308
309static void __init
310ip6(void)
311{
312}
313
314static void __init
315ip(void)
316{
317	ip4();
318	ip6();
319}
320
321static void __init
322uuid(void)
323{
324	const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
325			       0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
326
327	test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
328	test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
329	test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
330	test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
331}
332
333static struct dentry test_dentry[4] __initdata = {
334	{ .d_parent = &test_dentry[0],
335	  .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
336	  .d_iname = "foo" },
337	{ .d_parent = &test_dentry[0],
338	  .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
339	  .d_iname = "bravo" },
340	{ .d_parent = &test_dentry[1],
341	  .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
342	  .d_iname = "alfa" },
343	{ .d_parent = &test_dentry[2],
344	  .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
345	  .d_iname = "romeo" },
346};
347
348static void __init
349dentry(void)
350{
351	test("foo", "%pd", &test_dentry[0]);
352	test("foo", "%pd2", &test_dentry[0]);
353
 
 
 
 
 
354	test("romeo", "%pd", &test_dentry[3]);
355	test("alfa/romeo", "%pd2", &test_dentry[3]);
356	test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
357	test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
358	test("/bravo/alfa", "%pd4", &test_dentry[2]);
359
360	test("bravo/alfa  |bravo/alfa  ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
361	test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
362}
363
364static void __init
365struct_va_format(void)
366{
367}
368
369static void __init
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370struct_clk(void)
371{
372}
373
374static void __init
375large_bitmap(void)
376{
377	const int nbits = 1 << 16;
378	unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
379	if (!bits)
380		return;
381
382	bitmap_set(bits, 1, 20);
383	bitmap_set(bits, 60000, 15);
384	test("1-20,60000-60014", "%*pbl", nbits, bits);
385	kfree(bits);
386}
387
388static void __init
389bitmap(void)
390{
391	DECLARE_BITMAP(bits, 20);
392	const int primes[] = {2,3,5,7,11,13,17,19};
393	int i;
394
395	bitmap_zero(bits, 20);
396	test("00000|00000", "%20pb|%*pb", bits, 20, bits);
397	test("|", "%20pbl|%*pbl", bits, 20, bits);
398
399	for (i = 0; i < ARRAY_SIZE(primes); ++i)
400		set_bit(primes[i], bits);
401	test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
402	test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
403
404	bitmap_fill(bits, 20);
405	test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
406	test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
407
408	large_bitmap();
409}
410
411static void __init
412netdev_features(void)
413{
414}
415
416static void __init
417flags(void)
418{
419	unsigned long flags;
420	gfp_t gfp;
421	char *cmp_buffer;
422
423	flags = 0;
424	test("", "%pGp", &flags);
425
426	/* Page flags should filter the zone id */
427	flags = 1UL << NR_PAGEFLAGS;
428	test("", "%pGp", &flags);
429
430	flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
431		| 1UL << PG_active | 1UL << PG_swapbacked;
432	test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
433
434
435	flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
436			| VM_DENYWRITE;
437	test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
438
439	gfp = GFP_TRANSHUGE;
440	test("GFP_TRANSHUGE", "%pGg", &gfp);
441
442	gfp = GFP_ATOMIC|__GFP_DMA;
443	test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
444
445	gfp = __GFP_ATOMIC;
446	test("__GFP_ATOMIC", "%pGg", &gfp);
447
448	cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
449	if (!cmp_buffer)
450		return;
451
452	/* Any flags not translated by the table should remain numeric */
453	gfp = ~__GFP_BITS_MASK;
454	snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
455	test(cmp_buffer, "%pGg", &gfp);
456
457	snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
458							(unsigned long) gfp);
459	gfp |= __GFP_ATOMIC;
460	test(cmp_buffer, "%pGg", &gfp);
461
462	kfree(cmp_buffer);
463}
464
465static void __init
466test_pointer(void)
467{
468	plain();
 
 
469	symbol_ptr();
470	kernel_ptr();
471	struct_resource();
472	addr();
473	escaped_str();
474	hex_string();
475	mac();
476	ip();
477	uuid();
478	dentry();
479	struct_va_format();
 
480	struct_clk();
481	bitmap();
482	netdev_features();
483	flags();
484}
485
486static int __init
487test_printf_init(void)
488{
489	alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
490	if (!alloced_buffer)
491		return -ENOMEM;
492	test_buffer = alloced_buffer + PAD_SIZE;
493
494	test_basic();
495	test_number();
496	test_string();
497	test_pointer();
498
499	kfree(alloced_buffer);
500
501	if (failed_tests == 0)
502		pr_info("all %u tests passed\n", total_tests);
503	else
504		pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
505
506	return failed_tests ? -EINVAL : 0;
507}
508
509module_init(test_printf_init);
510
511MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
512MODULE_LICENSE("GPL");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Test cases for printf facility.
  4 */
  5
  6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7
  8#include <linux/init.h>
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/printk.h>
 12#include <linux/random.h>
 13#include <linux/rtc.h>
 14#include <linux/slab.h>
 15#include <linux/string.h>
 16
 17#include <linux/bitmap.h>
 18#include <linux/dcache.h>
 19#include <linux/socket.h>
 20#include <linux/in.h>
 21
 22#include <linux/gfp.h>
 23#include <linux/mm.h>
 24
 25#include "../tools/testing/selftests/kselftest_module.h"
 26
 27#define BUF_SIZE 256
 28#define PAD_SIZE 16
 29#define FILL_CHAR '$'
 30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 31static unsigned total_tests __initdata;
 32static unsigned failed_tests __initdata;
 33static char *test_buffer __initdata;
 34static char *alloced_buffer __initdata;
 35
 36static int __printf(4, 0) __init
 37do_test(int bufsize, const char *expect, int elen,
 38	const char *fmt, va_list ap)
 39{
 40	va_list aq;
 41	int ret, written;
 42
 43	total_tests++;
 44
 45	memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
 46	va_copy(aq, ap);
 47	ret = vsnprintf(test_buffer, bufsize, fmt, aq);
 48	va_end(aq);
 49
 50	if (ret != elen) {
 51		pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
 52			bufsize, fmt, ret, elen);
 53		return 1;
 54	}
 55
 56	if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
 57		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
 58		return 1;
 59	}
 60
 61	if (!bufsize) {
 62		if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
 63			pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
 64				fmt);
 65			return 1;
 66		}
 67		return 0;
 68	}
 69
 70	written = min(bufsize-1, elen);
 71	if (test_buffer[written]) {
 72		pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
 73			bufsize, fmt);
 74		return 1;
 75	}
 76
 77	if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
 78		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
 79			bufsize, fmt);
 80		return 1;
 81	}
 82
 83	if (memcmp(test_buffer, expect, written)) {
 84		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
 85			bufsize, fmt, test_buffer, written, expect);
 86		return 1;
 87	}
 88	return 0;
 89}
 90
 91static void __printf(3, 4) __init
 92__test(const char *expect, int elen, const char *fmt, ...)
 93{
 94	va_list ap;
 95	int rand;
 96	char *p;
 97
 98	if (elen >= BUF_SIZE) {
 99		pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
100		       elen, fmt);
101		failed_tests++;
102		return;
103	}
104
105	va_start(ap, fmt);
106
107	/*
108	 * Every fmt+args is subjected to four tests: Three where we
109	 * tell vsnprintf varying buffer sizes (plenty, not quite
110	 * enough and 0), and then we also test that kvasprintf would
111	 * be able to print it as expected.
112	 */
113	failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
114	rand = 1 + prandom_u32_max(elen+1);
115	/* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
116	failed_tests += do_test(rand, expect, elen, fmt, ap);
117	failed_tests += do_test(0, expect, elen, fmt, ap);
118
119	p = kvasprintf(GFP_KERNEL, fmt, ap);
120	if (p) {
121		total_tests++;
122		if (memcmp(p, expect, elen+1)) {
123			pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
124				fmt, p, expect);
125			failed_tests++;
126		}
127		kfree(p);
128	}
129	va_end(ap);
130}
131
132#define test(expect, fmt, ...)					\
133	__test(expect, strlen(expect), fmt, ##__VA_ARGS__)
134
135static void __init
136test_basic(void)
137{
138	/* Work around annoying "warning: zero-length gnu_printf format string". */
139	char nul = '\0';
140
141	test("", &nul);
142	test("100%", "100%%");
143	test("xxx%yyy", "xxx%cyyy", '%');
144	__test("xxx\0yyy", 7, "xxx%cyyy", '\0');
145}
146
147static void __init
148test_number(void)
149{
150	test("0x1234abcd  ", "%#-12x", 0x1234abcd);
151	test("  0x1234abcd", "%#12x", 0x1234abcd);
152	test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
153	test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
154	test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
155	test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
156	/*
157	 * POSIX/C99: »The result of converting zero with an explicit
158	 * precision of zero shall be no characters.« Hence the output
159	 * from the below test should really be "00|0||| ". However,
160	 * the kernel's printf also produces a single 0 in that
161	 * case. This test case simply documents the current
162	 * behaviour.
163	 */
164	test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
165#ifndef __CHAR_UNSIGNED__
166	{
167		/*
168		 * Passing a 'char' to a %02x specifier doesn't do
169		 * what was presumably the intention when char is
170		 * signed and the value is negative. One must either &
171		 * with 0xff or cast to u8.
172		 */
173		char val = -16;
174		test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
175	}
176#endif
177}
178
179static void __init
180test_string(void)
181{
182	test("", "%s%.0s", "", "123");
183	test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
184	test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
185	test("1234      ", "%-10.4s", "123456");
186	test("      1234", "%10.4s", "123456");
187	/*
188	 * POSIX and C99 say that a negative precision (which is only
189	 * possible to pass via a * argument) should be treated as if
190	 * the precision wasn't present, and that if the precision is
191	 * omitted (as in %.s), the precision should be taken to be
192	 * 0. However, the kernel's printf behave exactly opposite,
193	 * treating a negative precision as 0 and treating an omitted
194	 * precision specifier as if no precision was given.
195	 *
196	 * These test cases document the current behaviour; should
197	 * anyone ever feel the need to follow the standards more
198	 * closely, this can be revisited.
199	 */
200	test("    ", "%4.*s", -5, "123456");
201	test("123456", "%.s", "123456");
202	test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
203	test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
204}
205
206#define PLAIN_BUF_SIZE 64	/* leave some space so we don't oops */
207
208#if BITS_PER_LONG == 64
209
210#define PTR_WIDTH 16
211#define PTR ((void *)0xffff0123456789abUL)
212#define PTR_STR "ffff0123456789ab"
213#define PTR_VAL_NO_CRNG "(____ptrval____)"
214#define ZEROS "00000000"	/* hex 32 zero bits */
215
216static int __init
217plain_format(void)
218{
219	char buf[PLAIN_BUF_SIZE];
220	int nchars;
221
222	nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
223
224	if (nchars != PTR_WIDTH)
225		return -1;
226
227	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
228		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
229			PTR_VAL_NO_CRNG);
230		return 0;
231	}
232
233	if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
234		return -1;
235
236	return 0;
237}
238
239#else
240
241#define PTR_WIDTH 8
242#define PTR ((void *)0x456789ab)
243#define PTR_STR "456789ab"
244#define PTR_VAL_NO_CRNG "(ptrval)"
245#define ZEROS ""
246
247static int __init
248plain_format(void)
249{
250	/* Format is implicitly tested for 32 bit machines by plain_hash() */
251	return 0;
252}
253
254#endif	/* BITS_PER_LONG == 64 */
255
256static int __init
257plain_hash_to_buffer(const void *p, char *buf, size_t len)
258{
259	int nchars;
260
261	nchars = snprintf(buf, len, "%p", p);
262
263	if (nchars != PTR_WIDTH)
264		return -1;
265
266	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
267		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
268			PTR_VAL_NO_CRNG);
269		return 0;
270	}
271
272	return 0;
273}
274
275static int __init
276plain_hash(void)
277{
278	char buf[PLAIN_BUF_SIZE];
279	int ret;
280
281	ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
282	if (ret)
283		return ret;
284
285	if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
286		return -1;
287
288	return 0;
289}
290
291/*
292 * We can't use test() to test %p because we don't know what output to expect
293 * after an address is hashed.
294 */
295static void __init
296plain(void)
297{
298	int err;
299
300	err = plain_hash();
301	if (err) {
302		pr_warn("plain 'p' does not appear to be hashed\n");
303		failed_tests++;
304		return;
305	}
306
307	err = plain_format();
308	if (err) {
309		pr_warn("hashing plain 'p' has unexpected format\n");
310		failed_tests++;
311	}
312}
313
314static void __init
315test_hashed(const char *fmt, const void *p)
316{
317	char buf[PLAIN_BUF_SIZE];
318	int ret;
319
320	/*
321	 * No need to increase failed test counter since this is assumed
322	 * to be called after plain().
 
323	 */
324	ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
325	if (ret)
326		return;
327
328	test(buf, fmt, p);
329}
330
331static void __init
332null_pointer(void)
333{
334	test_hashed("%p", NULL);
335	test(ZEROS "00000000", "%px", NULL);
336	test("(null)", "%pE", NULL);
337}
338
339#define PTR_INVALID ((void *)0x000000ab)
340
341static void __init
342invalid_pointer(void)
343{
344	test_hashed("%p", PTR_INVALID);
345	test(ZEROS "000000ab", "%px", PTR_INVALID);
346	test("(efault)", "%pE", PTR_INVALID);
347}
348
349static void __init
350symbol_ptr(void)
351{
352}
353
354static void __init
355kernel_ptr(void)
356{
357	/* We can't test this without access to kptr_restrict. */
358}
359
360static void __init
361struct_resource(void)
362{
363}
364
365static void __init
366addr(void)
367{
368}
369
370static void __init
371escaped_str(void)
372{
373}
374
375static void __init
376hex_string(void)
377{
378	const char buf[3] = {0xc0, 0xff, 0xee};
379
380	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
381	     "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
382	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
383	     "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
384}
385
386static void __init
387mac(void)
388{
389	const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
390
391	test("2d:48:d6:fc:7a:05", "%pM", addr);
392	test("05:7a:fc:d6:48:2d", "%pMR", addr);
393	test("2d-48-d6-fc-7a-05", "%pMF", addr);
394	test("2d48d6fc7a05", "%pm", addr);
395	test("057afcd6482d", "%pmR", addr);
396}
397
398static void __init
399ip4(void)
400{
401	struct sockaddr_in sa;
402
403	sa.sin_family = AF_INET;
404	sa.sin_port = cpu_to_be16(12345);
405	sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
406
407	test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
408	test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
409	sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
410	test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
411}
412
413static void __init
414ip6(void)
415{
416}
417
418static void __init
419ip(void)
420{
421	ip4();
422	ip6();
423}
424
425static void __init
426uuid(void)
427{
428	const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
429			       0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
430
431	test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
432	test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
433	test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
434	test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
435}
436
437static struct dentry test_dentry[4] __initdata = {
438	{ .d_parent = &test_dentry[0],
439	  .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
440	  .d_iname = "foo" },
441	{ .d_parent = &test_dentry[0],
442	  .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
443	  .d_iname = "bravo" },
444	{ .d_parent = &test_dentry[1],
445	  .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
446	  .d_iname = "alfa" },
447	{ .d_parent = &test_dentry[2],
448	  .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
449	  .d_iname = "romeo" },
450};
451
452static void __init
453dentry(void)
454{
455	test("foo", "%pd", &test_dentry[0]);
456	test("foo", "%pd2", &test_dentry[0]);
457
458	test("(null)", "%pd", NULL);
459	test("(efault)", "%pd", PTR_INVALID);
460	test("(null)", "%pD", NULL);
461	test("(efault)", "%pD", PTR_INVALID);
462
463	test("romeo", "%pd", &test_dentry[3]);
464	test("alfa/romeo", "%pd2", &test_dentry[3]);
465	test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
466	test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
467	test("/bravo/alfa", "%pd4", &test_dentry[2]);
468
469	test("bravo/alfa  |bravo/alfa  ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
470	test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
471}
472
473static void __init
474struct_va_format(void)
475{
476}
477
478static void __init
479struct_rtc_time(void)
480{
481	/* 1543210543 */
482	const struct rtc_time tm = {
483		.tm_sec = 43,
484		.tm_min = 35,
485		.tm_hour = 5,
486		.tm_mday = 26,
487		.tm_mon = 10,
488		.tm_year = 118,
489	};
490
491	test("(%ptR?)", "%pt", &tm);
492	test("2018-11-26T05:35:43", "%ptR", &tm);
493	test("0118-10-26T05:35:43", "%ptRr", &tm);
494	test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
495	test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
496	test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
497	test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
498}
499
500static void __init
501struct_clk(void)
502{
503}
504
505static void __init
506large_bitmap(void)
507{
508	const int nbits = 1 << 16;
509	unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
510	if (!bits)
511		return;
512
513	bitmap_set(bits, 1, 20);
514	bitmap_set(bits, 60000, 15);
515	test("1-20,60000-60014", "%*pbl", nbits, bits);
516	bitmap_free(bits);
517}
518
519static void __init
520bitmap(void)
521{
522	DECLARE_BITMAP(bits, 20);
523	const int primes[] = {2,3,5,7,11,13,17,19};
524	int i;
525
526	bitmap_zero(bits, 20);
527	test("00000|00000", "%20pb|%*pb", bits, 20, bits);
528	test("|", "%20pbl|%*pbl", bits, 20, bits);
529
530	for (i = 0; i < ARRAY_SIZE(primes); ++i)
531		set_bit(primes[i], bits);
532	test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
533	test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
534
535	bitmap_fill(bits, 20);
536	test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
537	test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
538
539	large_bitmap();
540}
541
542static void __init
543netdev_features(void)
544{
545}
546
547static void __init
548flags(void)
549{
550	unsigned long flags;
551	gfp_t gfp;
552	char *cmp_buffer;
553
554	flags = 0;
555	test("", "%pGp", &flags);
556
557	/* Page flags should filter the zone id */
558	flags = 1UL << NR_PAGEFLAGS;
559	test("", "%pGp", &flags);
560
561	flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
562		| 1UL << PG_active | 1UL << PG_swapbacked;
563	test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
564
565
566	flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
567			| VM_DENYWRITE;
568	test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
569
570	gfp = GFP_TRANSHUGE;
571	test("GFP_TRANSHUGE", "%pGg", &gfp);
572
573	gfp = GFP_ATOMIC|__GFP_DMA;
574	test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
575
576	gfp = __GFP_ATOMIC;
577	test("__GFP_ATOMIC", "%pGg", &gfp);
578
579	cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
580	if (!cmp_buffer)
581		return;
582
583	/* Any flags not translated by the table should remain numeric */
584	gfp = ~__GFP_BITS_MASK;
585	snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
586	test(cmp_buffer, "%pGg", &gfp);
587
588	snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
589							(unsigned long) gfp);
590	gfp |= __GFP_ATOMIC;
591	test(cmp_buffer, "%pGg", &gfp);
592
593	kfree(cmp_buffer);
594}
595
596static void __init
597test_pointer(void)
598{
599	plain();
600	null_pointer();
601	invalid_pointer();
602	symbol_ptr();
603	kernel_ptr();
604	struct_resource();
605	addr();
606	escaped_str();
607	hex_string();
608	mac();
609	ip();
610	uuid();
611	dentry();
612	struct_va_format();
613	struct_rtc_time();
614	struct_clk();
615	bitmap();
616	netdev_features();
617	flags();
618}
619
620static void __init selftest(void)
 
621{
622	alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
623	if (!alloced_buffer)
624		return;
625	test_buffer = alloced_buffer + PAD_SIZE;
626
627	test_basic();
628	test_number();
629	test_string();
630	test_pointer();
631
632	kfree(alloced_buffer);
 
 
 
 
 
 
 
633}
634
635KSTM_MODULE_LOADERS(test_printf);
 
636MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
637MODULE_LICENSE("GPL");