-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.cpp
More file actions
1667 lines (1344 loc) · 49.5 KB
/
Copy pathunit_tests.cpp
File metadata and controls
1667 lines (1344 loc) · 49.5 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
/*
* MIT License
*
* Copyright (c) 2025 Salem B.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <array>
#include "jlizard/byte_array.h"
#include <cassert>
#include <functional>
#include <sstream>
#include <iostream>
using namespace jlizard;
#define PRINT_PASSED() do{ std::cout << __func__ << " passed!" << std::endl; }while(0)
// Helper function to capture stderr output during tests
std::string capture_stderr(const std::function<void()>& func) {
std::stringstream buffer;
std::streambuf* old_cerr = std::cerr.rdbuf(buffer.rdbuf());
func();
std::cerr.rdbuf(old_cerr);
return buffer.str();
}
// Test hex string constructor
void test_hex_string_constructor() {
ByteArray b1("fe81eabd5");
assert(b1.size() == 5);
assert(b1[0] == 0xfe);
assert(b1[1] == 0x81);
assert(b1[2] == 0xea);
assert(b1[3] == 0xbd);
assert(b1[4] == 0x05);
ByteArray b2("abcdef0123");
assert(b2.size() == 5);
assert(b2[0] == 0xab);
assert(b2[1] == 0xcd);
assert(b2[2] == 0xef);
assert(b2[3] == 0x01);
assert(b2[4] == 0x23);
}
// Test vector constructor
void test_vector_constructor() {
std::vector<unsigned char> vec{0xB8, 0xAB, 0xAF, 0xFF};
const ByteArray b1(vec);
assert(b1.size() == 4);
assert(b1[0] == 0xB8);
assert(b1[1] == 0xAB);
assert(b1[2] == 0xAF);
assert(b1[3] == 0xFF);
std::vector<unsigned char> empty_vec;
const ByteArray b2(empty_vec);
assert(b2.empty());
std::vector<unsigned char> move_vec{0x01, 0x02, 0x03};
ByteArray b3(std::move(move_vec));
assert(b3.size() == 3);
assert(b3[0] == 0x01);
assert(b3[1] == 0x02);
assert(b3[2] == 0x03);
assert(move_vec.empty());
}
// Test initializer list constructor
void test_initializer_list_constructor() {
const ByteArray b1({0xAF, 0xFF, 0xBA});
assert(b1.size() == 3);
assert(b1[0] == 0xAF);
assert(b1[1] == 0xFF);
assert(b1[2] == 0xBA);
const ByteArray b2({});
assert(b2.empty());
const ByteArray b3({0x42});
assert(b3.size() == 1);
assert(b3[0] == 0x42);
}
// Test single byte constructor
void test_single_byte_constructor() {
// Test with explicit construction
const ByteArray b1(static_cast<unsigned char>(0xAA));
assert(b1.size() == 1);
assert(b1[0] == 0xAA);
const ByteArray b2(static_cast<unsigned char>(0x00));
assert(b2.size() == 1);
assert(b2[0] == 0x00);
const ByteArray b3(static_cast<unsigned char>(0xFF));
assert(b3.size() == 1);
assert(b3[0] == 0xFF);
// Test single byte init list conversion
const ByteArray b4 = {0xBB};
assert(b4.size() == 1);
assert(b4[0] == 0xBB);
// Test copy assignment with implicit conversion
ByteArray b5; // NOLINT
b5 = {0xCC}; // Should work with init-list constructor
assert(b5.size() == 1);
assert(b5[0] == 0xCC);
}
// Test XOR operators
void test_xor_operators() {
// Test XOR with another ByteArray
const ByteArray b1({0xAA, 0xBB, 0xCC});
const ByteArray b2({0x55, 0x44, 0x33});
ByteArray b3 = b1 ^ b2;
assert(b3.size() == 3);
assert(b3[0] == (0xAA ^ 0x55));
assert(b3[1] == (0xBB ^ 0x44));
assert(b3[2] == (0xCC ^ 0x33));
// Test XOR with unequal lengths (right-aligned)
const ByteArray b4({0xAA, 0xBB});
const ByteArray b5({0x11, 0x22, 0x33});
ByteArray b6 = b4 ^ b5;
assert(b6.size() == 3);
// With right alignment, b4 is aligned at the end of b6
assert(b6[0] == 0x11); // Only b5's first byte
assert(b6[1] == (0xAA ^ 0x22)); // First byte of b4 XORed with second byte of b5
assert(b6[2] == (0xBB ^ 0x33)); // Second byte of b4 XORed with third byte of b5
// Test XOR with single byte (right-aligned)
const ByteArray b7({0x12, 0x34, 0x56});
ByteArray b8 = b7 ^ 0xFF;
assert(b8.size() == 3);
assert(b8[0] == 0x12); // Unchanged
assert(b8[1] == 0x34); // Unchanged
assert(b8[2] == (0x56 ^ 0xFF)); // Only last byte gets XORed
// Test XOR-assignment
ByteArray b9({0xA0, 0xB0, 0xC0});
const ByteArray b10({0x0A, 0x0B, 0x0C});
b9 ^= b10;
assert(b9.size() == 3);
assert(b9[0] == (0xA0 ^ 0x0A));
assert(b9[1] == (0xB0 ^ 0x0B));
assert(b9[2] == (0xC0 ^ 0x0C));
// Test XOR-assignment with single byte (right-aligned)
ByteArray b11({0x11, 0x22, 0x33});
b11 ^= 0x01;
assert(b11.size() == 3);
assert(b11[0] == 0x11); // Unchanged
assert(b11[1] == 0x22); // Unchanged
assert(b11[2] == (0x33 ^ 0x01)); // Only last byte gets XORed
}
// Test copy and move semantics
void test_copy_move_semantics() {
// Test copy constructor
const ByteArray original({0x01, 0x02, 0x03});
ByteArray copy = original; // NOLINT
assert(copy.size() == original.size());
assert(copy.data()[0] == 0x01);
assert(copy.data()[1] == 0x02);
assert(copy.data()[2] == 0x03);
// Test copy assignment
ByteArray destination({0xFF, 0xFF});
destination = original;
assert(destination.size() == original.size());
assert(destination.data()[0] == 0x01);
assert(destination.data()[1] == 0x02);
assert(destination.data()[2] == 0x03);
// Test move constructor
ByteArray to_move({0xAA, 0xBB, 0xCC});
ByteArray moved(std::move(to_move));
assert(moved.size() == 3);
assert(moved.data()[0] == 0xAA);
assert(moved.data()[1] == 0xBB);
assert(moved.data()[2] == 0xCC);
// Test move assignment
ByteArray to_move_assign({0xDD, 0xEE, 0xFF});
ByteArray move_assigned;
move_assigned = std::move(to_move_assign);
assert(move_assigned.size() == 3);
assert(move_assigned.data()[0] == 0xDD);
assert(move_assigned.data()[1] == 0xEE);
assert(move_assigned.data()[2] == 0xFF);
}
// Test iterators
void test_iterators() {
ByteArray ba({0x01, 0x02, 0x03, 0x04});
// Test begin/end iterators
unsigned char sum = 0;
for (auto it = ba.begin(); it != ba.end(); ++it) { // NOLINT
sum += *it;
}
assert(sum == 0x0A); // 0x01 + 0x02 + 0x03 + 0x04 = 0x0A
// Test range-based for loop
unsigned char prod = 1;
for (auto byte : ba) {
prod *= byte;
}
assert(prod == 0x18); // 0x01 * 0x02 * 0x03 * 0x04 = 0x18
}
// Test secure erase functionality
void test_secure_erase() {
// Test with a non-empty ByteArray
ByteArray sensitive_data({0xAA, 0xBB, 0xCC, 0xDD, 0xEE});
// Confirm data is as expected before wiping
assert(sensitive_data.size() == 5);
assert(sensitive_data.data()[0] == 0xAA);
assert(sensitive_data.data()[1] == 0xBB);
assert(sensitive_data.data()[2] == 0xCC);
assert(sensitive_data.data()[3] == 0xDD);
assert(sensitive_data.data()[4] == 0xEE);
// Perform secure wipe
bool wipe_result = sensitive_data.secure_wipe();
// Verify wipe operation was successful
assert(wipe_result);
// Verify size is now 0 (should be emptied by the secure_zero_vector operation)
assert(sensitive_data.empty());
// Test with an empty ByteArray
ByteArray empty_data({});
assert(empty_data.empty());
// Wiping an empty array should succeed without issues
wipe_result = empty_data.secure_wipe();
assert(wipe_result);
assert(empty_data.empty());
// Test with single byte value
ByteArray single_byte(static_cast<unsigned char>(0x42));
assert(single_byte.size() == 1);
assert(single_byte.data()[0] == 0x42);
wipe_result = single_byte.secure_wipe();
assert(wipe_result);
assert(single_byte.empty());
// Test with hex string constructed ByteArray
ByteArray hex_constructed("deadbeef");
assert(hex_constructed.size() == 4);
wipe_result = hex_constructed.secure_wipe();
assert(wipe_result);
assert(hex_constructed.empty());
// Test post-wipe operations to ensure object remains usable
// Create a new ByteArray, wipe it, then add new data
ByteArray reusable({0x11, 0x22, 0x33});
assert(reusable.size() == 3);
wipe_result = reusable.secure_wipe();
assert(wipe_result);
assert(reusable.empty());
// After wiping, the object should still be usable
// We can't directly assign new data since there's no method for that,
// but we can test that the object is in a valid state by assigning from another ByteArray
const ByteArray new_data({0x44, 0x55, 0x66});
reusable = new_data;
assert(reusable.size() == 3);
assert(reusable.data()[0] == 0x44);
assert(reusable.data()[1] == 0x55);
assert(reusable.data()[2] == 0x66);
}
// Test uint64_t constructor and as_64bit_uint conversion
void test_uint64_constructor_and_conversion() {
// Test constructor with small value
constexpr uint64_t small_value = 42;
ByteArray ba1 = {small_value};
// Check correct construction (should use big-endian byte order)
assert(ba1.size() == 1);
assert(ba1.data()[0] == 42);
// Test round-trip conversion
assert(ba1.as_64bit_uint() == small_value);
// Test constructor with multi-byte value
constexpr uint64_t multi_byte = 0x1122334455667788;
ByteArray ba2 = ByteArray::create_from_uint64(multi_byte);
// Check correct byte count and values (big-endian byte order)
assert(ba2.size() == 8);
assert(ba2.data()[0] == 0x11);
assert(ba2.data()[1] == 0x22);
assert(ba2.data()[2] == 0x33);
assert(ba2.data()[3] == 0x44);
assert(ba2.data()[4] == 0x55);
assert(ba2.data()[5] == 0x66);
assert(ba2.data()[6] == 0x77);
assert(ba2.data()[7] == 0x88);
// Test round-trip conversion
assert(ba2.as_64bit_uint() == multi_byte);
// Test with value that doesn't need all 8 bytes
constexpr uint64_t medium_value = 0x112233;
ByteArray ba3= ByteArray::create_from_uint64(medium_value);
// Should only use 3 bytes
assert(ba3.size() == 3);
assert(ba3.data()[0] == 0x11);
assert(ba3.data()[1] == 0x22);
assert(ba3.data()[2] == 0x33);
// Test round-trip conversion
assert(ba3.as_64bit_uint() == medium_value);
// Test with 0
constexpr uint64_t zero_value = 0;
ByteArray ba4 = ByteArray::create_from_uint64(zero_value);
// Should produce a 1-byte array with value 0
assert(ba4.size() == 1);
assert(ba4.data()[0] == 0);
assert(ba4.as_64bit_uint() == zero_value);
}
// Test as_64bit_uint exception handling
void test_as_64bit_uint_exceptions() {
// Create a ByteArray with more than 8 bytes
ByteArray large_array({0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09});
assert(large_array.size() == 9); // More than 8 bytes
// Attempting to convert should throw an exception
bool exception_thrown = false;
try {
uint64_t value = large_array.as_64bit_uint();
(void)value; // Avoid unused variable warning
} catch (const std::invalid_argument& e) {
exception_thrown = true;
assert(std::string(e.what()) == "Byte array is larger than 64-bit and cannot be represented as such");
}
assert(exception_thrown);
// Test boundary case with exactly 8 bytes
const ByteArray boundary_array({0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
assert(boundary_array.size() == 8);
// This should not throw
bool boundary_exception = false;
try {
uint64_t value = boundary_array.as_64bit_uint();
(void)value; // Avoid unused variable warning
} catch (...) {
boundary_exception = true;
}
assert(!boundary_exception);
// Test conversion result with 8 bytes
uint64_t expected = 0x0102030405060708;
assert(boundary_array.as_64bit_uint() == expected);
}
// Test size and value constructor
void test_size_and_value_constructor() {
// Test with non-zero value
ByteArray b1(5, 0xAA);
assert(b1.size() == 5);
assert(b1.data()[0] == 0xAA);
assert(b1.data()[1] == 0xAA);
assert(b1.data()[2] == 0xAA);
assert(b1.data()[3] == 0xAA);
assert(b1.data()[4] == 0xAA);
// Test with zero value
ByteArray b2(3, 0x00);
assert(b2.size() == 3);
assert(b2.data()[0] == 0x00);
assert(b2.data()[1] == 0x00);
assert(b2.data()[2] == 0x00);
// Test with zero size
ByteArray b3(0, 0xFF);
assert(b3.empty());
// Test with large size
constexpr size_t large_size = 1000;
ByteArray b4(large_size, 0xFF);
assert(b4.size() == large_size);
// Check some sample elements
assert(b4.data()[0] == 0xFF);
assert(b4.data()[large_size / 2] == 0xFF);
assert(b4.data()[large_size - 1] == 0xFF);
}
// Test subscript operator bounds checking
void test_subscript_operator_bounds_checking() {
// Create a test ByteArray with known values
ByteArray ba({0x11, 0x22, 0x33, 0x44, 0x55});
// Test valid access - should not throw
bool exception_thrown = false;
try {
// Access each valid index
assert(ba[0] == 0x11);
assert(ba[1] == 0x22);
assert(ba[2] == 0x33);
assert(ba[3] == 0x44);
assert(ba[4] == 0x55);
} catch (const std::out_of_range&) {
exception_thrown = true;
}
assert(!exception_thrown);
// Test out-of-bounds access - should throw std::out_of_range
exception_thrown = false;
try {
const unsigned char& value = ba[5]; // One past the end
(void)value; // Prevent unused variable warning
} catch (const std::out_of_range&) {
exception_thrown = true;
}
assert(exception_thrown && "Out of range exception should be thrown");
// Test access on empty ByteArray
ByteArray empty_ba; // NOLINT
exception_thrown = false;
try {
const unsigned char& value = empty_ba[0];
(void)value; // Prevent unused variable warning
} catch (const std::out_of_range&) {
exception_thrown = true;
}
assert(exception_thrown && "Out of range exception should be thrown");
// Test with large index
exception_thrown = false;
try {
const unsigned char& value = ba[SIZE_MAX]; //NOLINT
(void)value; // Prevent unused variable warning
} catch (const std::out_of_range&) {
exception_thrown = true;
}
assert(exception_thrown && "Out of range exception should be thrown");
// Verify const correctness - operator[] should not allow modification
// This is a compile-time check, not a runtime check
const ByteArray const_ba({0xAA, 0xBB, 0xCC});
assert(const_ba[0] == 0xAA);
assert(const_ba[1] == 0xBB);
assert(const_ba[2] == 0xCC);
// Test that const version throws on out-of-bounds
exception_thrown = false;
try {
const unsigned char& value = const_ba[3];
(void)value; // Prevent unused variable warning
} catch (const std::out_of_range&) {
exception_thrown = true;
}
assert(exception_thrown && "Out of range exception should be thrown");
}
// Test complement operators
void test_complement_operators() {
// Test unary complement operator (~) - returns a new ByteArray
ByteArray b1({0xAA, 0xBB, 0xCC});
ByteArray b2 = ~b1;
assert(b2.size() == 3);
assert(b2[0] == static_cast<unsigned char>(~0xAA)); // 0x55
assert(b2[1] == static_cast<unsigned char>(~0xBB)); // 0x44
assert(b2[2] == static_cast<unsigned char>(~0xCC)); // 0x33
// Test in-place complement operator
ByteArray b3({0x00, 0xFF, 0x55});
b3 = ~b3;
assert(b3.size() == 3);
assert(b3[0] == 0xFF); // ~0x00 = 0xFF
assert(b3[1] == 0x00); // ~0xFF = 0x00
assert(b3[2] == 0xAA); // ~0x55 = 0xAA
// Test complement of empty ByteArray
bool exception_thrown = false;
try {
ByteArray empty;
ByteArray result = ~empty;
(void)result; // Prevent unused variable warning
} catch (const std::invalid_argument&) {
exception_thrown = true;
}
assert(exception_thrown && "Complement of empty ByteArray should throw");
// Verify original ByteArray is unchanged by ~ operator
assert(b1.size() == 3);
assert(b1[0] == 0xAA);
assert(b1[1] == 0xBB);
assert(b1[2] == 0xCC);
// Test in-place complementation
b1 = ~b1;
assert(b1.size() == 3);
assert(b1[0] == static_cast<unsigned char>(~0xAA)); // 0x55
assert(b1[1] == static_cast<unsigned char>(~0xBB)); // 0x44
assert(b1[2] == static_cast<unsigned char>(~0xCC)); // 0x33
}
// Test iterator range constructor
void test_iterator_range_constructor() {
// Test with std::vector
std::vector<unsigned char> vec{0x11, 0x22, 0x33, 0x44, 0x55};
ByteArray from_vector(vec.begin(), vec.end());
assert(from_vector.size() == 5);
assert(from_vector[0] == 0x11);
assert(from_vector[1] == 0x22);
assert(from_vector[2] == 0x33);
assert(from_vector[3] == 0x44);
assert(from_vector[4] == 0x55);
// Test with partial range from a vector
ByteArray partial_range(vec.begin() + 1, vec.begin() + 4);
assert(partial_range.size() == 3);
assert(partial_range[0] == 0x22);
assert(partial_range[1] == 0x33);
assert(partial_range[2] == 0x44);
// Test with another ByteArray's iterators
ByteArray original({0x01, 0x02, 0x03, 0x04, 0x05});
ByteArray from_bytearray(original.begin() + 1, original.end() - 1);
assert(from_bytearray.size() == 3);
assert(from_bytearray[0] == 0x02);
assert(from_bytearray[1] == 0x03);
assert(from_bytearray[2] == 0x04);
// Test with empty range
ByteArray empty_range(vec.begin(), vec.begin());
assert(empty_range.empty());
// Test with string (as a sequence of bytes)
ByteArray str_bytes = {'A','B','C'};
const ByteArray from_str_bytes(str_bytes.begin(), str_bytes.end());
assert(from_str_bytes.size() == 3);
assert(from_str_bytes[0] == 'A');
assert(from_str_bytes[1] == 'B');
assert(from_str_bytes[2] == 'C');
}
// Test concat method
void test_concat() {
// Test basic concatenation
ByteArray b1({0x01, 0x02, 0x03});
ByteArray b2({0xAA, 0xBB});
// Modify b1 by concatenating b2
ByteArray& result = b1.concat(b2);
// Check result size and content
assert(b1.size() == 5);
assert(b1[0] == 0x01);
assert(b1[1] == 0x02);
assert(b1[2] == 0x03);
assert(b1[3] == 0xAA);
assert(b1[4] == 0xBB);
// Verify result reference is to b1
assert(&result == &b1);
// Test chaining multiple concats
ByteArray b3({0x01, 0x02});
ByteArray b4({0x03, 0x04});
ByteArray b5({0x05, 0x06});
b3.concat(b4).concat(b5);
assert(b3.size() == 6);
assert(b3[0] == 0x01);
assert(b3[1] == 0x02);
assert(b3[2] == 0x03);
assert(b3[3] == 0x04);
assert(b3[4] == 0x05);
assert(b3[5] == 0x06);
// Test concatenating empty ByteArray
ByteArray b6({0xCC, 0xDD});
ByteArray empty;
b6.concat(empty);
assert(b6.size() == 2);
assert(b6[0] == 0xCC);
assert(b6[1] == 0xDD);
// Test concatenating to empty ByteArray
ByteArray b7;
ByteArray b8({0xEE, 0xFF});
b7.concat(b8);
assert(b7.size() == 2);
assert(b7[0] == 0xEE);
assert(b7[1] == 0xFF);
}
// Test concat_copy method
void test_concat_copy() {
// Test basic copy concatenation
ByteArray b1({0x01, 0x02, 0x03});
ByteArray b2({0xAA, 0xBB});
// Create a new ByteArray with concatenated content
ByteArray result = b1.concat_copy(b2);
// Check result content
assert(result.size() == 5);
assert(result[0] == 0x01);
assert(result[1] == 0x02);
assert(result[2] == 0x03);
assert(result[3] == 0xAA);
assert(result[4] == 0xBB);
// Verify original arrays are unchanged
assert(b1.size() == 3);
assert(b1[0] == 0x01);
assert(b1[1] == 0x02);
assert(b1[2] == 0x03);
assert(b2.size() == 2);
assert(b2[0] == 0xAA);
assert(b2[1] == 0xBB);
// Test chaining multiple concat_copy operations
ByteArray b3({0x11, 0x22});
ByteArray b4({0x33, 0x44});
ByteArray b5({0x55, 0x66});
ByteArray chained = b3.concat_copy(b4).concat_copy(b5);
assert(chained.size() == 6);
assert(chained[0] == 0x11);
assert(chained[1] == 0x22);
assert(chained[2] == 0x33);
assert(chained[3] == 0x44);
assert(chained[4] == 0x55);
assert(chained[5] == 0x66);
// Test with empty ByteArray
ByteArray b6({0xCC, 0xDD});
ByteArray empty;
ByteArray result_with_empty = b6.concat_copy(empty);
assert(result_with_empty.size() == 2);
assert(result_with_empty[0] == 0xCC);
assert(result_with_empty[1] == 0xDD);
ByteArray empty_with_result = empty.concat_copy(b6);
assert(empty_with_result.size() == 2);
assert(empty_with_result[0] == 0xCC);
assert(empty_with_result[1] == 0xDD);
}
// Test concat_and_create method
void test_concat_and_create() {
// Test basic static concatenation with initializer list
ByteArray b1({0x01, 0x02});
ByteArray b2({0x03, 0x04});
ByteArray b3({0x05, 0x06});
ByteArray result = ByteArray::concat_and_create({b1, b2, b3});
assert(result.size() == 6);
assert(result[0] == 0x01);
assert(result[1] == 0x02);
assert(result[2] == 0x03);
assert(result[3] == 0x04);
assert(result[4] == 0x05);
assert(result[5] == 0x06);
// Test with empty ByteArray in the list
ByteArray empty;
ByteArray b4({0xAA, 0xBB});
ByteArray result_with_empty = ByteArray::concat_and_create({b4, empty, b4});
assert(result_with_empty.size() == 4);
assert(result_with_empty[0] == 0xAA);
assert(result_with_empty[1] == 0xBB);
assert(result_with_empty[2] == 0xAA);
assert(result_with_empty[3] == 0xBB);
// Test with all empty ByteArrays
ByteArray all_empty = ByteArray::concat_and_create({empty, empty, empty});
assert(all_empty.empty());
// Test with single ByteArray
ByteArray single = ByteArray::concat_and_create({b4});
assert(single.size() == 2);
assert(single[0] == 0xAA);
assert(single[1] == 0xBB);
// Test with empty initializer list
ByteArray no_arrays = ByteArray::concat_and_create({});
assert(no_arrays.empty());
// Test that originals are unchanged
assert(b1.size() == 2);
assert(b1[0] == 0x01);
assert(b1[1] == 0x02);
assert(b2.size() == 2);
assert(b2[0] == 0x03);
assert(b2[1] == 0x04);
assert(b3.size() == 2);
assert(b3[0] == 0x05);
assert(b3[1] == 0x06);
}
void test_create_from_prng() {
// Test basic random generation
constexpr size_t small_size = 10;
ByteArray random1 = ByteArray::create_from_prng(small_size);
assert(random1.size() == small_size);
// Generate another array to compare
ByteArray random2 = ByteArray::create_from_prng(small_size);
assert(random2.size() == small_size);
// Simple check that arrays differ
bool arrays_differ = false;
for (size_t i = 0; i < small_size; ++i) {
if (random1[i] != random2[i]) {
arrays_differ = true;
break;
}
}
assert(arrays_differ);
// Test zero-size case
ByteArray empty_random = ByteArray::create_from_prng(0);
assert(empty_random.empty());
// Test medium size
ByteArray medium_random = ByteArray::create_from_prng(1024);
assert(medium_random.size() == 1024);
// Test exception for exceeding max size
bool exception_thrown = false;
try {
ByteArray too_large = ByteArray::create_from_prng(ByteArray::MAX_RANDOM_BYTES + 1);
} catch (const std::invalid_argument&) {
exception_thrown = true;
}
assert(exception_thrown);
}
void test_partial_copy_constructor() {
// Test case 1: Copy less bytes than available
{
ByteArray original = {0x01, 0x02, 0x03, 0x04, 0x05};
ByteArray partial(original, 3);
// Check correct size
assert(partial.size() == 3);
// Check correct content
assert(partial[0] == 0x01);
assert(partial[1] == 0x02);
assert(partial[2] == 0x03);
}
// Test case 2: Try to copy more bytes than available
{
ByteArray original = {0xAA, 0xBB, 0xCC};
ByteArray copy(original, 6); // Request more bytes than exist
// Should pad the rest with 0x00
assert(copy.size() == 6);
// Check content matches original and pads
assert(copy[0] == 0xAA);
assert(copy[1] == 0xBB);
assert(copy[2] == 0xCC);
assert(copy[3] == 0x00);
assert(copy[4] == 0x00);
assert(copy[5] == 0x00);
}
// Test case 3: Empty source ByteArray
{
ByteArray empty;
ByteArray partial(empty, 3);
// Result should be all-zero padded
assert(partial.size() == 3);
assert(partial[0] == 0x00);
assert(partial[1] == 0x00);
assert(partial[2] == 0x00);
}
// Test case 4: Zero bytes requested
{
ByteArray original = {0x01, 0x02, 0x03, 0x04, 0x05};
ByteArray partial(original, 0);
// Result should be empty
assert(partial.empty());
}
PRINT_PASSED();
}
void test_partial_move_constructor() {
// Test basic move with truncation (LSB_PAD default)
{
jlizard::ByteArray original = {0x01, 0x02, 0x03, 0x04, 0x05};
jlizard::ByteArray moved_truncated(std::move(original), 3);
assert(moved_truncated.size() == 3);
assert(moved_truncated[0] == 0x01);
assert(moved_truncated[1] == 0x02);
assert(moved_truncated[2] == 0x03);
}
// Test move with extension (LSB_PAD default)
{
jlizard::ByteArray original = {0x01, 0x02, 0x03};
jlizard::ByteArray moved_extended(std::move(original), 6);
assert(moved_extended.size() == 6);
assert(moved_extended[0] == 0x01);
assert(moved_extended[1] == 0x02);
assert(moved_extended[2] == 0x03);
assert(moved_extended[3] == 0x00);
assert(moved_extended[4] == 0x00);
assert(moved_extended[5] == 0x00);
}
// Test move with MSB truncation
{
jlizard::ByteArray original = {0x01, 0x02, 0x03, 0x04, 0x05};
jlizard::ByteArray moved_truncated(std::move(original), 3, jlizard::EZeroPadDir::MSB_PAD);
assert(moved_truncated.size() == 3);
assert(moved_truncated[0] == 0x03);
assert(moved_truncated[1] == 0x04);
assert(moved_truncated[2] == 0x05);
}
// Test move with MSB extension
{
jlizard::ByteArray original = {0x01, 0x02, 0x03};
jlizard::ByteArray moved_extended(std::move(original), 6, jlizard::EZeroPadDir::MSB_PAD);
assert(moved_extended.size() == 6);
assert(moved_extended[0] == 0x00);
assert(moved_extended[1] == 0x00);
assert(moved_extended[2] == 0x00);
assert(moved_extended[3] == 0x01);
assert(moved_extended[4] == 0x02);
assert(moved_extended[5] == 0x03);
}
// Test move with same size
{
jlizard::ByteArray original = {0xAA, 0xBB, 0xCC};
jlizard::ByteArray moved_same_size(std::move(original), 3);
assert(moved_same_size.size() == 3);
assert(moved_same_size[0] == 0xAA);
assert(moved_same_size[1] == 0xBB);
assert(moved_same_size[2] == 0xCC);
}
// Test move to zero size
{
jlizard::ByteArray original = {0x01, 0x02, 0x03};
jlizard::ByteArray moved_empty(std::move(original), 0);
assert(moved_empty.size() == 0); //NOLINT
assert(moved_empty.empty());
}
PRINT_PASSED();
}
void test_copy_constructor_with_size_padding() {
// Test basic copy with truncation (LSB_PAD default)
{
jlizard::ByteArray original = {0x01, 0x02, 0x03, 0x04, 0x05};
jlizard::ByteArray copied_truncated(original, 3);
assert(copied_truncated.size() == 3);
assert(copied_truncated[0] == 0x01);
assert(copied_truncated[1] == 0x02);
assert(copied_truncated[2] == 0x03);
// Verify original is unchanged
assert(original.size() == 5);
assert(original[0] == 0x01);
}
// Test copy with extension (LSB_PAD default)
{
jlizard::ByteArray original = {0x01, 0x02, 0x03};
jlizard::ByteArray copied_extended(original, 6);
assert(copied_extended.size() == 6);
assert(copied_extended[0] == 0x01);
assert(copied_extended[1] == 0x02);
assert(copied_extended[2] == 0x03);
assert(copied_extended[3] == 0x00);
assert(copied_extended[4] == 0x00);
assert(copied_extended[5] == 0x00);
// Verify original is unchanged
assert(original.size() == 3);
}
// Test copy with MSB truncation
{
jlizard::ByteArray original = {0x01, 0x02, 0x03, 0x04, 0x05};
jlizard::ByteArray copied_truncated(original, 3, jlizard::EZeroPadDir::MSB_PAD);
assert(copied_truncated.size() == 3);
assert(copied_truncated[0] == 0x03);
assert(copied_truncated[1] == 0x04);
assert(copied_truncated[2] == 0x05);
// Verify original is unchanged
assert(original.size() == 5);
}
// Test copy with MSB extension
{
jlizard::ByteArray original = {0x01, 0x02, 0x03};
jlizard::ByteArray copied_extended(original, 6, jlizard::EZeroPadDir::MSB_PAD);
assert(copied_extended.size() == 6);
assert(copied_extended[0] == 0x00);
assert(copied_extended[1] == 0x00);
assert(copied_extended[2] == 0x00);
assert(copied_extended[3] == 0x01);
assert(copied_extended[4] == 0x02);
assert(copied_extended[5] == 0x03);
// Verify original is unchanged
assert(original.size() == 3);
}
// Test copy with same size
{
jlizard::ByteArray original = {0xAA, 0xBB, 0xCC};
jlizard::ByteArray copied_same_size(original, 3);