danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/spdy/core/spdy_simple_arena.h" |
| 6 | |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 7 | #include <string> |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | f3c80c9 | 2020-02-12 09:47:55 -0800 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/common/platform/api/quiche_test.h" |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 12 | |
| 13 | namespace spdy { |
| 14 | namespace { |
| 15 | |
| 16 | size_t kDefaultBlockSize = 2048; |
| 17 | const char kTestString[] = "This is a decently long test string."; |
| 18 | |
QUICHE team | bd5386d | 2019-12-10 07:42:53 -0800 | [diff] [blame] | 19 | TEST(SpdySimpleArenaTest, NoAllocationOnConstruction) { |
| 20 | SpdySimpleArena arena(kDefaultBlockSize); |
zhongyi | 96836b9 | 2019-12-10 20:38:25 -0800 | [diff] [blame] | 21 | EXPECT_EQ(0u, arena.status().bytes_allocated()); |
QUICHE team | bd5386d | 2019-12-10 07:42:53 -0800 | [diff] [blame] | 22 | } |
| 23 | |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 24 | TEST(SpdySimpleArenaTest, Memdup) { |
| 25 | SpdySimpleArena arena(kDefaultBlockSize); |
| 26 | const size_t length = strlen(kTestString); |
| 27 | char* c = arena.Memdup(kTestString, length); |
| 28 | EXPECT_NE(nullptr, c); |
| 29 | EXPECT_NE(c, kTestString); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 30 | EXPECT_EQ(quiche::QuicheStringPiece(c, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST(SpdySimpleArenaTest, MemdupLargeString) { |
| 34 | SpdySimpleArena arena(10 /* block size */); |
| 35 | const size_t length = strlen(kTestString); |
| 36 | char* c = arena.Memdup(kTestString, length); |
| 37 | EXPECT_NE(nullptr, c); |
| 38 | EXPECT_NE(c, kTestString); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 39 | EXPECT_EQ(quiche::QuicheStringPiece(c, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(SpdySimpleArenaTest, MultipleBlocks) { |
| 43 | SpdySimpleArena arena(40 /* block size */); |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 44 | std::vector<std::string> strings = { |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 45 | "One decently long string.", "Another string.", |
| 46 | "A third string that will surely go in a different block."}; |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 47 | std::vector<quiche::QuicheStringPiece> copies; |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 48 | for (const std::string& s : strings) { |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 49 | quiche::QuicheStringPiece sp(arena.Memdup(s.data(), s.size()), s.size()); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 50 | copies.push_back(sp); |
| 51 | } |
| 52 | EXPECT_EQ(strings.size(), copies.size()); |
| 53 | for (size_t i = 0; i < strings.size(); ++i) { |
| 54 | EXPECT_EQ(copies[i], strings[i]); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | TEST(SpdySimpleArenaTest, UseAfterReset) { |
| 59 | SpdySimpleArena arena(kDefaultBlockSize); |
| 60 | const size_t length = strlen(kTestString); |
| 61 | char* c = arena.Memdup(kTestString, length); |
| 62 | arena.Reset(); |
| 63 | c = arena.Memdup(kTestString, length); |
| 64 | EXPECT_NE(nullptr, c); |
| 65 | EXPECT_NE(c, kTestString); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 66 | EXPECT_EQ(quiche::QuicheStringPiece(c, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST(SpdySimpleArenaTest, Free) { |
| 70 | SpdySimpleArena arena(kDefaultBlockSize); |
| 71 | const size_t length = strlen(kTestString); |
| 72 | // Freeing memory not owned by the arena should be a no-op, and freeing |
| 73 | // before any allocations from the arena should be a no-op. |
| 74 | arena.Free(const_cast<char*>(kTestString), length); |
| 75 | char* c1 = arena.Memdup("Foo", 3); |
| 76 | char* c2 = arena.Memdup(kTestString, length); |
| 77 | arena.Free(const_cast<char*>(kTestString), length); |
| 78 | char* c3 = arena.Memdup("Bar", 3); |
| 79 | char* c4 = arena.Memdup(kTestString, length); |
| 80 | EXPECT_NE(c1, c2); |
| 81 | EXPECT_NE(c1, c3); |
| 82 | EXPECT_NE(c1, c4); |
| 83 | EXPECT_NE(c2, c3); |
| 84 | EXPECT_NE(c2, c4); |
| 85 | EXPECT_NE(c3, c4); |
| 86 | // Freeing c4 should succeed, since it was the most recent allocation. |
| 87 | arena.Free(c4, length); |
| 88 | // Freeing c2 should be a no-op. |
| 89 | arena.Free(c2, length); |
| 90 | // c5 should reuse memory that was previously used by c4. |
| 91 | char* c5 = arena.Memdup("Baz", 3); |
| 92 | EXPECT_EQ(c4, c5); |
| 93 | } |
| 94 | |
| 95 | TEST(SpdySimpleArenaTest, Alloc) { |
| 96 | SpdySimpleArena arena(kDefaultBlockSize); |
| 97 | const size_t length = strlen(kTestString); |
| 98 | char* c1 = arena.Alloc(length); |
| 99 | char* c2 = arena.Alloc(2 * length); |
| 100 | char* c3 = arena.Alloc(3 * length); |
| 101 | char* c4 = arena.Memdup(kTestString, length); |
| 102 | EXPECT_EQ(c1 + length, c2); |
| 103 | EXPECT_EQ(c2 + 2 * length, c3); |
| 104 | EXPECT_EQ(c3 + 3 * length, c4); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 105 | EXPECT_EQ(quiche::QuicheStringPiece(c4, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST(SpdySimpleArenaTest, Realloc) { |
| 109 | SpdySimpleArena arena(kDefaultBlockSize); |
| 110 | const size_t length = strlen(kTestString); |
| 111 | // Simple realloc that fits in the block. |
| 112 | char* c1 = arena.Memdup(kTestString, length); |
| 113 | char* c2 = arena.Realloc(c1, length, 2 * length); |
| 114 | EXPECT_TRUE(c1); |
| 115 | EXPECT_EQ(c1, c2); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 116 | EXPECT_EQ(quiche::QuicheStringPiece(c1, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 117 | // Multiple reallocs. |
| 118 | char* c3 = arena.Memdup(kTestString, length); |
| 119 | EXPECT_EQ(c2 + 2 * length, c3); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 120 | EXPECT_EQ(quiche::QuicheStringPiece(c3, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 121 | char* c4 = arena.Realloc(c3, length, 2 * length); |
| 122 | EXPECT_EQ(c3, c4); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 123 | EXPECT_EQ(quiche::QuicheStringPiece(c4, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 124 | char* c5 = arena.Realloc(c4, 2 * length, 3 * length); |
| 125 | EXPECT_EQ(c4, c5); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 126 | EXPECT_EQ(quiche::QuicheStringPiece(c5, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 127 | char* c6 = arena.Memdup(kTestString, length); |
| 128 | EXPECT_EQ(c5 + 3 * length, c6); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 129 | EXPECT_EQ(quiche::QuicheStringPiece(c6, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 130 | // Realloc that does not fit in the remainder of the first block. |
| 131 | char* c7 = arena.Realloc(c6, length, kDefaultBlockSize); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 132 | EXPECT_EQ(quiche::QuicheStringPiece(c7, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 133 | arena.Free(c7, kDefaultBlockSize); |
| 134 | char* c8 = arena.Memdup(kTestString, length); |
| 135 | EXPECT_NE(c6, c7); |
| 136 | EXPECT_EQ(c7, c8); |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 137 | EXPECT_EQ(quiche::QuicheStringPiece(c8, length), kTestString); |
danzh | 2ac2346 | 2019-04-11 13:42:16 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | } // namespace |
| 141 | } // namespace spdy |