Bug 1272371 - Add gtest to cover nsMultiplexInputStream set seek. r=froydnj
authorBryce Van Dyk <bvandyk@mozilla.com>
Tue, 30 May 2017 11:27:18 +1200
changeset 410052 c612f1bb40a4dc755d406322b02211e175f46abb
parent 410051 eb9b5fe6b4c7c59507847f3cf9f62d0e577d6a6c
child 410053 f30bf8724ee98a6e4ccb88edbd0d3af746838233
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1272371
milestone55.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 1272371 - Add gtest to cover nsMultiplexInputStream set seek. r=froydnj MozReview-Commit-ID: b3kTP3rerq
xpcom/tests/gtest/TestMultiplexInputStream.cpp
xpcom/tests/gtest/moz.build
new file mode 100644
--- /dev/null
+++ b/xpcom/tests/gtest/TestMultiplexInputStream.cpp
@@ -0,0 +1,101 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+#include "nsComponentManagerUtils.h"
+#include "nsCOMPtr.h"
+#include "nsIInputStream.h"
+#include "nsIMultiplexInputStream.h"
+#include "nsISeekableStream.h"
+#include "nsStringStream.h"
+
+TEST(MultiplexInputStream, Seek_SET)
+{
+  nsCString buf1;
+  nsCString buf2;
+  nsCString buf3;
+  buf1.Assign("Hello world");
+  buf2.Assign("The quick brown fox jumped over the lazy dog");
+  buf3.Assign("Foo bar");
+
+  nsCOMPtr<nsIInputStream> inputStream1;
+  nsCOMPtr<nsIInputStream> inputStream2;
+  nsCOMPtr<nsIInputStream> inputStream3;
+  nsresult rv = NS_NewCStringInputStream(getter_AddRefs(inputStream1), buf1);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = NS_NewCStringInputStream(getter_AddRefs(inputStream2), buf2);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = NS_NewCStringInputStream(getter_AddRefs(inputStream3), buf3);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+
+  nsCOMPtr<nsIMultiplexInputStream> multiplexStream =
+    do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1");
+  ASSERT_TRUE(multiplexStream);
+
+  rv = multiplexStream->AppendStream(inputStream1);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = multiplexStream->AppendStream(inputStream2);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = multiplexStream->AppendStream(inputStream3);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+
+  uint64_t length;
+  uint32_t count;
+  char readBuf[4096];
+  nsCOMPtr<nsISeekableStream> seekStream = do_QueryInterface(multiplexStream);
+  ASSERT_TRUE(seekStream);
+
+  // Seek forward in first input stream
+  rv = seekStream->Seek(nsISeekableStream::NS_SEEK_SET, 1);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = multiplexStream->Available(&length);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)buf1.Length() + buf2.Length() + buf3.Length() - 1,
+            length);
+
+  // Check read is correct
+  rv = multiplexStream->Read(readBuf, 3, &count);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)3, count);
+  rv = multiplexStream->Available(&length);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)buf1.Length() + buf2.Length() + buf3.Length() - 4,
+            length);
+  ASSERT_EQ(0, strncmp(readBuf, "ell", count));
+
+  // Seek to start of third input stream
+  rv = seekStream->Seek(nsISeekableStream::NS_SEEK_SET,
+                        buf1.Length() + buf2.Length());
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = multiplexStream->Available(&length);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)buf3.Length(), length);
+
+  // Check read is correct
+  rv = multiplexStream->Read(readBuf, 5, &count);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)5, count);
+  rv = multiplexStream->Available(&length);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)buf3.Length() - 5, length);
+  ASSERT_EQ(0, strncmp(readBuf, "Foo b", count));
+
+  // Seek back to start of second stream (covers bug 1272371)
+  rv = seekStream->Seek(nsISeekableStream::NS_SEEK_SET, buf1.Length());
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  rv = multiplexStream->Available(&length);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)buf2.Length() + buf3.Length(), length);
+
+  // Check read is correct
+  rv = multiplexStream->Read(readBuf, 6, &count);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)6, count);
+  rv = multiplexStream->Available(&length);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_EQ((uint64_t)buf2.Length() - 6 + buf3.Length(), length);
+  ASSERT_EQ(0, strncmp(readBuf, "The qu", count));
+}
\ No newline at end of file
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -16,16 +16,17 @@ UNIFIED_SOURCES += [
     'TestCOMPtrEq.cpp',
     'TestCRT.cpp',
     'TestEncoding.cpp',
     'TestEscapeURL.cpp',
     'TestExpirationTracker.cpp',
     'TestFile.cpp',
     'TestGCPostBarriers.cpp',
     'TestID.cpp',
+    'TestMultiplexInputStream.cpp',
     'TestNsDeque.cpp',
     'TestNSPRLogModulesParser.cpp',
     'TestObserverArray.cpp',
     'TestObserverService.cpp',
     'TestPipes.cpp',
     'TestPLDHash.cpp',
     'TestPriorityQueue.cpp',
     'TestRacingServiceManager.cpp',