Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2025 Simon Glass <sjg@chromium.org>
 *
 * Test for pager functionality
 */

#include <errno.h>
#include <malloc.h>
#include <pager.h>
#include <string.h>
#include <test/common.h>
#include <test/test.h>
#include <test/ut.h>
#include <asm/global_data.h>

DECLARE_GLOBAL_DATA_PTR;

/* Test basic pager init and cleanup */
static int pager_test_basic_init(struct unit_test_state *uts)
{
	struct pager *pag;

	/* Test successful init */
	ut_assertok(pager_init(&pag, 20, 1024));
	ut_assertnonnull(pag);
	ut_asserteq(20, pag->page_len);
	ut_asserteq(0, pag->line_count);
	ut_assertnull(pag->overflow);
	ut_assertnull(pag->nulch);

	/* Clean up */
	pager_uninit(pag);

	/* Test init with different parameters */
	ut_assertok(pager_init(&pag, 10, 2048));
	ut_assertnonnull(pag);
	ut_asserteq(10, pag->page_len);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_basic_init, 0);

/* Test pager with simple text */
static int pager_test_simple_text(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *text = "Hello, World!";
	const char *result;

	ut_assertok(pager_init(&pag, 20, 1024));

	/* Post some text and get it back */
	result = pager_post(pag, true, text);
	ut_assertnonnull(result);
	ut_asserteq_str(text, result);

	/* Should be no more text */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_simple_text, 0);

/* Test pager with multiple lines */
static int pager_test_multiline(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *text1 = "Line 1\n";
	const char *text2 = "Line 2\n";
	const char *text3 = "Line 3\n";
	const char *result;
	ulong start;

	start = ut_check_free();

	ut_assertok(pager_init(&pag, 20, 1024));

	/* Post multiple pieces of text */
	result = pager_post(pag, true, text1);
	ut_assertnonnull(result);
	ut_asserteq_str(text1, result);

	/* Should be no more text after first post */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	result = pager_post(pag, true, text2);
	ut_assertnonnull(result);
	ut_asserteq_str(text2, result);

	/* Should be no more text after second post */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	result = pager_post(pag, true, text3);
	ut_assertnonnull(result);
	ut_asserteq_str(text3, result);

	/* Should be no more text after third post */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	/* Check for memory leaks */
	ut_assertok(ut_check_delta(start));

	return 0;
}
COMMON_TEST(pager_test_multiline, 0);

/* Test pager with large text that fills the buffer */
static int pager_test_large_text(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *result;

	ut_assertok(pager_init(&pag, 20, 16)); /* Small buffer */

	/* Post large text - should fit in buffer */
	result = pager_post(pag, true, "this is 16 chars");
	ut_assertnonnull(result);
	ut_asserteq_str("this is 16 chars", result);
	ut_assertnull(pager_next(pag, true, 0));

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_large_text, 0);

/* Test pager overflow handling */
static int pager_test_overflow(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *result;

	ut_assertok(pager_init(&pag, 20, 4)); /* Small buffer */

	/* send some text which is too long for the buffer */
	result = pager_post(pag, true, "test1");
	ut_assertnonnull(result);

	/* overflow handling should return the text */
	ut_asserteq_str("test1", result);
	ut_assertnull(pager_next(pag, true, 0));

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_overflow, 0);

/* Test pager with NULL input */
static int pager_test_null_input(struct unit_test_state *uts)
{
	const char *result;

	/* Test pager_post with NULL pager */
	result = pager_post(NULL, true, "test");
	ut_asserteq_str("test", result);

	return 0;
}
COMMON_TEST(pager_test_null_input, 0);

/* Test pager with empty strings */
static int pager_test_empty_strings(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *result;

	ut_assertok(pager_init(&pag, 20, 1024));

	/* Post empty string */
	result = pager_post(pag, true, "");
	ut_assertnull(result);

	/* Should be no more text */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_empty_strings, 0);

