diff options
author | Andrea Marchesini <amarchesini@mozilla.com> | 2020-07-29 10:52:30 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-07-29 14:28:45 +0000 |
commit | 3323a8eda3d4cbb29909f6761cee96cc5e164cf2 (patch) | |
tree | e2b69428455e26be4e48d75c99e407825b2a4f99 /netwerk | |
parent | 71293f0fe4db15d4e0309488337ab4669e9c55b3 (diff) | |
download | UXP-3323a8eda3d4cbb29909f6761cee96cc5e164cf2.tar UXP-3323a8eda3d4cbb29909f6761cee96cc5e164cf2.tar.gz UXP-3323a8eda3d4cbb29909f6761cee96cc5e164cf2.tar.lz UXP-3323a8eda3d4cbb29909f6761cee96cc5e164cf2.tar.xz UXP-3323a8eda3d4cbb29909f6761cee96cc5e164cf2.zip |
[xpcom] Make Base64 compatible with ReadSegments() with small buffers.
Diffstat (limited to 'netwerk')
-rw-r--r-- | netwerk/test/gtest/TestBase64Stream.cpp | 95 | ||||
-rw-r--r-- | netwerk/test/gtest/moz.build | 1 |
2 files changed, 96 insertions, 0 deletions
diff --git a/netwerk/test/gtest/TestBase64Stream.cpp b/netwerk/test/gtest/TestBase64Stream.cpp new file mode 100644 index 000000000..37f5cb824 --- /dev/null +++ b/netwerk/test/gtest/TestBase64Stream.cpp @@ -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 diff --git a/netwerk/test/gtest/moz.build b/netwerk/test/gtest/moz.build index 6e6c80152..e463feb65 100644 --- a/netwerk/test/gtest/moz.build +++ b/netwerk/test/gtest/moz.build @@ -5,6 +5,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. UNIFIED_SOURCES += [ + 'TestBase64Stream.cpp', 'TestProtocolProxyService.cpp', 'TestStandardURL.cpp', ] |