|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020-2021 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @file |
| 21 | + * This file implements unit tests for the SessionManager implementation. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <lib/support/UnitTestRegistration.h> |
| 25 | +#include <transport/PeerMessageCounter.h> |
| 26 | + |
| 27 | +#include <errno.h> |
| 28 | +#include <nlbyteorder.h> |
| 29 | +#include <nlunit-test.h> |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +using namespace chip; |
| 35 | + |
| 36 | +static uint32_t counterValuesArray[] = { 0, 10, 0x7FFFFFFF, 0x80000000, 0x80000001, 0x80000002, 0xFFFFFFF0, 0xFFFFFFFF }; |
| 37 | + |
| 38 | +void GroupRollOverTest(nlTestSuite * inSuite, void * inContext) |
| 39 | +{ |
| 40 | + for (auto n : counterValuesArray) |
| 41 | + { |
| 42 | + for (uint32_t k = 1; k <= 2 * CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k++) |
| 43 | + { |
| 44 | + chip::Transport::PeerMessageCounter counter; |
| 45 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR); |
| 46 | + |
| 47 | + counter.CommitWithRollOver(n); |
| 48 | + |
| 49 | + // 1. A counter value of N + k comes in, we detect it as valid and commit it. |
| 50 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + k, true) == CHIP_NO_ERROR); |
| 51 | + counter.CommitWithRollOver(n + k); |
| 52 | + |
| 53 | + // 2. A counter value of N comes in, we detect it as duplicate. |
| 54 | + if (k <= CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) |
| 55 | + { |
| 56 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_ERROR_MESSAGE_COUNTER_OUT_OF_WINDOW); |
| 61 | + } |
| 62 | + |
| 63 | + // 3. A counter value between N - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE and N + k - |
| 64 | + // CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE |
| 65 | + // (but not including N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) comes in, we treat it as duplicate. |
| 66 | + for (uint32_t i = n - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; i != (n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE); |
| 67 | + i++) |
| 68 | + { |
| 69 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(i, true) != CHIP_NO_ERROR); |
| 70 | + } |
| 71 | + |
| 72 | + // 4. A counter value of N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE comes in, is treated as valid. |
| 73 | + if (k != CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) |
| 74 | + { |
| 75 | + NL_TEST_ASSERT( |
| 76 | + inSuite, counter.VerifyOrTrustFirst((n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE), true) == CHIP_NO_ERROR); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + NL_TEST_ASSERT( |
| 81 | + inSuite, counter.VerifyOrTrustFirst((n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE), true) != CHIP_NO_ERROR); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void GroupBackTrackTest(nlTestSuite * inSuite, void * inContext) |
| 88 | +{ |
| 89 | + for (auto n : counterValuesArray) |
| 90 | + { |
| 91 | + chip::Transport::PeerMessageCounter counter; |
| 92 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR); |
| 93 | + |
| 94 | + counter.CommitWithRollOver(n); |
| 95 | + // 1. Some set of values N - k come in, for 0 < k < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE. |
| 96 | + // All of those should be considered valid and committed. |
| 97 | + for (uint32_t k = 1; k * k < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k++) |
| 98 | + { |
| 99 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n - (k * k), true) == CHIP_NO_ERROR); |
| 100 | + counter.CommitWithRollOver(n - (k * k)); |
| 101 | + } |
| 102 | + // 2. Counter value N + 3 comes in |
| 103 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + 3, true) == CHIP_NO_ERROR); |
| 104 | + counter.CommitWithRollOver(n + 3); |
| 105 | + |
| 106 | + // 3. The same set of values N - k come in as in step (1) and are all considered duplicates/out of window. |
| 107 | + for (uint32_t k = 1; k * k < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k++) |
| 108 | + { |
| 109 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n - (k * k), true) != CHIP_NO_ERROR); |
| 110 | + } |
| 111 | + |
| 112 | + // 4. The values that were not in the set in step (a) (but are at least N + 3 - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) |
| 113 | + // come in, and all are treated as allowed. |
| 114 | + for (uint32_t k = n + 3 - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k != n + 3; ++k) |
| 115 | + { |
| 116 | + if (n - k == 0 || n - k == 1 || n - k == 4 || n - k == 9 || n - k == 16 || n - k == 25) |
| 117 | + { |
| 118 | + continue; |
| 119 | + } |
| 120 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(k, true) == CHIP_NO_ERROR); |
| 121 | + counter.CommitWithRollOver(k); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void GroupBigLeapTest(nlTestSuite * inSuite, void * inContext) |
| 127 | +{ |
| 128 | + for (auto n : counterValuesArray) |
| 129 | + { |
| 130 | + for (uint32_t k = (static_cast<uint32_t>(1 << 31) - 5); k <= (static_cast<uint32_t>(1 << 31) - 1); k++) |
| 131 | + { |
| 132 | + chip::Transport::PeerMessageCounter counter; |
| 133 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR); |
| 134 | + |
| 135 | + counter.CommitWithRollOver(n); |
| 136 | + |
| 137 | + // 1. A counter value of N + k comes in, we detect it as valid and commit it. |
| 138 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + k, true) == CHIP_NO_ERROR); |
| 139 | + counter.CommitWithRollOver(n + k); |
| 140 | + |
| 141 | + // 2. A counter value of N comes in, we detect it as duplicate. |
| 142 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) != CHIP_NO_ERROR); |
| 143 | + |
| 144 | + // 3. A counter value between N - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE and N + k - |
| 145 | + // CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE |
| 146 | + // (but not including N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) comes in, we treat it as duplicate. |
| 147 | + |
| 148 | + // Only test some values to save processing time |
| 149 | + std::vector<uint32_t> testValues; |
| 150 | + testValues.push_back(static_cast<uint32_t>(n + (k / 32) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)); |
| 151 | + testValues.push_back(static_cast<uint32_t>(n + (k / 16) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)); |
| 152 | + testValues.push_back(static_cast<uint32_t>(n + (k / 8) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)); |
| 153 | + testValues.push_back(static_cast<uint32_t>(n + (k / 4) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)); |
| 154 | + testValues.push_back(static_cast<uint32_t>(n + (k / 3) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)); |
| 155 | + testValues.push_back(static_cast<uint32_t>(n + (k / 2) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)); |
| 156 | + testValues.push_back(static_cast<uint32_t>(n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE - 1)); |
| 157 | + |
| 158 | + // Will be inside the valid window of counter + (2^31 -1) |
| 159 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE, true) == CHIP_NO_ERROR); |
| 160 | + |
| 161 | + for (auto it : testValues) |
| 162 | + { |
| 163 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(it, true) != CHIP_NO_ERROR); |
| 164 | + } |
| 165 | + |
| 166 | + // 4. A counter value of N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE comes in, is treated as valid. |
| 167 | + NL_TEST_ASSERT(inSuite, |
| 168 | + counter.VerifyOrTrustFirst((n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE), true) == CHIP_NO_ERROR); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +void GroupOutOfWindow(nlTestSuite * inSuite, void * inContext) |
| 174 | +{ |
| 175 | + for (auto n : counterValuesArray) |
| 176 | + { |
| 177 | + for (uint32_t k = (static_cast<uint32_t>(1 << 31)); k <= (static_cast<uint32_t>(1 << 31) + 2); k++) |
| 178 | + { |
| 179 | + chip::Transport::PeerMessageCounter counter; |
| 180 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR); |
| 181 | + |
| 182 | + counter.CommitWithRollOver(n); |
| 183 | + |
| 184 | + // 1. A counter value of N + k comes in, we detect it as out of window |
| 185 | + NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + k, true) == CHIP_ERROR_MESSAGE_COUNTER_OUT_OF_WINDOW); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace |
| 191 | + |
| 192 | +/** |
| 193 | + * Test Suite that lists all the test functions. |
| 194 | + */ |
| 195 | +// clang-format off |
| 196 | +const nlTest sTests[] = |
| 197 | +{ |
| 198 | + NL_TEST_DEF("Group Roll over Test", GroupRollOverTest), |
| 199 | + NL_TEST_DEF("Group Backtrack Test", GroupBackTrackTest), |
| 200 | + NL_TEST_DEF("Group All value test", GroupBigLeapTest), |
| 201 | + NL_TEST_DEF("Group Out of Window Test", GroupOutOfWindow), |
| 202 | + NL_TEST_SENTINEL() |
| 203 | +}; |
| 204 | +// clang-format on |
| 205 | + |
| 206 | +/** |
| 207 | + * Main |
| 208 | + */ |
| 209 | +int TestPeerMessageCounter() |
| 210 | +{ |
| 211 | + // Run test suit against one context |
| 212 | + |
| 213 | + nlTestSuite theSuite = { "Transport-TestPeerMessageCounter", &sTests[0], nullptr, nullptr }; |
| 214 | + nlTestRunner(&theSuite, nullptr); |
| 215 | + |
| 216 | + return (nlTestRunnerStats(&theSuite)); |
| 217 | +} |
| 218 | + |
| 219 | +CHIP_REGISTER_TEST_SUITE(TestPeerMessageCounter); |
0 commit comments