/* Test pager buffer management */
static int pager_test_buffer_management(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *text = "Test buffer management";
	const char *result;

	ut_assertok(pager_init(&pag, 20, 1024));

	/* Verify buffer is properly inited */
	ut_assertnonnull(pag->buf.data);
	ut_asserteq(1024, pag->buf.size);

	/* Post text and verify buffer state */
	result = pager_post(pag, true, text);
	ut_assertnonnull(result);

	/* Verify the buffer contains our text */
	ut_asserteq_str(text, result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_buffer_management, 0);

/* Test pager with very long single line */
static int pager_test_long_single_line(struct unit_test_state *uts)
{
	struct pager *pag;
	char long_line[1000];
	const char *result;

	ut_assertok(pager_init(&pag, 20, 1024));

	/* Create a very long line without newlines */
	memset(long_line, 'X', sizeof(long_line) - 1);
	long_line[sizeof(long_line) - 1] = '\0';

	/* Post the long line */
	result = pager_post(pag, true, long_line);
	ut_assertnonnull(result);

	/* Should get our text back */
	ut_asserteq_str(long_line, result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_long_single_line, 0);

/* Test pager line counting and page breaks */
static int pager_test_line_counting(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *multiline_text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n";
	const char *result;

	/* Init with page length of 4 lines */
	ut_assertok(pager_init(&pag, 4, 1024));

	/* Post multiline text */
	result = pager_post(pag, true, multiline_text);
	ut_assertnonnull(result);

	/* Should get first 3 lines (excluding the 3rd newline) */
	ut_asserteq_str("Line 1\nLine 2\nLine 3", result);
	/* line_count is reset to 0 when page limit is reached */
	ut_asserteq(0, pag->line_count);

	/* Next call should return pager prompt */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_PROMPT, result);

	/* Press space to continue */
	result = pager_next(pag, true, ' ');
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_BLANK, result);

	/* Get remaining lines */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str("Line 4\nLine 5\n", result);

	/* Should be no more text */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_line_counting, 0);

/* Test that PAGER_WAITING is returned when pager waits for user input */
static int pager_test_pager_waiting(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *result;

	/* Create pager with small page size to trigger waiting quickly */
	ut_assertok(pager_init(&pag, 3, 1024));

	/* Post text that fills exactly the page limit */
	result = pager_post(pag, true, "Line 1\nLine 2\n");
	ut_assertnonnull(result);
	ut_asserteq_str("Line 1\nLine 2", result);

	/* Next call should return the prompt */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_PROMPT, result);

	/* Next call without space key should return PAGER_WAITING */
	result = pager_next(pag, true, 0);
	ut_asserteq_ptr(PAGER_WAITING, result);

	/* Another call without space should still return PAGER_WAITING */
	result = pager_next(pag, true, 'x');  /* Wrong key */
	ut_asserteq_ptr(PAGER_WAITING, result);

	/* Pressing space should clear the prompt */
	result = pager_next(pag, true, ' ');
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_BLANK, result);

	/* Now should return NULL (no more content) */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_pager_waiting, 0);

/* Test use_pager parameter - output text directly, while buffer is non-empty */
static int pager_test_use_pager_param(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *buffered_text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n";
	const char *direct_text = "This should be written immediately";
	const char *result;

	/* Init with small page length to ensure paging occurs */
	ut_assertok(pager_init(&pag, 3, 1024));

	/* Post text with use_pager=true - should trigger paging */
	result = pager_post(pag, true, buffered_text);
	ut_assertnonnull(result);
	/* Should get first 2 lines */
	ut_asserteq_str("Line 1\nLine 2", result);

	/* Now call pager_post with use_pager=false while text is still buffered */
	result = pager_post(pag, false, direct_text);
	/* Should get the text immediately, not from buffer */
	ut_asserteq_ptr(direct_text, result);

	/* Call pager_next with use_pager=false - should return NULL */
	result = pager_next(pag, false, 0);
	ut_assertnull(result);

	/* Now continue with use_pager=true to get buffered text */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	/* Should get the pager prompt */
	ut_asserteq_str(PAGER_PROMPT, result);

	/* Press space to continue */
	result = pager_next(pag, true, ' ');
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_BLANK, result);

	/* Get remaining buffered lines - should be next 2 lines due to page limit */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str("Line 3\nLine 4", result);

	/* Should get pager prompt again */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_PROMPT, result);

	/* Press space to continue */
	result = pager_next(pag, true, ' ');
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_BLANK, result);

	/* Get final line */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str("Line 5\n", result);

	/* Should be no more text */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_use_pager_param, 0);

