2015-09-19 09:12:27 +03: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/. */
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "mozilla/Vector.h"
|
|
|
|
#include "StreamingLexer.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::image;
|
|
|
|
|
|
|
|
enum class TestState
|
|
|
|
{
|
|
|
|
ONE,
|
|
|
|
TWO,
|
|
|
|
THREE,
|
2016-07-18 08:51:19 +03:00
|
|
|
UNBUFFERED,
|
|
|
|
TRUNCATED_SUCCESS,
|
|
|
|
TRUNCATED_FAILURE
|
2015-09-19 09:12:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(const char* aData,
|
|
|
|
size_t aLength,
|
|
|
|
size_t aOffset,
|
|
|
|
size_t aExpectedLength)
|
2015-09-19 09:12:27 +03:00
|
|
|
{
|
2016-07-15 05:55:35 +03:00
|
|
|
EXPECT_TRUE(aLength == aExpectedLength);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < aLength; ++i) {
|
2016-07-16 09:21:20 +03:00
|
|
|
EXPECT_EQ(aData[i], char(aOffset + i + 1));
|
2016-07-15 05:55:35 +03:00
|
|
|
}
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLex(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
2015-09-19 09:12:27 +03:00
|
|
|
return Transition::To(TestState::TWO, 3);
|
|
|
|
case TestState::TWO:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 3, 3);
|
2015-09-19 09:12:27 +03:00
|
|
|
return Transition::To(TestState::THREE, 3);
|
|
|
|
case TestState::THREE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 6, 3);
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2016-07-18 08:51:19 +03:00
|
|
|
case TestState::TRUNCATED_SUCCESS:
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
case TestState::TRUNCATED_FAILURE:
|
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:27 +03:00
|
|
|
default:
|
2016-07-15 05:55:35 +03:00
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithUnbuffered(TestState aState, const char* aData, size_t aLength,
|
|
|
|
Vector<char>& aUnbufferedVector)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
2015-09-19 09:12:27 +03:00
|
|
|
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
|
|
|
|
case TestState::TWO:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aUnbufferedVector.begin(), aUnbufferedVector.length(), 3, 3);
|
2015-09-19 09:12:27 +03:00
|
|
|
return Transition::To(TestState::THREE, 3);
|
|
|
|
case TestState::THREE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 6, 3);
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2016-07-16 09:23:40 +03:00
|
|
|
case TestState::UNBUFFERED:
|
|
|
|
EXPECT_TRUE(aLength <= 3);
|
|
|
|
EXPECT_TRUE(aUnbufferedVector.append(aData, aLength));
|
|
|
|
return Transition::ContinueUnbuffered(TestState::UNBUFFERED);
|
2015-09-19 09:12:27 +03:00
|
|
|
default:
|
2016-07-15 05:55:35 +03:00
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithUnbufferedTerminate(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
2015-09-19 09:12:27 +03:00
|
|
|
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
|
|
|
|
case TestState::UNBUFFERED:
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2015-09-19 09:12:27 +03:00
|
|
|
default:
|
2016-07-15 05:55:35 +03:00
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithYield(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
|
|
|
return Transition::ToAfterYield(TestState::TWO);
|
|
|
|
case TestState::TWO:
|
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
|
|
|
return Transition::To(TestState::THREE, 6);
|
|
|
|
case TestState::THREE:
|
|
|
|
CheckLexedData(aData, aLength, 3, 6);
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithTerminateAfterYield(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
|
|
|
return Transition::ToAfterYield(TestState::TWO);
|
|
|
|
case TestState::TWO:
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 05:55:35 +03:00
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithZeroLengthStates(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::To(TestState::TWO, 0);
|
|
|
|
case TestState::TWO:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::To(TestState::THREE, 9);
|
|
|
|
case TestState::THREE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 0, 9);
|
2016-07-15 05:55:35 +03:00
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithZeroLengthStatesAtEnd(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
CheckLexedData(aData, aLength, 0, 9);
|
|
|
|
return Transition::To(TestState::TWO, 0);
|
|
|
|
case TestState::TWO:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::To(TestState::THREE, 0);
|
|
|
|
case TestState::THREE:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithZeroLengthYield(TestState aState, const char* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
EXPECT_EQ(0u, aLength);
|
|
|
|
return Transition::ToAfterYield(TestState::TWO);
|
|
|
|
case TestState::TWO:
|
|
|
|
EXPECT_EQ(0u, aLength);
|
|
|
|
return Transition::To(TestState::THREE, 9);
|
|
|
|
case TestState::THREE:
|
|
|
|
CheckLexedData(aData, aLength, 0, 9);
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 05:55:35 +03:00
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithZeroLengthStatesUnbuffered(TestState aState,
|
|
|
|
const char* aData,
|
|
|
|
size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 0);
|
|
|
|
case TestState::TWO:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::To(TestState::THREE, 9);
|
|
|
|
case TestState::THREE:
|
2016-07-16 09:21:20 +03:00
|
|
|
CheckLexedData(aData, aLength, 0, 9);
|
2016-07-15 05:55:35 +03:00
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
case TestState::UNBUFFERED:
|
|
|
|
ADD_FAILURE() << "Should not enter zero-length unbuffered state";
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
LexerTransition<TestState>
|
|
|
|
DoLexWithZeroLengthStatesAfterUnbuffered(TestState aState,
|
|
|
|
const char* aData,
|
|
|
|
size_t aLength)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 9);
|
|
|
|
case TestState::TWO:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::To(TestState::THREE, 0);
|
|
|
|
case TestState::THREE:
|
|
|
|
EXPECT_TRUE(aLength == 0);
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
case TestState::UNBUFFERED:
|
|
|
|
CheckLexedData(aData, aLength, 0, 9);
|
|
|
|
return Transition::ContinueUnbuffered(TestState::UNBUFFERED);
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:23:30 +03:00
|
|
|
class ImageStreamingLexer : public ::testing::Test
|
2015-09-19 09:12:27 +03:00
|
|
|
{
|
2016-07-12 10:23:30 +03:00
|
|
|
public:
|
2016-07-18 08:51:19 +03:00
|
|
|
// Note that mLexer is configured to enter TerminalState::FAILURE immediately
|
|
|
|
// if the input data is truncated. We don't expect that to happen in most
|
|
|
|
// tests, so we want to detect that issue. If a test needs a different
|
|
|
|
// behavior, we create a special StreamingLexer just for that test.
|
2016-07-12 10:23:30 +03:00
|
|
|
ImageStreamingLexer()
|
2016-07-18 08:51:19 +03:00
|
|
|
: mLexer(Transition::To(TestState::ONE, 3), Transition::TerminateFailure())
|
2016-07-12 10:23:30 +03:00
|
|
|
, mSourceBuffer(new SourceBuffer)
|
|
|
|
, mIterator(mSourceBuffer->Iterator())
|
|
|
|
, mExpectNoResume(new ExpectNoResume)
|
|
|
|
, mCountResumes(new CountResumes)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
protected:
|
2016-07-18 08:51:19 +03:00
|
|
|
void CheckTruncatedState(StreamingLexer<TestState>& aLexer,
|
|
|
|
TerminalState aExpectedTerminalState,
|
|
|
|
nsresult aCompletionStatus = NS_OK)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
|
|
|
if (i < 2) {
|
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
|
|
|
} else if (i == 2) {
|
|
|
|
mSourceBuffer->Complete(aCompletionStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerResult result = aLexer.Lex(mIterator, mCountResumes, DoLex);
|
|
|
|
|
|
|
|
if (i >= 2) {
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(aExpectedTerminalState, result.as<TerminalState>());
|
|
|
|
} else {
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, mCountResumes->Count());
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:23:30 +03:00
|
|
|
AutoInitializeImageLib mInit;
|
2016-07-16 09:21:20 +03:00
|
|
|
const char mData[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
2016-07-12 10:23:30 +03:00
|
|
|
StreamingLexer<TestState> mLexer;
|
|
|
|
RefPtr<SourceBuffer> mSourceBuffer;
|
|
|
|
SourceBufferIterator mIterator;
|
|
|
|
RefPtr<ExpectNoResume> mExpectNoResume;
|
|
|
|
RefPtr<CountResumes> mCountResumes;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthData)
|
|
|
|
{
|
2016-07-15 05:55:35 +03:00
|
|
|
// Test a zero-length input.
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result = mLexer.Lex(mIterator, mExpectNoResume, DoLex);
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::FAILURE, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
2015-09-19 09:12:27 +03:00
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthDataUnbuffered)
|
|
|
|
{
|
|
|
|
// Test a zero-length input.
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be unbuffered.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::ToUnbuffered(TestState::ONE,
|
|
|
|
TestState::UNBUFFERED,
|
2016-07-18 08:51:19 +03:00
|
|
|
sizeof(mData)),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-16 09:23:40 +03:00
|
|
|
|
|
|
|
LexerResult result = lexer.Lex(mIterator, mExpectNoResume, DoLex);
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::FAILURE, result.as<TerminalState>());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, StartWithTerminal)
|
|
|
|
{
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be a terminal state. This doesn't really make sense, but we should
|
|
|
|
// handle it.
|
2016-07-18 08:51:19 +03:00
|
|
|
StreamingLexer<TestState> lexer(Transition::TerminateSuccess(),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-16 09:23:40 +03:00
|
|
|
LexerResult result = lexer.Lex(mIterator, mExpectNoResume, DoLex);
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SingleChunk)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
2015-09-19 09:12:27 +03:00
|
|
|
// Test delivering all the data at once.
|
2016-07-12 10:23:30 +03:00
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result = mLexer.Lex(mIterator, mExpectNoResume, DoLex);
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SingleChunkWithUnbuffered)
|
2015-09-19 09:12:27 +03:00
|
|
|
{
|
2016-07-12 10:23:30 +03:00
|
|
|
Vector<char> unbufferedVector;
|
|
|
|
|
|
|
|
// Test delivering all the data at once.
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(mIterator, mExpectNoResume,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
|
|
|
return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
|
|
|
|
});
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
2015-09-19 09:12:27 +03:00
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SingleChunkWithYield)
|
|
|
|
{
|
|
|
|
// Test delivering all the data at once.
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
LexerResult result = mLexer.Lex(mIterator, mExpectNoResume, DoLexWithYield);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
|
|
|
|
result = mLexer.Lex(mIterator, mExpectNoResume, DoLexWithYield);
|
|
|
|
ASSERT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ChunkPerState)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
|
|
|
// Test delivering in perfectly-sized chunks, one per state.
|
|
|
|
for (unsigned i = 0; i < 3; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + 3 * i, 3);
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result = mLexer.Lex(mIterator, mCountResumes, DoLex);
|
2016-07-12 10:23:30 +03:00
|
|
|
|
|
|
|
if (i == 2) {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
} else {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ChunkPerStateWithUnbuffered)
|
2015-09-19 09:12:27 +03:00
|
|
|
{
|
2016-07-12 10:23:30 +03:00
|
|
|
Vector<char> unbufferedVector;
|
2015-09-19 09:12:27 +03:00
|
|
|
|
2016-07-12 10:23:30 +03:00
|
|
|
// Test delivering in perfectly-sized chunks, one per state.
|
|
|
|
for (unsigned i = 0; i < 3; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + 3 * i, 3);
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(mIterator, mCountResumes,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
|
|
|
return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (i == 2) {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
} else {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ChunkPerStateWithYield)
|
|
|
|
{
|
|
|
|
// Test delivering in perfectly-sized chunks, one per state.
|
|
|
|
mSourceBuffer->Append(mData, 3);
|
|
|
|
LexerResult result = mLexer.Lex(mIterator, mCountResumes, DoLexWithYield);
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
|
|
|
|
result = mLexer.Lex(mIterator, mCountResumes, DoLexWithYield);
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
|
|
|
|
mSourceBuffer->Append(mData + 3, 6);
|
|
|
|
result = mLexer.Lex(mIterator, mCountResumes, DoLexWithYield);
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
|
|
|
|
EXPECT_EQ(1u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, ChunkPerStateWithUnbufferedYield)
|
|
|
|
{
|
|
|
|
size_t unbufferedCallCount = 0;
|
|
|
|
Vector<char> unbufferedVector;
|
|
|
|
auto lexerFunc = [&](TestState aState, const char* aData, size_t aLength)
|
|
|
|
-> LexerTransition<TestState> {
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
|
|
|
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
|
|
|
|
case TestState::TWO:
|
|
|
|
CheckLexedData(unbufferedVector.begin(), unbufferedVector.length(), 3, 3);
|
|
|
|
return Transition::To(TestState::THREE, 3);
|
|
|
|
case TestState::THREE:
|
|
|
|
CheckLexedData(aData, aLength, 6, 3);
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
case TestState::UNBUFFERED:
|
|
|
|
switch (unbufferedCallCount) {
|
|
|
|
case 0:
|
|
|
|
CheckLexedData(aData, aLength, 3, 3);
|
|
|
|
EXPECT_TRUE(unbufferedVector.append(aData, 2));
|
|
|
|
unbufferedCallCount++;
|
|
|
|
|
|
|
|
// Continue after yield, telling StreamingLexer we consumed 2 bytes.
|
|
|
|
return Transition::ContinueUnbufferedAfterYield(TestState::UNBUFFERED, 2);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
CheckLexedData(aData, aLength, 5, 1);
|
|
|
|
EXPECT_TRUE(unbufferedVector.append(aData, 1));
|
|
|
|
unbufferedCallCount++;
|
|
|
|
|
|
|
|
// Continue after yield, telling StreamingLexer we consumed 1 byte.
|
|
|
|
// We should end up in the TWO state.
|
|
|
|
return Transition::ContinueUnbuffered(TestState::UNBUFFERED);
|
|
|
|
}
|
|
|
|
ADD_FAILURE() << "Too many invocations of TestState::UNBUFFERED";
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test delivering in perfectly-sized chunks, one per state.
|
|
|
|
for (unsigned i = 0; i < 3; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + 3 * i, 3);
|
|
|
|
LexerResult result = mLexer.Lex(mIterator, mCountResumes, lexerFunc);
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
EXPECT_EQ(0u, unbufferedCallCount);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
EXPECT_EQ(1u, unbufferedCallCount);
|
|
|
|
|
|
|
|
result = mLexer.Lex(mIterator, mCountResumes, lexerFunc);
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
EXPECT_EQ(2u, unbufferedCallCount);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(2u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
LexerResult result = mLexer.Lex(mIterator, mCountResumes, lexerFunc);
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, OneByteChunks)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
2015-09-19 09:12:27 +03:00
|
|
|
// Test delivering in one byte chunks.
|
2016-02-27 08:01:49 +03:00
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
2016-07-12 10:23:30 +03:00
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result = mLexer.Lex(mIterator, mCountResumes, DoLex);
|
2015-09-19 09:12:27 +03:00
|
|
|
|
|
|
|
if (i == 8) {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
} else {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 10:23:30 +03:00
|
|
|
|
|
|
|
EXPECT_EQ(8u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, OneByteChunksWithUnbuffered)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
|
|
|
Vector<char> unbufferedVector;
|
|
|
|
|
|
|
|
// Test delivering in one byte chunks.
|
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(mIterator, mCountResumes,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
|
|
|
return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (i == 8) {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
} else {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(8u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, OneByteChunksWithYield)
|
|
|
|
{
|
|
|
|
// Test delivering in one byte chunks.
|
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
|
|
|
LexerResult result = mLexer.Lex(mIterator, mCountResumes, DoLexWithYield);
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 2:
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
|
|
|
|
result = mLexer.Lex(mIterator, mCountResumes, DoLexWithYield);
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
EXPECT_TRUE(i < 9);
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(8u, mCountResumes->Count());
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:00:21 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthState)
|
|
|
|
{
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be zero length.
|
2016-07-18 08:51:19 +03:00
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 0),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-15 06:00:21 +03:00
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-15 06:00:21 +03:00
|
|
|
lexer.Lex(mIterator, mExpectNoResume, DoLexWithZeroLengthStates);
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-15 06:00:21 +03:00
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthStatesAtEnd)
|
|
|
|
{
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to consume the full input.
|
2016-07-18 08:51:19 +03:00
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 9),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-16 09:23:40 +03:00
|
|
|
|
|
|
|
LexerResult result =
|
|
|
|
lexer.Lex(mIterator, mExpectNoResume, DoLexWithZeroLengthStatesAtEnd);
|
|
|
|
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthStateWithYield)
|
|
|
|
{
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be zero length.
|
2016-07-18 08:51:19 +03:00
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 0),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-16 09:23:40 +03:00
|
|
|
|
|
|
|
mSourceBuffer->Append(mData, 3);
|
|
|
|
LexerResult result =
|
|
|
|
lexer.Lex(mIterator, mExpectNoResume, DoLexWithZeroLengthYield);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
|
|
|
|
result = lexer.Lex(mIterator, mCountResumes, DoLexWithZeroLengthYield);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
|
|
|
|
mSourceBuffer->Append(mData + 3, sizeof(mData) - 3);
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
result = lexer.Lex(mIterator, mExpectNoResume, DoLexWithZeroLengthYield);
|
|
|
|
ASSERT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
EXPECT_EQ(1u, mCountResumes->Count());
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:00:21 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthStateWithUnbuffered)
|
|
|
|
{
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be both zero length and unbuffered.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::ToUnbuffered(TestState::ONE,
|
|
|
|
TestState::UNBUFFERED,
|
2016-07-18 08:51:19 +03:00
|
|
|
0),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-15 06:00:21 +03:00
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-15 06:00:21 +03:00
|
|
|
lexer.Lex(mIterator, mExpectNoResume, DoLexWithZeroLengthStatesUnbuffered);
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-15 06:00:21 +03:00
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthStateAfterUnbuffered)
|
|
|
|
{
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be zero length.
|
2016-07-18 08:51:19 +03:00
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 0),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-16 09:23:40 +03:00
|
|
|
|
|
|
|
LexerResult result =
|
|
|
|
lexer.Lex(mIterator, mExpectNoResume, DoLexWithZeroLengthStatesAfterUnbuffered);
|
|
|
|
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, ZeroLengthStateWithUnbufferedYield)
|
|
|
|
{
|
|
|
|
size_t unbufferedCallCount = 0;
|
|
|
|
auto lexerFunc = [&](TestState aState, const char* aData, size_t aLength)
|
|
|
|
-> LexerTransition<TestState> {
|
|
|
|
switch (aState) {
|
|
|
|
case TestState::ONE:
|
|
|
|
EXPECT_EQ(0u, aLength);
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
|
|
|
|
case TestState::UNBUFFERED:
|
|
|
|
switch (unbufferedCallCount) {
|
|
|
|
case 0:
|
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
|
|
|
unbufferedCallCount++;
|
|
|
|
|
|
|
|
// Continue after yield, telling StreamingLexer we consumed 0 bytes.
|
|
|
|
return Transition::ContinueUnbufferedAfterYield(TestState::UNBUFFERED, 0);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
CheckLexedData(aData, aLength, 0, 3);
|
|
|
|
unbufferedCallCount++;
|
|
|
|
|
|
|
|
// Continue after yield, telling StreamingLexer we consumed 2 bytes.
|
|
|
|
return Transition::ContinueUnbufferedAfterYield(TestState::UNBUFFERED, 2);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
EXPECT_EQ(1u, aLength);
|
|
|
|
CheckLexedData(aData, aLength, 2, 1);
|
|
|
|
unbufferedCallCount++;
|
|
|
|
|
|
|
|
// Continue after yield, telling StreamingLexer we consumed 1 bytes.
|
|
|
|
return Transition::ContinueUnbufferedAfterYield(TestState::UNBUFFERED, 1);
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
CheckLexedData(aData, aLength, 3, 6);
|
|
|
|
unbufferedCallCount++;
|
|
|
|
|
|
|
|
// Continue after yield, telling StreamingLexer we consumed 6 bytes.
|
|
|
|
// We should transition to TestState::ONE when we return from the
|
|
|
|
// yield.
|
|
|
|
return Transition::ContinueUnbufferedAfterYield(TestState::UNBUFFERED, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_FAILURE() << "Too many invocations of TestState::UNBUFFERED";
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected or unhandled TestState");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a special StreamingLexer for this test because we want the first
|
|
|
|
// state to be unbuffered.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::ToUnbuffered(TestState::ONE,
|
|
|
|
TestState::UNBUFFERED,
|
2016-07-18 08:51:19 +03:00
|
|
|
sizeof(mData)),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-16 09:23:40 +03:00
|
|
|
|
|
|
|
mSourceBuffer->Append(mData, 3);
|
|
|
|
LexerResult result = lexer.Lex(mIterator, mExpectNoResume, lexerFunc);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
EXPECT_EQ(1u, unbufferedCallCount);
|
|
|
|
|
|
|
|
result = lexer.Lex(mIterator, mExpectNoResume, lexerFunc);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
EXPECT_EQ(2u, unbufferedCallCount);
|
|
|
|
|
|
|
|
result = lexer.Lex(mIterator, mExpectNoResume, lexerFunc);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
EXPECT_EQ(3u, unbufferedCallCount);
|
|
|
|
|
|
|
|
result = lexer.Lex(mIterator, mCountResumes, lexerFunc);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
EXPECT_EQ(3u, unbufferedCallCount);
|
|
|
|
|
|
|
|
mSourceBuffer->Append(mData + 3, 6);
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
EXPECT_EQ(1u, mCountResumes->Count());
|
|
|
|
result = lexer.Lex(mIterator, mExpectNoResume, lexerFunc);
|
|
|
|
ASSERT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
EXPECT_EQ(4u, unbufferedCallCount);
|
|
|
|
|
|
|
|
result = lexer.Lex(mIterator, mExpectNoResume, lexerFunc);
|
|
|
|
ASSERT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, TerminateSuccess)
|
2015-09-19 09:12:27 +03:00
|
|
|
{
|
2016-07-12 10:23:30 +03:00
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
2015-09-19 09:12:27 +03:00
|
|
|
|
|
|
|
// Test that Terminate is "sticky".
|
2016-07-12 10:23:30 +03:00
|
|
|
SourceBufferIterator iterator = mSourceBuffer->Iterator();
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(iterator, mExpectNoResume,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
2015-09-19 09:12:27 +03:00
|
|
|
EXPECT_TRUE(aState == TestState::ONE);
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2015-09-19 09:12:27 +03:00
|
|
|
});
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
|
2016-07-12 10:23:30 +03:00
|
|
|
SourceBufferIterator iterator2 = mSourceBuffer->Iterator();
|
2015-09-19 09:12:27 +03:00
|
|
|
result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(iterator2, mExpectNoResume,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
2015-09-19 09:12:27 +03:00
|
|
|
EXPECT_TRUE(false); // Shouldn't get here.
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:27 +03:00
|
|
|
});
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, TerminateFailure)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
|
|
|
mSourceBuffer->Append(mData, sizeof(mData));
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
2015-09-19 09:12:27 +03:00
|
|
|
|
|
|
|
// Test that Terminate is "sticky".
|
2016-07-12 10:23:30 +03:00
|
|
|
SourceBufferIterator iterator = mSourceBuffer->Iterator();
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(iterator, mExpectNoResume,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
2015-09-19 09:12:27 +03:00
|
|
|
EXPECT_TRUE(aState == TestState::ONE);
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:27 +03:00
|
|
|
});
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::FAILURE, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
|
2016-07-12 10:23:30 +03:00
|
|
|
SourceBufferIterator iterator2 = mSourceBuffer->Iterator();
|
2015-09-19 09:12:27 +03:00
|
|
|
result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(iterator2, mExpectNoResume,
|
|
|
|
[&](TestState aState, const char* aData, size_t aLength) {
|
2015-09-19 09:12:27 +03:00
|
|
|
EXPECT_TRUE(false); // Shouldn't get here.
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:27 +03:00
|
|
|
});
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::FAILURE, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 06:01:18 +03:00
|
|
|
TEST_F(ImageStreamingLexer, TerminateUnbuffered)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
2015-09-19 09:12:27 +03:00
|
|
|
// Test that Terminate works during an unbuffered read.
|
2016-02-27 08:01:49 +03:00
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
2016-07-12 10:23:30 +03:00
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result =
|
2016-07-12 10:23:30 +03:00
|
|
|
mLexer.Lex(mIterator, mCountResumes, DoLexWithUnbufferedTerminate);
|
2015-09-19 09:12:27 +03:00
|
|
|
|
|
|
|
if (i > 2) {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2015-09-19 09:12:27 +03:00
|
|
|
} else {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 10:23:30 +03:00
|
|
|
|
|
|
|
// We expect 3 resumes because TestState::ONE consumes 3 bytes and then
|
|
|
|
// transitions to TestState::UNBUFFERED, which calls TerminateSuccess() as
|
|
|
|
// soon as it receives a single byte. That's four bytes total, which are
|
|
|
|
// delivered one at a time, requiring 3 resumes.
|
|
|
|
EXPECT_EQ(3u, mCountResumes->Count());
|
|
|
|
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-16 09:23:40 +03:00
|
|
|
TEST_F(ImageStreamingLexer, TerminateAfterYield)
|
|
|
|
{
|
|
|
|
// Test that Terminate works after yielding.
|
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
|
|
|
LexerResult result =
|
|
|
|
mLexer.Lex(mIterator, mCountResumes, DoLexWithTerminateAfterYield);
|
|
|
|
|
|
|
|
if (i > 2) {
|
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
|
|
|
} else if (i == 2) {
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::OUTPUT_AVAILABLE, result.as<Yield>());
|
|
|
|
} else {
|
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect 2 resumes because TestState::ONE consumes 3 bytes and then
|
|
|
|
// yields. When the lexer resumes at TestState::TWO, which receives the same 3
|
|
|
|
// bytes, TerminateSuccess() gets called immediately. That's three bytes
|
|
|
|
// total, which are delivered one at a time, requiring 2 resumes.
|
|
|
|
EXPECT_EQ(2u, mCountResumes->Count());
|
|
|
|
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:23:30 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SourceBufferImmediateComplete)
|
|
|
|
{
|
|
|
|
// Test calling SourceBuffer::Complete() without appending any data. This
|
|
|
|
// causes the SourceBuffer to automatically have a failing completion status,
|
|
|
|
// no matter what you pass, so we expect TerminalState::FAILURE below.
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result = mLexer.Lex(mIterator, mExpectNoResume, DoLex);
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::FAILURE, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SourceBufferTruncatedTerminalStateSuccess)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
2016-07-18 08:51:19 +03:00
|
|
|
// Test that using a terminal state (in this case TerminalState::SUCCESS) as a
|
|
|
|
// truncated state works.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3),
|
|
|
|
Transition::TerminateSuccess());
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
CheckTruncatedState(lexer, TerminalState::SUCCESS);
|
|
|
|
}
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SourceBufferTruncatedTerminalStateFailure)
|
|
|
|
{
|
|
|
|
// Test that using a terminal state (in this case TerminalState::FAILURE) as a
|
|
|
|
// truncated state works.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3),
|
|
|
|
Transition::TerminateFailure());
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
CheckTruncatedState(lexer, TerminalState::FAILURE);
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SourceBufferTruncatedStateReturningSuccess)
|
2016-07-12 10:23:30 +03:00
|
|
|
{
|
2016-07-18 08:51:19 +03:00
|
|
|
// Test that a truncated state that returns TerminalState::SUCCESS works. When
|
|
|
|
// |lexer| discovers that the data is truncated, it invokes the
|
|
|
|
// TRUNCATED_SUCCESS state, which returns TerminalState::SUCCESS.
|
|
|
|
// CheckTruncatedState() verifies that this happens.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3),
|
|
|
|
Transition::To(TestState::TRUNCATED_SUCCESS, 0));
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
CheckTruncatedState(lexer, TerminalState::SUCCESS);
|
|
|
|
}
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
TEST_F(ImageStreamingLexer, SourceBufferTruncatedStateReturningFailure)
|
|
|
|
{
|
|
|
|
// Test that a truncated state that returns TerminalState::FAILURE works. When
|
|
|
|
// |lexer| discovers that the data is truncated, it invokes the
|
|
|
|
// TRUNCATED_FAILURE state, which returns TerminalState::FAILURE.
|
|
|
|
// CheckTruncatedState() verifies that this happens.
|
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3),
|
|
|
|
Transition::To(TestState::TRUNCATED_FAILURE, 0));
|
2016-07-12 10:23:30 +03:00
|
|
|
|
2016-07-18 08:51:19 +03:00
|
|
|
CheckTruncatedState(lexer, TerminalState::FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, SourceBufferTruncatedFailingCompleteStatus)
|
|
|
|
{
|
|
|
|
// Test that calling SourceBuffer::Complete() with a failing status results in
|
|
|
|
// an immediate TerminalState::FAILURE result. (Note that |lexer|'s truncated
|
|
|
|
// state is TerminalState::SUCCESS, so if we ignore the failing status, the
|
|
|
|
// test will fail.)
|
|
|
|
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3),
|
|
|
|
Transition::TerminateSuccess());
|
|
|
|
|
|
|
|
CheckTruncatedState(lexer, TerminalState::FAILURE, NS_ERROR_FAILURE);
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImageStreamingLexer, NoSourceBufferResumable)
|
|
|
|
{
|
|
|
|
// Test delivering in one byte chunks with no IResumable.
|
|
|
|
for (unsigned i = 0; i < 9; ++i) {
|
|
|
|
mSourceBuffer->Append(mData + i, 1);
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult result = mLexer.Lex(mIterator, nullptr, DoLex);
|
2016-07-12 10:23:30 +03:00
|
|
|
|
|
|
|
if (i == 8) {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<TerminalState>());
|
|
|
|
EXPECT_EQ(TerminalState::SUCCESS, result.as<TerminalState>());
|
2016-07-12 10:23:30 +03:00
|
|
|
} else {
|
2016-07-16 08:27:12 +03:00
|
|
|
EXPECT_TRUE(result.is<Yield>());
|
|
|
|
EXPECT_EQ(Yield::NEED_MORE_DATA, result.as<Yield>());
|
2016-07-12 10:23:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mSourceBuffer->Complete(NS_OK);
|
2015-09-19 09:12:27 +03:00
|
|
|
}
|