Home | History | Annotate | Download | only in quic
      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
      3 // found in the LICENSE file.
      4 
      5 #include "net/quic/iovector.h"
      6 
      7 #include <string.h>
      8 
      9 #include "base/logging.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 using std::string;
     13 
     14 namespace net {
     15 namespace test {
     16 namespace {
     17 
     18 const char* const test_data[] = {
     19   "test string 1, a medium size one.",
     20   "test string2",
     21   "test string      3, a looooooooooooong loooooooooooooooong string"
     22 };
     23 
     24 TEST(IOVectorTest, CopyConstructor) {
     25   IOVector iov1;
     26   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
     27     iov1.Append(const_cast<char*>(test_data[i]), strlen(test_data[i]));
     28   }
     29   IOVector iov2 = iov1;
     30   EXPECT_EQ(iov2.Size(), iov1.Size());
     31   for (size_t i = 0; i < iov2.Size(); ++i) {
     32     EXPECT_TRUE(iov2.iovec()[i].iov_base == iov1.iovec()[i].iov_base);
     33     EXPECT_EQ(iov2.iovec()[i].iov_len, iov1.iovec()[i].iov_len);
     34   }
     35   EXPECT_EQ(iov2.TotalBufferSize(), iov1.TotalBufferSize());
     36 }
     37 
     38 TEST(IOVectorTest, AssignmentOperator) {
     39   IOVector iov1;
     40   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
     41     iov1.Append(const_cast<char*>(test_data[i]), strlen(test_data[i]));
     42   }
     43   IOVector iov2;
     44   iov2.Append(const_cast<char*>("ephemeral string"), 16);
     45   // The following assignment results in a shallow copy;
     46   // both IOVectors point to the same underlying data.
     47   iov2 = iov1;
     48   EXPECT_EQ(iov2.Size(), iov1.Size());
     49   for (size_t i = 0; i < iov2.Size(); ++i) {
     50     EXPECT_TRUE(iov2.iovec()[i].iov_base == iov1.iovec()[i].iov_base);
     51     EXPECT_EQ(iov2.iovec()[i].iov_len, iov1.iovec()[i].iov_len);
     52   }
     53   EXPECT_EQ(iov2.TotalBufferSize(), iov1.TotalBufferSize());
     54 }
     55 
     56 TEST(IOVectorTest, Append) {
     57   IOVector iov;
     58   int length = 0;
     59   const struct iovec* iov2 = iov.iovec();
     60 
     61   ASSERT_EQ(0u, iov.Size());
     62   ASSERT_TRUE(iov2 == NULL);
     63   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
     64     const int str_len = strlen(test_data[i]);
     65     const int append_len = str_len / 2;
     66     // This should append a new block
     67     iov.Append(const_cast<char*>(test_data[i]), append_len);
     68     length += append_len;
     69     ASSERT_EQ(i + 1, static_cast<size_t>(iov.Size()));
     70     ASSERT_TRUE(iov.LastBlockEnd() == test_data[i] + append_len);
     71     // This should just lengthen the existing block.
     72     iov.Append(const_cast<char*>(test_data[i] + append_len),
     73                str_len - append_len);
     74     length += (str_len - append_len);
     75     ASSERT_EQ(i + 1, static_cast<size_t>(iov.Size()));
     76     ASSERT_TRUE(iov.LastBlockEnd() == test_data[i] + str_len);
     77   }
     78 
     79   iov2 = iov.iovec();
     80   ASSERT_TRUE(iov2 != NULL);
     81   for (size_t i = 0; i < iov.Size(); ++i) {
     82     ASSERT_TRUE(test_data[i] == iov2[i].iov_base);
     83     ASSERT_EQ(strlen(test_data[i]), iov2[i].iov_len);
     84   }
     85 }
     86 
     87 TEST(IOVectorTest, AppendIovec) {
     88   IOVector iov;
     89   const struct iovec test_iov[] = {
     90     {const_cast<char*>("foo"), 3},
     91     {const_cast<char*>("bar"), 3},
     92     {const_cast<char*>("buzzzz"), 6}
     93   };
     94   iov.AppendIovec(test_iov, ARRAYSIZE_UNSAFE(test_iov));
     95   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_iov); ++i) {
     96     EXPECT_EQ(test_iov[i].iov_base, iov.iovec()[i].iov_base);
     97     EXPECT_EQ(test_iov[i].iov_len, iov.iovec()[i].iov_len);
     98   }
     99 
    100   // Test AppendIovecAtMostBytes.
    101   iov.Clear();
    102   // Stop in the middle of a block.
    103   EXPECT_EQ(5u, iov.AppendIovecAtMostBytes(test_iov, ARRAYSIZE_UNSAFE(test_iov),
    104                                            5));
    105   EXPECT_EQ(5u, iov.TotalBufferSize());
    106   iov.Append(static_cast<char*>(test_iov[1].iov_base) + 2, 1);
    107   // Make sure the boundary case, where max_bytes == size of block also works.
    108   EXPECT_EQ(6u, iov.AppendIovecAtMostBytes(&test_iov[2], 1, 6));
    109   ASSERT_LE(ARRAYSIZE_UNSAFE(test_iov), static_cast<size_t>(iov.Size()));
    110   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_iov); ++i) {
    111     EXPECT_EQ(test_iov[i].iov_base, iov.iovec()[i].iov_base);
    112     EXPECT_EQ(test_iov[i].iov_len, iov.iovec()[i].iov_len);
    113   }
    114 }
    115 
    116 TEST(IOVectorTest, ConsumeHalfBlocks) {
    117   IOVector iov;
    118   int length = 0;
    119 
    120   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
    121     const int str_len = strlen(test_data[i]);
    122     iov.Append(const_cast<char*>(test_data[i]), str_len);
    123     length += str_len;
    124   }
    125   const char* endp = iov.LastBlockEnd();
    126   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
    127     const struct iovec* iov2 = iov.iovec();
    128     const size_t str_len = strlen(test_data[i]);
    129     size_t tmp = str_len / 2;
    130 
    131     ASSERT_TRUE(iov2 != NULL);
    132     ASSERT_TRUE(iov2[0].iov_base == test_data[i]);
    133     ASSERT_EQ(str_len, iov2[0].iov_len);
    134 
    135     // Consume half of the first block.
    136     size_t consumed = iov.Consume(tmp);
    137     ASSERT_EQ(tmp, consumed);
    138     ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data) - i, static_cast<size_t>(iov.Size()));
    139     iov2 = iov.iovec();
    140     ASSERT_TRUE(iov2 != NULL);
    141     ASSERT_TRUE(iov2[0].iov_base == test_data[i] + tmp);
    142     ASSERT_EQ(iov2[0].iov_len, str_len - tmp);
    143 
    144     // Consume the rest of the first block
    145     consumed = iov.Consume(str_len - tmp);
    146     ASSERT_EQ(str_len - tmp, consumed);
    147     ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data) - i - 1,
    148               static_cast<size_t>(iov.Size()));
    149     iov2 = iov.iovec();
    150     if (iov.Size() > 0) {
    151       ASSERT_TRUE(iov2 != NULL);
    152       ASSERT_TRUE(iov.LastBlockEnd() == endp);
    153     } else {
    154       ASSERT_TRUE(iov2 == NULL);
    155       ASSERT_TRUE(iov.LastBlockEnd() == NULL);
    156     }
    157   }
    158 }
    159 
    160 TEST(IOVectorTest, ConsumeTwoAndHalfBlocks) {
    161   IOVector iov;
    162   int length = 0;
    163 
    164   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
    165     const int str_len = strlen(test_data[i]);
    166     iov.Append(const_cast<char*>(test_data[i]), str_len);
    167     length += str_len;
    168   }
    169   const size_t last_len = strlen(test_data[ARRAYSIZE_UNSAFE(test_data) - 1]);
    170   const size_t half_len = last_len / 2;
    171 
    172   const char* endp = iov.LastBlockEnd();
    173   size_t consumed = iov.Consume(length - half_len);
    174   ASSERT_EQ(length - half_len, consumed);
    175   const struct iovec* iov2 = iov.iovec();
    176   ASSERT_TRUE(iov2 != NULL);
    177   ASSERT_EQ(1u, iov.Size());
    178   ASSERT_TRUE(iov2[0].iov_base ==
    179               test_data[ARRAYSIZE_UNSAFE(test_data) - 1] + last_len - half_len);
    180   ASSERT_EQ(half_len, iov2[0].iov_len);
    181   ASSERT_TRUE(iov.LastBlockEnd() == endp);
    182 
    183   consumed = iov.Consume(half_len);
    184   ASSERT_EQ(half_len, consumed);
    185   iov2 = iov.iovec();
    186   ASSERT_EQ(0u, iov.Size());
    187   ASSERT_TRUE(iov2 == NULL);
    188   ASSERT_TRUE(iov.LastBlockEnd() == NULL);
    189 }
    190 
    191 TEST(IOVectorTest, ConsumeTooMuch) {
    192   IOVector iov;
    193   int length = 0;
    194 
    195   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
    196     const int str_len = strlen(test_data[i]);
    197     iov.Append(const_cast<char*>(test_data[i]), str_len);
    198     length += str_len;
    199   }
    200 
    201   int consumed = 0;
    202   consumed = iov.Consume(length);
    203   // TODO(rtenneti): enable when chromium supports EXPECT_DFATAL.
    204   /*
    205   EXPECT_DFATAL(
    206       {consumed = iov.Consume(length + 1);},
    207       "Attempting to consume 1 non-existent bytes.");
    208   */
    209   ASSERT_EQ(length, consumed);
    210   const struct iovec* iov2 = iov.iovec();
    211   ASSERT_EQ(0u, iov.Size());
    212   ASSERT_TRUE(iov2 == NULL);
    213   ASSERT_TRUE(iov.LastBlockEnd() == NULL);
    214 }
    215 
    216 TEST(IOVectorTest, Clear) {
    217   IOVector iov;
    218   int length = 0;
    219 
    220   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
    221     const int str_len = strlen(test_data[i]);
    222     iov.Append(const_cast<char*>(test_data[i]), str_len);
    223     length += str_len;
    224   }
    225   const struct iovec* iov2 = iov.iovec();
    226   ASSERT_TRUE(iov2 != NULL);
    227   ASSERT_EQ(ARRAYSIZE_UNSAFE(test_data), static_cast<size_t>(iov.Size()));
    228 
    229   iov.Clear();
    230   iov2 = iov.iovec();
    231   ASSERT_EQ(0u, iov.Size());
    232   ASSERT_TRUE(iov2 == NULL);
    233 }
    234 
    235 TEST(IOVectorTest, Capacity) {
    236   IOVector 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
    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],
    241   // and makes them non-adjacent).
    242   char a[2], b[2], c[2];
    243   iov.Append(&a[0], 1);
    244   iov.Append(&b[0], 1);
    245   iov.Append(&c[0], 1);
    246   ASSERT_EQ(3u, iov.Size());
    247   size_t capacity = iov.Capacity();
    248   EXPECT_LE(iov.Size(), capacity);
    249   iov.Consume(2);
    250   // The capacity should not have changed.
    251   EXPECT_EQ(capacity, iov.Capacity());
    252 }
    253 
    254 TEST(IOVectorTest, Swap) {
    255   IOVector iov1, iov2;
    256   // See IOVector merge comment above.
    257   char a[2], b[2], c[2], d[2], e[2];
    258   iov1.Append(&a[0], 1);
    259   iov1.Append(&b[0], 1);
    260 
    261   iov2.Append(&c[0], 1);
    262   iov2.Append(&d[0], 1);
    263   iov2.Append(&e[0], 1);
    264   iov1.Swap(&iov2);
    265 
    266   ASSERT_EQ(3u, iov1.Size());
    267   EXPECT_EQ(&c[0], iov1.iovec()[0].iov_base);
    268   EXPECT_EQ(1u, iov1.iovec()[0].iov_len);
    269   EXPECT_EQ(&d[0], iov1.iovec()[1].iov_base);
    270   EXPECT_EQ(1u, iov1.iovec()[1].iov_len);
    271   EXPECT_EQ(&e[0], iov1.iovec()[2].iov_base);
    272   EXPECT_EQ(1u, iov1.iovec()[2].iov_len);
    273 
    274   ASSERT_EQ(2u, iov2.Size());
    275   EXPECT_EQ(&a[0], iov2.iovec()[0].iov_base);
    276   EXPECT_EQ(1u, iov2.iovec()[0].iov_len);
    277   EXPECT_EQ(&b[0], iov2.iovec()[1].iov_base);
    278   EXPECT_EQ(1u, iov2.iovec()[1].iov_len);
    279 }
    280 
    281 }  // namespace
    282 }  // namespace test
    283 }  // namespace net
    284