| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/iovector.h" | 5 #include "net/quic/iovector.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 } | 52 } |
| 53 EXPECT_EQ(iov2.TotalBufferSize(), iov1.TotalBufferSize()); | 53 EXPECT_EQ(iov2.TotalBufferSize(), iov1.TotalBufferSize()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 TEST(IOVectorTest, Append) { | 56 TEST(IOVectorTest, Append) { |
| 57 IOVector iov; | 57 IOVector iov; |
| 58 int length = 0; | 58 int length = 0; |
| 59 const struct iovec* iov2 = iov.iovec(); | 59 const struct iovec* iov2 = iov.iovec(); |
| 60 | 60 |
| 61 ASSERT_EQ(0u, iov.Size()); | 61 ASSERT_EQ(0u, iov.Size()); |
| 62 ASSERT_TRUE(iov2 == NULL); | 62 ASSERT_TRUE(iov2 == nullptr); |
| 63 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 63 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 64 const int str_len = strlen(test_data[i]); | 64 const int str_len = strlen(test_data[i]); |
| 65 const int append_len = str_len / 2; | 65 const int append_len = str_len / 2; |
| 66 // This should append a new block | 66 // This should append a new block |
| 67 iov.Append(const_cast<char*>(test_data[i]), append_len); | 67 iov.Append(const_cast<char*>(test_data[i]), append_len); |
| 68 length += append_len; | 68 length += append_len; |
| 69 ASSERT_EQ(i + 1, static_cast<size_t>(iov.Size())); | 69 ASSERT_EQ(i + 1, static_cast<size_t>(iov.Size())); |
| 70 ASSERT_TRUE(iov.LastBlockEnd() == test_data[i] + append_len); | 70 ASSERT_TRUE(iov.LastBlockEnd() == test_data[i] + append_len); |
| 71 // This should just lengthen the existing block. | 71 // This should just lengthen the existing block. |
| 72 iov.Append(const_cast<char*>(test_data[i] + append_len), | 72 iov.Append(const_cast<char*>(test_data[i] + append_len), |
| 73 str_len - append_len); | 73 str_len - append_len); |
| 74 length += (str_len - append_len); | 74 length += (str_len - append_len); |
| 75 ASSERT_EQ(i + 1, static_cast<size_t>(iov.Size())); | 75 ASSERT_EQ(i + 1, static_cast<size_t>(iov.Size())); |
| 76 ASSERT_TRUE(iov.LastBlockEnd() == test_data[i] + str_len); | 76 ASSERT_TRUE(iov.LastBlockEnd() == test_data[i] + str_len); |
| 77 } | 77 } |
| 78 | 78 |
| 79 iov2 = iov.iovec(); | 79 iov2 = iov.iovec(); |
| 80 ASSERT_TRUE(iov2 != NULL); | 80 ASSERT_TRUE(iov2 != nullptr); |
| 81 for (size_t i = 0; i < iov.Size(); ++i) { | 81 for (size_t i = 0; i < iov.Size(); ++i) { |
| 82 ASSERT_TRUE(test_data[i] == iov2[i].iov_base); | 82 ASSERT_TRUE(test_data[i] == iov2[i].iov_base); |
| 83 ASSERT_EQ(strlen(test_data[i]), iov2[i].iov_len); | 83 ASSERT_EQ(strlen(test_data[i]), iov2[i].iov_len); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST(IOVectorTest, AppendIovec) { | 87 TEST(IOVectorTest, AppendIovec) { |
| 88 IOVector iov; | 88 IOVector iov; |
| 89 const struct iovec test_iov[] = { | 89 const struct iovec test_iov[] = { |
| 90 {const_cast<char*>("foo"), 3}, | 90 {const_cast<char*>("foo"), 3}, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 121 const int str_len = strlen(test_data[i]); | 121 const int str_len = strlen(test_data[i]); |
| 122 iov.Append(const_cast<char*>(test_data[i]), str_len); | 122 iov.Append(const_cast<char*>(test_data[i]), str_len); |
| 123 length += str_len; | 123 length += str_len; |
| 124 } | 124 } |
| 125 const char* endp = iov.LastBlockEnd(); | 125 const char* endp = iov.LastBlockEnd(); |
| 126 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 126 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 127 const struct iovec* iov2 = iov.iovec(); | 127 const struct iovec* iov2 = iov.iovec(); |
| 128 const size_t str_len = strlen(test_data[i]); | 128 const size_t str_len = strlen(test_data[i]); |
| 129 size_t tmp = str_len / 2; | 129 size_t tmp = str_len / 2; |
| 130 | 130 |
| 131 ASSERT_TRUE(iov2 != NULL); | 131 ASSERT_TRUE(iov2 != nullptr); |
| 132 ASSERT_TRUE(iov2[0].iov_base == test_data[i]); | 132 ASSERT_TRUE(iov2[0].iov_base == test_data[i]); |
| 133 ASSERT_EQ(str_len, iov2[0].iov_len); | 133 ASSERT_EQ(str_len, iov2[0].iov_len); |
| 134 | 134 |
| 135 // Consume half of the first block. | 135 // Consume half of the first block. |
| 136 size_t consumed = iov.Consume(tmp); | 136 size_t consumed = iov.Consume(tmp); |
| 137 ASSERT_EQ(tmp, consumed); | 137 ASSERT_EQ(tmp, consumed); |
| 138 ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data) - i, static_cast<size_t>(iov.Size())); | 138 ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data) - i, static_cast<size_t>(iov.Size())); |
| 139 iov2 = iov.iovec(); | 139 iov2 = iov.iovec(); |
| 140 ASSERT_TRUE(iov2 != NULL); | 140 ASSERT_TRUE(iov2 != nullptr); |
| 141 ASSERT_TRUE(iov2[0].iov_base == test_data[i] + tmp); | 141 ASSERT_TRUE(iov2[0].iov_base == test_data[i] + tmp); |
| 142 ASSERT_EQ(iov2[0].iov_len, str_len - tmp); | 142 ASSERT_EQ(iov2[0].iov_len, str_len - tmp); |
| 143 | 143 |
| 144 // Consume the rest of the first block | 144 // Consume the rest of the first block |
| 145 consumed = iov.Consume(str_len - tmp); | 145 consumed = iov.Consume(str_len - tmp); |
| 146 ASSERT_EQ(str_len - tmp, consumed); | 146 ASSERT_EQ(str_len - tmp, consumed); |
| 147 ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data) - i - 1, | 147 ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data) - i - 1, |
| 148 static_cast<size_t>(iov.Size())); | 148 static_cast<size_t>(iov.Size())); |
| 149 iov2 = iov.iovec(); | 149 iov2 = iov.iovec(); |
| 150 if (iov.Size() > 0) { | 150 if (iov.Size() > 0) { |
| 151 ASSERT_TRUE(iov2 != NULL); | 151 ASSERT_TRUE(iov2 != nullptr); |
| 152 ASSERT_TRUE(iov.LastBlockEnd() == endp); | 152 ASSERT_TRUE(iov.LastBlockEnd() == endp); |
| 153 } else { | 153 } else { |
| 154 ASSERT_TRUE(iov2 == NULL); | 154 ASSERT_TRUE(iov2 == nullptr); |
| 155 ASSERT_TRUE(iov.LastBlockEnd() == NULL); | 155 ASSERT_TRUE(iov.LastBlockEnd() == nullptr); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 | 159 |
| 160 TEST(IOVectorTest, ConsumeTwoAndHalfBlocks) { | 160 TEST(IOVectorTest, ConsumeTwoAndHalfBlocks) { |
| 161 IOVector iov; | 161 IOVector iov; |
| 162 int length = 0; | 162 int length = 0; |
| 163 | 163 |
| 164 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 164 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 165 const int str_len = strlen(test_data[i]); | 165 const int str_len = strlen(test_data[i]); |
| 166 iov.Append(const_cast<char*>(test_data[i]), str_len); | 166 iov.Append(const_cast<char*>(test_data[i]), str_len); |
| 167 length += str_len; | 167 length += str_len; |
| 168 } | 168 } |
| 169 const size_t last_len = strlen(test_data[ARRAYSIZE_UNSAFE(test_data) - 1]); | 169 const size_t last_len = strlen(test_data[ARRAYSIZE_UNSAFE(test_data) - 1]); |
| 170 const size_t half_len = last_len / 2; | 170 const size_t half_len = last_len / 2; |
| 171 | 171 |
| 172 const char* endp = iov.LastBlockEnd(); | 172 const char* endp = iov.LastBlockEnd(); |
| 173 size_t consumed = iov.Consume(length - half_len); | 173 size_t consumed = iov.Consume(length - half_len); |
| 174 ASSERT_EQ(length - half_len, consumed); | 174 ASSERT_EQ(length - half_len, consumed); |
| 175 const struct iovec* iov2 = iov.iovec(); | 175 const struct iovec* iov2 = iov.iovec(); |
| 176 ASSERT_TRUE(iov2 != NULL); | 176 ASSERT_TRUE(iov2 != nullptr); |
| 177 ASSERT_EQ(1u, iov.Size()); | 177 ASSERT_EQ(1u, iov.Size()); |
| 178 ASSERT_TRUE(iov2[0].iov_base == | 178 ASSERT_TRUE(iov2[0].iov_base == |
| 179 test_data[ARRAYSIZE_UNSAFE(test_data) - 1] + last_len - half_len); | 179 test_data[ARRAYSIZE_UNSAFE(test_data) - 1] + last_len - half_len); |
| 180 ASSERT_EQ(half_len, iov2[0].iov_len); | 180 ASSERT_EQ(half_len, iov2[0].iov_len); |
| 181 ASSERT_TRUE(iov.LastBlockEnd() == endp); | 181 ASSERT_TRUE(iov.LastBlockEnd() == endp); |
| 182 | 182 |
| 183 consumed = iov.Consume(half_len); | 183 consumed = iov.Consume(half_len); |
| 184 ASSERT_EQ(half_len, consumed); | 184 ASSERT_EQ(half_len, consumed); |
| 185 iov2 = iov.iovec(); | 185 iov2 = iov.iovec(); |
| 186 ASSERT_EQ(0u, iov.Size()); | 186 ASSERT_EQ(0u, iov.Size()); |
| 187 ASSERT_TRUE(iov2 == NULL); | 187 ASSERT_TRUE(iov2 == nullptr); |
| 188 ASSERT_TRUE(iov.LastBlockEnd() == NULL); | 188 ASSERT_TRUE(iov.LastBlockEnd() == nullptr); |
| 189 } | 189 } |
| 190 | 190 |
| 191 TEST(IOVectorTest, ConsumeTooMuch) { | 191 TEST(IOVectorTest, ConsumeTooMuch) { |
| 192 IOVector iov; | 192 IOVector iov; |
| 193 int length = 0; | 193 int length = 0; |
| 194 | 194 |
| 195 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 195 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 196 const int str_len = strlen(test_data[i]); | 196 const int str_len = strlen(test_data[i]); |
| 197 iov.Append(const_cast<char*>(test_data[i]), str_len); | 197 iov.Append(const_cast<char*>(test_data[i]), str_len); |
| 198 length += str_len; | 198 length += str_len; |
| 199 } | 199 } |
| 200 | 200 |
| 201 int consumed = 0; | 201 int consumed = 0; |
| 202 consumed = iov.Consume(length); | 202 consumed = iov.Consume(length); |
| 203 // TODO(rtenneti): enable when chromium supports EXPECT_DFATAL. | 203 // TODO(rtenneti): enable when chromium supports EXPECT_DFATAL. |
| 204 /* | 204 /* |
| 205 EXPECT_DFATAL( | 205 EXPECT_DFATAL( |
| 206 {consumed = iov.Consume(length + 1);}, | 206 {consumed = iov.Consume(length + 1);}, |
| 207 "Attempting to consume 1 non-existent bytes."); | 207 "Attempting to consume 1 non-existent bytes."); |
| 208 */ | 208 */ |
| 209 ASSERT_EQ(length, consumed); | 209 ASSERT_EQ(length, consumed); |
| 210 const struct iovec* iov2 = iov.iovec(); | 210 const struct iovec* iov2 = iov.iovec(); |
| 211 ASSERT_EQ(0u, iov.Size()); | 211 ASSERT_EQ(0u, iov.Size()); |
| 212 ASSERT_TRUE(iov2 == NULL); | 212 ASSERT_TRUE(iov2 == nullptr); |
| 213 ASSERT_TRUE(iov.LastBlockEnd() == NULL); | 213 ASSERT_TRUE(iov.LastBlockEnd() == nullptr); |
| 214 } | 214 } |
| 215 | 215 |
| 216 TEST(IOVectorTest, Clear) { | 216 TEST(IOVectorTest, Clear) { |
| 217 IOVector iov; | 217 IOVector iov; |
| 218 int length = 0; | 218 int length = 0; |
| 219 | 219 |
| 220 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 220 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 221 const int str_len = strlen(test_data[i]); | 221 const int str_len = strlen(test_data[i]); |
| 222 iov.Append(const_cast<char*>(test_data[i]), str_len); | 222 iov.Append(const_cast<char*>(test_data[i]), str_len); |
| 223 length += str_len; | 223 length += str_len; |
| 224 } | 224 } |
| 225 const struct iovec* iov2 = iov.iovec(); | 225 const struct iovec* iov2 = iov.iovec(); |
| 226 ASSERT_TRUE(iov2 != NULL); | 226 ASSERT_TRUE(iov2 != nullptr); |
| 227 ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data), static_cast<size_t>(iov.Size())); | 227 ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data), static_cast<size_t>(iov.Size())); |
| 228 | 228 |
| 229 iov.Clear(); | 229 iov.Clear(); |
| 230 iov2 = iov.iovec(); | 230 iov2 = iov.iovec(); |
| 231 ASSERT_EQ(0u, iov.Size()); | 231 ASSERT_EQ(0u, iov.Size()); |
| 232 ASSERT_TRUE(iov2 == NULL); | 232 ASSERT_TRUE(iov2 == nullptr); |
| 233 } | 233 } |
| 234 | 234 |
| 235 TEST(IOVectorTest, Capacity) { | 235 TEST(IOVectorTest, Capacity) { |
| 236 IOVector iov; | 236 IOVector iov; |
| 237 // Note: IOVector merges adjacent Appends() into a single iov. | 237 // Note: IOVector merges adjacent Appends() into a single iov. |
| 238 // Therefore, if we expect final size of iov to be 3, we must insure | 238 // Therefore, if we expect final size of iov to be 3, we must insure |
| 239 // that the items we are appending are not adjacent. To achieve that | 239 // that the items we are appending are not adjacent. To achieve that |
| 240 // we use use an array (a[1] provides a buffer between a[0] and b[0], | 240 // we use use an array (a[1] provides a buffer between a[0] and b[0], |
| 241 // and makes them non-adjacent). | 241 // and makes them non-adjacent). |
| 242 char a[2], b[2], c[2]; | 242 char a[2], b[2], c[2]; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 ASSERT_EQ(2u, iov2.Size()); | 274 ASSERT_EQ(2u, iov2.Size()); |
| 275 EXPECT_EQ(&a[0], iov2.iovec()[0].iov_base); | 275 EXPECT_EQ(&a[0], iov2.iovec()[0].iov_base); |
| 276 EXPECT_EQ(1u, iov2.iovec()[0].iov_len); | 276 EXPECT_EQ(1u, iov2.iovec()[0].iov_len); |
| 277 EXPECT_EQ(&b[0], iov2.iovec()[1].iov_base); | 277 EXPECT_EQ(&b[0], iov2.iovec()[1].iov_base); |
| 278 EXPECT_EQ(1u, iov2.iovec()[1].iov_len); | 278 EXPECT_EQ(1u, iov2.iovec()[1].iov_len); |
| 279 } | 279 } |
| 280 | 280 |
| 281 } // namespace | 281 } // namespace |
| 282 } // namespace test | 282 } // namespace test |
| 283 } // namespace net | 283 } // namespace net |
| OLD | NEW |