2011-06-08 18:08:31 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2011-06-08 18:08:31 +00:00
|
|
|
|
|
|
|
#include "TestHarness.h"
|
|
|
|
|
2013-01-10 08:39:40 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-06-08 18:08:31 +00:00
|
|
|
#include "nsIScriptableBase64Encoder.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsStringAPI.h"
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
struct Chunk {
|
2012-08-22 15:56:38 +00:00
|
|
|
Chunk(uint32_t l, const char* c)
|
2011-06-08 18:08:31 +00:00
|
|
|
: mLength(l), mData(c)
|
|
|
|
{}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mLength;
|
2011-06-08 18:08:31 +00:00
|
|
|
const char* mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Test {
|
|
|
|
Test(Chunk* c, const char* r)
|
|
|
|
: mChunks(c), mResult(r)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Chunk* mChunks;
|
|
|
|
const char* mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest1Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(9, "Hello sir"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest2Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(3, "Hel"),
|
|
|
|
Chunk(3, "lo "),
|
|
|
|
Chunk(3, "sir"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest3Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(1, "I"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest4Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(2, "Hi"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest5Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(1, "B"),
|
|
|
|
Chunk(2, "ob"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest6Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(2, "Bo"),
|
|
|
|
Chunk(1, "b"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest7Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(1, "F"), // Carry over 1
|
|
|
|
Chunk(4, "iref"), // Carry over 2
|
|
|
|
Chunk(2, "ox"), // 1
|
|
|
|
Chunk(4, " is "), // 2
|
|
|
|
Chunk(2, "aw"), // 1
|
|
|
|
Chunk(4, "esom"), // 2
|
|
|
|
Chunk(2, "e!"),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Chunk kTest8Chunks[] =
|
|
|
|
{
|
|
|
|
Chunk(5, "ALL T"),
|
|
|
|
Chunk(1, "H"),
|
|
|
|
Chunk(4, "ESE "),
|
|
|
|
Chunk(2, "WO"),
|
|
|
|
Chunk(21, "RLDS ARE YOURS EXCEPT"),
|
|
|
|
Chunk(9, " EUROPA. "),
|
|
|
|
Chunk(25, "ATTEMPT NO LANDING THERE."),
|
2012-07-30 14:20:58 +00:00
|
|
|
Chunk(0, nullptr)
|
2011-06-08 18:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Test kTests[] =
|
|
|
|
{
|
|
|
|
// Test 1, test a simple round string in one chunk
|
|
|
|
Test(
|
|
|
|
kTest1Chunks,
|
|
|
|
"SGVsbG8gc2ly"
|
|
|
|
),
|
|
|
|
// Test 2, test a simple round string split into round chunks
|
|
|
|
Test(
|
|
|
|
kTest2Chunks,
|
|
|
|
"SGVsbG8gc2ly"
|
|
|
|
),
|
|
|
|
// Test 3, test a single chunk that's 2 short
|
|
|
|
Test(
|
|
|
|
kTest3Chunks,
|
|
|
|
"SQ=="
|
|
|
|
),
|
|
|
|
// Test 4, test a single chunk that's 1 short
|
|
|
|
Test(
|
|
|
|
kTest4Chunks,
|
|
|
|
"SGk="
|
|
|
|
),
|
|
|
|
// Test 5, test a single chunk that's 2 short, followed by a chunk of 2
|
|
|
|
Test(
|
|
|
|
kTest5Chunks,
|
|
|
|
"Qm9i"
|
|
|
|
),
|
|
|
|
// Test 6, test a single chunk that's 1 short, followed by a chunk of 1
|
|
|
|
Test(
|
|
|
|
kTest6Chunks,
|
|
|
|
"Qm9i"
|
|
|
|
),
|
|
|
|
// Test 7, test alternating carryovers
|
|
|
|
Test(
|
|
|
|
kTest7Chunks,
|
|
|
|
"RmlyZWZveCBpcyBhd2Vzb21lIQ=="
|
|
|
|
),
|
|
|
|
// Test 8, test a longish string
|
|
|
|
Test(
|
|
|
|
kTest8Chunks,
|
|
|
|
"QUxMIFRIRVNFIFdPUkxEUyBBUkUgWU9VUlMgRVhDRVBUIEVVUk9QQS4gQVRURU1QVCBOTyBMQU5ESU5HIFRIRVJFLg=="
|
|
|
|
),
|
|
|
|
// Terminator
|
|
|
|
Test(
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
|
|
|
nullptr
|
2011-06-08 18:08:31 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2013-01-10 08:39:40 +00:00
|
|
|
class FakeInputStream MOZ_FINAL : public nsIInputStream
|
2011-06-08 18:08:31 +00:00
|
|
|
{
|
2014-07-17 20:48:00 +00:00
|
|
|
~FakeInputStream() {}
|
|
|
|
|
2011-06-08 18:08:31 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
FakeInputStream()
|
|
|
|
: mTestNumber(0),
|
|
|
|
mTest(&kTests[0]),
|
|
|
|
mChunk(&mTest->mChunks[0]),
|
|
|
|
mClosed(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
|
|
|
|
void Reset();
|
|
|
|
bool NextTest();
|
|
|
|
bool CheckTest(nsACString& aResult);
|
|
|
|
bool CheckTest(nsAString& aResult);
|
|
|
|
private:
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mTestNumber;
|
2011-06-08 18:08:31 +00:00
|
|
|
const Test* mTest;
|
|
|
|
const Chunk* mChunk;
|
|
|
|
bool mClosed;
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(FakeInputStream, nsIInputStream)
|
2011-06-08 18:08:31 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FakeInputStream::Close()
|
|
|
|
{
|
|
|
|
mClosed = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
FakeInputStream::Available(uint64_t* aAvailable)
|
2011-06-08 18:08:31 +00:00
|
|
|
{
|
|
|
|
*aAvailable = 0;
|
|
|
|
|
|
|
|
if (mClosed)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
const Chunk* chunk = mChunk;
|
|
|
|
while (chunk->mLength) {
|
|
|
|
*aAvailable += chunk->mLength;
|
|
|
|
chunk++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
FakeInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aOut)
|
2011-06-08 18:08:31 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FakeInputStream::ReadSegments(nsWriteSegmentFun aWriter,
|
|
|
|
void* aClosure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aRead)
|
2011-06-08 18:08:31 +00:00
|
|
|
{
|
|
|
|
*aRead = 0;
|
|
|
|
|
|
|
|
if (mClosed)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
while (mChunk->mLength) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t written = 0;
|
2011-06-08 18:08:31 +00:00
|
|
|
|
|
|
|
nsresult rv = (*aWriter)(this, aClosure, mChunk->mData,
|
|
|
|
*aRead, mChunk->mLength, &written);
|
|
|
|
|
|
|
|
*aRead += written;
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mChunk++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
FakeInputStream::IsNonBlocking(bool* aIsBlocking)
|
2011-06-08 18:08:31 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*aIsBlocking = false;
|
2011-06-08 18:08:31 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FakeInputStream::Reset()
|
|
|
|
{
|
|
|
|
mClosed = false;
|
|
|
|
mChunk = &mTest->mChunks[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FakeInputStream::NextTest()
|
|
|
|
{
|
|
|
|
mTestNumber++;
|
|
|
|
mTest = &kTests[mTestNumber];
|
|
|
|
mChunk = &mTest->mChunks[0];
|
|
|
|
mClosed = false;
|
|
|
|
|
|
|
|
return mTest->mChunks ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FakeInputStream::CheckTest(nsACString& aResult)
|
|
|
|
{
|
|
|
|
return !strcmp(aResult.BeginReading(), mTest->mResult) ? true : false;
|
|
|
|
}
|
|
|
|
|
2011-06-08 19:50:14 +00:00
|
|
|
#ifdef XP_WIN
|
2013-12-04 12:19:09 +00:00
|
|
|
static inline int NS_tstrcmp(char16ptr_t x, char16ptr_t y) {
|
|
|
|
return wcscmp(x, y);
|
|
|
|
}
|
2011-06-08 19:50:14 +00:00
|
|
|
#else
|
|
|
|
#define NS_tstrcmp strcmp
|
|
|
|
#endif
|
|
|
|
|
2011-06-08 18:08:31 +00:00
|
|
|
bool
|
|
|
|
FakeInputStream::CheckTest(nsAString& aResult)
|
|
|
|
{
|
2011-06-08 19:50:14 +00:00
|
|
|
return !NS_tstrcmp(aResult.BeginReading(),
|
|
|
|
NS_ConvertASCIItoUTF16(mTest->mResult).BeginReading())
|
|
|
|
? true : false;
|
2011-06-08 18:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
ScopedXPCOM xpcom("Base64");
|
|
|
|
NS_ENSURE_FALSE(xpcom.failed(), 1);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptableBase64Encoder> encoder =
|
|
|
|
do_CreateInstance("@mozilla.org/scriptablebase64encoder;1");
|
|
|
|
NS_ENSURE_TRUE(encoder, 1);
|
|
|
|
|
|
|
|
nsRefPtr<FakeInputStream> stream = new FakeInputStream();
|
|
|
|
do {
|
|
|
|
nsString wideString;
|
|
|
|
nsCString string;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = encoder->EncodeToString(stream, 0, wideString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 1);
|
|
|
|
|
|
|
|
stream->Reset();
|
|
|
|
|
|
|
|
rv = encoder->EncodeToCString(stream, 0, string);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 1);
|
|
|
|
|
|
|
|
if (!stream->CheckTest(wideString) || !stream->CheckTest(string))
|
|
|
|
fail("Failed to convert properly\n");
|
|
|
|
|
|
|
|
} while (stream->NextTest());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|