tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestReadStreamToString.cpp (5633B)


      1 #include "gtest/gtest.h"
      2 
      3 #include "Helpers.h"
      4 #include "nsCOMPtr.h"
      5 #include "nsNetUtil.h"
      6 #include "nsStringStream.h"
      7 
      8 // Here we test the reading a pre-allocated size
      9 TEST(TestReadStreamToString, SyncStreamPreAllocatedSize)
     10 {
     11  nsCString buffer;
     12  buffer.AssignLiteral("Hello world!");
     13 
     14  nsCOMPtr<nsIInputStream> stream;
     15  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
     16 
     17  uint64_t written;
     18  nsAutoCString result;
     19  result.SetLength(5);
     20 
     21  void* ptr = result.BeginWriting();
     22 
     23  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
     24  ASSERT_EQ((uint64_t)5, written);
     25  ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
     26 
     27  // The pointer should be equal: no relocation.
     28  ASSERT_EQ(ptr, result.BeginWriting());
     29 }
     30 
     31 // Here we test the reading the full size of a sync stream
     32 TEST(TestReadStreamToString, SyncStreamFullSize)
     33 {
     34  nsCString buffer;
     35  buffer.AssignLiteral("Hello world!");
     36 
     37  nsCOMPtr<nsIInputStream> stream;
     38  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
     39 
     40  uint64_t written;
     41  nsAutoCString result;
     42 
     43  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, buffer.Length(),
     44                                              &written));
     45  ASSERT_EQ(buffer.Length(), written);
     46  ASSERT_TRUE(buffer.Equals(result));
     47 }
     48 
     49 // Here we test the reading less than the full size of a sync stream
     50 TEST(TestReadStreamToString, SyncStreamLessThan)
     51 {
     52  nsCString buffer;
     53  buffer.AssignLiteral("Hello world!");
     54 
     55  nsCOMPtr<nsIInputStream> stream;
     56  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
     57 
     58  uint64_t written;
     59  nsAutoCString result;
     60 
     61  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
     62  ASSERT_EQ((uint64_t)5, written);
     63  ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
     64 }
     65 
     66 // Here we test the reading more than the full size of a sync stream
     67 TEST(TestReadStreamToString, SyncStreamMoreThan)
     68 {
     69  nsCString buffer;
     70  buffer.AssignLiteral("Hello world!");
     71 
     72  nsCOMPtr<nsIInputStream> stream;
     73  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
     74 
     75  uint64_t written;
     76  nsAutoCString result;
     77 
     78  // Reading more than the buffer size.
     79  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result,
     80                                              buffer.Length() + 5, &written));
     81  ASSERT_EQ(buffer.Length(), written);
     82  ASSERT_TRUE(buffer.Equals(result));
     83 }
     84 
     85 // Here we test the reading a sync stream without passing the size
     86 TEST(TestReadStreamToString, SyncStreamUnknownSize)
     87 {
     88  nsCString buffer;
     89  buffer.AssignLiteral("Hello world!");
     90 
     91  nsCOMPtr<nsIInputStream> stream;
     92  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
     93 
     94  uint64_t written;
     95  nsAutoCString result;
     96 
     97  // Reading all without passing the size
     98  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
     99  ASSERT_EQ(buffer.Length(), written);
    100  ASSERT_TRUE(buffer.Equals(result));
    101 }
    102 
    103 // Here we test the reading the full size of an async stream
    104 TEST(TestReadStreamToString, AsyncStreamFullSize)
    105 {
    106  nsCString buffer;
    107  buffer.AssignLiteral("Hello world!");
    108 
    109  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
    110 
    111  uint64_t written;
    112  nsAutoCString result;
    113 
    114  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, buffer.Length(),
    115                                              &written));
    116  ASSERT_EQ(buffer.Length(), written);
    117  ASSERT_TRUE(buffer.Equals(result));
    118 }
    119 
    120 // Here we test the reading less than the full size of an async stream
    121 TEST(TestReadStreamToString, AsyncStreamLessThan)
    122 {
    123  nsCString buffer;
    124  buffer.AssignLiteral("Hello world!");
    125 
    126  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
    127 
    128  uint64_t written;
    129  nsAutoCString result;
    130 
    131  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
    132  ASSERT_EQ((uint64_t)5, written);
    133  ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
    134 }
    135 
    136 // Here we test the reading more than the full size of an async stream
    137 TEST(TestReadStreamToString, AsyncStreamMoreThan)
    138 {
    139  nsCString buffer;
    140  buffer.AssignLiteral("Hello world!");
    141 
    142  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
    143 
    144  uint64_t written;
    145  nsAutoCString result;
    146 
    147  // Reading more than the buffer size.
    148  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result,
    149                                              buffer.Length() + 5, &written));
    150  ASSERT_EQ(buffer.Length(), written);
    151  ASSERT_TRUE(buffer.Equals(result));
    152 }
    153 
    154 // Here we test the reading an async stream without passing the size
    155 TEST(TestReadStreamToString, AsyncStreamUnknownSize)
    156 {
    157  nsCString buffer;
    158  buffer.AssignLiteral("Hello world!");
    159 
    160  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
    161 
    162  uint64_t written;
    163  nsAutoCString result;
    164 
    165  // Reading all without passing the size
    166  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
    167  ASSERT_EQ(buffer.Length(), written);
    168  ASSERT_TRUE(buffer.Equals(result));
    169 }
    170 
    171 // Here we test the reading an async big stream without passing the size
    172 TEST(TestReadStreamToString, AsyncStreamUnknownBigSize)
    173 {
    174  nsCString buffer;
    175 
    176  buffer.SetLength(4096 * 2);
    177  for (uint32_t i = 0; i < 4096 * 2; ++i) {
    178    buffer.BeginWriting()[i] = i % 10;
    179  }
    180 
    181  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);
    182 
    183  uint64_t written;
    184  nsAutoCString result;
    185 
    186  // Reading all without passing the size
    187  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
    188  ASSERT_EQ(buffer.Length(), written);
    189  ASSERT_TRUE(buffer.Equals(result));
    190 }