1 | /* |
2 | * Copyright (C) 2016-2017 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2015 Canon Inc. All rights reserved. |
4 | * Copyright (C) 2013 Google Inc. All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * |
15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
17 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | * THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #include "config.h" |
29 | |
30 | #include "SharedBufferTest.h" |
31 | #include "Test.h" |
32 | #include <WebCore/SharedBuffer.h> |
33 | #include <wtf/MainThread.h> |
34 | #include <wtf/StringExtras.h> |
35 | |
36 | using namespace WebCore; |
37 | |
38 | namespace TestWebKitAPI { |
39 | |
40 | TEST_F(SharedBufferTest, createWithContentsOfMissingFile) |
41 | { |
42 | RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(String("not_existing_file" )); |
43 | ASSERT_NULL(buffer); |
44 | } |
45 | |
46 | TEST_F(SharedBufferTest, createWithContentsOfExistingFile) |
47 | { |
48 | RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempFilePath()); |
49 | ASSERT_NOT_NULL(buffer); |
50 | EXPECT_TRUE(buffer->size() == strlen(SharedBufferTest::testData())); |
51 | EXPECT_TRUE(String(SharedBufferTest::testData()) == String(buffer->data(), buffer->size())); |
52 | } |
53 | |
54 | TEST_F(SharedBufferTest, createWithContentsOfExistingEmptyFile) |
55 | { |
56 | RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempEmptyFilePath()); |
57 | ASSERT_NOT_NULL(buffer); |
58 | EXPECT_TRUE(buffer->isEmpty()); |
59 | } |
60 | |
61 | TEST_F(SharedBufferTest, copyBufferCreatedWithContentsOfExistingFile) |
62 | { |
63 | RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempFilePath()); |
64 | ASSERT_NOT_NULL(buffer); |
65 | RefPtr<SharedBuffer> copy = buffer->copy(); |
66 | EXPECT_GT(buffer->size(), 0U); |
67 | EXPECT_TRUE(buffer->size() == copy->size()); |
68 | EXPECT_TRUE(!memcmp(buffer->data(), copy->data(), buffer->size())); |
69 | } |
70 | |
71 | TEST_F(SharedBufferTest, clearBufferCreatedWithContentsOfExistingFile) |
72 | { |
73 | RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempFilePath()); |
74 | ASSERT_NOT_NULL(buffer); |
75 | buffer->clear(); |
76 | EXPECT_TRUE(!buffer->size()); |
77 | EXPECT_TRUE(!buffer->data()); |
78 | } |
79 | |
80 | TEST_F(SharedBufferTest, appendBufferCreatedWithContentsOfExistingFile) |
81 | { |
82 | RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempFilePath()); |
83 | ASSERT_NOT_NULL(buffer); |
84 | buffer->append("a" , 1); |
85 | EXPECT_TRUE(buffer->size() == (strlen(SharedBufferTest::testData()) + 1)); |
86 | EXPECT_TRUE(!memcmp(buffer->data(), SharedBufferTest::testData(), strlen(SharedBufferTest::testData()))); |
87 | EXPECT_EQ('a', buffer->data()[strlen(SharedBufferTest::testData())]); |
88 | } |
89 | |
90 | TEST_F(SharedBufferTest, tryCreateArrayBuffer) |
91 | { |
92 | char testData0[] = "Hello" ; |
93 | char testData1[] = "World" ; |
94 | char testData2[] = "Goodbye" ; |
95 | RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData0, strlen(testData0)); |
96 | sharedBuffer->append(testData1, strlen(testData1)); |
97 | sharedBuffer->append(testData2, strlen(testData2)); |
98 | RefPtr<ArrayBuffer> arrayBuffer = sharedBuffer->tryCreateArrayBuffer(); |
99 | char expectedConcatenation[] = "HelloWorldGoodbye" ; |
100 | ASSERT_EQ(strlen(expectedConcatenation), arrayBuffer->byteLength()); |
101 | EXPECT_EQ(0, memcmp(expectedConcatenation, arrayBuffer->data(), strlen(expectedConcatenation))); |
102 | } |
103 | |
104 | TEST_F(SharedBufferTest, tryCreateArrayBufferLargeSegments) |
105 | { |
106 | Vector<char> vector0(0x4000, 'a'); |
107 | Vector<char> vector1(0x4000, 'b'); |
108 | Vector<char> vector2(0x4000, 'c'); |
109 | |
110 | RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(WTFMove(vector0)); |
111 | sharedBuffer->append(WTFMove(vector1)); |
112 | sharedBuffer->append(WTFMove(vector2)); |
113 | RefPtr<ArrayBuffer> arrayBuffer = sharedBuffer->tryCreateArrayBuffer(); |
114 | ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, arrayBuffer->byteLength()); |
115 | int position = 0; |
116 | for (int i = 0; i < 0x4000; ++i) { |
117 | EXPECT_EQ('a', static_cast<char*>(arrayBuffer->data())[position]); |
118 | ++position; |
119 | } |
120 | for (int i = 0; i < 0x4000; ++i) { |
121 | EXPECT_EQ('b', static_cast<char*>(arrayBuffer->data())[position]); |
122 | ++position; |
123 | } |
124 | for (int i = 0; i < 0x4000; ++i) { |
125 | EXPECT_EQ('c', static_cast<char*>(arrayBuffer->data())[position]); |
126 | ++position; |
127 | } |
128 | } |
129 | |
130 | TEST_F(SharedBufferTest, copy) |
131 | { |
132 | char testData[] = "Habitasse integer eros tincidunt a scelerisque! Enim elit? Scelerisque magnis," |
133 | "et montes ultrices tristique a! Pid. Velit turpis, dapibus integer rhoncus sociis amet facilisis," |
134 | "adipiscing pulvinar nascetur magnis tempor sit pulvinar, massa urna enim porttitor sociis sociis proin enim?" |
135 | "Lectus, platea dolor, integer a. A habitasse hac nunc, nunc, nec placerat vut in sit nunc nec, sed. Sociis," |
136 | "vut! Hac, velit rhoncus facilisis. Rhoncus et, enim, sed et in tristique nunc montes," |
137 | "natoque nunc sagittis elementum parturient placerat dolor integer? Pulvinar," |
138 | "magnis dignissim porttitor ac pulvinar mid tempor. A risus sed mid! Magnis elit duis urna," |
139 | "cras massa, magna duis. Vut magnis pid a! Penatibus aliquet porttitor nunc, adipiscing massa odio lundium," |
140 | "risus elementum ac turpis massa pellentesque parturient augue. Purus amet turpis pid aliquam?" |
141 | "Dolor est tincidunt? Dolor? Dignissim porttitor sit in aliquam! Tincidunt, non nunc, rhoncus dictumst!" |
142 | "Porta augue etiam. Cursus augue nunc lacus scelerisque. Rhoncus lectus, integer hac, nec pulvinar augue massa," |
143 | "integer amet nisi facilisis? A! A, enim velit pulvinar elit in non scelerisque in et ultricies amet est!" |
144 | "in porttitor montes lorem et, hac aliquet pellentesque a sed? Augue mid purus ridiculus vel dapibus," |
145 | "sagittis sed, tortor auctor nascetur rhoncus nec, rhoncus, magna integer. Sit eu massa vut?" |
146 | "Porta augue porttitor elementum, enim, rhoncus pulvinar duis integer scelerisque rhoncus natoque," |
147 | "mattis dignissim massa ac pulvinar urna, nunc ut. Sagittis, aliquet penatibus proin lorem, pulvinar lectus," |
148 | "augue proin! Ac, arcu quis. Placerat habitasse, ridiculus ridiculus." ; |
149 | unsigned length = strlen(testData); |
150 | RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData, length); |
151 | sharedBuffer->append(testData, length); |
152 | sharedBuffer->append(testData, length); |
153 | sharedBuffer->append(testData, length); |
154 | // sharedBuffer must contain data more than segmentSize (= 0x1000) to check copy(). |
155 | ASSERT_EQ(length * 4, sharedBuffer->size()); |
156 | RefPtr<SharedBuffer> clone = sharedBuffer->copy(); |
157 | ASSERT_EQ(length * 4, clone->size()); |
158 | ASSERT_EQ(0, memcmp(clone->data(), sharedBuffer->data(), clone->size())); |
159 | clone->append(testData, length); |
160 | ASSERT_EQ(length * 5, clone->size()); |
161 | } |
162 | |
163 | static void checkBuffer(const char* buffer, size_t bufferLength, const char* expected) |
164 | { |
165 | // expected is null terminated, buffer is not. |
166 | size_t length = strlen(expected); |
167 | EXPECT_EQ(length, bufferLength); |
168 | for (size_t i = 0; i < length; ++i) |
169 | EXPECT_EQ(buffer[i], expected[i]); |
170 | } |
171 | |
172 | TEST_F(SharedBufferTest, getSomeData) |
173 | { |
174 | Vector<char> s1 = {'a', 'b', 'c', 'd'}; |
175 | Vector<char> s2 = {'e', 'f', 'g', 'h'}; |
176 | Vector<char> s3 = {'i', 'j', 'k', 'l'}; |
177 | |
178 | auto buffer = SharedBuffer::create(); |
179 | buffer->append(WTFMove(s1)); |
180 | buffer->append(WTFMove(s2)); |
181 | buffer->append(WTFMove(s3)); |
182 | |
183 | auto abcd = buffer->getSomeData(0); |
184 | auto gh = buffer->getSomeData(6); |
185 | auto h = buffer->getSomeData(7); |
186 | auto ijkl = buffer->getSomeData(8); |
187 | auto kl = buffer->getSomeData(10); |
188 | auto abcdefghijkl = buffer->data(); |
189 | auto ghijkl = buffer->getSomeData(6); |
190 | auto l = buffer->getSomeData(11); |
191 | checkBuffer(abcd.data(), abcd.size(), "abcd" ); |
192 | checkBuffer(gh.data(), gh.size(), "gh" ); |
193 | checkBuffer(h.data(), h.size(), "h" ); |
194 | checkBuffer(ijkl.data(), ijkl.size(), "ijkl" ); |
195 | checkBuffer(kl.data(), kl.size(), "kl" ); |
196 | checkBuffer(abcdefghijkl, buffer->size(), "abcdefghijkl" ); |
197 | checkBuffer(ghijkl.data(), ghijkl.size(), "ghijkl" ); |
198 | checkBuffer(l.data(), l.size(), "l" ); |
199 | } |
200 | |
201 | TEST_F(SharedBufferTest, isEqualTo) |
202 | { |
203 | auto makeBuffer = [] (Vector<Vector<char>>&& contents) { |
204 | auto buffer = SharedBuffer::create(); |
205 | for (auto& content : contents) |
206 | buffer->append(WTFMove(content)); |
207 | return buffer; |
208 | }; |
209 | auto buffer1 = makeBuffer({{'a', 'b', 'c', 'd'}}); |
210 | EXPECT_EQ(buffer1, buffer1); |
211 | |
212 | buffer1->append(Vector<char>({'a', 'b', 'c', 'd'})); |
213 | EXPECT_EQ(buffer1, makeBuffer({{'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'}})); |
214 | EXPECT_EQ(makeBuffer({{'a'}, {'b', 'c'}, {'d'}}), makeBuffer({{'a', 'b'}, {'c', 'd'}})); |
215 | EXPECT_NE(makeBuffer({{'a', 'b'}}), makeBuffer({{'a', 'b', 'c'}})); |
216 | EXPECT_NE(makeBuffer({{'a', 'b'}}), makeBuffer({{'b', 'c'}})); |
217 | EXPECT_NE(makeBuffer({{'a'}, {'b'}}), makeBuffer({{'a'}, {'a'}})); |
218 | } |
219 | |
220 | } |
221 | |