This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
#include "config.h"
#include "modules/websockets/WebSocketDeflater.h"
#include "wtf/Vector.h"
#include <gtest/gtest.h>
using namespace WebCore;
namespace {
TEST(WebSocketDeflaterTest, TestCompressHello)
{
OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15);
ASSERT_TRUE(deflater->initialize());
OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
ASSERT_TRUE(inflater->initialize());
const char* inputData = "Hello";
const size_t inputLength = strlen(inputData);
ASSERT_TRUE(deflater->addBytes(inputData, inputLength));
ASSERT_TRUE(deflater->finish());
const char* expectedFirst = "\xf2\x48\xcd\xc9\xc9\x07\x00";
EXPECT_EQ(7U, deflater->size());
EXPECT_EQ(0, memcmp(expectedFirst, deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->finish());
EXPECT_EQ(inputLength, inflater->size());
EXPECT_EQ(0, memcmp(inputData, inflater->data(), inflater->size()));
deflater->reset();
inflater->reset();
ASSERT_TRUE(deflater->addBytes(inputData, inputLength));
ASSERT_TRUE(deflater->finish());
const char* expectedSecond = "\xf2\x00\x11\x00\x00";
EXPECT_EQ(5U, deflater->size());
EXPECT_EQ(0, memcmp(expectedSecond, deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->finish());
EXPECT_EQ(inputLength, inflater->size());
EXPECT_EQ(0, memcmp(inputData, inflater->data(), inflater->size()));
}
TEST(WebSocketDeflaterTest, TestMultipleAddBytesCalls)
{
OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15);
ASSERT_TRUE(deflater->initialize());
OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
ASSERT_TRUE(inflater->initialize());
Vector<char> inputData(32);
inputData.fill('a');
for (size_t i = 0; i < inputData.size(); ++i)
ASSERT_TRUE(deflater->addBytes(inputData.data() + i, 1));
ASSERT_TRUE(deflater->finish());
for (size_t i = 0; i < deflater->size(); ++i)
ASSERT_TRUE(inflater->addBytes(deflater->data() + i, 1));
ASSERT_TRUE(inflater->finish());
EXPECT_EQ(inputData.size(), inflater->size());
EXPECT_EQ(0, memcmp(inputData.data(), inflater->data(), inflater->size()));
}
TEST(WebSocketDeflaterTest, TestNoContextTakeOver)
{
OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15, WebSocketDeflater::DoNotTakeOverContext);
ASSERT_TRUE(deflater->initialize());
OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
ASSERT_TRUE(inflater->initialize());
const char* expected = "\xf2\x48\xcd\xc9\xc9\x07\x00";
const char* inputData = "Hello";
const size_t inputLength = strlen(inputData);
for (size_t i = 0; i < 2; ++i) {
ASSERT_TRUE(deflater->addBytes(inputData, inputLength));
ASSERT_TRUE(deflater->finish());
EXPECT_EQ(7U, deflater->size());
EXPECT_EQ(0, memcmp(expected, deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->finish());
EXPECT_EQ(inputLength, inflater->size());
EXPECT_EQ(0, memcmp(inputData, inflater->data(), inflater->size()));
deflater->reset();
inflater->reset();
}
}
TEST(WebSocketDeflaterTest, TestWindowBits)
{
Vector<char> inputData(1024 + 64 * 2);
inputData.fill('a');
for (size_t j = 0; j < 64; ++j) {
inputData[j] = 'b';
inputData[inputData.size() - j - 1] = 'b';
}
OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(8);
ASSERT_TRUE(deflater->initialize());
ASSERT_TRUE(deflater->addBytes(inputData.data(), inputData.size()));
ASSERT_TRUE(deflater->finish());
OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create(8);
ASSERT_TRUE(inflater->initialize());
ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->finish());
EXPECT_EQ(inputData.size(), inflater->size());
EXPECT_EQ(0, memcmp(inputData.data(), inflater->data(), inflater->size()));
}
TEST(WebSocketDeflaterTest, TestLargeData)
{
OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15);
ASSERT_TRUE(deflater->initialize());
OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
ASSERT_TRUE(inflater->initialize());
Vector<char> inputData(16 * 1024 * 1024);
inputData.fill('a');
ASSERT_TRUE(deflater->addBytes(inputData.data(), inputData.size()));
ASSERT_TRUE(deflater->finish());
ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
ASSERT_TRUE(inflater->finish());
EXPECT_EQ(inputData.size(), inflater->size());
EXPECT_EQ(0, memcmp(inputData.data(), inflater->data(), inflater->size()));
}
}