mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-13 19:41:49 +00:00
Bug 1650811 - Make Base64 compatible with ReadSegments() with small buffers, r=asuth,necko-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D82522
This commit is contained in:
parent
ab1d1d9708
commit
45f7415671
95
netwerk/test/gtest/TestBase64Stream.cpp
Normal file
95
netwerk/test/gtest/TestBase64Stream.cpp
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||||
|
/* 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 "mozilla/Base64.h"
|
||||||
|
#include "nsIInputStream.h"
|
||||||
|
|
||||||
|
namespace mozilla {
|
||||||
|
namespace net {
|
||||||
|
|
||||||
|
// An input stream whose ReadSegments method calls aWriter with writes of size
|
||||||
|
// aStep from the provided aInput in order to test edge-cases related to small
|
||||||
|
// buffers.
|
||||||
|
class TestStream final : public nsIInputStream {
|
||||||
|
public:
|
||||||
|
NS_DECL_ISUPPORTS;
|
||||||
|
|
||||||
|
TestStream(const nsACString& aInput, uint32_t aStep)
|
||||||
|
: mInput(aInput), mStep(aStep) {}
|
||||||
|
|
||||||
|
NS_IMETHOD Close() override { MOZ_CRASH("This should not be called"); }
|
||||||
|
|
||||||
|
NS_IMETHOD Available(uint64_t* aLength) override {
|
||||||
|
*aLength = mInput.Length() - mPos;
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
NS_IMETHOD Read(char* aBuffer, uint32_t aCount,
|
||||||
|
uint32_t* aReadCount) override {
|
||||||
|
MOZ_CRASH("This should not be called");
|
||||||
|
}
|
||||||
|
|
||||||
|
NS_IMETHOD ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
||||||
|
uint32_t aCount, uint32_t* aResult) override {
|
||||||
|
*aResult = 0;
|
||||||
|
|
||||||
|
if (mPos == mInput.Length()) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (aCount > 0) {
|
||||||
|
uint32_t amt = std::min(mStep, (uint32_t)(mInput.Length() - mPos));
|
||||||
|
|
||||||
|
uint32_t read = 0;
|
||||||
|
nsresult rv =
|
||||||
|
aWriter(this, aClosure, mInput.get() + mPos, *aResult, amt, &read);
|
||||||
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
*aResult += read;
|
||||||
|
aCount -= read;
|
||||||
|
mPos += read;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
NS_IMETHOD IsNonBlocking(bool* aNonBlocking) override {
|
||||||
|
*aNonBlocking = true;
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
~TestStream() = default;
|
||||||
|
|
||||||
|
nsCString mInput;
|
||||||
|
const uint32_t mStep;
|
||||||
|
uint32_t mPos = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
NS_IMPL_ISUPPORTS(TestStream, nsIInputStream)
|
||||||
|
|
||||||
|
// Test the base64 encoder with writer buffer sizes between 1 byte and the
|
||||||
|
// entire length of "Hello World!" in order to exercise various edge cases.
|
||||||
|
TEST(TestBase64Stream, Run)
|
||||||
|
{
|
||||||
|
nsCString input;
|
||||||
|
input.AssignLiteral("Hello World!");
|
||||||
|
|
||||||
|
for (uint32_t step = 1; step <= input.Length(); ++step) {
|
||||||
|
RefPtr<TestStream> ts = new TestStream(input, step);
|
||||||
|
|
||||||
|
nsAutoString encodedData;
|
||||||
|
nsresult rv = Base64EncodeInputStream(ts, encodedData, input.Length());
|
||||||
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
||||||
|
|
||||||
|
EXPECT_TRUE(encodedData.EqualsLiteral("SGVsbG8gV29ybGQh"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace net
|
||||||
|
} // namespace mozilla
|
@ -5,6 +5,7 @@
|
|||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
UNIFIED_SOURCES += [
|
UNIFIED_SOURCES += [
|
||||||
|
'TestBase64Stream.cpp',
|
||||||
'TestBind.cpp',
|
'TestBind.cpp',
|
||||||
'TestBufferedInputStream.cpp',
|
'TestBufferedInputStream.cpp',
|
||||||
'TestCommon.cpp',
|
'TestCommon.cpp',
|
||||||
|
@ -102,35 +102,56 @@ template <typename T>
|
|||||||
nsresult EncodeInputStream_Encoder(nsIInputStream* aStream, void* aClosure,
|
nsresult EncodeInputStream_Encoder(nsIInputStream* aStream, void* aClosure,
|
||||||
const char* aFromSegment, uint32_t aToOffset,
|
const char* aFromSegment, uint32_t aToOffset,
|
||||||
uint32_t aCount, uint32_t* aWriteCount) {
|
uint32_t aCount, uint32_t* aWriteCount) {
|
||||||
NS_ASSERTION(aCount > 0, "Er, what?");
|
MOZ_ASSERT(aCount > 0, "Er, what?");
|
||||||
|
|
||||||
EncodeInputStream_State<T>* state =
|
EncodeInputStream_State<T>* state =
|
||||||
static_cast<EncodeInputStream_State<T>*>(aClosure);
|
static_cast<EncodeInputStream_State<T>*>(aClosure);
|
||||||
|
|
||||||
|
// We consume the whole data always.
|
||||||
|
*aWriteCount = aCount;
|
||||||
|
|
||||||
// If we have any data left from last time, encode it now.
|
// If we have any data left from last time, encode it now.
|
||||||
uint32_t countRemaining = aCount;
|
uint32_t countRemaining = aCount;
|
||||||
const unsigned char* src = (const unsigned char*)aFromSegment;
|
const unsigned char* src = (const unsigned char*)aFromSegment;
|
||||||
if (state->charsOnStack) {
|
if (state->charsOnStack) {
|
||||||
|
MOZ_ASSERT(state->charsOnStack == 1 || state->charsOnStack == 2);
|
||||||
|
|
||||||
|
// Not enough data to compose a triple.
|
||||||
|
if (state->charsOnStack == 1 && countRemaining == 1) {
|
||||||
|
state->charsOnStack = 2;
|
||||||
|
state->c[1] = src[0];
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t consumed = 0;
|
||||||
unsigned char firstSet[4];
|
unsigned char firstSet[4];
|
||||||
if (state->charsOnStack == 1) {
|
if (state->charsOnStack == 1) {
|
||||||
firstSet[0] = state->c[0];
|
firstSet[0] = state->c[0];
|
||||||
firstSet[1] = src[0];
|
firstSet[1] = src[0];
|
||||||
firstSet[2] = (countRemaining > 1) ? src[1] : '\0';
|
firstSet[2] = src[1];
|
||||||
firstSet[3] = '\0';
|
firstSet[3] = '\0';
|
||||||
|
consumed = 2;
|
||||||
} else /* state->charsOnStack == 2 */ {
|
} else /* state->charsOnStack == 2 */ {
|
||||||
firstSet[0] = state->c[0];
|
firstSet[0] = state->c[0];
|
||||||
firstSet[1] = state->c[1];
|
firstSet[1] = state->c[1];
|
||||||
firstSet[2] = src[0];
|
firstSet[2] = src[0];
|
||||||
firstSet[3] = '\0';
|
firstSet[3] = '\0';
|
||||||
|
consumed = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Encode(firstSet, 3, state->buffer);
|
Encode(firstSet, 3, state->buffer);
|
||||||
state->buffer += 4;
|
state->buffer += 4;
|
||||||
countRemaining -= (3 - state->charsOnStack);
|
countRemaining -= consumed;
|
||||||
src += (3 - state->charsOnStack);
|
src += consumed;
|
||||||
state->charsOnStack = 0;
|
state->charsOnStack = 0;
|
||||||
|
|
||||||
|
// Nothing is left.
|
||||||
|
if (!countRemaining) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode the bulk of the
|
// Encode as many full triplets as possible.
|
||||||
uint32_t encodeLength = countRemaining - countRemaining % 3;
|
uint32_t encodeLength = countRemaining - countRemaining % 3;
|
||||||
MOZ_ASSERT(encodeLength % 3 == 0, "Should have an exact number of triplets!");
|
MOZ_ASSERT(encodeLength % 3 == 0, "Should have an exact number of triplets!");
|
||||||
Encode(src, encodeLength, state->buffer);
|
Encode(src, encodeLength, state->buffer);
|
||||||
@ -138,9 +159,6 @@ nsresult EncodeInputStream_Encoder(nsIInputStream* aStream, void* aClosure,
|
|||||||
src += encodeLength;
|
src += encodeLength;
|
||||||
countRemaining -= encodeLength;
|
countRemaining -= encodeLength;
|
||||||
|
|
||||||
// We must consume all data, so if there's some data left stash it
|
|
||||||
*aWriteCount = aCount;
|
|
||||||
|
|
||||||
if (countRemaining) {
|
if (countRemaining) {
|
||||||
// We should never have a full triplet left at this point.
|
// We should never have a full triplet left at this point.
|
||||||
MOZ_ASSERT(countRemaining < 3, "We should have encoded more!");
|
MOZ_ASSERT(countRemaining < 3, "We should have encoded more!");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user