Bug 1203680 P4 Fix bug in nsStorageStream with reading streams created before data is populated. r=froydnj

This commit is contained in:
Ben Kelly 2015-09-15 10:15:45 -07:00
parent 118647a2cf
commit c2410a8eb0
2 changed files with 48 additions and 1 deletions

View File

@ -460,7 +460,15 @@ nsStorageInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
goto out;
}
mSegmentNum++;
// We have data in the stream, but if mSegmentEnd is zero, then we
// were likely constructed prior to any data being written into
// the stream. Therefore, if mSegmentEnd is non-zero, we should
// move into the next segment; otherwise, we should stay in this
// segment so our input state can be updated and we can properly
// perform the initial read.
if (mSegmentEnd > 0) {
mSegmentNum++;
}
mReadCursor = 0;
mSegmentEnd = XPCOM_MIN(mSegmentSize, available);
availableInSegment = mSegmentEnd;

View File

@ -14,6 +14,7 @@
#include "nsIStorageStream.h"
namespace {
void
WriteData(nsIOutputStream* aOut, nsTArray<char>& aData, uint32_t aNumBytes,
nsACString& aDataWritten)
@ -25,6 +26,7 @@ WriteData(nsIOutputStream* aOut, nsTArray<char>& aData, uint32_t aNumBytes,
}
} // namespace
TEST(StorageStreams, Main)
{
// generate some test data we will write in 4k chunks to the stream
@ -89,3 +91,40 @@ TEST(StorageStreams, Main)
testing::ConsumeAndValidateStream(in, dataWritten);
in = nullptr;
}
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;
}