Bug 1415081 - part 1 - gTest for NS_ReadInputStreamToString, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 09 Nov 2017 11:18:08 +0100
changeset 444203 7cf6867bed423f52c684994c77cd1db376de3bf1
parent 444202 057c4132d24848a144135516b2a5f11ee7cc29c3
child 444204 90939e355e8d9a508ab2dae5f5e98c7ab7dd67fd
push id1618
push userCallek@gmail.com
push dateThu, 11 Jan 2018 17:45:48 +0000
treeherdermozilla-release@882ca853e05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1415081
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1415081 - part 1 - gTest for NS_ReadInputStreamToString, r=smaug
netwerk/test/gtest/TestReadStreamToString.cpp
netwerk/test/gtest/moz.build
new file mode 100644
--- /dev/null
+++ b/netwerk/test/gtest/TestReadStreamToString.cpp
@@ -0,0 +1,156 @@
+#include "gtest/gtest.h"
+
+#include "nsCOMPtr.h"
+#include "nsNetUtil.h"
+
+// 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));
+}
--- a/netwerk/test/gtest/moz.build
+++ b/netwerk/test/gtest/moz.build
@@ -6,16 +6,17 @@
 
 UNIFIED_SOURCES += [
     'TestBufferedInputStream.cpp',
     'TestHeaders.cpp',
     'TestHttpAuthUtils.cpp',
     'TestMozURL.cpp',
     'TestPartiallySeekableInputStream.cpp',
     'TestProtocolProxyService.cpp',
+    'TestReadStreamToString.cpp',
     'TestStandardURL.cpp',
 ]
 
 TEST_DIRS += [
     'parse-ftp',
 ]
 
 LOCAL_INCLUDES += [