2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2004-09-30 00:24:53 +04:00
|
|
|
#include <stdlib.h>
|
2015-02-11 07:55:43 +03:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "Helpers.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsICloneableInputStream.h"
|
2004-09-29 20:48:10 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2015-02-11 07:55:43 +03:00
|
|
|
#include "nsIStorageStream.h"
|
2016-11-05 02:21:37 +03:00
|
|
|
#include "nsTArray.h"
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
namespace {
|
2015-09-15 20:15:45 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
void
|
|
|
|
WriteData(nsIOutputStream* aOut, nsTArray<char>& aData, uint32_t aNumBytes,
|
|
|
|
nsACString& aDataWritten)
|
2004-09-29 20:48:10 +04:00
|
|
|
{
|
2015-02-11 07:55:43 +03:00
|
|
|
uint32_t n;
|
|
|
|
nsresult rv = aOut->Write(aData.Elements(), aNumBytes, &n);
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
aDataWritten.Append(aData.Elements(), aNumBytes);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-09-15 20:15:45 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
TEST(StorageStreams, Main)
|
|
|
|
{
|
|
|
|
// generate some test data we will write in 4k chunks to the stream
|
|
|
|
nsTArray<char> kData;
|
|
|
|
testing::CreateData(4096, kData);
|
|
|
|
|
|
|
|
// track how much data was written so we can compare at the end
|
|
|
|
nsAutoCString dataWritten;
|
2004-09-29 20:48:10 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStorageStream> stor;
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
rv = NS_NewStorageStream(kData.Length(), UINT32_MAX, getter_AddRefs(stor));
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2004-09-29 20:48:10 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
rv = stor->GetOutputStream(0, getter_AddRefs(out));
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
2004-09-29 20:48:10 +04:00
|
|
|
|
|
|
|
rv = out->Close();
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2012-07-30 18:20:58 +04:00
|
|
|
out = nullptr;
|
2015-01-21 07:09:58 +03:00
|
|
|
|
2004-09-29 20:48:10 +04:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
rv = stor->NewInputStream(0, getter_AddRefs(in));
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(in);
|
|
|
|
ASSERT_TRUE(cloneable != nullptr);
|
|
|
|
ASSERT_TRUE(cloneable->GetCloneable());
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsCOMPtr<nsIInputStream> clone;
|
|
|
|
rv = cloneable->Clone(getter_AddRefs(clone));
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
testing::ConsumeAndValidateStream(in, dataWritten);
|
|
|
|
testing::ConsumeAndValidateStream(clone, dataWritten);
|
2012-07-30 18:20:58 +04:00
|
|
|
in = nullptr;
|
2015-02-11 07:55:43 +03:00
|
|
|
clone = nullptr;
|
2004-09-29 20:48:10 +04:00
|
|
|
|
|
|
|
// now, write 3 more full 4k segments + 11 bytes, starting at 8192
|
|
|
|
// total written equals 20491 bytes
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
rv = stor->GetOutputStream(dataWritten.Length(), getter_AddRefs(out));
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
|
|
|
WriteData(out, kData, 11, dataWritten);
|
2004-09-29 20:48:10 +04:00
|
|
|
|
|
|
|
rv = out->Close();
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2012-07-30 18:20:58 +04:00
|
|
|
out = nullptr;
|
2004-09-29 20:48:10 +04:00
|
|
|
|
|
|
|
// now, read all
|
|
|
|
rv = stor->NewInputStream(0, getter_AddRefs(in));
|
2015-01-21 07:09:58 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2004-09-29 20:48:10 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
testing::ConsumeAndValidateStream(in, dataWritten);
|
2012-07-30 18:20:58 +04:00
|
|
|
in = nullptr;
|
2004-09-29 20:48:10 +04:00
|
|
|
}
|
2015-09-15 20:15:45 +03:00
|
|
|
|
|
|
|
TEST(StorageStreams, EarlyInputStream)
|
|
|
|
{
|
|
|
|
// generate some test data we will write in 4k chunks to the stream
|
|
|
|
nsTArray<char> kData;
|
|
|
|
testing::CreateData(4096, kData);
|
|
|
|
|
|
|
|
// track how much data was written so we can compare at the end
|
|
|
|
nsAutoCString dataWritten;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStorageStream> stor;
|
|
|
|
|
|
|
|
rv = NS_NewStorageStream(kData.Length(), UINT32_MAX, getter_AddRefs(stor));
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Get input stream before writing data into the output stream
|
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
rv = stor->NewInputStream(0, getter_AddRefs(in));
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Write data to output stream
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
rv = stor->GetOutputStream(0, getter_AddRefs(out));
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
|
|
|
WriteData(out, kData, kData.Length(), dataWritten);
|
|
|
|
|
|
|
|
rv = out->Close();
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
out = nullptr;
|
|
|
|
|
|
|
|
// Should be able to consume input stream
|
|
|
|
testing::ConsumeAndValidateStream(in, dataWritten);
|
|
|
|
in = nullptr;
|
|
|
|
}
|