| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <gtest/gtest.h> |
| 16 |
| 17 | #include <openssl/aead.h> |
| 18 | #include <openssl/ssl.h> |
| 19 |
| 20 | #include "internal.h" |
| 21 |
| 22 |
| 23 | #if !defined(BORINGSSL_SHARED_LIBRARY) |
| 24 | BSSL_NAMESPACE_BEGIN |
| 25 | namespace { |
| 26 |
| 27 | TEST(ArrayTest, InitValueConstructs) { |
| 28 | Array<uint8_t> array; |
| 29 | ASSERT_TRUE(array.Init(10)); |
| 30 | EXPECT_EQ(array.size(), 10u); |
| 31 | for (size_t i = 0; i < 10u; i++) { |
| 32 | EXPECT_EQ(0u, array[i]); |
| 33 | } |
| 34 | } |
| 35 |
| 36 | TEST(ArrayDeathTest, BoundsChecks) { |
| 37 | Array<int> array; |
| 38 | const int v[] = {1, 2, 3, 4}; |
| 39 | ASSERT_TRUE(array.CopyFrom(v)); |
| 40 | EXPECT_DEATH_IF_SUPPORTED(array[4], ""); |
| 41 | } |
| 42 |
| 43 | TEST(VectorTest, Resize) { |
| 44 | Vector<size_t> vec; |
| 45 | ASSERT_TRUE(vec.empty()); |
| 46 | EXPECT_EQ(vec.size(), 0u); |
| 47 |
| 48 | ASSERT_TRUE(vec.Push(42)); |
| 49 | ASSERT_TRUE(!vec.empty()); |
| 50 | EXPECT_EQ(vec.size(), 1u); |
| 51 |
| 52 | // Force a resize operation to occur |
| 53 | for (size_t i = 0; i < 16; i++) { |
| 54 | ASSERT_TRUE(vec.Push(i + 1)); |
| 55 | } |
| 56 |
| 57 | EXPECT_EQ(vec.size(), 17u); |
| 58 |
| 59 | // Verify that expected values are still contained in vec |
| 60 | for (size_t i = 0; i < vec.size(); i++) { |
| 61 | EXPECT_EQ(vec[i], i == 0 ? 42 : i); |
| 62 | } |
| 63 |
| 64 | // Clearing the vector should give an empty one. |
| 65 | vec.clear(); |
| 66 | ASSERT_TRUE(vec.empty()); |
| 67 | EXPECT_EQ(vec.size(), 0u); |
| 68 |
| 69 | ASSERT_TRUE(vec.Push(42)); |
| 70 | ASSERT_TRUE(!vec.empty()); |
| 71 | EXPECT_EQ(vec.size(), 1u); |
| 72 | EXPECT_EQ(vec[0], 42u); |
| 73 | } |
| 74 |
| 75 | TEST(VectorTest, MoveConstructor) { |
| 76 | Vector<size_t> vec; |
| 77 | for (size_t i = 0; i < 100; i++) { |
| 78 | ASSERT_TRUE(vec.Push(i)); |
| 79 | } |
| 80 |
| 81 | Vector<size_t> vec_moved(std::move(vec)); |
| 82 | for (size_t i = 0; i < 100; i++) { |
| 83 | EXPECT_EQ(vec_moved[i], i); |
| 84 | } |
| 85 | } |
| 86 |
| 87 | TEST(VectorTest, VectorContainingVectors) { |
| 88 | // Representative example of a struct that contains a Vector. |
| 89 | struct TagAndArray { |
| 90 | size_t tag; |
| 91 | Vector<size_t> vec; |
| 92 | }; |
| 93 |
| 94 | Vector<TagAndArray> vec; |
| 95 | for (size_t i = 0; i < 100; i++) { |
| 96 | TagAndArray elem; |
| 97 | elem.tag = i; |
| 98 | for (size_t j = 0; j < i; j++) { |
| 99 | ASSERT_TRUE(elem.vec.Push(j)); |
| 100 | } |
| 101 | ASSERT_TRUE(vec.Push(std::move(elem))); |
| 102 | } |
| 103 | EXPECT_EQ(vec.size(), static_cast<size_t>(100)); |
| 104 |
| 105 | Vector<TagAndArray> vec_moved(std::move(vec)); |
| 106 | EXPECT_EQ(vec_moved.size(), static_cast<size_t>(100)); |
| 107 | size_t count = 0; |
| 108 | for (const TagAndArray &elem : vec_moved) { |
| 109 | // Test the square bracket operator returns the same value as iteration. |
| 110 | EXPECT_EQ(&elem, &vec_moved[count]); |
| 111 |
| 112 | EXPECT_EQ(elem.tag, count); |
| 113 | EXPECT_EQ(elem.vec.size(), count); |
| 114 | for (size_t j = 0; j < count; j++) { |
| 115 | EXPECT_EQ(elem.vec[j], j); |
| 116 | } |
| 117 | count++; |
| 118 | } |
| 119 | } |
| 120 |
| 121 | TEST(VectorTest, NotDefaultConstructible) { |
| 122 | struct NotDefaultConstructible { |
| 123 | explicit NotDefaultConstructible(size_t n) { BSSL_CHECK(array.Init(n)); } |
| 124 | Array<int> array; |
| 125 | }; |
| 126 |
| 127 | Vector<NotDefaultConstructible> vec; |
| 128 | ASSERT_TRUE(vec.Push(NotDefaultConstructible(0))); |
| 129 | ASSERT_TRUE(vec.Push(NotDefaultConstructible(1))); |
| 130 | ASSERT_TRUE(vec.Push(NotDefaultConstructible(2))); |
| 131 | ASSERT_TRUE(vec.Push(NotDefaultConstructible(3))); |
| 132 | EXPECT_EQ(vec.size(), 4u); |
| 133 | EXPECT_EQ(0u, vec[0].array.size()); |
| 134 | EXPECT_EQ(1u, vec[1].array.size()); |
| 135 | EXPECT_EQ(2u, vec[2].array.size()); |
| 136 | EXPECT_EQ(3u, vec[3].array.size()); |
| 137 | } |
| 138 |
| 139 | TEST(VectorDeathTest, BoundsChecks) { |
| 140 | Vector<int> vec; |
| 141 | ASSERT_TRUE(vec.Push(1)); |
| 142 | // Within bounds of the capacity, but not the vector. |
| 143 | EXPECT_DEATH_IF_SUPPORTED(vec[1], ""); |
| 144 | // Not within bounds of the capacity either. |
| 145 | EXPECT_DEATH_IF_SUPPORTED(vec[10000], ""); |
| 146 | } |
| 147 |
| 148 | TEST(InplaceVector, Basic) { |
| 149 | InplaceVector<int, 4> vec; |
| 150 | EXPECT_TRUE(vec.empty()); |
| 151 | EXPECT_EQ(0u, vec.size()); |
| 152 | EXPECT_EQ(vec.begin(), vec.end()); |
| 153 |
| 154 | int data3[] = {1, 2, 3}; |
| 155 | ASSERT_TRUE(vec.TryCopyFrom(data3)); |
| 156 | EXPECT_FALSE(vec.empty()); |
| 157 | EXPECT_EQ(3u, vec.size()); |
| 158 | auto iter = vec.begin(); |
| 159 | EXPECT_EQ(1, vec[0]); |
| 160 | EXPECT_EQ(1, *iter); |
| 161 | iter++; |
| 162 | EXPECT_EQ(2, vec[1]); |
| 163 | EXPECT_EQ(2, *iter); |
| 164 | iter++; |
| 165 | EXPECT_EQ(3, vec[2]); |
| 166 | EXPECT_EQ(3, *iter); |
| 167 | iter++; |
| 168 | EXPECT_EQ(iter, vec.end()); |
| 169 | EXPECT_EQ(Span(vec), Span(data3)); |
| 170 |
| 171 | InplaceVector<int, 4> vec2 = vec; |
| 172 | EXPECT_EQ(Span(vec), Span(vec2)); |
| 173 |
| 174 | InplaceVector<int, 4> vec3; |
| 175 | vec3 = vec; |
| 176 | EXPECT_EQ(Span(vec), Span(vec2)); |
| 177 |
| 178 | int data4[] = {1, 2, 3, 4}; |
| 179 | ASSERT_TRUE(vec.TryCopyFrom(data4)); |
| 180 | EXPECT_EQ(Span(vec), Span(data4)); |
| 181 |
| 182 | int data5[] = {1, 2, 3, 4, 5}; |
| 183 | EXPECT_FALSE(vec.TryCopyFrom(data5)); |
| 184 | EXPECT_FALSE(vec.TryResize(5)); |
| 185 |
| 186 | // Shrink the vector. |
| 187 | ASSERT_TRUE(vec.TryResize(3)); |
| 188 | EXPECT_EQ(Span(vec), Span(data3)); |
| 189 |
| 190 | // Enlarge it again. The new value should have been value-initialized. |
| 191 | ASSERT_TRUE(vec.TryResize(4)); |
| 192 | EXPECT_EQ(vec[3], 0); |
| 193 |
| 194 | // Self-assignment should not break the vector. Indirect through a pointer to |
| 195 | // avoid tripping a compiler warning. |
| 196 | vec.CopyFrom(data4); |
| 197 | const auto *ptr = &vec; |
| 198 | vec = *ptr; |
| 199 | EXPECT_EQ(Span(vec), Span(data4)); |
| 200 | } |
| 201 |
| 202 | TEST(InplaceVectorTest, ComplexType) { |
| 203 | InplaceVector<std::vector<int>, 4> vec_of_vecs; |
| 204 | const std::vector<int> data[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 205 | vec_of_vecs.CopyFrom(data); |
| 206 | EXPECT_EQ(Span(vec_of_vecs), Span(data)); |
| 207 |
| 208 | vec_of_vecs.Resize(2); |
| 209 | EXPECT_EQ(Span(vec_of_vecs), Span(data, 2)); |
| 210 |
| 211 | vec_of_vecs.Resize(4); |
| 212 | EXPECT_EQ(4u, vec_of_vecs.size()); |
| 213 | EXPECT_EQ(vec_of_vecs[0], data[0]); |
| 214 | EXPECT_EQ(vec_of_vecs[1], data[1]); |
| 215 | EXPECT_TRUE(vec_of_vecs[2].empty()); |
| 216 | EXPECT_TRUE(vec_of_vecs[3].empty()); |
| 217 |
| 218 | // Copy-construction. |
| 219 | InplaceVector<std::vector<int>, 4> vec_of_vecs2 = vec_of_vecs; |
| 220 | EXPECT_EQ(4u, vec_of_vecs2.size()); |
| 221 | EXPECT_EQ(vec_of_vecs2[0], data[0]); |
| 222 | EXPECT_EQ(vec_of_vecs2[1], data[1]); |
| 223 | EXPECT_TRUE(vec_of_vecs2[2].empty()); |
| 224 | EXPECT_TRUE(vec_of_vecs2[3].empty()); |
| 225 |
| 226 | // Copy-assignment. |
| 227 | InplaceVector<std::vector<int>, 4> vec_of_vecs3; |
| 228 | vec_of_vecs3 = vec_of_vecs; |
| 229 | EXPECT_EQ(4u, vec_of_vecs3.size()); |
| 230 | EXPECT_EQ(vec_of_vecs3[0], data[0]); |
| 231 | EXPECT_EQ(vec_of_vecs3[1], data[1]); |
| 232 | EXPECT_TRUE(vec_of_vecs3[2].empty()); |
| 233 | EXPECT_TRUE(vec_of_vecs3[3].empty()); |
| 234 |
| 235 | // Move-construction. |
| 236 | InplaceVector<std::vector<int>, 4> vec_of_vecs4 = std::move(vec_of_vecs); |
| 237 | EXPECT_EQ(4u, vec_of_vecs4.size()); |
| 238 | EXPECT_EQ(vec_of_vecs4[0], data[0]); |
| 239 | EXPECT_EQ(vec_of_vecs4[1], data[1]); |
| 240 | EXPECT_TRUE(vec_of_vecs4[2].empty()); |
| 241 | EXPECT_TRUE(vec_of_vecs4[3].empty()); |
| 242 |
| 243 | // The elements of the original vector should have been moved-from. |
| 244 | EXPECT_EQ(4u, vec_of_vecs.size()); |
| 245 | for (const auto &vec : vec_of_vecs) { |
| 246 | EXPECT_TRUE(vec.empty()); |
| 247 | } |
| 248 |
| 249 | // Move-assignment. |
| 250 | InplaceVector<std::vector<int>, 4> vec_of_vecs5; |
| 251 | vec_of_vecs5 = std::move(vec_of_vecs4); |
| 252 | EXPECT_EQ(4u, vec_of_vecs5.size()); |
| 253 | EXPECT_EQ(vec_of_vecs5[0], data[0]); |
| 254 | EXPECT_EQ(vec_of_vecs5[1], data[1]); |
| 255 | EXPECT_TRUE(vec_of_vecs5[2].empty()); |
| 256 | EXPECT_TRUE(vec_of_vecs5[3].empty()); |
| 257 |
| 258 | // The elements of the original vector should have been moved-from. |
| 259 | EXPECT_EQ(4u, vec_of_vecs4.size()); |
| 260 | for (const auto &vec : vec_of_vecs4) { |
| 261 | EXPECT_TRUE(vec.empty()); |
| 262 | } |
| 263 |
| 264 | std::vector<int> v = {42}; |
| 265 | vec_of_vecs5.Resize(3); |
| 266 | EXPECT_TRUE(vec_of_vecs5.TryPushBack(v)); |
| 267 | EXPECT_EQ(v, vec_of_vecs5[3]); |
| 268 | EXPECT_FALSE(vec_of_vecs5.TryPushBack(v)); |
| 269 | } |
| 270 |
| 271 | TEST(InplaceVectorTest, EraseIf) { |
| 272 | // Test that EraseIf never causes a self-move, and also correctly works with |
| 273 | // a move-only type that cannot be default-constructed. |
| 274 | class NoSelfMove { |
| 275 | public: |
| 276 | explicit NoSelfMove(int v) : v_(std::make_unique<int>(v)) {} |
| 277 | NoSelfMove(NoSelfMove &&other) { *this = std::move(other); } |
| 278 | NoSelfMove &operator=(NoSelfMove &&other) { |
| 279 | BSSL_CHECK(this != &other); |
| 280 | v_ = std::move(other.v_); |
| 281 | return *this; |
| 282 | } |
| 283 |
| 284 | int value() const { return *v_; } |
| 285 |
| 286 | private: |
| 287 | std::unique_ptr<int> v_; |
| 288 | }; |
| 289 |
| 290 | InplaceVector<NoSelfMove, 8> vec; |
| 291 | auto reset = [&] { |
| 292 | vec.clear(); |
| 293 | for (int i = 0; i < 8; i++) { |
| 294 | vec.PushBack(NoSelfMove(i)); |
| 295 | } |
| 296 | }; |
| 297 | auto expect = [&](const std::vector<int> &expected) { |
| 298 | ASSERT_EQ(vec.size(), expected.size()); |
| 299 | for (size_t i = 0; i < vec.size(); i++) { |
| 300 | SCOPED_TRACE(i); |
| 301 | EXPECT_EQ(vec[i].value(), expected[i]); |
| 302 | } |
| 303 | }; |
| 304 |
| 305 | reset(); |
| 306 | vec.EraseIf([](const auto &) { return false; }); |
| 307 | expect({0, 1, 2, 3, 4, 5, 6, 7}); |
| 308 |
| 309 | reset(); |
| 310 | vec.EraseIf([](const auto &) { return true; }); |
| 311 | expect({}); |
| 312 |
| 313 | reset(); |
| 314 | vec.EraseIf([](const auto &v) { return v.value() < 4; }); |
| 315 | expect({4, 5, 6, 7}); |
| 316 |
| 317 | reset(); |
| 318 | vec.EraseIf([](const auto &v) { return v.value() >= 4; }); |
| 319 | expect({0, 1, 2, 3}); |
| 320 |
| 321 | reset(); |
| 322 | vec.EraseIf([](const auto &v) { return v.value() % 2 == 0; }); |
| 323 | expect({1, 3, 5, 7}); |
| 324 |
| 325 | reset(); |
| 326 | vec.EraseIf([](const auto &v) { return v.value() % 2 == 1; }); |
| 327 | expect({0, 2, 4, 6}); |
| 328 |
| 329 | reset(); |
| 330 | vec.EraseIf([](const auto &v) { return 2 <= v.value() && v.value() <= 5; }); |
| 331 | expect({0, 1, 6, 7}); |
| 332 |
| 333 | reset(); |
| 334 | vec.EraseIf([](const auto &v) { return v.value() == 0; }); |
| 335 | expect({1, 2, 3, 4, 5, 6, 7}); |
| 336 |
| 337 | reset(); |
| 338 | vec.EraseIf([](const auto &v) { return v.value() == 4; }); |
| 339 | expect({0, 1, 2, 3, 5, 6, 7}); |
| 340 |
| 341 | reset(); |
| 342 | vec.EraseIf([](const auto &v) { return v.value() == 7; }); |
| 343 | expect({0, 1, 2, 3, 4, 5, 6}); |
| 344 | } |
| 345 |
| 346 | TEST(InplaceVectorDeathTest, BoundsChecks) { |
| 347 | InplaceVector<int, 4> vec; |
| 348 | // The vector is currently empty. |
| 349 | EXPECT_DEATH_IF_SUPPORTED(vec[0], ""); |
| 350 | int data[] = {1, 2, 3}; |
| 351 | vec.CopyFrom(data); |
| 352 | // Some more out-of-bounds elements. |
| 353 | EXPECT_DEATH_IF_SUPPORTED(vec[3], ""); |
| 354 | EXPECT_DEATH_IF_SUPPORTED(vec[4], ""); |
| 355 | EXPECT_DEATH_IF_SUPPORTED(vec[1000], ""); |
| 356 | // The vector cannot be resized past the capacity. |
| 357 | EXPECT_DEATH_IF_SUPPORTED(vec.Resize(5), ""); |
| 358 | EXPECT_DEATH_IF_SUPPORTED(vec.ResizeForOverwrite(5), ""); |
| 359 | int too_much_data[] = {1, 2, 3, 4, 5}; |
| 360 | EXPECT_DEATH_IF_SUPPORTED(vec.CopyFrom(too_much_data), ""); |
| 361 | vec.Resize(4); |
| 362 | EXPECT_DEATH_IF_SUPPORTED(vec.PushBack(42), ""); |
| 363 | } |
| 364 |
| 365 | TEST(ReconstructSeqnumTest, Increment) { |
| 366 | // Test simple cases from the beginning of an epoch with both 8- and 16-bit |
| 367 | // wire sequence numbers. |
| 368 | EXPECT_EQ(reconstruct_seqnum(0, 0xff, 0), 0u); |
| 369 | EXPECT_EQ(reconstruct_seqnum(1, 0xff, 0), 1u); |
| 370 | EXPECT_EQ(reconstruct_seqnum(2, 0xff, 0), 2u); |
| 371 | EXPECT_EQ(reconstruct_seqnum(0, 0xffff, 0), 0u); |
| 372 | EXPECT_EQ(reconstruct_seqnum(1, 0xffff, 0), 1u); |
| 373 | EXPECT_EQ(reconstruct_seqnum(2, 0xffff, 0), 2u); |
| 374 |
| 375 | // When the max seen sequence number is 0, the numerically closest |
| 376 | // reconstructed sequence number could be negative. Sequence numbers are |
| 377 | // non-negative, so reconstruct_seqnum should instead return the closest |
| 378 | // non-negative number instead of returning a number congruent to that |
| 379 | // closest negative number mod 2^64. |
| 380 | EXPECT_EQ(reconstruct_seqnum(0xff, 0xff, 0), 0xffu); |
| 381 | EXPECT_EQ(reconstruct_seqnum(0xfe, 0xff, 0), 0xfeu); |
| 382 | EXPECT_EQ(reconstruct_seqnum(0xffff, 0xffff, 0), 0xffffu); |
| 383 | EXPECT_EQ(reconstruct_seqnum(0xfffe, 0xffff, 0), 0xfffeu); |
| 384 |
| 385 | // When the wire sequence number is less than the corresponding low bytes of |
| 386 | // the max seen sequence number, check that the next larger sequence number |
| 387 | // is reconstructed as its numerically closer than the corresponding sequence |
| 388 | // number that would keep the high order bits the same. |
| 389 | EXPECT_EQ(reconstruct_seqnum(0, 0xff, 0xff), 0x100u); |
| 390 | EXPECT_EQ(reconstruct_seqnum(1, 0xff, 0xff), 0x101u); |
| 391 | EXPECT_EQ(reconstruct_seqnum(2, 0xff, 0xff), 0x102u); |
| 392 | EXPECT_EQ(reconstruct_seqnum(0, 0xffff, 0xffff), 0x10000u); |
| 393 | EXPECT_EQ(reconstruct_seqnum(1, 0xffff, 0xffff), 0x10001u); |
| 394 | EXPECT_EQ(reconstruct_seqnum(2, 0xffff, 0xffff), 0x10002u); |
| 395 |
| 396 | // Test cases when the wire sequence number is close to the largest magnitude |
| 397 | // that can be represented in 8 or 16 bits. |
| 398 | EXPECT_EQ(reconstruct_seqnum(0xff, 0xff, 0x2f0), 0x2ffu); |
| 399 | EXPECT_EQ(reconstruct_seqnum(0xfe, 0xff, 0x2f0), 0x2feu); |
| 400 | EXPECT_EQ(reconstruct_seqnum(0xffff, 0xffff, 0x2f000), 0x2ffffu); |
| 401 | EXPECT_EQ(reconstruct_seqnum(0xfffe, 0xffff, 0x2f000), 0x2fffeu); |
| 402 |
| 403 | // Test that reconstruct_seqnum can return the maximum sequence number, |
| 404 | // 2^48-1. |
| 405 | constexpr uint64_t kMaxSequence = (uint64_t{1} << 48) - 1; |
| 406 | EXPECT_EQ(reconstruct_seqnum(0xff, 0xff, kMaxSequence), kMaxSequence); |
| 407 | EXPECT_EQ(reconstruct_seqnum(0xff, 0xff, kMaxSequence - 1), kMaxSequence); |
| 408 | EXPECT_EQ(reconstruct_seqnum(0xffff, 0xffff, kMaxSequence), kMaxSequence); |
| 409 | EXPECT_EQ(reconstruct_seqnum(0xffff, 0xffff, kMaxSequence - 1), kMaxSequence); |
| 410 | } |
| 411 |
| 412 | TEST(ReconstructSeqnumTest, Decrement) { |
| 413 | // Test that the sequence number 0 can be reconstructed when the max |
| 414 | // seen sequence number is greater than 0. |
| 415 | EXPECT_EQ(reconstruct_seqnum(0, 0xff, 0x10), 0u); |
| 416 | EXPECT_EQ(reconstruct_seqnum(0, 0xffff, 0x1000), 0u); |
| 417 |
| 418 | // Test cases where the reconstructed sequence number is less than the max |
| 419 | // seen sequence number. |
| 420 | EXPECT_EQ(reconstruct_seqnum(0, 0xff, 0x210), 0x200u); |
| 421 | EXPECT_EQ(reconstruct_seqnum(2, 0xff, 0x210), 0x202u); |
| 422 | EXPECT_EQ(reconstruct_seqnum(0, 0xffff, 0x43210), 0x40000u); |
| 423 | EXPECT_EQ(reconstruct_seqnum(2, 0xffff, 0x43210), 0x40002u); |
| 424 |
| 425 | // Test when the wire sequence number is greater than the low bits of the |
| 426 | // max seen sequence number. |
| 427 | EXPECT_EQ(reconstruct_seqnum(0xff, 0xff, 0x200), 0x1ffu); |
| 428 | EXPECT_EQ(reconstruct_seqnum(0xfe, 0xff, 0x200), 0x1feu); |
| 429 | EXPECT_EQ(reconstruct_seqnum(0xffff, 0xffff, 0x20000), 0x1ffffu); |
| 430 | EXPECT_EQ(reconstruct_seqnum(0xfffe, 0xffff, 0x20000), 0x1fffeu); |
| 431 |
| 432 | constexpr uint64_t kMaxSequence = (uint64_t{1} << 48) - 1; |
| 433 | // kMaxSequence00 is kMaxSequence with the last byte replaced with 0x00. |
| 434 | constexpr uint64_t kMaxSequence00 = kMaxSequence - 0xff; |
| 435 | // kMaxSequence0000 is kMaxSequence with the last byte replaced with 0x0000. |
| 436 | constexpr uint64_t kMaxSequence0000 = kMaxSequence - 0xffff; |
| 437 |
| 438 | // Test when the max seen sequence number is close to the 2^48-1 max value. |
| 439 | // In some cases, the closest numerical value in the integers will exceed the |
| 440 | // limit. In this case, reconstruct_seqnum should return the closest integer |
| 441 | // within range. |
| 442 | EXPECT_EQ(reconstruct_seqnum(0, 0xff, kMaxSequence), kMaxSequence00); |
| 443 | EXPECT_EQ(reconstruct_seqnum(0, 0xff, kMaxSequence - 1), kMaxSequence00); |
| 444 | EXPECT_EQ(reconstruct_seqnum(1, 0xff, kMaxSequence), kMaxSequence00 + 0x01); |
| 445 | EXPECT_EQ(reconstruct_seqnum(1, 0xff, kMaxSequence - 1), |
| 446 | kMaxSequence00 + 0x01); |
| 447 | EXPECT_EQ(reconstruct_seqnum(0xfe, 0xff, kMaxSequence), |
| 448 | kMaxSequence00 + 0xfe); |
| 449 | EXPECT_EQ(reconstruct_seqnum(0xfd, 0xff, kMaxSequence - 1), |
| 450 | kMaxSequence00 + 0xfd); |
| 451 | EXPECT_EQ(reconstruct_seqnum(0, 0xffff, kMaxSequence), kMaxSequence0000); |
| 452 | EXPECT_EQ(reconstruct_seqnum(0, 0xffff, kMaxSequence - 1), kMaxSequence0000); |
| 453 | EXPECT_EQ(reconstruct_seqnum(1, 0xffff, kMaxSequence), |
| 454 | kMaxSequence0000 + 0x0001); |
| 455 | EXPECT_EQ(reconstruct_seqnum(1, 0xffff, kMaxSequence - 1), |
| 456 | kMaxSequence0000 + 0x0001); |
| 457 | EXPECT_EQ(reconstruct_seqnum(0xfffe, 0xffff, kMaxSequence), |
| 458 | kMaxSequence0000 + 0xfffe); |
| 459 | EXPECT_EQ(reconstruct_seqnum(0xfffd, 0xffff, kMaxSequence - 1), |
| 460 | kMaxSequence0000 + 0xfffd); |
| 461 | } |
| 462 |
| 463 | TEST(ReconstructSeqnumTest, Halfway) { |
| 464 | // Test wire sequence numbers that are close to halfway away from the max |
| 465 | // seen sequence number. The algorithm specifies that the output should be |
| 466 | // numerically closest to 1 plus the max seen (0x100 in the following test |
| 467 | // cases). With a max seen of 0x100 and a wire sequence of 0x81, the two |
| 468 | // closest values to 1+0x100 are 0x81 and 0x181, which are both the same |
| 469 | // amount away. The algorithm doesn't specify what to do on this edge case; |
| 470 | // our implementation chooses the larger value (0x181), on the assumption that |
| 471 | // it's more likely to be a new or larger sequence number rather than a replay |
| 472 | // or an out-of-order packet. |
| 473 | EXPECT_EQ(reconstruct_seqnum(0x80, 0xff, 0x100), 0x180u); |
| 474 | EXPECT_EQ(reconstruct_seqnum(0x81, 0xff, 0x100), 0x181u); |
| 475 | EXPECT_EQ(reconstruct_seqnum(0x82, 0xff, 0x100), 0x82u); |
| 476 |
| 477 | // Repeat these tests with 16-bit wire sequence numbers. |
| 478 | EXPECT_EQ(reconstruct_seqnum(0x8000, 0xffff, 0x10000), 0x18000u); |
| 479 | EXPECT_EQ(reconstruct_seqnum(0x8001, 0xffff, 0x10000), 0x18001u); |
| 480 | EXPECT_EQ(reconstruct_seqnum(0x8002, 0xffff, 0x10000), 0x8002u); |
| 481 | } |
| 482 |
| 483 | TEST(DTLSMessageBitmapTest, Basic) { |
| 484 | // expect_bitmap checks that |b|'s unmarked bits are those listed in |ranges|. |
| 485 | // Each element of |ranges| must be non-empty and non-overlapping, and |
| 486 | // |ranges| must be sorted. |
| 487 | auto expect_bitmap = [](const DTLSMessageBitmap &b, |
| 488 | const std::vector<DTLSMessageBitmap::Range> &ranges) { |
| 489 | EXPECT_EQ(ranges.empty(), b.IsComplete()); |
| 490 | size_t start = 0; |
| 491 | for (const auto &r : ranges) { |
| 492 | for (; start < r.start; start++) { |
| 493 | SCOPED_TRACE(start); |
| 494 | EXPECT_EQ(b.NextUnmarkedRange(start), r); |
| 495 | } |
| 496 | for (; start < r.end; start++) { |
| 497 | SCOPED_TRACE(start); |
| 498 | EXPECT_EQ(b.NextUnmarkedRange(start), |
| 499 | (DTLSMessageBitmap::Range{start, r.end})); |
| 500 | } |
| 501 | } |
| 502 | EXPECT_TRUE(b.NextUnmarkedRange(start).empty()); |
| 503 | EXPECT_TRUE(b.NextUnmarkedRange(start + 1).empty()); |
| 504 | EXPECT_TRUE(b.NextUnmarkedRange(start + 42).empty()); |
| 505 |
| 506 | // This is implied from the previous checks, but NextUnmarkedRange should |
| 507 | // work as an iterator to reconstruct the ranges. |
| 508 | std::vector<DTLSMessageBitmap::Range> got_ranges; |
| 509 | for (auto r = b.NextUnmarkedRange(0); !r.empty(); |
| 510 | r = b.NextUnmarkedRange(r.end)) { |
| 511 | got_ranges.push_back(r); |
| 512 | } |
| 513 | EXPECT_EQ(ranges, got_ranges); |
| 514 | }; |
| 515 |
| 516 | // Initially, the bitmap is empty (fully marked). |
| 517 | DTLSMessageBitmap bitmap; |
| 518 | expect_bitmap(bitmap, {}); |
| 519 |
| 520 | // It can also be initialized to the empty message and marked. |
| 521 | ASSERT_TRUE(bitmap.Init(0)); |
| 522 | expect_bitmap(bitmap, {}); |
| 523 | bitmap.MarkRange(0, 0); |
| 524 | expect_bitmap(bitmap, {}); |
| 525 |
| 526 | // Track 100 bits and mark byte by byte. |
| 527 | ASSERT_TRUE(bitmap.Init(100)); |
| 528 | expect_bitmap(bitmap, {{0, 100}}); |
| 529 | for (size_t i = 0; i < 100; i++) { |
| 530 | SCOPED_TRACE(i); |
| 531 | bitmap.MarkRange(i, i + 1); |
| 532 | if (i < 99) { |
| 533 | expect_bitmap(bitmap, {{i + 1, 100}}); |
| 534 | } else { |
| 535 | expect_bitmap(bitmap, {}); |
| 536 | } |
| 537 | } |
| 538 |
| 539 | // Do the same but in reverse. |
| 540 | ASSERT_TRUE(bitmap.Init(100)); |
| 541 | expect_bitmap(bitmap, {{0, 100}}); |
| 542 | for (size_t i = 100; i > 0; i--) { |
| 543 | SCOPED_TRACE(i); |
| 544 | bitmap.MarkRange(i - 1, i); |
| 545 | if (i > 1) { |
| 546 | expect_bitmap(bitmap, {{0, i - 1}}); |
| 547 | } else { |
| 548 | expect_bitmap(bitmap, {}); |
| 549 | } |
| 550 | } |
| 551 |
| 552 | // Overlapping ranges are fine. |
| 553 | ASSERT_TRUE(bitmap.Init(100)); |
| 554 | expect_bitmap(bitmap, {{0, 100}}); |
| 555 | for (size_t i = 0; i < 100; i++) { |
| 556 | SCOPED_TRACE(i); |
| 557 | bitmap.MarkRange(i / 2, i + 1); |
| 558 | if (i < 99) { |
| 559 | expect_bitmap(bitmap, {{i + 1, 100}}); |
| 560 | } else { |
| 561 | expect_bitmap(bitmap, {}); |
| 562 | } |
| 563 | } |
| 564 |
| 565 | // Mark the middle chunk of every power of 3. |
| 566 | ASSERT_TRUE(bitmap.Init(100)); |
| 567 | bitmap.MarkRange(1, 2); |
| 568 | bitmap.MarkRange(3, 6); |
| 569 | bitmap.MarkRange(9, 18); |
| 570 | bitmap.MarkRange(27, 54); |
| 571 | bitmap.MarkRange(81, 162); |
| 572 | expect_bitmap(bitmap, {{0, 1}, {2, 3}, {6, 9}, {18, 27}, {54, 81}}); |
| 573 |
| 574 | // Mark most of the chunk shifted down a bit, so it both overlaps the previous |
| 575 | // and also leaves some of the right chunks unmarked. |
| 576 | bitmap.MarkRange(6 - 2, 9 - 2); |
| 577 | bitmap.MarkRange(18 - 4, 27 - 4); |
| 578 | bitmap.MarkRange(54 - 8, 81 - 8); |
| 579 | expect_bitmap(bitmap, |
| 580 | {{0, 1}, {2, 3}, {9 - 2, 9}, {27 - 4, 27}, {81 - 8, 81}}); |
| 581 |
| 582 | // Re-mark things that have already been marked. |
| 583 | bitmap.MarkRange(1, 2); |
| 584 | bitmap.MarkRange(3, 6); |
| 585 | bitmap.MarkRange(9, 18); |
| 586 | bitmap.MarkRange(27, 54); |
| 587 | bitmap.MarkRange(81, 162); |
| 588 | expect_bitmap(bitmap, |
| 589 | {{0, 1}, {2, 3}, {9 - 2, 9}, {27 - 4, 27}, {81 - 8, 81}}); |
| 590 |
| 591 | // Moves should work. |
| 592 | DTLSMessageBitmap bitmap2 = std::move(bitmap); |
| 593 | expect_bitmap(bitmap, {}); |
| 594 | expect_bitmap(bitmap2, |
| 595 | {{0, 1}, {2, 3}, {9 - 2, 9}, {27 - 4, 27}, {81 - 8, 81}}); |
| 596 |
| 597 | // Mark everything in two large ranges. |
| 598 | bitmap2.MarkRange(27 - 2, 100); |
| 599 | expect_bitmap(bitmap2, {{0, 1}, {2, 3}, {9 - 2, 9}, {27 - 4, 27 - 2}}); |
| 600 | bitmap2.MarkRange(0, 50); |
| 601 | expect_bitmap(bitmap2, {}); |
| 602 |
| 603 | // MarkRange inputs may be "out of bounds". The bitmap has conceptually |
| 604 | // infinitely many marked bits past where it was initialized. |
| 605 | ASSERT_TRUE(bitmap.Init(10)); |
| 606 | expect_bitmap(bitmap, {{0, 10}}); |
| 607 | bitmap.MarkRange(5, SIZE_MAX); |
| 608 | expect_bitmap(bitmap, {{0, 5}}); |
| 609 | bitmap.MarkRange(0, SIZE_MAX); |
| 610 | expect_bitmap(bitmap, {}); |
| 611 | } |
| 612 |
| 613 | TEST(MRUQueueTest, Basic) { |
| 614 | // Use a complex type to confirm the queue handles them correctly. |
| 615 | MRUQueue<std::unique_ptr<int>, 8> queue; |
| 616 | auto expect_queue = [&](const std::vector<int> &expected) { |
| 617 | EXPECT_EQ(queue.size(), expected.size()); |
| 618 | EXPECT_EQ(queue.empty(), expected.empty()); |
| 619 | std::vector<int> queue_values; |
| 620 | for (size_t i = 0; i < queue.size(); i++) { |
| 621 | queue_values.push_back(*queue[i]); |
| 622 | } |
| 623 | EXPECT_EQ(queue_values, expected); |
| 624 | }; |
| 625 |
| 626 | expect_queue({}); |
| 627 | queue.PushBack(std::make_unique<int>(1)); |
| 628 | expect_queue({1}); |
| 629 | queue.PushBack(std::make_unique<int>(2)); |
| 630 | expect_queue({1, 2}); |
| 631 | queue.PushBack(std::make_unique<int>(3)); |
| 632 | expect_queue({1, 2, 3}); |
| 633 | queue.PushBack(std::make_unique<int>(4)); |
| 634 | expect_queue({1, 2, 3, 4}); |
| 635 | queue.PushBack(std::make_unique<int>(5)); |
| 636 | expect_queue({1, 2, 3, 4, 5}); |
| 637 | queue.PushBack(std::make_unique<int>(6)); |
| 638 | expect_queue({1, 2, 3, 4, 5, 6}); |
| 639 | queue.PushBack(std::make_unique<int>(7)); |
| 640 | expect_queue({1, 2, 3, 4, 5, 6, 7}); |
| 641 | queue.PushBack(std::make_unique<int>(8)); |
| 642 | expect_queue({1, 2, 3, 4, 5, 6, 7, 8}); |
| 643 |
| 644 | // We are at capacity, so later additions will drop the start. Do more than 8 |
| 645 | // insertions to test that the start index can wrap around. |
| 646 | queue.PushBack(std::make_unique<int>(9)); |
| 647 | expect_queue({2, 3, 4, 5, 6, 7, 8, 9}); |
| 648 | queue.PushBack(std::make_unique<int>(10)); |
| 649 | expect_queue({3, 4, 5, 6, 7, 8, 9, 10}); |
| 650 | queue.PushBack(std::make_unique<int>(11)); |
| 651 | expect_queue({4, 5, 6, 7, 8, 9, 10, 11}); |
| 652 | queue.PushBack(std::make_unique<int>(12)); |
| 653 | expect_queue({5, 6, 7, 8, 9, 10, 11, 12}); |
| 654 | queue.PushBack(std::make_unique<int>(13)); |
| 655 | expect_queue({6, 7, 8, 9, 10, 11, 12, 13}); |
| 656 | queue.PushBack(std::make_unique<int>(14)); |
| 657 | expect_queue({7, 8, 9, 10, 11, 12, 13, 14}); |
| 658 | queue.PushBack(std::make_unique<int>(15)); |
| 659 | expect_queue({8, 9, 10, 11, 12, 13, 14, 15}); |
| 660 | queue.PushBack(std::make_unique<int>(16)); |
| 661 | expect_queue({9, 10, 11, 12, 13, 14, 15, 16}); |
| 662 | queue.PushBack(std::make_unique<int>(17)); |
| 663 | expect_queue({10, 11, 12, 13, 14, 15, 16, 17}); |
| 664 |
| 665 | // Clearing the queue should not leave the start index in a bad place. |
| 666 | queue.Clear(); |
| 667 | expect_queue({}); |
| 668 | queue.PushBack(std::make_unique<int>(1)); |
| 669 | expect_queue({1}); |
| 670 | queue.PushBack(std::make_unique<int>(2)); |
| 671 | expect_queue({1, 2}); |
| 672 | queue.PushBack(std::make_unique<int>(3)); |
| 673 | expect_queue({1, 2, 3}); |
| 674 | } |
| 675 |
| 676 | TEST(SSLAEADContextTest, Lengths) { |
| 677 | struct LengthTest { |
| 678 | // All plaintext lengths from |min_plaintext_len| to |max_plaintext_len| |
| 679 | // should return in |cipertext_len|. |
| 680 | size_t min_plaintext_len; |
| 681 | size_t max_plaintext_len; |
| 682 | size_t ciphertext_len; |
| 683 | }; |
| 684 |
| 685 | struct CipherLengthTest { |
| 686 | // |SSL3_CK_*| and |TLS1_CK_*| constants include an extra byte at the front, |
| 687 | // so these constants must be masked with 0xffff. |
| 688 | uint16_t cipher; |
| 689 | uint16_t version; |
| 690 | size_t enc_key_len, mac_key_len, fixed_iv_len; |
| 691 | size_t block_size; |
| 692 | std::vector<LengthTest> length_tests; |
| 693 | }; |
| 694 |
| 695 | const CipherLengthTest kTests[] = { |
| 696 | // 20-byte MAC, 8-byte CBC blocks with padding |
| 697 | { |
| 698 | /*cipher=*/SSL3_CK_RSA_DES_192_CBC3_SHA & 0xffff, |
| 699 | /*version=*/TLS1_2_VERSION, |
| 700 | /*enc_key_len=*/24, |
| 701 | /*mac_key_len=*/20, |
| 702 | /*fixed_iv_len=*/0, |
| 703 | /*block_size=*/8, |
| 704 | { |
| 705 | {/*min_plaintext_len=*/0, |
| 706 | /*max_plaintext_len=*/3, |
| 707 | /*ciphertext_len=*/32}, |
| 708 | {/*min_plaintext_len=*/4, |
| 709 | /*max_plaintext_len=*/11, |
| 710 | /*ciphertext_len=*/40}, |
| 711 | {/*min_plaintext_len=*/12, |
| 712 | /*max_plaintext_len=*/19, |
| 713 | /*ciphertext_len=*/48}, |
| 714 | }, |
| 715 | }, |
| 716 | // 20-byte MAC, 16-byte CBC blocks with padding |
| 717 | { |
| 718 | /*cipher=*/TLS1_CK_RSA_WITH_AES_128_SHA & 0xffff, |
| 719 | /*version=*/TLS1_2_VERSION, |
| 720 | /*enc_key_len=*/16, |
| 721 | /*mac_key_len=*/20, |
| 722 | /*fixed_iv_len=*/0, |
| 723 | /*block_size=*/16, |
| 724 | { |
| 725 | {/*min_plaintext_len=*/0, |
| 726 | /*max_plaintext_len=*/11, |
| 727 | /*ciphertext_len=*/48}, |
| 728 | {/*min_plaintext_len=*/12, |
| 729 | /*max_plaintext_len=*/27, |
| 730 | /*ciphertext_len=*/64}, |
| 731 | {/*min_plaintext_len=*/38, |
| 732 | /*max_plaintext_len=*/43, |
| 733 | /*ciphertext_len=*/80}, |
| 734 | }, |
| 735 | }, |
| 736 | // 32-byte MAC, 16-byte CBC blocks with padding |
| 737 | { |
| 738 | /*cipher=*/TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA256 & 0xffff, |
| 739 | /*version=*/TLS1_2_VERSION, |
| 740 | /*enc_key_len=*/16, |
| 741 | /*mac_key_len=*/32, |
| 742 | /*fixed_iv_len=*/0, |
| 743 | /*block_size=*/16, |
| 744 | { |
| 745 | {/*min_plaintext_len=*/0, |
| 746 | /*max_plaintext_len=*/15, |
| 747 | /*ciphertext_len=*/64}, |
| 748 | {/*min_plaintext_len=*/16, |
| 749 | /*max_plaintext_len=*/31, |
| 750 | /*ciphertext_len=*/80}, |
| 751 | {/*min_plaintext_len=*/32, |
| 752 | /*max_plaintext_len=*/47, |
| 753 | /*ciphertext_len=*/96}, |
| 754 | }, |
| 755 | }, |
| 756 | // 8-byte explicit IV, 16-byte tag |
| 757 | { |
| 758 | /*cipher=*/TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256 & 0xffff, |
| 759 | /*version=*/TLS1_2_VERSION, |
| 760 | /*enc_key_len=*/16, |
| 761 | /*mac_key_len=*/0, |
| 762 | /*fixed_iv_len=*/4, |
| 763 | /*block_size=*/1, |
| 764 | { |
| 765 | {/*min_plaintext_len=*/0, |
| 766 | /*max_plaintext_len=*/0, |
| 767 | /*ciphertext_len=*/24}, |
| 768 | {/*min_plaintext_len=*/1, |
| 769 | /*max_plaintext_len=*/1, |
| 770 | /*ciphertext_len=*/25}, |
| 771 | {/*min_plaintext_len=*/2, |
| 772 | /*max_plaintext_len=*/2, |
| 773 | /*ciphertext_len=*/26}, |
| 774 | {/*min_plaintext_len=*/42, |
| 775 | /*max_plaintext_len=*/42, |
| 776 | /*ciphertext_len=*/66}, |
| 777 | }, |
| 778 | }, |
| 779 | // No explicit IV, 16-byte tag. TLS 1.3's padding and record type overhead |
| 780 | // is added at another layer. |
| 781 | { |
| 782 | /*cipher=*/TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 & 0xffff, |
| 783 | /*version=*/TLS1_2_VERSION, |
| 784 | /*enc_key_len=*/32, |
| 785 | /*mac_key_len=*/0, |
| 786 | /*fixed_iv_len=*/12, |
| 787 | /*block_size=*/1, |
| 788 | { |
| 789 | {/*min_plaintext_len=*/0, |
| 790 | /*max_plaintext_len=*/0, |
| 791 | /*ciphertext_len=*/16}, |
| 792 | {/*min_plaintext_len=*/1, |
| 793 | /*max_plaintext_len=*/1, |
| 794 | /*ciphertext_len=*/17}, |
| 795 | {/*min_plaintext_len=*/2, |
| 796 | /*max_plaintext_len=*/2, |
| 797 | /*ciphertext_len=*/18}, |
| 798 | {/*min_plaintext_len=*/42, |
| 799 | /*max_plaintext_len=*/42, |
| 800 | /*ciphertext_len=*/58}, |
| 801 | }, |
| 802 | }, |
| 803 | { |
| 804 | /*cipher=*/TLS1_CK_AES_128_GCM_SHA256 & 0xffff, |
| 805 | /*version=*/TLS1_3_VERSION, |
| 806 | /*enc_key_len=*/16, |
| 807 | /*mac_key_len=*/0, |
| 808 | /*fixed_iv_len=*/12, |
| 809 | /*block_size=*/1, |
| 810 | { |
| 811 | {/*min_plaintext_len=*/0, |
| 812 | /*max_plaintext_len=*/0, |
| 813 | /*ciphertext_len=*/16}, |
| 814 | {/*min_plaintext_len=*/1, |
| 815 | /*max_plaintext_len=*/1, |
| 816 | /*ciphertext_len=*/17}, |
| 817 | {/*min_plaintext_len=*/2, |
| 818 | /*max_plaintext_len=*/2, |
| 819 | /*ciphertext_len=*/18}, |
| 820 | {/*min_plaintext_len=*/42, |
| 821 | /*max_plaintext_len=*/42, |
| 822 | /*ciphertext_len=*/58}, |
| 823 | }, |
| 824 | }, |
| 825 | { |
| 826 | /*cipher=*/TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff, |
| 827 | /*version=*/TLS1_3_VERSION, |
| 828 | /*enc_key_len=*/32, |
| 829 | /*mac_key_len=*/0, |
| 830 | /*fixed_iv_len=*/12, |
| 831 | /*block_size=*/1, |
| 832 | { |
| 833 | {/*min_plaintext_len=*/0, |
| 834 | /*max_plaintext_len=*/0, |
| 835 | /*ciphertext_len=*/16}, |
| 836 | {/*min_plaintext_len=*/1, |
| 837 | /*max_plaintext_len=*/1, |
| 838 | /*ciphertext_len=*/17}, |
| 839 | {/*min_plaintext_len=*/2, |
| 840 | /*max_plaintext_len=*/2, |
| 841 | /*ciphertext_len=*/18}, |
| 842 | {/*min_plaintext_len=*/42, |
| 843 | /*max_plaintext_len=*/42, |
| 844 | /*ciphertext_len=*/58}, |
| 845 | }, |
| 846 | }, |
| 847 | }; |
| 848 |
| 849 | for (const auto &cipher_test : kTests) { |
| 850 | const SSL_CIPHER *cipher = |
| 851 | SSL_get_cipher_by_value(static_cast<uint16_t>(cipher_test.cipher)); |
| 852 | ASSERT_TRUE(cipher) << "Could not find cipher " << cipher_test.cipher; |
| 853 | SCOPED_TRACE(SSL_CIPHER_standard_name(cipher)); |
| 854 |
| 855 | const uint8_t kZeros[EVP_AEAD_MAX_KEY_LENGTH] = {0}; |
| 856 | UniquePtr<SSLAEADContext> aead = |
| 857 | SSLAEADContext::Create(evp_aead_seal, cipher_test.version, cipher, |
| 858 | Span(kZeros).first(cipher_test.enc_key_len), |
| 859 | Span(kZeros).first(cipher_test.mac_key_len), |
| 860 | Span(kZeros).first(cipher_test.fixed_iv_len)); |
| 861 | ASSERT_TRUE(aead); |
| 862 |
| 863 | for (const auto &t : cipher_test.length_tests) { |
| 864 | SCOPED_TRACE(t.ciphertext_len); |
| 865 |
| 866 | for (size_t plaintext_len = t.min_plaintext_len; |
| 867 | plaintext_len <= t.max_plaintext_len; plaintext_len++) { |
| 868 | SCOPED_TRACE(plaintext_len); |
| 869 | size_t out_len; |
| 870 | ASSERT_TRUE(aead->CiphertextLen(&out_len, plaintext_len, 0)); |
| 871 | EXPECT_EQ(out_len, t.ciphertext_len); |
| 872 | } |
| 873 |
| 874 | EXPECT_EQ(aead->MaxSealInputLen(t.ciphertext_len), t.max_plaintext_len); |
| 875 | for (size_t extra = 0; extra < cipher_test.block_size; extra++) { |
| 876 | // Adding up to block_size - 1 bytes of space should not change how much |
| 877 | // room we have. |
| 878 | SCOPED_TRACE(extra); |
| 879 | EXPECT_EQ(aead->MaxSealInputLen(t.ciphertext_len + extra), |
| 880 | t.max_plaintext_len); |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 |
| 886 | } // namespace |
| 887 | BSSL_NAMESPACE_END |
| 888 | #endif // !BORINGSSL_SHARED_LIBRARY |
| 889 |