Coverage Report

Created: 2018-09-25 14:53

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