/* Test pager bypass mode */
static int pager_test_bypass_mode(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *text = "This text should be returned directly";
	const char *result;
	bool was_bypassed;

	/* Init with small page length to ensure paging would normally occur */
	ut_assertok(pager_init(&pag, 2, 1024));

	/* Enable bypass mode */
	was_bypassed = pager_set_test_bypass(pag, true);

	/* Post text - should get original string back directly */
	result = pager_post(pag, true, text);
	ut_asserteq_ptr(text, result); /* Should be same pointer */

	/* pager_next should return NULL in bypass mode */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	/* Restore old bypass mode */
	pager_set_test_bypass(pag, was_bypassed);

	/* Now pager should work normally */
	result = pager_post(pag, true, text);
	ut_assertnonnull(result);
	/* In normal mode, result should be different from original text */
	ut_assert(result != text);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_bypass_mode, 0);

/* Test that single character output via putc goes through pager */
static int pager_test_putc(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *result;

	/* Init pager */
	ut_assertok(pager_init(&pag, 20, 1024));
	pager_set_bypass(pag, true);

	/*
	 * Test that individual characters can be posted via pager API
	 * This verifies that console_putc_pager() routes through the pager
	 * system
	 */
	result = pager_post(pag, true, "A");
	ut_asserteq_ptr("A", result); /* Bypass mode returns original pointer */

	result = pager_post(pag, true, "\n");
	ut_asserteq_ptr("\n", result);

	result = pager_post(pag, true, "B");
	ut_asserteq_ptr("B", result);

	/* Disable bypass to test normal functionality with single chars */
	pager_set_bypass(pag, false);

	result = pager_post(pag, true, "X");
	ut_assertnonnull(result);
	ut_asserteq_str("X", result);

	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_putc, 0);

/* Test writing up to page limit then adding final newline */
static int pager_test_limit_plus_newline(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *result;

	/* Init with page length of 3 lines */
	ut_assertok(pager_init(&pag, 3, 1024));

	/* Write text that reaches exactly the page limit (2 newlines) */
	result = pager_post(pag, true, "Line 1\nLine 2");
	ut_assertnonnull(result);
	ut_asserteq_str("Line 1\nLine 2", result);
	ut_asserteq(1, pag->line_count); /* Should have 1 line counted */

	/* Should be no more text yet - haven't hit limit */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	/* Now post a single newline - this should trigger the page limit */
	result = pager_post(pag, true, "\n");
	ut_assertnonnull(result);
	/*
	 * Should get empty string since we hit the limit and the newline is
	 * consumed
	 */
	ut_asserteq_str("", result);

	/* Next call should return the pager prompt since we hit the limit */
	result = pager_next(pag, true, 0);
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_PROMPT, result);

	/* Press space to continue */
	result = pager_next(pag, true, ' ');
	ut_assertnonnull(result);
	ut_asserteq_str(PAGER_BLANK, result);

	/* Should be no more text */
	result = pager_next(pag, true, 0);
	ut_assertnull(result);

	pager_uninit(pag);

	return 0;
}
COMMON_TEST(pager_test_limit_plus_newline, 0);

