WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / tests / WebSocketDeflaterTest.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "WebSocketDeflater.h"
29
30 #include <gtest/gtest.h>
31 #include <wtf/Vector.h>
32
33 using namespace WebCore;
34
35 namespace {
36
37 TEST(WebSocketDeflaterTest, TestCompressHello)
38 {
39     // Test the first example on section 4.3 of the specification.
40     OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15);
41     ASSERT_TRUE(deflater->initialize());
42     OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
43     ASSERT_TRUE(inflater->initialize());
44     const char* inputData = "Hello";
45     const size_t inputLength = strlen(inputData);
46
47     ASSERT_TRUE(deflater->addBytes(inputData, inputLength));
48     ASSERT_TRUE(deflater->finish());
49     const char* expectedFirst = "\xf2\x48\xcd\xc9\xc9\x07\x00";
50     EXPECT_EQ(7U, deflater->size());
51     EXPECT_EQ(0, memcmp(expectedFirst, deflater->data(), deflater->size()));
52     ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
53     ASSERT_TRUE(inflater->finish());
54     EXPECT_EQ(inputLength, inflater->size());
55     EXPECT_EQ(0, memcmp(inputData, inflater->data(), inflater->size()));
56
57     deflater->reset();
58     inflater->reset();
59
60     ASSERT_TRUE(deflater->addBytes(inputData, inputLength));
61     ASSERT_TRUE(deflater->finish());
62     const char* expectedSecond = "\xf2\x00\x11\x00\x00";
63     EXPECT_EQ(5U, deflater->size());
64     EXPECT_EQ(0, memcmp(expectedSecond, deflater->data(), deflater->size()));
65     ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
66     ASSERT_TRUE(inflater->finish());
67     EXPECT_EQ(inputLength, inflater->size());
68     EXPECT_EQ(0, memcmp(inputData, inflater->data(), inflater->size()));
69 }
70
71 TEST(WebSocketDeflaterTest, TestMultipleAddBytesCalls)
72 {
73     OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15);
74     ASSERT_TRUE(deflater->initialize());
75     OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
76     ASSERT_TRUE(inflater->initialize());
77     Vector<char> inputData(32);
78     inputData.fill('a');
79
80     for (size_t i = 0; i < inputData.size(); ++i)
81         ASSERT_TRUE(deflater->addBytes(inputData.data() + i, 1));
82     ASSERT_TRUE(deflater->finish());
83     for (size_t i = 0; i < deflater->size(); ++i)
84         ASSERT_TRUE(inflater->addBytes(deflater->data() + i, 1));
85     ASSERT_TRUE(inflater->finish());
86     EXPECT_EQ(inputData.size(), inflater->size());
87     EXPECT_EQ(0, memcmp(inputData.data(), inflater->data(), inflater->size()));
88 }
89
90 TEST(WebSocketDeflaterTest, TestNoContextTakeOver)
91 {
92     OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15, WebSocketDeflater::DoNotTakeOverContext);
93     ASSERT_TRUE(deflater->initialize());
94     OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
95     ASSERT_TRUE(inflater->initialize());
96     const char* expected = "\xf2\x48\xcd\xc9\xc9\x07\x00";
97     const char* inputData = "Hello";
98     const size_t inputLength = strlen(inputData);
99
100     // If we don't take over context, the second result should be the identical
101     // with the first one.
102     for (size_t i = 0; i < 2; ++i) {
103         ASSERT_TRUE(deflater->addBytes(inputData, inputLength));
104         ASSERT_TRUE(deflater->finish());
105         EXPECT_EQ(7U, deflater->size());
106         EXPECT_EQ(0, memcmp(expected, deflater->data(), deflater->size()));
107         ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
108         ASSERT_TRUE(inflater->finish());
109         EXPECT_EQ(inputLength, inflater->size());
110         EXPECT_EQ(0, memcmp(inputData, inflater->data(), inflater->size()));
111         deflater->reset();
112         inflater->reset();
113     }
114 }
115
116 TEST(WebSocketDeflaterTest, TestWindowBits)
117 {
118     Vector<char> inputData(1024 + 64 * 2);
119     inputData.fill('a');
120     // Modify the head and tail of the inputData so that back-reference
121     // can be used if the window size is sufficiently-large.
122     for (size_t j = 0; j < 64; ++j) {
123         inputData[j] = 'b';
124         inputData[inputData.size() - j - 1] = 'b';
125     }
126
127     OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(8);
128     ASSERT_TRUE(deflater->initialize());
129     ASSERT_TRUE(deflater->addBytes(inputData.data(), inputData.size()));
130     ASSERT_TRUE(deflater->finish());
131
132     OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create(8);
133     ASSERT_TRUE(inflater->initialize());
134     ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
135     ASSERT_TRUE(inflater->finish());
136     EXPECT_EQ(inputData.size(), inflater->size());
137     EXPECT_EQ(0, memcmp(inputData.data(), inflater->data(), inflater->size()));
138 }
139
140 TEST(WebSocketDeflaterTest, TestLargeData)
141 {
142     OwnPtr<WebSocketDeflater> deflater = WebSocketDeflater::create(15);
143     ASSERT_TRUE(deflater->initialize());
144     OwnPtr<WebSocketInflater> inflater = WebSocketInflater::create();
145     ASSERT_TRUE(inflater->initialize());
146     Vector<char> inputData(16 * 1024 * 1024);
147     inputData.fill('a');
148
149     ASSERT_TRUE(deflater->addBytes(inputData.data(), inputData.size()));
150     ASSERT_TRUE(deflater->finish());
151     ASSERT_TRUE(inflater->addBytes(deflater->data(), deflater->size()));
152     ASSERT_TRUE(inflater->finish());
153     EXPECT_EQ(inputData.size(), inflater->size());
154     EXPECT_EQ(0, memcmp(inputData.data(), inflater->data(), inflater->size()));
155 }
156
157 }