Source
2
2
* Test cases for printf facility.
3
3
*/
4
4
5
5
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
6
7
7
#include <linux/init.h>
8
8
#include <linux/kernel.h>
9
9
#include <linux/module.h>
10
10
#include <linux/printk.h>
11
11
#include <linux/random.h>
12
+
#include <linux/rtc.h>
12
13
#include <linux/slab.h>
13
14
#include <linux/string.h>
14
15
15
16
#include <linux/bitmap.h>
16
17
#include <linux/dcache.h>
17
18
#include <linux/socket.h>
18
19
#include <linux/in.h>
19
20
20
21
#include <linux/gfp.h>
21
22
#include <linux/mm.h>
242
243
static int __init
243
244
plain_format(void)
244
245
{
245
246
/* Format is implicitly tested for 32 bit machines by plain_hash() */
246
247
return 0;
247
248
}
248
249
249
250
#endif /* BITS_PER_LONG == 64 */
250
251
251
252
static int __init
252
-
plain_hash(void)
253
+
plain_hash_to_buffer(const void *p, char *buf, size_t len)
253
254
{
254
-
char buf[PLAIN_BUF_SIZE];
255
255
int nchars;
256
256
257
-
nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
257
+
nchars = snprintf(buf, len, "%p", p);
258
258
259
259
if (nchars != PTR_WIDTH)
260
260
return -1;
261
261
262
262
if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
263
263
pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
264
264
PTR_VAL_NO_CRNG);
265
265
return 0;
266
266
}
267
267
268
+
return 0;
269
+
}
270
+
271
+
272
+
static int __init
273
+
plain_hash(void)
274
+
{
275
+
char buf[PLAIN_BUF_SIZE];
276
+
int ret;
277
+
278
+
ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
279
+
if (ret)
280
+
return ret;
281
+
268
282
if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
269
283
return -1;
270
284
271
285
return 0;
272
286
}
273
287
274
288
/*
275
289
* We can't use test() to test %p because we don't know what output to expect
276
290
* after an address is hashed.
277
291
*/
287
301
return;
288
302
}
289
303
290
304
err = plain_format();
291
305
if (err) {
292
306
pr_warn("hashing plain 'p' has unexpected format\n");
293
307
failed_tests++;
294
308
}
295
309
}
296
310
311
+
static void __init
312
+
test_hashed(const char *fmt, const void *p)
313
+
{
314
+
char buf[PLAIN_BUF_SIZE];
315
+
int ret;
316
+
317
+
/*
318
+
* No need to increase failed test counter since this is assumed
319
+
* to be called after plain().
320
+
*/
321
+
ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
322
+
if (ret)
323
+
return;
324
+
325
+
test(buf, fmt, p);
326
+
}
327
+
297
328
static void __init
298
329
symbol_ptr(void)
299
330
{
300
331
}
301
332
302
333
static void __init
303
334
kernel_ptr(void)
304
335
{
305
336
/* We can't test this without access to kptr_restrict. */
306
337
}
411
442
412
443
test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
413
444
test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
414
445
}
415
446
416
447
static void __init
417
448
struct_va_format(void)
418
449
{
419
450
}
420
451
452
+
static void __init
453
+
struct_rtc_time(void)
454
+
{
455
+
/* 1543210543 */
456
+
const struct rtc_time tm = {
457
+
.tm_sec = 43,
458
+
.tm_min = 35,
459
+
.tm_hour = 5,
460
+
.tm_mday = 26,
461
+
.tm_mon = 10,
462
+
.tm_year = 118,
463
+
};
464
+
465
+
test_hashed("%pt", &tm);
466
+
467
+
test("2018-11-26T05:35:43", "%ptR", &tm);
468
+
test("0118-10-26T05:35:43", "%ptRr", &tm);
469
+
test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
470
+
test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
471
+
test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
472
+
test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
473
+
}
474
+
421
475
static void __init
422
476
struct_clk(void)
423
477
{
424
478
}
425
479
426
480
static void __init
427
481
large_bitmap(void)
428
482
{
429
483
const int nbits = 1 << 16;
430
484
unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
522
576
kernel_ptr();
523
577
struct_resource();
524
578
addr();
525
579
escaped_str();
526
580
hex_string();
527
581
mac();
528
582
ip();
529
583
uuid();
530
584
dentry();
531
585
struct_va_format();
586
+
struct_rtc_time();
532
587
struct_clk();
533
588
bitmap();
534
589
netdev_features();
535
590
flags();
536
591
}
537
592
538
593
static int __init
539
594
test_printf_init(void)
540
595
{
541
596
alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);