-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcode_test.cs
More file actions
3339 lines (2374 loc) · 133 KB
/
netcode_test.cs
File metadata and controls
3339 lines (2374 loc) · 133 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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
namespace networkprotocol
{
public static partial class netcode
{
static void check_handler(string condition, string function, string file, int line)
{
Console.Write($"check failed: ( {condition} ), function {function}, file {file}, line {line}\n");
Debugger.Break();
Environment.Exit(1);
}
public static void check(bool condition)
{
if (!condition)
{
var stackFrame = new StackTrace().GetFrame(1);
check_handler("n/a", stackFrame.GetMethod().Name, stackFrame.GetFileName(), stackFrame.GetFileLineNumber());
}
}
static void test_queue()
{
var queue = new packet_queue_t();
packet_queue_init(queue, null, null, null);
check(queue.num_packets == 0);
check(queue.start_index == 0);
// attempting to pop a packet off an empty queue should return null
check(packet_queue_pop(queue, out var na) == null);
// add some packets to the queue and make sure they pop off in the correct order
{
const int NUM_PACKETS = 100;
var packets2 = new object[NUM_PACKETS];
int i2;
for (i2 = 0; i2 < NUM_PACKETS; ++i2)
{
packets2[i2] = new byte[(i2 + 1) * 256];
check(packet_queue_push(queue, ref packets2[i2], (ulong)i2));
}
check(queue.num_packets == NUM_PACKETS);
for (i2 = 0; i2 < NUM_PACKETS; ++i2)
{
var packet = packet_queue_pop(queue, out var sequence);
check(sequence == (ulong)i2);
check(packet == packets2[i2]);
packet = null;
}
}
// after all entries are popped off, the queue is empty, so calls to pop should return null
check(queue.num_packets == 0);
check(packet_queue_pop(queue, out var na2) == null);
// test that the packet queue can be filled to max capacity
var packets = new object[PACKET_QUEUE_SIZE];
int i;
for (i = 0; i < PACKET_QUEUE_SIZE; ++i)
{
packets[i] = new byte[i * 256];
check(packet_queue_push(queue, ref packets[i], (ulong)i));
}
check(queue.num_packets == PACKET_QUEUE_SIZE);
// when the queue is full, attempting to push a packet should fail and return 0
var packet3 = (object)new byte[100];
check(!packet_queue_push(queue, ref packet3, 0));
// make sure all packets pop off in the correct order
for (i = 0; i < PACKET_QUEUE_SIZE; ++i)
{
var packet = packet_queue_pop(queue, out var sequence);
check(sequence == (ulong)i);
check(packet == packets[i]);
packet = null;
}
// add some packets again
for (i = 0; i < PACKET_QUEUE_SIZE; ++i)
{
packets[i] = new byte[i * 256];
check(packet_queue_push(queue, ref packets[i], (ulong)i));
}
// clear the queue and make sure that all packets are freed
packet_queue_clear(queue);
check(queue.start_index == 0);
check(queue.num_packets == 0);
for (i = 0; i < PACKET_QUEUE_SIZE; ++i)
check(queue.packet_data[i] == null);
}
static void test_endian()
{
const ulong value = 0x11223344U;
var bytes = BitConverter.GetBytes(value);
check(bytes[0] == 0x44);
check(bytes[1] == 0x33);
check(bytes[2] == 0x22);
check(bytes[3] == 0x11);
//check(bytes[3] == 0x44);
//check(bytes[2] == 0x33);
//check(bytes[1] == 0x22);
//check(bytes[0] == 0x11);
}
static void test_sequence()
{
check(sequence_number_bytes_required(0) == 1);
check(sequence_number_bytes_required(0x11) == 1);
check(sequence_number_bytes_required(0x1122) == 2);
check(sequence_number_bytes_required(0x112233) == 3);
check(sequence_number_bytes_required(0x11223344) == 4);
check(sequence_number_bytes_required(0x1122334455) == 5);
check(sequence_number_bytes_required(0x112233445566) == 6);
check(sequence_number_bytes_required(0x11223344556677) == 7);
check(sequence_number_bytes_required(0x1122334455667788) == 8);
}
static void test_address()
{
{
check(parse_address("", out var address) == ERROR);
check(parse_address("[", out address) == ERROR);
check(parse_address("[]", out address) == ERROR);
check(parse_address("[]:", out address) == ERROR);
check(parse_address(":", out address) == ERROR);
//check(parse_address("1", out address) == ERROR);
//check(parse_address("12", out address) == ERROR);
//check(parse_address("123", out address) == ERROR);
//check(parse_address("1234", out address) == ERROR);
check(parse_address("1234.0.12313.0000", out address) == ERROR);
check(parse_address("1234.0.12313.0000.0.0.0.0.0", out address) == ERROR);
check(parse_address("1312313:123131:1312313:123131:1312313:123131:1312313:123131:1312313:123131:1312313:123131", out address) == ERROR);
check(parse_address(".", out address) == ERROR);
check(parse_address("..", out address) == ERROR);
check(parse_address("...", out address) == ERROR);
check(parse_address("....", out address) == ERROR);
check(parse_address(".....", out address) == ERROR);
}
{
check(parse_address("107.77.207.77", out var address) == OK);
check(address.type == ADDRESS_IPV4);
check(address.port == 0);
check(BufferEx.Equal(address.data.GetAddressBytes(), new byte[] {
107, 77, 207, 77
}, 4));
}
{
check(parse_address("127.0.0.1", out var address) == OK);
check(address.type == ADDRESS_IPV4);
check(address.port == 0);
check(BufferEx.Equal(address.data.GetAddressBytes(), new byte[] {
127, 0, 0, 1
}, 4));
}
{
check(parse_address("107.77.207.77:40000", out var address) == OK);
check(address.type == ADDRESS_IPV4);
check(address.port == 40000);
check(BufferEx.Equal(address.data.GetAddressBytes(), new byte[] {
107, 77, 207, 77
}, 4));
}
{
check(parse_address("127.0.0.1:40000", out var address) == OK);
check(address.type == ADDRESS_IPV4);
check(address.port == 40000);
check(BufferEx.Equal(address.data.GetAddressBytes(), new byte[] {
127, 0, 0, 1
}, 4));
}
{
check(parse_address("fe80::202:b3ff:fe1e:8329", out var address) == OK);
check(address.type == ADDRESS_IPV6);
check(address.port == 0);
check(BufferEx.Equal(address.data.GetAddressBytes(), new ushort[] {
0xfe80, 0x0000, 0x0000, 0x0000, 0x0202, 0xb3ff, 0xfe1e, 0x8329
}.SelectMany(z => BitConverter.GetBytes(z).Reverse()).ToArray(), 16));
}
{
check(parse_address("::", out var address) == OK);
check(address.type == ADDRESS_IPV6);
check(address.port == 0);
check(BufferEx.Equal(address.data.GetAddressBytes(), new ushort[] {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
}.SelectMany(z => BitConverter.GetBytes(z).Reverse()).ToArray(), 16));
}
{
check(parse_address("::1", out var address) == OK);
check(address.type == ADDRESS_IPV6);
check(address.port == 0);
check(BufferEx.Equal(address.data.GetAddressBytes(), new ushort[] {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001
}.SelectMany(z => BitConverter.GetBytes(z).Reverse()).ToArray(), 16));
}
{
check(parse_address("[fe80::202:b3ff:fe1e:8329]:40000", out var address) == OK);
check(address.type == ADDRESS_IPV6);
check(address.port == 40000);
check(BufferEx.Equal(address.data.GetAddressBytes(), new ushort[] {
0xfe80, 0x0000, 0x0000, 0x0000, 0x0202, 0xb3ff, 0xfe1e, 0x8329
}.SelectMany(z => BitConverter.GetBytes(z).Reverse()).ToArray(), 16));
}
{
check(parse_address("[::]:40000", out var address) == OK);
check(address.type == ADDRESS_IPV6);
check(address.port == 40000);
check(BufferEx.Equal(address.data.GetAddressBytes(), new ushort[] {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
}.SelectMany(z => BitConverter.GetBytes(z).Reverse()).ToArray(), 16));
}
{
check(parse_address("[::1]:40000", out var address) == OK);
check(address.type == ADDRESS_IPV6);
check(address.port == 40000);
check(BufferEx.Equal(address.data.GetAddressBytes(), new ushort[] {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001
}.SelectMany(z => BitConverter.GetBytes(z).Reverse()).ToArray(), 16));
}
}
const ulong TEST_PROTOCOL_ID = 0x1122334455667788UL;
const ulong TEST_CLIENT_ID = 0x1UL;
const int TEST_SERVER_PORT = 40000;
const int TEST_CONNECT_TOKEN_EXPIRY = 30;
const int TEST_TIMEOUT_SECONDS = 15;
static void test_connect_token()
{
// generate a connect token
var server_address = new netcode_address_t();
server_address.type = ADDRESS_IPV4;
server_address.data = IPAddress.Loopback;
server_address.port = TEST_SERVER_PORT;
var user_data = new byte[USER_DATA_BYTES];
random_bytes(user_data, USER_DATA_BYTES);
var input_token = new connect_token_private_t();
generate_connect_token_private(input_token, TEST_CLIENT_ID, TEST_TIMEOUT_SECONDS, 1, new[] { server_address }, user_data);
check(input_token.client_id == TEST_CLIENT_ID);
check(input_token.num_server_addresses == 1);
check(BufferEx.Equal(input_token.user_data, user_data, USER_DATA_BYTES));
check(address_equal(input_token.server_addresses[0], server_address));
// write it to a buffer
var buffer = new byte[CONNECT_TOKEN_PRIVATE_BYTES];
write_connect_token_private(input_token, buffer, CONNECT_TOKEN_PRIVATE_BYTES);
// encrypt the buffer
var expire_timestamp = (ulong)(DateTime.Now.Ticks + 30);
var nonce = new byte[CONNECT_TOKEN_NONCE_BYTES];
generate_nonce(nonce);
var key = new byte[KEY_BYTES];
generate_key(key);
check(encrypt_connect_token_private(
buffer, 0,
CONNECT_TOKEN_PRIVATE_BYTES,
VERSION_INFO,
TEST_PROTOCOL_ID,
expire_timestamp,
nonce,
key) == OK);
// decrypt the buffer
check(decrypt_connect_token_private(
buffer, 0,
CONNECT_TOKEN_PRIVATE_BYTES,
VERSION_INFO,
TEST_PROTOCOL_ID,
expire_timestamp,
nonce,
key) == OK);
// read the connect token back in
var output_token = new connect_token_private_t();
check(read_connect_token_private(buffer, CONNECT_TOKEN_PRIVATE_BYTES, output_token) == OK);
// make sure that everything matches the original connect token
check(output_token.client_id == input_token.client_id);
check(output_token.timeout_seconds == input_token.timeout_seconds);
check(output_token.num_server_addresses == input_token.num_server_addresses);
check(address_equal(output_token.server_addresses[0], input_token.server_addresses[0]));
check(BufferEx.Equal(output_token.client_to_server_key, input_token.client_to_server_key, KEY_BYTES));
check(BufferEx.Equal(output_token.server_to_client_key, input_token.server_to_client_key, KEY_BYTES));
check(BufferEx.Equal(output_token.user_data, input_token.user_data, USER_DATA_BYTES));
}
static void test_challenge_token()
{
// generate a challenge token
var input_token = new challenge_token_t();
input_token.client_id = TEST_CLIENT_ID;
random_bytes(input_token.user_data, USER_DATA_BYTES);
// write it to a buffer
var buffer = new byte[CHALLENGE_TOKEN_BYTES];
write_challenge_token(input_token, buffer, CHALLENGE_TOKEN_BYTES);
// encrypt the buffer
var sequence = 1000UL;
var key = new byte[KEY_BYTES];
generate_key(key);
check(encrypt_challenge_token(buffer, 0, CHALLENGE_TOKEN_BYTES, sequence, key) == OK);
// decrypt the buffer
check(decrypt_challenge_token(buffer, 0, CHALLENGE_TOKEN_BYTES, sequence, key) == OK);
// read the challenge token back in
var output_token = new challenge_token_t();
check(read_challenge_token(buffer, CHALLENGE_TOKEN_BYTES, output_token) == OK);
// make sure that everything matches the original challenge token
check(output_token.client_id == input_token.client_id);
check(BufferEx.Equal(output_token.user_data, input_token.user_data, USER_DATA_BYTES));
}
static void test_connection_request_packet()
{
// generate a connect token
var server_address = new netcode_address_t();
server_address.type = ADDRESS_IPV4;
server_address.data = IPAddress.Loopback;
server_address.port = TEST_SERVER_PORT;
var user_data = new byte[USER_DATA_BYTES];
random_bytes(user_data, USER_DATA_BYTES);
var input_token = new connect_token_private_t();
generate_connect_token_private(input_token, TEST_CLIENT_ID, TEST_TIMEOUT_SECONDS, 1, new[] { server_address }, user_data);
check(input_token.client_id == TEST_CLIENT_ID);
check(input_token.num_server_addresses == 1);
check(BufferEx.Equal(input_token.user_data, user_data, USER_DATA_BYTES));
check(address_equal(input_token.server_addresses[0], server_address));
// write the conect token to a buffer (non-encrypted)
var connect_token_data = new byte[CONNECT_TOKEN_PRIVATE_BYTES];
write_connect_token_private(input_token, connect_token_data, CONNECT_TOKEN_PRIVATE_BYTES);
// copy to a second buffer then encrypt it in place (we need the unencrypted token for verification later on)
var encrypted_connect_token_data = new byte[CONNECT_TOKEN_PRIVATE_BYTES];
BufferEx.Copy(encrypted_connect_token_data, connect_token_data, CONNECT_TOKEN_PRIVATE_BYTES);
var connect_token_expire_timestamp = (ulong)(DateTime.Now.Ticks + 30);
var connect_token_nonce = new byte[CONNECT_TOKEN_NONCE_BYTES];
generate_nonce(connect_token_nonce);
var connect_token_key = new byte[KEY_BYTES];
generate_key(connect_token_key);
check(encrypt_connect_token_private(
encrypted_connect_token_data, 0,
CONNECT_TOKEN_PRIVATE_BYTES,
VERSION_INFO,
TEST_PROTOCOL_ID,
connect_token_expire_timestamp,
connect_token_nonce,
connect_token_key) == OK);
// setup a connection request packet wrapping the encrypted connect token
var input_packet = new connection_request_packet_t();
input_packet.packet_type = CONNECTION_REQUEST_PACKET;
BufferEx.Copy(input_packet.version_info, VERSION_INFO, VERSION_INFO_BYTES);
input_packet.protocol_id = TEST_PROTOCOL_ID;
input_packet.connect_token_expire_timestamp = connect_token_expire_timestamp;
BufferEx.Copy(input_packet.connect_token_nonce, connect_token_nonce, CONNECT_TOKEN_NONCE_BYTES);
BufferEx.Copy(input_packet.connect_token_data, encrypted_connect_token_data, CONNECT_TOKEN_PRIVATE_BYTES);
// write the connection request packet to a buffer
var buffer = new byte[2048];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the connection request packet back in from the buffer (the connect token data is decrypted as part of the read packet validation)
var allowed_packets = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packets, 1);
var output_packet = (connection_request_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), connect_token_key, allowed_packets, null, null, null);
check(output_packet != null);
// make sure the read packet matches what was written
check(output_packet.packet_type == CONNECTION_REQUEST_PACKET);
check(BufferEx.Equal(output_packet.version_info, input_packet.version_info, VERSION_INFO_BYTES));
check(output_packet.protocol_id == input_packet.protocol_id);
check(output_packet.connect_token_expire_timestamp == input_packet.connect_token_expire_timestamp);
check(BufferEx.Equal(output_packet.connect_token_nonce, input_packet.connect_token_nonce, CONNECT_TOKEN_NONCE_BYTES));
check(BufferEx.Equal(output_packet.connect_token_data, connect_token_data, CONNECT_TOKEN_PRIVATE_BYTES - MAC_BYTES));
output_packet = null;
}
static void test_connection_denied_packet()
{
// setup a connection denied packet
var input_packet = new connection_denied_packet_t();
input_packet.packet_type = CONNECTION_DENIED_PACKET;
// write the packet to a buffer
var buffer = new byte[MAX_PACKET_BYTES];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the packet back in from the buffer
var allowed_packet_types = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packet_types, 1);
var output_packet = (connection_denied_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), null, allowed_packet_types, null, null, null);
check(output_packet != null);
// make sure the read packet matches what was written
check(output_packet.packet_type == CONNECTION_DENIED_PACKET);
output_packet = null;
}
static void test_connection_challenge_packet()
{
// setup a connection challenge packet
var input_packet = new connection_challenge_packet_t();
input_packet.packet_type = CONNECTION_CHALLENGE_PACKET;
input_packet.challenge_token_sequence = 0;
random_bytes(input_packet.challenge_token_data, CHALLENGE_TOKEN_BYTES);
// write the packet to a buffer
var buffer = new byte[MAX_PACKET_BYTES];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the packet back in from the buffer
var allowed_packet_types = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packet_types, 1);
var output_packet = (connection_challenge_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), null, allowed_packet_types, null, null, null);
check(output_packet != null);
// make sure the read packet packet matches what was written
check(output_packet.packet_type == CONNECTION_CHALLENGE_PACKET);
check(output_packet.challenge_token_sequence == input_packet.challenge_token_sequence);
check(BufferEx.Equal(output_packet.challenge_token_data, input_packet.challenge_token_data, CHALLENGE_TOKEN_BYTES));
output_packet = null;
}
static void test_connection_response_packet()
{
// setup a connection response packet
var input_packet = new connection_response_packet_t();
input_packet.packet_type = CONNECTION_RESPONSE_PACKET;
input_packet.challenge_token_sequence = 0;
random_bytes(input_packet.challenge_token_data, CHALLENGE_TOKEN_BYTES);
// write the packet to a buffer
var buffer = new byte[MAX_PACKET_BYTES];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the packet back in from the buffer
var allowed_packet_types = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packet_types, 1);
var output_packet = (connection_response_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), null, allowed_packet_types, null, null, null);
check(output_packet != null);
// make sure the read packet matches what was written
check(output_packet.packet_type == CONNECTION_RESPONSE_PACKET);
check(output_packet.challenge_token_sequence == input_packet.challenge_token_sequence);
check(BufferEx.Equal(output_packet.challenge_token_data, input_packet.challenge_token_data, CHALLENGE_TOKEN_BYTES));
output_packet = null;
}
static void test_connection_keep_alive_packet()
{
// setup a connection keep alive packet
var input_packet = new connection_keep_alive_packet_t();
input_packet.packet_type = CONNECTION_KEEP_ALIVE_PACKET;
input_packet.client_index = 10;
input_packet.max_clients = 16;
// write the packet to a buffer
var buffer = new byte[MAX_PACKET_BYTES];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the packet back in from the buffer
var allowed_packet_types = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packet_types, 1);
var output_packet = (connection_keep_alive_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), null, allowed_packet_types, null, null, null);
check(output_packet != null);
// make sure the read packet matches what was written
check(output_packet.packet_type == CONNECTION_KEEP_ALIVE_PACKET);
check(output_packet.client_index == input_packet.client_index);
check(output_packet.max_clients == input_packet.max_clients);
output_packet = null;
}
static void test_connection_payload_packet()
{
// setup a connection payload packet
var input_packet = create_payload_packet(MAX_PAYLOAD_BYTES, null, null);
check(input_packet.packet_type == CONNECTION_PAYLOAD_PACKET);
check(input_packet.payload_bytes == MAX_PAYLOAD_BYTES);
random_bytes(input_packet.payload_data, MAX_PAYLOAD_BYTES);
// write the packet to a buffer
var buffer = new byte[MAX_PACKET_BYTES];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the packet back in from the buffer
var allowed_packet_types = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packet_types, 1);
var output_packet = (connection_payload_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), null, allowed_packet_types, null, null, null);
check(output_packet != null);
// make sure the read packet matches what was written
check(output_packet.packet_type == CONNECTION_PAYLOAD_PACKET);
check(output_packet.payload_bytes == input_packet.payload_bytes);
check(BufferEx.Equal(output_packet.payload_data, input_packet.payload_data, MAX_PAYLOAD_BYTES));
input_packet = null;
output_packet = null;
}
static void test_connection_disconnect_packet()
{
// setup a connection disconnect packet
var input_packet = new connection_disconnect_packet_t();
input_packet.packet_type = CONNECTION_DISCONNECT_PACKET;
// write the packet to a buffer
var buffer = new byte[MAX_PACKET_BYTES];
var packet_key = new byte[KEY_BYTES];
generate_key(packet_key);
var bytes_written = write_packet(input_packet, buffer, buffer.Length, 1000, packet_key, TEST_PROTOCOL_ID);
check(bytes_written > 0);
// read the packet back in from the buffer
var allowed_packet_types = new bool[CONNECTION_NUM_PACKETS];
BufferEx.Set(allowed_packet_types, 1);
var output_packet = (connection_disconnect_packet_t)read_packet(buffer, bytes_written, out var sequence, packet_key, TEST_PROTOCOL_ID, ctime(), null, allowed_packet_types, null, null, null);
check(output_packet != null);
// make sure the read packet matches what was written
check(output_packet.packet_type == CONNECTION_DISCONNECT_PACKET);
output_packet = null;
}
static void test_connect_token_public()
{
// generate a private connect token
var server_address = new netcode_address_t();
server_address.type = ADDRESS_IPV4;
server_address.data = IPAddress.Loopback;
server_address.port = TEST_SERVER_PORT;
var user_data = new byte[USER_DATA_BYTES];
random_bytes(user_data, USER_DATA_BYTES);
var connect_token_private = new connect_token_private_t();
generate_connect_token_private(connect_token_private, TEST_CLIENT_ID, TEST_TIMEOUT_SECONDS, 1, new[] { server_address }, user_data);
check(connect_token_private.client_id == TEST_CLIENT_ID);
check(connect_token_private.num_server_addresses == 1);
check(BufferEx.Equal(connect_token_private.user_data, user_data, USER_DATA_BYTES));
check(address_equal(connect_token_private.server_addresses[0], server_address));
// write it to a buffer
var connect_token_private_data = new byte[CONNECT_TOKEN_PRIVATE_BYTES];
write_connect_token_private(connect_token_private, connect_token_private_data, CONNECT_TOKEN_PRIVATE_BYTES);
// encrypt the buffer
var create_timestamp = ctime();
var expire_timestamp = create_timestamp + 30;
var connect_token_nonce = new byte[CONNECT_TOKEN_NONCE_BYTES];
generate_nonce(connect_token_nonce);
var key = new byte[KEY_BYTES];
generate_key(key);
check(encrypt_connect_token_private(
connect_token_private_data, 0,
CONNECT_TOKEN_PRIVATE_BYTES,
VERSION_INFO,
TEST_PROTOCOL_ID,
expire_timestamp,
connect_token_nonce,
key) == 1);
// wrap a public connect token around the private connect token data
var input_connect_token = new connect_token_t();
BufferEx.Copy(input_connect_token.version_info, VERSION_INFO, VERSION_INFO_BYTES);
input_connect_token.protocol_id = TEST_PROTOCOL_ID;
input_connect_token.create_timestamp = create_timestamp;
input_connect_token.expire_timestamp = expire_timestamp;
BufferEx.Copy(input_connect_token.nonce, connect_token_nonce, CONNECT_TOKEN_NONCE_BYTES);
BufferEx.Copy(input_connect_token.private_data, connect_token_private_data, CONNECT_TOKEN_PRIVATE_BYTES);
input_connect_token.num_server_addresses = 1;
input_connect_token.server_addresses[0] = server_address;
BufferEx.Copy(input_connect_token.client_to_server_key, connect_token_private.client_to_server_key, KEY_BYTES);
BufferEx.Copy(input_connect_token.server_to_client_key, connect_token_private.server_to_client_key, KEY_BYTES);
input_connect_token.timeout_seconds = TEST_TIMEOUT_SECONDS;
// write the connect token to a buffer
var buffer = new byte[CONNECT_TOKEN_BYTES];
write_connect_token(input_connect_token, buffer, CONNECT_TOKEN_BYTES);
// read the buffer back in
var output_connect_token = new connect_token_t();
check(read_connect_token(buffer, CONNECT_TOKEN_BYTES, output_connect_token) == 1);
// make sure the public connect token matches what was written
check(BufferEx.Equal(output_connect_token.version_info, input_connect_token.version_info, VERSION_INFO_BYTES));
check(output_connect_token.protocol_id == input_connect_token.protocol_id);
check(output_connect_token.create_timestamp == input_connect_token.create_timestamp);
check(output_connect_token.expire_timestamp == input_connect_token.expire_timestamp);
check(BufferEx.Equal(output_connect_token.nonce, input_connect_token.nonce, CONNECT_TOKEN_NONCE_BYTES));
check(BufferEx.Equal(output_connect_token.private_data, input_connect_token.private_data, CONNECT_TOKEN_PRIVATE_BYTES));
check(output_connect_token.num_server_addresses == input_connect_token.num_server_addresses);
check(address_equal(output_connect_token.server_addresses[0], input_connect_token.server_addresses[0]));
check(BufferEx.Equal(output_connect_token.client_to_server_key, input_connect_token.client_to_server_key, KEY_BYTES));
check(BufferEx.Equal(output_connect_token.server_to_client_key, input_connect_token.server_to_client_key, KEY_BYTES));
check(output_connect_token.timeout_seconds == input_connect_token.timeout_seconds);
}
internal class encryption_mapping_t
{
public netcode_address_t address = new netcode_address_t();
public byte[] send_key = new byte[KEY_BYTES];
public byte[] receive_key = new byte[KEY_BYTES];
}
const int NUM_ENCRYPTION_MAPPINGS = 5;
static void test_encryption_manager()
{
var encryption_manager = new encryption_manager_t();
encryption_manager_reset(encryption_manager);
var time = 100.0;
// generate some test encryption mappings
var encryption_mapping = new encryption_mapping_t[NUM_ENCRYPTION_MAPPINGS];
BufferEx.SetT(encryption_mapping, 0);
int i;
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
encryption_mapping[i].address.type = ADDRESS_IPV6;
encryption_mapping[i].address.data = IPAddress.Loopback;
encryption_mapping[i].address.port = (ushort)(20000 + i);
generate_key(encryption_mapping[i].send_key);
generate_key(encryption_mapping[i].receive_key);
}
// add the encryption mappings to the manager and make sure they can be looked up by address
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
var encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
check(encryption_index == -1);
check(encryption_manager_get_send_key(encryption_manager, encryption_index) == null);
check(encryption_manager_get_receive_key(encryption_manager, encryption_index) == null);
check(encryption_manager_add_encryption_mapping(
encryption_manager,
encryption_mapping[i].address,
encryption_mapping[i].send_key,
encryption_mapping[i].receive_key,
time,
-1.0,
TEST_TIMEOUT_SECONDS));
encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
var send_key = encryption_manager_get_send_key(encryption_manager, encryption_index);
var receive_key = encryption_manager_get_receive_key(encryption_manager, encryption_index);
check(send_key != null);
check(receive_key != null);
check(BufferEx.Equal(send_key, encryption_mapping[i].send_key, KEY_BYTES));
check(BufferEx.Equal(receive_key, encryption_mapping[i].receive_key, KEY_BYTES));
}
// removing an encryption mapping that doesn't exist should return 0
{
var address = new netcode_address_t();
address.type = ADDRESS_IPV6;
address.data = IPAddress.Loopback;
address.port = 50000;
check(!encryption_manager_remove_encryption_mapping(encryption_manager, address, time));
}
// remove the first and last encryption mappings
check(encryption_manager_remove_encryption_mapping(encryption_manager, encryption_mapping[0].address, time));
check(encryption_manager_remove_encryption_mapping(encryption_manager, encryption_mapping[NUM_ENCRYPTION_MAPPINGS - 1].address, time));
// make sure the encryption mappings that were removed can no longer be looked up by address
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
var encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
var send_key = encryption_manager_get_send_key(encryption_manager, encryption_index);
var receive_key = encryption_manager_get_receive_key(encryption_manager, encryption_index);
if (i != 0 && i != NUM_ENCRYPTION_MAPPINGS - 1)
{
check(send_key != null);
check(receive_key != null);
check(BufferEx.Equal(send_key, encryption_mapping[i].send_key, KEY_BYTES));
check(BufferEx.Equal(receive_key, encryption_mapping[i].receive_key, KEY_BYTES));
}
else
{
check(send_key == null);
check(receive_key == null);
}
}
// add the encryption mappings back in
check(encryption_manager_add_encryption_mapping(
encryption_manager,
encryption_mapping[0].address,
encryption_mapping[0].send_key,
encryption_mapping[0].receive_key,
time,
-1.0,
TEST_TIMEOUT_SECONDS));
check(encryption_manager_add_encryption_mapping(
encryption_manager,
encryption_mapping[NUM_ENCRYPTION_MAPPINGS - 1].address,
encryption_mapping[NUM_ENCRYPTION_MAPPINGS - 1].send_key,
encryption_mapping[NUM_ENCRYPTION_MAPPINGS - 1].receive_key,
time,
-1.0,
TEST_TIMEOUT_SECONDS));
// all encryption mappings should be able to be looked up by address again
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
var encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
var send_key = encryption_manager_get_send_key(encryption_manager, encryption_index);
var receive_key = encryption_manager_get_receive_key(encryption_manager, encryption_index);
check(send_key != null);
check(receive_key != null);
check(BufferEx.Equal(send_key, encryption_mapping[i].send_key, KEY_BYTES));
check(BufferEx.Equal(receive_key, encryption_mapping[i].receive_key, KEY_BYTES));
}
// check that encryption mappings time out properly
time += TEST_TIMEOUT_SECONDS * 2;
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
var encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
var send_key = encryption_manager_get_send_key(encryption_manager, encryption_index);
var receive_key = encryption_manager_get_receive_key(encryption_manager, encryption_index);
check(send_key == null);
check(receive_key == null);
}
// add the same encryption mappings after timeout
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
var encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
check(encryption_index == -1);
check(encryption_manager_get_send_key(encryption_manager, encryption_index) == null);
check(encryption_manager_get_receive_key(encryption_manager, encryption_index) == null);
check(encryption_manager_add_encryption_mapping(
encryption_manager,
encryption_mapping[i].address,
encryption_mapping[i].send_key,
encryption_mapping[i].receive_key,
time,
-1.0,
TEST_TIMEOUT_SECONDS));
encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
var send_key = encryption_manager_get_send_key(encryption_manager, encryption_index);
var receive_key = encryption_manager_get_receive_key(encryption_manager, encryption_index);
check(send_key != null);
check(receive_key != null);
check(BufferEx.Equal(send_key, encryption_mapping[i].send_key, KEY_BYTES));
check(BufferEx.Equal(receive_key, encryption_mapping[i].receive_key, KEY_BYTES));
}
// reset the encryption mapping and verify that all encryption mappings have been removed
encryption_manager_reset(encryption_manager);
for (i = 0; i < NUM_ENCRYPTION_MAPPINGS; ++i)
{
var encryption_index = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[i].address, time);
var send_key = encryption_manager_get_send_key(encryption_manager, encryption_index);
var receive_key = encryption_manager_get_receive_key(encryption_manager, encryption_index);
check(send_key == null);
check(receive_key == null);
}
// test the expire time for encryption mapping works as expected
check(encryption_manager_add_encryption_mapping(
encryption_manager,
encryption_mapping[0].address,
encryption_mapping[0].send_key,
encryption_mapping[0].receive_key,
time,
time + 1.0,
TEST_TIMEOUT_SECONDS));
var encryption_index2 = encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[0].address, time);
check(encryption_index2 != -1);
check(encryption_manager_find_encryption_mapping(encryption_manager, encryption_mapping[0].address, time + 1.1f) == -1);
encryption_manager_set_expire_time(encryption_manager, encryption_index2, -1.0);