summaryrefslogtreecommitdiffstats
path: root/netwerk/test/gtest/TestReadStreamToString.cpp
blob: f5fa0a4979450c8b3f72de2d591e6b9e92a4b308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "gtest/gtest.h"

#include "Helpers.h"
#include "nsCOMPtr.h"
#include "nsNetUtil.h"
#include "nsStringStream.h"

// Here we test the reading a pre-allocated size
TEST(TestReadStreamToString, SyncStreamPreAllocatedSize)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream;
  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));

  uint64_t written;
  nsAutoCString result;
  result.SetLength(5);

  void* ptr = result.BeginWriting();

  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
  ASSERT_EQ((uint64_t)5, written);
  ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));

  // The pointer should be equal: no relocation.
  ASSERT_EQ(ptr, result.BeginWriting());
}

// Here we test the reading the full size of a sync stream
TEST(TestReadStreamToString, SyncStreamFullSize)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream;
  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));

  uint64_t written;
  nsAutoCString result;

  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, buffer.Length(),
                                              &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}

// Here we test the reading less than the full size of a sync stream
TEST(TestReadStreamToString, SyncStreamLessThan)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream;
  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));

  uint64_t written;
  nsAutoCString result;

  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
  ASSERT_EQ((uint64_t)5, written);
  ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
}

// Here we test the reading more than the full size of a sync stream
TEST(TestReadStreamToString, SyncStreamMoreThan)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream;
  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));

  uint64_t written;
  nsAutoCString result;

  // Reading more than the buffer size.
  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result,
                                              buffer.Length() + 5, &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}

// Here we test the reading a sync stream without passing the size
TEST(TestReadStreamToString, SyncStreamUnknownSize)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream;
  ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));

  uint64_t written;
  nsAutoCString result;

  // Reading all without passing the size
  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}

// Here we test the reading the full size of an async stream
TEST(TestReadStreamToString, AsyncStreamFullSize)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);

  uint64_t written;
  nsAutoCString result;

  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, buffer.Length(),
                                              &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}

// Here we test the reading less than the full size of an async stream
TEST(TestReadStreamToString, AsyncStreamLessThan)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);

  uint64_t written;
  nsAutoCString result;

  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
  ASSERT_EQ((uint64_t)5, written);
  ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
}

// Here we test the reading more than the full size of an async stream
TEST(TestReadStreamToString, AsyncStreamMoreThan)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);

  uint64_t written;
  nsAutoCString result;

  // Reading more than the buffer size.
  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result,
                                              buffer.Length() + 5, &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}

// Here we test the reading an async stream without passing the size
TEST(TestReadStreamToString, AsyncStreamUnknownSize)
{
  nsCString buffer;
  buffer.AssignLiteral("Hello world!");

  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);

  uint64_t written;
  nsAutoCString result;

  // Reading all without passing the size
  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}

// Here we test the reading an async big stream without passing the size
TEST(TestReadStreamToString, AsyncStreamUnknownBigSize)
{
  nsCString buffer;

  buffer.SetLength(4096 * 2);
  for (uint32_t i = 0; i < 4096 * 2; ++i) {
    buffer.BeginWriting()[i] = i % 10;
  }

  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(buffer);

  uint64_t written;
  nsAutoCString result;

  // Reading all without passing the size
  ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, -1, &written));
  ASSERT_EQ(buffer.Length(), written);
  ASSERT_TRUE(buffer.Equals(result));
}