-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.s
235 lines (194 loc) · 4.46 KB
/
tests.s
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
%include "minserv.s"
section .data
sample_string_poop:
db 'poop', 10, 0
sample_string_empty:
db '', 0
test_failed:
db 9, 0x1b, 0x5b, 0x33, 0x31, 0x6d, 'test failed', 0x1b, 0x5b, 0x33, 0x39, 0x6d, 10, 0
test_passed:
db 9, 0x1b, 0x5b, 0x33, 0x32, 0x6d, 'test passed', 0x1b, 0x5b, 0x33, 0x39, 0x6d, 10, 0
test_name_strlen_poop:
db 'strlen_poop', 0
test_name_strlen_empty:
db 'strlen_empty', 0
test_name_strcmp_equal:
db 'strcmp_equal', 0
test_name_read_write_socket:
db 'write_to_socket', 0
seen_failure:
db 0
final_pass:
db 0x1b, 0x5b, 0x33, 0x32, 0x6d, 'all tests passed! :)', 0x1b, 0x5b, 0x33, 0x39, 0x6d, 10, 0
final_fail:
db 0x1b, 0x5b, 0x33, 0x31, 0x6d, 'some tests failed! :(', 0x1b, 0x5b, 0x33, 0x39, 0x6d, 10, 0
section .bss
misc1:
resb 256
misc2:
resb 256
misc3:
resb 256
misc4:
resb 256
section .text
global _start
_start:
call test_strlen
call test_strcmp
call test_write_to_socket
call print_verdict
mov rdi, rax
mov rax, 60
syscall ; sys_exit
test_strcmp:
mov rdi, sample_string_poop
mov rsi, sample_string_poop
mov rdx, test_name_strcmp_equal
call assert_strings_equal
ret
test_write_to_socket:
push rdi
push rsi
push rdx
push rcx
mov rdi, misc1
call make_pipes
mov rdi, misc1
mov ecx, dword [rdi+4]
mov rdi, rcx
mov rsi, 0x00216f6c6c6568
push rsi
mov rsi, rsp
call write_string
pop rsi
mov rdi, misc1
mov ecx, dword [rdi]
mov rdi, rcx
mov rsi, misc2
mov rdx, 255
call read_into_buffer
mov rsi, 0x00216f6c6c6568
push rsi
mov rsi, rsp
mov rdi, misc2
mov rdx, test_name_read_write_socket
call assert_strings_equal
pop rsi
mov rdi, misc1
mov edi, dword [rdi]
call close_socket
mov rdi, misc1
mov edi, dword [rdi+4]
call close_socket
pop rcx
pop rdx
pop rsi
pop rdi
ret
test_strlen:
mov rdi, sample_string_poop
call strlen
mov rdi, rax
mov rsi, 5
mov rdx, test_name_strlen_poop
call assert_ints_equal
mov rdi, sample_string_empty
call strlen
mov rdi, rax
mov rsi, 0
mov rdx, test_name_strlen_empty
call assert_ints_equal
ret
assert_strings_not_equal:
push rdi
push rsi
mov rdi, 0
mov rsi, rdx
call write_string
pop rsi
pop rdi
call strcmp
jnz assert_strings_not_equal_yes
mov rsi, test_failed
call set_failure
jmp assert_strings_not_equal_ret
assert_strings_not_equal_yes:
mov rsi, test_passed
assert_strings_not_equal_ret:
mov rdi, 0
call write_string
ret
assert_strings_equal:
push rdi
push rsi
mov rdi, 0
mov rsi, rdx
call write_string
pop rsi
pop rdi
call strcmp
jz assert_strings_equal_yes
mov rsi, test_failed
call set_failure
jmp assert_strings_equal_ret
assert_strings_equal_yes:
mov rsi, test_passed
assert_strings_equal_ret:
mov rdi, 0
call write_string
ret
assert_ints_equal:
push rdi
push rsi
mov rdi, 0
mov rsi, rdx
call write_string
pop rsi
pop rdi
cmp rdi, rsi
jz assert_ints_equal_yes
mov rsi, test_failed
call set_failure
jmp assert_ints_equal_ret
assert_ints_equal_yes:
mov rsi, test_passed
assert_ints_equal_ret:
mov rdi, 0
call write_string
ret
set_failure:
push rdi
mov rdi, seen_failure
mov byte [rdi], 1
pop rdi
ret
print_verdict:
push rdi
push rsi
mov rdi, seen_failure
mov dl, byte [rdi]
cmp dl, 0
jz print_verdict_success
mov rsi, final_fail
mov rax, 1
jmp print_verdict_ret
print_verdict_success:
mov rsi, final_pass
mov rax, 0
print_verdict_ret:
mov rdi, 0
push rax
call write_string
pop rax
pop rsi
pop rdi
ret
make_pipes:
mov rax, 22 ; sys_pipe
syscall ; rdi = int pipefd[2]
ret
close_socket:
mov rax, 3
syscall ; rdi = fd
ret