1*** Keywords ***
2Create Machine
3    Execute Command          i @scripts/single-node/sifive_fe310.resc
4
5Create Trivial Machine
6    Execute Command          mach create
7    Execute Command          machine LoadPlatformDescriptionFromString "uart0: UART.TrivialUart @ sysbus 0x1000"
8
9Write Bytes To Trivial Uart
10    [Arguments]              ${bytes}
11
12    # Just @{bytes} does not work as $bytes is not list or list-like.
13    # It does work for str though, just not bytes.
14    FOR  ${byte}  IN  @{{[b for b in $bytes]}}
15        Execute Command      sysbus.uart0 WriteDoubleWord 0 ${byte}
16    END
17
18*** Test Cases ***
19Should Fail On Non-Existing UART
20    Create Machine
21    Run Keyword And Expect Error
22    ...   *not found or of wrong type*
23    ...   Create Terminal Tester  sysbus.non_existing_uart
24
25Should Not Require Tester Id When Single Tester
26    Create Machine
27    Create Terminal Tester  sysbus.uart0
28
29    Wait For Line On Uart   ZEPHYR
30
31Should Require Tester Id When Two Testers
32    Create Machine
33    Create Terminal Tester  sysbus.uart0
34    Create Terminal Tester  sysbus.uart1
35
36    Run Keyword And Expect Error
37    ...   *There is more than one tester available*
38    ...   Wait For Line On Uart   ZEPHYR
39
40Should Respect Tester Id In The Keyword When Two Testers
41    Create Machine
42    ${u0}=  Create Terminal Tester  sysbus.uart0
43    ${u1}=  Create Terminal Tester  sysbus.uart1
44
45    Wait For Line On Uart   ZEPHYR    testerId=${u0}
46
47Should Respect The Default Tester Id
48    Create Machine
49    ${u0}=  Create Terminal Tester  sysbus.uart0
50    ${u1}=  Create Terminal Tester  sysbus.uart1
51    Set Default Tester      ${u0}
52
53    Wait For Line On Uart   ZEPHYR
54
55Should Use Tester Id Selected By Keyword
56    Create Machine
57    ${u0}=  Create Terminal Tester  sysbus.uart0
58    ${u1}=  Create Terminal Tester  sysbus.uart1
59    Set Default Tester      ${u1}
60
61    Wait For Line On Uart   ZEPHYR   testerId=${u0}
62
63Should Respect The Default Tester Id 2
64    Create Machine
65    ${u0}=  Create Terminal Tester  sysbus.uart0
66    ${u1}=  Create Terminal Tester  sysbus.uart1
67    Set Default Tester      ${u1}
68
69    Run Keyword And Expect Error
70    ...   *Terminal tester failed!*
71    ...   Wait For Line On Uart   ZEPHYR  timeout=1
72
73Should Overwrite The Default Tester Id
74    Create Machine
75    ${u0}=  Create Terminal Tester  sysbus.uart0
76    ${u1}=  Create Terminal Tester  sysbus.uart1
77    Set Default Tester      ${u1}
78    Set Default Tester      ${u0}
79
80    Wait For Line On Uart   ZEPHYR
81
82Should Allow To Clear The Default Tester Id
83    Create Machine
84    ${u0}=  Create Terminal Tester  sysbus.uart0
85            Create Terminal Tester  sysbus.uart1
86
87    Set Default Tester      ${u0}
88    Set Default Tester      null
89
90    Run Keyword And Expect Error
91    ...   *There is more than one tester available*
92    ...   Wait For Line On Uart   ZEPHYR
93
94Should Not Allow Waiting For Byte String On A Tester Not Created In Binary Mode
95    Create Machine
96    Create Terminal Tester  sysbus.uart0  defaultPauseEmulation=true
97
98    Run Keyword And Expect Error
99    ...   *Attempt to wait for bytes on a tester configured in text mode*
100    ...   Wait For Bytes On Uart  59 52 20 4f 53  timeout=1
101
102Should Not Allow Waiting For Text String On A Tester Created In Binary Mode
103    Create Machine
104    Create Terminal Tester  sysbus.uart0  defaultPauseEmulation=true  binaryMode=true
105
106    Run Keyword And Expect Error
107    ...   *Attempt to wait for text on a tester configured in binary mode*
108    ...   Wait For Line On Uart  ZEPHYR  timeout=1
109
110Should Allow Waiting For Byte String Spanning Lines
111    Create Machine
112    Create Terminal Tester  sysbus.uart0  defaultPauseEmulation=true  binaryMode=true
113
114    # ****\r\nsh
115    Wait For Bytes On Uart  2a 2a 2a 2a 0d 0a 73 68
116
117Should Allow Waiting For Immediately Adjacent Byte Strings
118    Create Machine
119    Create Terminal Tester  sysbus.uart0  defaultPauseEmulation=true  binaryMode=true
120
121    # ZEPHYR
122    Wait For Bytes On Uart  5a 45 50 48 59 52
123
124    # R OS, making sure the characters from ZEPHYR have left the match window
125    Run Keyword And Expect Error
126    ...   *Terminal tester failed*
127    ...   Wait For Bytes On Uart  52 20 4f 53  timeout=1
128
129    # ****\r\nsh
130    Wait For Bytes On Uart  2a 2a 2a 2a 0d 0a 73 68  timeout=0
131
132    # ell
133    Wait For Bytes On Uart  65 6c 6c  timeout=0
134
135Should Allow Waiting For Byte String At Start
136    Create Machine
137    Create Terminal Tester  sysbus.uart0  defaultPauseEmulation=true  binaryMode=true
138
139    # ZEPHYR
140    Wait For Bytes On Uart  5a 45 50 48 59 52
141
142    # OS, want at start (but there's a space in the way, so no match)
143    Run Keyword And Expect Error
144    ...   *Terminal tester failed*
145    ...   Wait For Bytes On Uart  4f 53  timeout=1  matchStart=true
146
147    # And now with the space, which should match (" OS")
148    Wait For Bytes On Uart  20 4f 53  timeout=0  matchStart=true
149
150Should Allow Waiting For Byte String Containing All Byte Values
151    Create Trivial Machine
152    Create Terminal Tester  sysbus.uart0  binaryMode=true
153
154    ${bytes}=  Evaluate     bytes(range(2**8))
155    Write Bytes To Trivial Uart  ${bytes}
156
157    # Exact match required
158    ${m}=  Wait For Bytes On Uart  ${{$bytes.hex()}}  matchStart=true
159    Should Be Equal         ${m.content}  ${bytes}
160
161Should Allow Waiting For Byte String Containing All Byte Values As Regex
162    Create Trivial Machine
163    Create Terminal Tester  sysbus.uart0  binaryMode=true
164
165    ${bytes}=  Evaluate     bytes(range(2**8))
166    # Queue it up 3 times and then let's match each repetition in a different way
167    FOR  ${i}  IN RANGE  3
168        Write Bytes To Trivial Uart  ${bytes}
169    END
170
171    # First one verbatim (might as well not have used a regex)
172    ${bytes_re}=  Evaluate  "".join(rf"\\x{b:02x}" for b in $bytes)
173    ${m}=  Wait For Bytes On Uart  ${bytes_re}  matchStart=true  treatAsRegex=true
174    Should Be Equal         ${m.content}  ${bytes}
175
176    # Note that due to how the tester works (evaluating the pattern after every byte written)
177    # if the entire string hadn't already been printed at assertion time then the ? would not
178    # be required for a non-greedy match
179    ${m}=  Wait For Bytes On Uart  \\x00.*?\\xff  matchStart=true  treatAsRegex=true
180    Should Be Equal         ${m.content}  ${bytes}
181
182    # And now with some groups
183    ${m}=  Wait For Bytes On Uart  (\\x11.*)\\x41.*?([\\x50-\\x5f]+).*?\\xfd  treatAsRegex=true
184    Should Be Equal         ${m.content}  ${{$bytes[0x11:-2]}}
185    Should Be Equal         ${m.groups[0]}  ${{bytes(range(0x11, 0x41))}}
186    Should Be Equal         ${m.groups[1]}  ${{bytes(range(0x50, 0x60))}}
187