/* Test console integration - pager prompt appears in console output */
static int pager_test_console(struct unit_test_state *uts)
{
	struct pager *pag, *orig_pag;
	char line[100];
	int avail, ret;

	/* Save original pager */
	orig_pag = gd_pager();

	/* Create our own pager for testing */
	ret = pager_init(&pag, 2, 1024);
	if (ret) {
		gd->pager = orig_pag;
		return CMD_RET_FAILURE;
	}

	/* Set up pager to be one away from limit (1 line already counted) */
	pag->line_count = 1;

	/* Assign our pager to the global data */
	gd->pager = pag;

	/* Trigger paging with a second newline */
	putc('\n');

	/* Check if there's any console output available at all */
	avail = console_record_avail();

	/* Restore original pager first */
	gd->pager = orig_pag;
	pager_uninit(pag);

	/* Now check what we got */
	if (!avail) {
		ut_reportf("No console output was recorded at all");
		return CMD_RET_FAILURE;
	}

	/* Try to read the actual output */
	ret = console_record_readline(line, sizeof(line));
	if (ret < 0) {
		ut_reportf("Failed to read first line, avail was %d", avail);
		return CMD_RET_FAILURE;
	}

	/*
	 * console recording does not see the pager prompt, so we should have
	 * just got a newline
	 */
	ut_asserteq_str("", line);

	ut_assert_console_end();

	return 0;
}
COMMON_TEST(pager_test_console, UTF_CONSOLE);

/* Test bypass keypress ('Q') functionality */
static int pager_test_bypass_keypress(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *out;
	int ret;

	ret = pager_init(&pag, 3, SZ_1K);
	ut_assertok(ret);

	/* Post text that will trigger paging */
	out = pager_post(pag, true, "line1\nline2\nline3\nline4\n");
	ut_assertnonnull(out);
	ut_asserteq_str("line1\nline2", out);

	/* Should be waiting for user input */
	out = pager_next(pag, true, 0);
	ut_asserteq_str(PAGER_PROMPT, out);

	/* Press 'Q' to bypass */
	out = pager_next(pag, true, 'Q');
	ut_asserteq_str(PAGER_BLANK, out);

	/* Verify pager is now in bypass mode */
	ut_asserteq(PAGERST_BYPASS, pag->state);

	/* Next call should return the remaining text without paging */
	out = pager_next(pag, true, 0);
	ut_asserteq_str("line3\nline4\n", out);

	/* No more text should be available */
	out = pager_next(pag, true, 0);
	ut_asserteq_ptr(NULL, out);

	pager_uninit(pag);
	return 0;
}
COMMON_TEST(pager_test_bypass_keypress, 0);

/* Test quit keypress ('q') functionality */
static int pager_test_quit_keypress(struct unit_test_state *uts)
{
	struct pager *pag;
	const char *out;
	int ret;

	ret = pager_init(&pag, 3, SZ_1K);
	ut_assertok(ret);

	/* Post text that will trigger paging */
	out = pager_post(pag, true, "line1\nline2\nline3\nline4\n");
	ut_assertnonnull(out);
	ut_asserteq_str("line1\nline2", out);

	/* Should be waiting for user input */
	out = pager_next(pag, true, 0);
	ut_asserteq_str(PAGER_PROMPT, out);

	/* Press 'q' to quit and suppress */
	out = pager_next(pag, true, 'q');
	ut_asserteq_str("\r                                                  \r", out);

	/* Verify pager is now in quit suppress mode */
	ut_asserteq(PAGERST_QUIT_SUPPRESS, pag->state);

	/* Next call should return NULL (suppressed) */
	out = pager_next(pag, true, 0);
	ut_asserteq_ptr(NULL, out);

	/* Posting new text should also return NULL (suppressed) */
	out = pager_post(pag, true, "new text\n");
	ut_asserteq_ptr(NULL, out);

	/* Test that pager_clear_quit() restores normal operation */
	pager_clear_quit(pag);
	ut_asserteq(PAGERST_OK, pag->state);

	/* and that any new test appears */
	out = pager_post(pag, true, "more new text\n");
	ut_asserteq_str("more new text\n", out);

	pager_uninit(pag);
	return 0;
}
COMMON_TEST(pager_test_quit_keypress, 0);