vm_test.anubis
16.2 KB
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
The Anubis Project.
Testing the Virtual Machine.
Copyright (c) Alain Prouté 2005.
Author: Alain Prouté
This file contains test for the Anubis Virtual Machine.
*** (1) Tests.
type TestItem:
test_item (String banner,
One -> One run),
ask_test_item (String banner,
One -> One run).
Running a test:
define One
empty_stdin
=
if *stdin is
{
failure then unique,
success(c) then
if c = 10
then unique
else empty_stdin
}.
define One
press_ENTER_to_continue
=
print("\n\nPress [ENTER] to continue.");
empty_stdin.
define Bool
yes_or_no
(
String prompt
) =
print("\n\n "+prompt+" [Y/N]: ");
if *stdin is
{
failure then false,
success(c) then
with result = if c = 'y' then true else c = 'Y',
empty_stdin;
result
}.
define One
run_test
(
TestItem i
) =
if i is
{
test_item(banner,run) then
print("\n--- " + banner + " ---");
run(unique),
ask_test_item(banner,run) then
print("\n--- "+banner+" ---");
run(unique);
press_ENTER_to_continue
}.
define String
String s + Int32 n
=
s + integer_to_string(n).
define String
Int32 n + String s
=
integer_to_string(n) + s.
*** (1.1) Version numbers and directories.
define TestItem
test_ver_dir
=
ask_test_item("version numbers and directories",
(One u) |->
print("\n Version number is: "+major_version_number+"."+minor_version_number);
print("\n 'anubis' directory is: "+anubis_directory);
print("\n 'my_anubis' directory is: "+my_anubis_directory);
print("\n current directory is: "+get_current_directory)).
*** (1.2) Environment variables.
define TestItem
test_env_vars
=
with varname = "GABUZOMEU",
value = "Pr. Shadoko",
ask_test_item("environment variables",
(One u) |->
print("\nSetting variable '"+varname+"="+value+"'.");
if set_environment_variable(varname,value) is
{
failure then print("\nSetting of variable failed.")
success(_) then
print("\nReading variable '"+varname+"'.");
if get_environment_variable(varname) is
{
failure then print("\nCannot read variable."),
success(v) then
print("\nVariable value is: '"+v+"'.")
}
}).
*** Time test.
define String
pad2
(
Int32 n
) =
if n < 10
then "0"+integer_to_string(n)
else integer_to_string(n).
define TestItem
test_time
=
ask_test_item("date and time",
(One u) |->
with dt = convert_time(now),
print("\nCurrent date and time is: ");
print(integer_to_string(year(dt))+"/"+
integer_to_string(month(dt))+"/"+
integer_to_string(day(dt))+" "+
pad2(hour(dt))+":"+pad2(minute(dt))+":"+pad2(second(dt)))).
*** 'execute' test.
define TestItem
execute_test
=
ask_test_item("execution de programmes de l'hôte",
(One u) |->
forget(execute("anubis",[]))).
*** (1.3) TCP/IP Server test.
'?' and '!' are used as message delimiters.
Receiving a message.
define String
receive_message
(
RWStream conn,
List(Word8) so_far
) =
if *conn is
{
failure then implode(reverse(so_far)),
success(c) then
if c = '?' then implode(reverse([c . so_far])) else
if c = '!' then implode(reverse([c . so_far])) else
receive_message(conn,[c . so_far])
}.
define One
send_message
(
String me, // either "Server" or "Client"
RWStream conn,
String message,
Int32 i // starting point in message
) =
if nth(i,message) is
{
failure then unique,
success(c) then
if (conn <- c) is
{
failure then print("\n"+me+": Cannot write into connection."),
success(_) then send_message(me,conn,message,i+1)
}
}.
define String
ip_addr_to_string
(
Int32 addr
) =
integer_to_string(addr&255) + "." +
integer_to_string((addr>>8)&255) + "." +
integer_to_string((addr>>16)&255) + "." +
integer_to_string((addr>>24)&255).
define One
server_handler
(
RWStream conn
) =
with answer = "Very well, thank you !",
if local_IP_address_and_port(conn) is (loc_a,loc_p) then
print("\nServer: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p);
if remote_IP_address_and_port(conn) is (rem_a,rem_p) then
print("\nServer: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p);
print("\nServer: Waiting for a question.");
with question = receive_message(conn,[]),
print("\nServer: question is: '"+question+"'");
print("\nServer: Sending message: '"+answer+"'");
send_message("Server",conn,answer,0).
define One
client_handler
(
RWStream conn,
Server server
) =
with question = "How do you do ?",
if local_IP_address_and_port(conn) is (loc_a,loc_p) then
print("\nClient: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p);
if remote_IP_address_and_port(conn) is (rem_a,rem_p) then
print("\nClient: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p);
print("\nClient: Sending message: '"+question+"'");
send_message("Client",conn,question,0);
print("\nClient: Waiting for the answer.");
with answer = receive_message(conn,[]),
print("\nClient: Answer is: '"+answer+"'").
define Int32
ip_address
(
(Word8,Word8,Word8,Word8) addr
) =
if addr is (b3,b2,b1,b0) then
(word8_to_Word32(b0) << 24) |
(word8_to_Word32(b1) << 16) |
(word8_to_Word32(b2) << 8) |
(word8_to_Word32(b3)).
define One
sleep
(
Int32 ms
) =
checking every ms milliseconds, wait for true then unique.
define Server
force_start_server
(
Int32 address,
Int32 port,
Server -> ((RWStream) -> One) handler,
One -> One notify,
Int32 retry
) =
if start_server(address,port,handler,notify) is
{
cannot_create_the_socket then print("\nCannot create the listening socket."); alert,
cannot_bind_to_port then sleep(1000);
print("\rRetry number "+(retry+1)+" ");
force_start_server(address,port,handler,notify,retry+1),
cannot_listen_on_port then print("\nCannot listen on port "+port); alert,
ok(server) then server
}.
Reading a line from the standard input.
define String
read_line_from_stdin
(
List(Word8) so_far
) =
if *stdin is
{
failure then implode(reverse(so_far)),
success(c) then
if c = '\n'
then implode(reverse(so_far))
else read_line_from_stdin([c . so_far])
}.
Printing a prompt and reading the answer.
define String
prompt
(
String header
) =
print(header);
read_line_from_stdin([]).
We may also need to transform a string like "123.456.789.123" into an integer IP address.
define Bool
is_digit
(
Word8 c
) =
with n = word8_to_Word32(c),
if n < '0' then false else n =< '9'.
define Maybe((Int32,Int32)) // returns the next position for reading and a label.
read_label
(
String s,
Int32 i,
Int32 so_far
) =
if nth(i,s) is
{
failure then success((i,so_far)),
success(c) then
if c = '.'
then success((i+1,so_far))
else if is_digit(c)
then read_label(s,i+1,10*so_far+word8_to_Word32(c)-'0')
else failure
}.
public define Maybe(Int32)
ip_address
(
String s
) =
if read_label(s,0,0) is
{
failure then failure,
success(p1) then if p1 is (i2,lab1) then
if read_label(s,i2,0) is
{
failure then failure,
success(p2) then if p2 is (i3,lab2) then
if read_label(s,i3,0) is
{
failure then failure,
success(p3) then if p3 is (i4,lab3) then
if read_label(s,i4,0) is
{
failure then failure,
success(p4) then if p4 is (i4a,lab4) then
if i4a = length(s)
then success((lab4<<24)|(lab3<<16)|(lab2<<8)|lab1)
else failure
}
}
}
}.
define TestItem
server_test
=
test_item("TCP/IP client/server test",
(One u) |->
with port = (Int32)4321,
local_host = if ip_address(prompt("IP address: ")) is
{
failure then ip_address((127,0,0,1)),
success(n) then n
},
print("\nStarting a server on port number "+port+"\n");
with server = force_start_server(0,port,(Server s) |-> server_handler,(One u) |-> u,0),
print("\nServer started.");
print("\nClient: Opening a connection to the server.");
if connect(local_host,port) is
{
error(msg) then print(if msg is
{
cannot_create_the_socket then "Cannot create the socket.\n"
address_port_not_available then "Address:port not available.\n"
connection_refused then "Connection refused.\n"
network_unreachable then "Network unreachable.\n"
address_port_already_in_use then "Address:port already in use.\n"
out_of_time then "Out of time.\n"
}),
ok(conn) then
client_handler(conn,server);
print("\nShutting the server down.");
shutdown(server)
}
).
*** (1.4) SSL client/server test.
define String
ssl_receive_message
(
SSL_Connection conn,
List(Word8) so_far
) =
if (Maybe(String))read(conn,1,60) is
{
failure then implode(reverse(so_far)),
success(str) then if nth(0,str) is
{
failure then implode(reverse(so_far)),
success(c) then
if c = '?' then implode(reverse([c . so_far])) else
if c = '!' then implode(reverse([c . so_far])) else
ssl_receive_message(conn,[c . so_far])
}
}.
define One
ssl_send_message
(
String me,
SSL_Connection conn,
String message,
Int32 i
) =
if write(conn,message) is
{
failure then print("\n"+me+": Cannot write into SSL connection."),
success(n) then
if n = length(message)
then unique
else print("\n"+me+": Was not able to write the whole message into SSL connection.")
}.
define One
ssl_server_handler
(
SSL_Connection conn
) =
with answer = "Very well, thank you !",
if local_SSL_address_and_port(conn) is (loc_a,loc_p) then
print("\nServer: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p);
if remote_SSL_address_and_port(conn) is (rem_a,rem_p) then
print("\nServer: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p);
print("\nServer: Waiting for a question.");
with question = ssl_receive_message(conn,[]),
print("\nServer: question is: '"+question+"'");
print("\nServer: Sending message: '"+answer+"'");
ssl_send_message("Server",conn,answer,0).
define One
ssl_client_handler
(
SSL_Connection conn,
Server server
) =
with question = "How do you do ?",
if local_SSL_address_and_port(conn) is (loc_a,loc_p) then
print("\nClient: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p);
if remote_SSL_address_and_port(conn) is (rem_a,rem_p) then
print("\nClient: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p);
print("\nClient: Sending message: '"+question+"'");
ssl_send_message("Client",conn,question,0);
print("\nClient: Waiting for the answer.");
with answer = ssl_receive_message(conn,[]),
print("\nClient: Answer is: '"+answer+"'").
define Server
force_start_ssl_server
(
Int32 address,
Int32 port,
String server_name,
Server -> ((SSL_Connection) -> One) handler,
One -> One notify,
Int32 retry
) =
if start_ssl_server(address,port,server_name,handler,notify) is
{
cannot_create_the_socket then print("\nCannot create the listening socket."); alert,
cannot_bind_to_port then sleep(1000);
print("\rRetry number "+(retry+1)+" ");
force_start_ssl_server(address,port,server_name,handler,notify,retry+1),
cannot_listen_on_port then print("\nCannot listen on port "+port); alert,
ok(server) then server
}.
define (Maybe(X509)) -> Bool
policy
(
String server_name,
) =
(Maybe(X509) mb_cert) |->
if mb_cert is
{
failure then print("\nClient: No SSL certificate received from server."); true,
success(cert) then
print("\nClient Invalid server certificate:\n");
print(to_string(cert));
print("\n");
true
}.
define TestItem
ssl_server_test
=
with server_name = "Shadoko",
ask_test_item("SSL client/server test",
(One u) |->
with port = (Int32)5432,
local_host = ip_address((127,0,0,1)),
print("\nStarting an SSL server on port number "+port+"\n");
with server = force_start_ssl_server(0,port,server_name,
(Server s) |-> ssl_server_handler,(One u) |-> u,0),
print("\nSSL server started.");
print("\nClient: Opening an SSL connection to the SSL server.");
if open_SSL_connection(server_name,local_host,port,policy(server_name)) is
{
error(msg) then print("\nClient: Cannot connect to SSL server."),
ok(conn) then
ssl_client_handler(conn,server);
print("\nShutting the server down.");
shutdown(server)
}
).
*** (2) Running all tests.
The list of all tests:
define List(TestItem)
all_tests
=
[
test_ver_dir,
test_env_vars,
test_time,
execute_test,
server_test,
ssl_server_test
].
Selecting test whose banner contains a word
Finding a substring into a string.
define Bool
is_substring // test if 'b' is a substring of 'a'
(
String a,
String b,
Int32 i
) =
if nth(i,a) is
{
failure then false,
success(_) then
if sub_string(a,i,length(b)) is
{
failure then is_substring(a,b,i+1),
success(b1) then
if b = b1
then true
else is_substring(a,b,i+1)
}
}.
define List(TestItem)
select
(
String key,
List(TestItem) all
) =
if all is
{
[ ] then [ ],
[h . t] then
if is_substring(banner(h),key,0)
then [h . select(key,t)]
else select(key,t)
}.
Performing the tests:
global define One
vm_test
(
List(String) args
) =
if args is
{
[ ] then forget(map(run_test,all_tests)),
[h . _] then forget(map(run_test,select(h,all_tests)))
}.