Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016, Google Inc. |
| 3 | # |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 4 | # U-Boot Verified Boot Test |
| 5 | |
| 6 | """ |
| 7 | This tests U-Boot logging. It uses the 'log test' command with various options |
| 8 | and checks that the output is correct. |
| 9 | """ |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | LOGL_FIRST, LOGL_WARNING, LOGL_INFO = (0, 4, 6) |
| 14 | |
Tom Rini | 686dc7e | 2018-05-25 08:28:45 -0400 | [diff] [blame] | 15 | @pytest.mark.buildconfigspec('cmd_log') |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 16 | def test_log(u_boot_console): |
| 17 | """Test that U-Boot logging works correctly.""" |
| 18 | def check_log_entries(lines, mask, max_level=LOGL_INFO): |
| 19 | """Check that the expected log records appear in the output |
| 20 | |
| 21 | Args: |
| 22 | lines: iterator containing lines to check |
| 23 | mask: bit mask to select which lines to check for: |
| 24 | bit 0: standard log line |
| 25 | bit 1: _log line |
| 26 | max_level: maximum log level to expect in the output |
| 27 | """ |
| 28 | for i in range(max_level): |
| 29 | if mask & 1: |
Tom Rini | 7f24c19 | 2019-10-24 11:59:20 -0400 | [diff] [blame] | 30 | assert 'log_run() log %d' % i == next(lines) |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 31 | if mask & 3: |
Tom Rini | 7f24c19 | 2019-10-24 11:59:20 -0400 | [diff] [blame] | 32 | assert 'func() _log %d' % i == next(lines) |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 33 | |
| 34 | def run_test(testnum): |
| 35 | """Run a particular test number (the 'log test' command) |
| 36 | |
| 37 | Args: |
| 38 | testnum: Test number to run |
| 39 | Returns: |
| 40 | iterator containing the lines output from the command |
| 41 | """ |
Heinrich Schuchardt | 2d349c7 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 42 | output = u_boot_console.run_command('log format fm') |
| 43 | assert output == '' |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 44 | with cons.log.section('basic'): |
| 45 | output = u_boot_console.run_command('log test %d' % testnum) |
| 46 | split = output.replace('\r', '').splitlines() |
| 47 | lines = iter(split) |
Tom Rini | 7f24c19 | 2019-10-24 11:59:20 -0400 | [diff] [blame] | 48 | assert 'test %d' % testnum == next(lines) |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 49 | return lines |
| 50 | |
| 51 | def test0(): |
| 52 | lines = run_test(0) |
| 53 | check_log_entries(lines, 3) |
| 54 | |
| 55 | def test1(): |
| 56 | lines = run_test(1) |
| 57 | check_log_entries(lines, 3) |
| 58 | |
| 59 | def test2(): |
| 60 | lines = run_test(2) |
| 61 | |
| 62 | def test3(): |
| 63 | lines = run_test(3) |
| 64 | check_log_entries(lines, 2) |
| 65 | |
| 66 | def test4(): |
| 67 | lines = run_test(4) |
| 68 | assert next(lines, None) == None |
| 69 | |
| 70 | def test5(): |
| 71 | lines = run_test(5) |
| 72 | check_log_entries(lines, 2) |
| 73 | |
| 74 | def test6(): |
| 75 | lines = run_test(6) |
| 76 | check_log_entries(lines, 3) |
| 77 | |
| 78 | def test7(): |
| 79 | lines = run_test(7) |
| 80 | check_log_entries(lines, 3, LOGL_WARNING) |
| 81 | |
| 82 | def test8(): |
| 83 | lines = run_test(8) |
| 84 | check_log_entries(lines, 3) |
| 85 | |
| 86 | def test9(): |
| 87 | lines = run_test(9) |
| 88 | check_log_entries(lines, 3) |
| 89 | |
Simon Glass | 1b69a99 | 2018-10-01 11:55:06 -0600 | [diff] [blame] | 90 | def test10(): |
| 91 | lines = run_test(10) |
| 92 | for i in range(7): |
Tom Rini | 7f24c19 | 2019-10-24 11:59:20 -0400 | [diff] [blame] | 93 | assert 'log_test() level %d' % i == next(lines) |
Simon Glass | 1b69a99 | 2018-10-01 11:55:06 -0600 | [diff] [blame] | 94 | |
Simon Glass | 5f0e0c8 | 2017-12-04 13:48:30 -0700 | [diff] [blame] | 95 | # TODO(sjg@chromium.org): Consider structuring this as separate tests |
| 96 | cons = u_boot_console |
| 97 | test0() |
| 98 | test1() |
| 99 | test2() |
| 100 | test3() |
| 101 | test4() |
| 102 | test5() |
| 103 | test6() |
| 104 | test7() |
| 105 | test8() |
| 106 | test9() |
Simon Glass | 1b69a99 | 2018-10-01 11:55:06 -0600 | [diff] [blame] | 107 | test10() |
Simon Glass | 32e9b16 | 2017-12-28 13:14:21 -0700 | [diff] [blame] | 108 | |
Tom Rini | 686dc7e | 2018-05-25 08:28:45 -0400 | [diff] [blame] | 109 | @pytest.mark.buildconfigspec('cmd_log') |
Simon Glass | 32e9b16 | 2017-12-28 13:14:21 -0700 | [diff] [blame] | 110 | def test_log_format(u_boot_console): |
| 111 | """Test the 'log format' and 'log rec' commands""" |
| 112 | def run_with_format(fmt, expected_output): |
| 113 | """Set up the log format and then write a log record |
| 114 | |
| 115 | Args: |
| 116 | fmt: Format to use for 'log format' |
| 117 | expected_output: Expected output from the 'log rec' command |
| 118 | """ |
| 119 | output = cons.run_command('log format %s' % fmt) |
| 120 | assert output == '' |
| 121 | output = cons.run_command('log rec arch notice file.c 123 func msg') |
| 122 | assert output == expected_output |
| 123 | |
| 124 | cons = u_boot_console |
| 125 | with cons.log.section('format'): |
| 126 | run_with_format('all', 'NOTICE.arch,file.c:123-func() msg') |
| 127 | output = cons.run_command('log format') |
| 128 | assert output == 'Log format: clFLfm' |
| 129 | |
| 130 | run_with_format('fm', 'func() msg') |
| 131 | run_with_format('clfm', 'NOTICE.arch,func() msg') |
| 132 | run_with_format('FLfm', 'file.c:123-func() msg') |
| 133 | run_with_format('lm', 'NOTICE. msg') |
| 134 | run_with_format('m', 'msg') |