diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-08-26 09:42:08 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-08-26 17:32:19 +0000 |
commit | a1035cf81494c3ab22c49b01788ca8ad933c5eb0 (patch) | |
tree | fee86f8751cc93eca9702fd50c3380483ff0f86a /toxcore/ping_array_test.cc | |
parent | 64ddb7fff21e8cfcd197ba31bbb08ee6d3964c54 (diff) |
Add some tests for `ping_array`.
No timeout test here yet, because we don't yet have the ability to
manipulate time at will, so we would have to actually sleep.
Diffstat (limited to 'toxcore/ping_array_test.cc')
-rw-r--r-- | toxcore/ping_array_test.cc | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/toxcore/ping_array_test.cc b/toxcore/ping_array_test.cc new file mode 100644 index 00000000..fca5009a --- /dev/null +++ b/toxcore/ping_array_test.cc | |||
@@ -0,0 +1,119 @@ | |||
1 | #include "ping_array.h" | ||
2 | |||
3 | #include <memory> | ||
4 | |||
5 | #include <gtest/gtest.h> | ||
6 | #include "mono_time.h" | ||
7 | |||
8 | namespace { | ||
9 | |||
10 | struct Ping_Array_Deleter { | ||
11 | void operator()(Ping_Array *arr) { ping_array_kill(arr); } | ||
12 | }; | ||
13 | |||
14 | using Ping_Array_Ptr = std::unique_ptr<Ping_Array, Ping_Array_Deleter>; | ||
15 | |||
16 | struct Mono_Time_Deleter { | ||
17 | void operator()(Mono_Time *arr) { mono_time_free(arr); } | ||
18 | }; | ||
19 | |||
20 | using Mono_Time_Ptr = std::unique_ptr<Mono_Time, Mono_Time_Deleter>; | ||
21 | |||
22 | TEST(PingArray, MinimumTimeoutIsOne) { | ||
23 | EXPECT_EQ(ping_array_new(1, 0), nullptr); | ||
24 | EXPECT_NE(Ping_Array_Ptr(ping_array_new(1, 1)), nullptr); | ||
25 | } | ||
26 | |||
27 | TEST(PingArray, MinimumArraySizeIsOne) { | ||
28 | EXPECT_EQ(ping_array_new(0, 1), nullptr); | ||
29 | EXPECT_NE(Ping_Array_Ptr(ping_array_new(1, 1)), nullptr); | ||
30 | } | ||
31 | |||
32 | TEST(PingArray, ArraySizeMustBePowerOfTwo) { | ||
33 | Ping_Array_Ptr arr; | ||
34 | arr.reset(ping_array_new(2, 1)); | ||
35 | EXPECT_NE(arr, nullptr); | ||
36 | arr.reset(ping_array_new(4, 1)); | ||
37 | EXPECT_NE(arr, nullptr); | ||
38 | arr.reset(ping_array_new(1024, 1)); | ||
39 | EXPECT_NE(arr, nullptr); | ||
40 | |||
41 | EXPECT_EQ(ping_array_new(1023, 1), nullptr); | ||
42 | EXPECT_EQ(ping_array_new(1234, 1), nullptr); | ||
43 | } | ||
44 | |||
45 | TEST(PingArray, StoredDataCanBeRetrieved) { | ||
46 | Ping_Array_Ptr const arr(ping_array_new(2, 1)); | ||
47 | Mono_Time_Ptr const mono_time(mono_time_new()); | ||
48 | |||
49 | uint64_t const ping_id = | ||
50 | ping_array_add(arr.get(), mono_time.get(), std::vector<uint8_t>{1, 2, 3, 4}.data(), 4); | ||
51 | EXPECT_NE(ping_id, 0); | ||
52 | |||
53 | std::vector<uint8_t> data(4); | ||
54 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), data.data(), data.size(), ping_id), 4); | ||
55 | EXPECT_EQ(data, std::vector<uint8_t>({1, 2, 3, 4})); | ||
56 | } | ||
57 | |||
58 | TEST(PingArray, RetrievingDataWithTooSmallOutputBufferHasNoEffect) { | ||
59 | Ping_Array_Ptr const arr(ping_array_new(2, 1)); | ||
60 | Mono_Time_Ptr const mono_time(mono_time_new()); | ||
61 | |||
62 | uint64_t const ping_id = | ||
63 | ping_array_add(arr.get(), mono_time.get(), (std::vector<uint8_t>{1, 2, 3, 4}).data(), 4); | ||
64 | EXPECT_NE(ping_id, 0); | ||
65 | |||
66 | std::vector<uint8_t> data(4); | ||
67 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), data.data(), 3, ping_id), -1); | ||
68 | // It doesn't write anything to the data array. | ||
69 | EXPECT_EQ(data, std::vector<uint8_t>({0, 0, 0, 0})); | ||
70 | // Afterwards, we can still read it. | ||
71 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), data.data(), 4, ping_id), 4); | ||
72 | EXPECT_EQ(data, std::vector<uint8_t>({1, 2, 3, 4})); | ||
73 | } | ||
74 | |||
75 | TEST(PingArray, ZeroLengthDataCanBeAdded) { | ||
76 | Ping_Array_Ptr const arr(ping_array_new(2, 1)); | ||
77 | Mono_Time_Ptr const mono_time(mono_time_new()); | ||
78 | |||
79 | uint64_t const ping_id = ping_array_add(arr.get(), mono_time.get(), nullptr, 0); | ||
80 | EXPECT_NE(ping_id, 0); | ||
81 | |||
82 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), nullptr, 0, ping_id), 0); | ||
83 | } | ||
84 | |||
85 | TEST(PingArray, PingId0IsInvalid) { | ||
86 | Ping_Array_Ptr const arr(ping_array_new(2, 1)); | ||
87 | Mono_Time_Ptr const mono_time(mono_time_new()); | ||
88 | |||
89 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), nullptr, 0, 0), -1); | ||
90 | } | ||
91 | |||
92 | // Protection against replay attacks. | ||
93 | TEST(PingArray, DataCanOnlyBeRetrievedOnce) { | ||
94 | Ping_Array_Ptr const arr(ping_array_new(2, 1)); | ||
95 | Mono_Time_Ptr const mono_time(mono_time_new()); | ||
96 | |||
97 | uint64_t const ping_id = ping_array_add(arr.get(), mono_time.get(), nullptr, 0); | ||
98 | EXPECT_NE(ping_id, 0); | ||
99 | |||
100 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), nullptr, 0, ping_id), 0); | ||
101 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), nullptr, 0, ping_id), -1); | ||
102 | } | ||
103 | |||
104 | TEST(PingArray, PingIdMustMatchOnCheck) { | ||
105 | Ping_Array_Ptr const arr(ping_array_new(1, 1)); | ||
106 | Mono_Time_Ptr const mono_time(mono_time_new()); | ||
107 | |||
108 | uint64_t const ping_id = ping_array_add(arr.get(), mono_time.get(), nullptr, 0); | ||
109 | EXPECT_NE(ping_id, 0); | ||
110 | |||
111 | uint64_t const bad_ping_id = ping_id == 1 ? 2 : 1; | ||
112 | |||
113 | // bad_ping_id will also be pointing at the same element, but won't match the | ||
114 | // actual ping_id. | ||
115 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), nullptr, 0, bad_ping_id), -1); | ||
116 | EXPECT_EQ(ping_array_check(arr.get(), mono_time.get(), nullptr, 0, ping_id), 0); | ||
117 | } | ||
118 | |||
119 | } // namespace | ||