1/*
2 * Copyright (C) 2016 Apple 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include <wtf/LEBDecoder.h>
29#include <wtf/Vector.h>
30
31namespace TestWebKitAPI {
32
33static void testUInt32LEBDecode(std::initializer_list<uint8_t> data, size_t startOffset, bool expectedStatus, uint32_t expectedResult, size_t expectedOffset)
34{
35 Vector<uint8_t> vector(data);
36 uint32_t result;
37 bool status = WTF::LEBDecoder::decodeUInt32(vector.data(), vector.size(), startOffset, result);
38 EXPECT_EQ(expectedStatus, status);
39 if (expectedStatus) {
40 EXPECT_EQ(expectedResult, result);
41 EXPECT_EQ(expectedOffset, startOffset);
42 }
43}
44
45TEST(WTF, LEBDecoderUInt32)
46{
47 // Simple tests that use all the bits in the array
48 testUInt32LEBDecode({ 0x07 }, 0, true, 0x7lu, 1lu);
49 testUInt32LEBDecode({ 0x77 }, 0, true, 0x77lu, 1lu);
50 testUInt32LEBDecode({ 0x80, 0x07 }, 0, true, 0x380lu, 2lu);
51 testUInt32LEBDecode({ 0x89, 0x12 }, 0, true, 0x909lu, 2lu);
52 testUInt32LEBDecode({ 0xf3, 0x85, 0x02 }, 0, true, 0x82f3lu, 3lu);
53 testUInt32LEBDecode({ 0xf3, 0x85, 0xff, 0x74 }, 0, true, 0xe9fc2f3lu, 4lu);
54 testUInt32LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x7f }, 0, true, 0xfe9fc2f3lu, 5lu);
55 // Test with extra trailing numbers
56 testUInt32LEBDecode({ 0x07, 0x80 }, 0, true, 0x7lu, 1lu);
57 testUInt32LEBDecode({ 0x07, 0x75 }, 0, true, 0x7lu, 1lu);
58 testUInt32LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x43 }, 0, true, 0xe9fc2f3lu, 4lu);
59 testUInt32LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x80 }, 0, true, 0xe9fc2f3lu, 4lu);
60 // Test with preceeding numbers
61 testUInt32LEBDecode({ 0xf3, 0x07 }, 1, true, 0x7lu, 2lu);
62 testUInt32LEBDecode({ 0x03, 0x07 }, 1, true, 0x7lu, 2lu);
63 testUInt32LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77 }, 5, true, 0x77lu, 6lu);
64 testUInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77 }, 5, true, 0x77lu, 6ul);
65 testUInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02 }, 3, true, 0x82f3lu, 6lu);
66 // Test in the middle
67 testUInt32LEBDecode({ 0xf3, 0x07, 0x89 }, 1, true, 0x7lu, 2lu);
68 testUInt32LEBDecode({ 0x03, 0x07, 0x23 }, 1, true, 0x7lu, 2lu);
69 testUInt32LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77, 0x43 }, 5, true, 0x77lu, 6lu);
70 testUInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77, 0xf9 }, 5, true, 0x77lu, 6lu);
71 testUInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02, 0xa4 }, 3, true, 0x82f3lu, 6lu);
72 // Test decode too long
73 testUInt32LEBDecode({ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }, 0, false, 0x0lu, 0lu);
74 testUInt32LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 1, false, 0x0lu, 0lu);
75 testUInt32LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 0, false, 0x0lu, 0lu);
76 // Test decode off end of array
77 testUInt32LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 2, false, 0x0lu, 0lu);
78}
79
80static void testUInt64LEBDecode(std::initializer_list<uint8_t> data, size_t startOffset, bool expectedStatus, uint64_t expectedResult, size_t expectedOffset)
81{
82 Vector<uint8_t> vector(data);
83 uint64_t result;
84 bool status = WTF::LEBDecoder::decodeUInt64(vector.data(), vector.size(), startOffset, result);
85 EXPECT_EQ(expectedStatus, status);
86 if (expectedStatus) {
87 EXPECT_EQ(expectedResult, result);
88 EXPECT_EQ(expectedOffset, startOffset);
89 }
90}
91
92TEST(WTF, LEBDecoderUInt64)
93{
94 // Simple tests that use all the bits in the array
95 testUInt64LEBDecode({ 0x07 }, 0, true, 0x7lu, 1lu);
96 testUInt64LEBDecode({ 0x77 }, 0, true, 0x77lu, 1lu);
97 testUInt64LEBDecode({ 0x80, 0x07 }, 0, true, 0x380lu, 2lu);
98 testUInt64LEBDecode({ 0x89, 0x12 }, 0, true, 0x909lu, 2lu);
99 testUInt64LEBDecode({ 0xf3, 0x85, 0x02 }, 0, true, 0x82f3lu, 3lu);
100 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0x74 }, 0, true, 0xe9fc2f3lu, 4lu);
101 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x7f }, 0, true, 0x7fe9fc2f3lu, 5lu);
102 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0x4b }, 0, true, 0x25ffe9fc2f3lu, 6lu);
103 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0x3a }, 0, true, 0xea5ffe9fc2f3lu, 7lu);
104 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x0f }, 0, true, 0x1eea5ffe9fc2f3lu, 8lu);
105 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0x69 }, 0, true, 0x691eea5ffe9fc2f3lu, 9lu);
106 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0xe9, 0x01 }, 0, true, 0xe91eea5ffe9fc2f3lu, 10lu);
107 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0xe9, 0x70 }, 0, true, 0x691eea5ffe9fc2f3lu, 10lu);
108 // Test with extra trailing numbers
109 testUInt64LEBDecode({ 0x07, 0x80 }, 0, true, 0x7lu, 1lu);
110 testUInt64LEBDecode({ 0x07, 0x75 }, 0, true, 0x7lu, 1lu);
111 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x43 }, 0, true, 0xe9fc2f3lu, 4lu);
112 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x80 }, 0, true, 0xe9fc2f3lu, 4lu);
113 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0x69, 0x45 }, 0, true, 0x691eea5ffe9fc2f3lu, 9lu);
114 // Test with preceeding numbers
115 testUInt64LEBDecode({ 0xf3, 0x07 }, 1, true, 0x7lu, 2lu);
116 testUInt64LEBDecode({ 0x03, 0x07 }, 1, true, 0x7lu, 2lu);
117 testUInt64LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77 }, 5, true, 0x77lu, 6lu);
118 testUInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77 }, 5, true, 0x77lu, 6ul);
119 testUInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02 }, 3, true, 0x82f3lu, 6lu);
120 testUInt64LEBDecode({ 0x92, 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0x69 }, 1, true, 0x691eea5ffe9fc2f3lu, 10lu);
121 // Test in the middle
122 testUInt64LEBDecode({ 0xf3, 0x07, 0x89 }, 1, true, 0x7lu, 2lu);
123 testUInt64LEBDecode({ 0x03, 0x07, 0x23 }, 1, true, 0x7lu, 2lu);
124 testUInt64LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77, 0x43 }, 5, true, 0x77lu, 6lu);
125 testUInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77, 0xf9 }, 5, true, 0x77lu, 6lu);
126 testUInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02, 0xa4 }, 3, true, 0x82f3lu, 6lu);
127 testUInt64LEBDecode({ 0x92, 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0x69, 0x85, 0x75 }, 1, true, 0x691eea5ffe9fc2f3lu, 10lu);
128 testUInt64LEBDecode({ 0x92, 0x65, 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0x69, 0x85, 0x75 }, 2, true, 0x691eea5ffe9fc2f3lu, 11lu);
129 // Test decode too long
130 testUInt64LEBDecode({ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }, 0, false, 0x0lu, 0lu);
131 testUInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xa3, 0x9f, 0xd2, 0xef, 0x8a, 0x4e }, 1, false, 0x0lu, 0lu);
132 testUInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff, 0xef, 0xd8, 0xee, 0xaa, 0xbb }, 0, false, 0x0lu, 0lu);
133 testUInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f, 0xa9, 0xa8, 0x05 }, 0, false, 0x0lu, 0lu);
134 // Test decode off end of array
135 testUInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 2, false, 0x0lu, 0lu);
136 testUInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 2, false, 0x0lu, 0lu);
137 testUInt64LEBDecode({ 0x92, 0xf3, 0x85, 0xff, 0xf4, 0xff, 0xcb, 0xba, 0x8f }, 1, false, 0x0lu, 0lu);
138}
139
140static void testInt32LEBDecode(std::initializer_list<uint8_t> data, size_t startOffset, bool expectedStatus, int32_t expectedResult, size_t expectedOffset)
141{
142 Vector<uint8_t> vector(data);
143 int32_t result;
144 bool status = WTF::LEBDecoder::decodeInt32(vector.data(), vector.size(), startOffset, result);
145 EXPECT_EQ(expectedStatus, status);
146 if (expectedStatus) {
147 EXPECT_EQ(expectedResult, result);
148 EXPECT_EQ(expectedOffset, startOffset);
149 }
150}
151
152TEST(WTF, LEBDecoderInt32)
153{
154 // Simple tests that use all the bits in the array
155 testInt32LEBDecode({ 0x07 }, 0, true, 0x7, 1lu);
156 testInt32LEBDecode({ 0x77 }, 0, true, -0x9, 1lu);
157 testInt32LEBDecode({ 0x80, 0x07 }, 0, true, 0x380, 2lu);
158 testInt32LEBDecode({ 0x89, 0x12 }, 0, true, 0x909, 2lu);
159 testInt32LEBDecode({ 0xf3, 0x85, 0x02 }, 0, true, 0x82f3, 3lu);
160 testInt32LEBDecode({ 0xf3, 0x85, 0xff, 0x74 }, 0, true, 0xfe9fc2f3, 4lu);
161 testInt32LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x7f }, 0, true, 0xfe9fc2f3, 5lu);
162 // Test with extra trailing numbers
163 testInt32LEBDecode({ 0x07, 0x80 }, 0, true, 0x7, 1lu);
164 testInt32LEBDecode({ 0x07, 0x75 }, 0, true, 0x7, 1lu);
165 testInt32LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x43 }, 0, true, 0xfe9fc2f3, 4lu);
166 testInt32LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x80 }, 0, true, 0xfe9fc2f3, 4lu);
167 // Test with preceeding numbers
168 testInt32LEBDecode({ 0xf3, 0x07 }, 1, true, 0x7, 2lu);
169 testInt32LEBDecode({ 0x03, 0x07 }, 1, true, 0x7, 2lu);
170 testInt32LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77 }, 5, true, -0x9, 6lu);
171 testInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77 }, 5, true, -0x9, 6lu);
172 testInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02 }, 3, true, 0x82f3, 6lu);
173 // Test in the middle
174 testInt32LEBDecode({ 0xf3, 0x07, 0x89 }, 1, true, 0x7, 2lu);
175 testInt32LEBDecode({ 0x03, 0x07, 0x23 }, 1, true, 0x7, 2lu);
176 testInt32LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77, 0x43 }, 5, true, -0x9, 6lu);
177 testInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77, 0xf9 }, 5, true, -0x9, 6lu);
178 testInt32LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02, 0xa4 }, 3, true, 0x82f3, 6lu);
179 // Test decode too long
180 testInt32LEBDecode({ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }, 0, false, 0x0, 0lu);
181 testInt32LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 1, false, 0x0, 0lu);
182 testInt32LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 0, false, 0x0, 0lu);
183 // Test decode off end of array
184 testInt32LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 2, false, 0x0, 0lu);
185}
186
187static void testInt64LEBDecode(std::initializer_list<uint8_t> data, size_t startOffset, bool expectedStatus, int64_t expectedResult, size_t expectedOffset)
188{
189 Vector<uint8_t> vector(data);
190 int64_t result;
191 bool status = WTF::LEBDecoder::decodeInt64(vector.data(), vector.size(), startOffset, result);
192 EXPECT_EQ(expectedStatus, status);
193 if (expectedStatus) {
194 EXPECT_EQ(expectedResult, result);
195 EXPECT_EQ(expectedOffset, startOffset);
196 }
197}
198
199TEST(WTF, LEBDecoderInt64)
200{
201 // Simple tests that use all the bits in the array
202 testInt64LEBDecode({ 0x07 }, 0, true, 0x7, 1lu);
203 testInt64LEBDecode({ 0x77 }, 0, true, -0x9, 1lu);
204 testInt64LEBDecode({ 0x80, 0x07 }, 0, true, 0x380, 2lu);
205 testInt64LEBDecode({ 0x89, 0x12 }, 0, true, 0x909, 2lu);
206 testInt64LEBDecode({ 0xf3, 0x85, 0x02 }, 0, true, 0x82f3, 3lu);
207 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0x74 }, 0, true, 0xfffffffffe9fc2f3, 4lu);
208 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x7f }, 0, true, 0xfffffffffe9fc2f3, 5lu);
209 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x3f }, 0, true, 0x3fe9fc2f3, 5lu);
210 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x8f, 0x1a }, 0, true, 0xd0fe9fc2f3, 6lu);
211 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x8f, 0x9a, 0x80, 0x2a }, 0, true, 0x5400d0fe9fc2f3, 8lu);
212 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x8f, 0x9a, 0x80, 0xaa, 0x41 }, 0, true, 0xc15400d0fe9fc2f3, 9lu);
213 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x8f, 0x9a, 0x80, 0xaa, 0xc1, 0x01 }, 0, true, 0xc15400d0fe9fc2f3, 10lu);
214 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0xf4, 0x8f, 0x9a, 0x80, 0xaa, 0xc1, 0x62 }, 0, true, 0x415400d0fe9fc2f3, 10lu);
215 // Test with extra trailing numbers
216 testInt64LEBDecode({ 0x07, 0x80 }, 0, true, 0x7, 1lu);
217 testInt64LEBDecode({ 0x07, 0x75 }, 0, true, 0x7, 1lu);
218 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x43 }, 0, true, 0xfffffffffe9fc2f3, 4lu);
219 testInt64LEBDecode({ 0xf3, 0x85, 0xff, 0x74, 0x80 }, 0, true, 0xfffffffffe9fc2f3, 4lu);
220 // Test with preceeding numbers
221 testInt64LEBDecode({ 0xf3, 0x07 }, 1, true, 0x7, 2lu);
222 testInt64LEBDecode({ 0x03, 0x07 }, 1, true, 0x7, 2lu);
223 testInt64LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77 }, 5, true, -0x9, 6lu);
224 testInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77 }, 5, true, -0x9, 6lu);
225 testInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02 }, 3, true, 0x82f3, 6lu);
226 // Test in the middle
227 testInt64LEBDecode({ 0xf3, 0x07, 0x89 }, 1, true, 0x7, 2lu);
228 testInt64LEBDecode({ 0x03, 0x07, 0x23 }, 1, true, 0x7, 2lu);
229 testInt64LEBDecode({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77, 0x43 }, 5, true, -0x9, 6lu);
230 testInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77, 0xf9 }, 5, true, -0x9, 6lu);
231 testInt64LEBDecode({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02, 0xa4 }, 3, true, 0x82f3, 6lu);
232 // Test decode too long
233 testInt64LEBDecode({ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }, 0, false, 0x0, 0lu);
234 testInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 1, false, 0x0, 0lu);
235 testInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 0, false, 0x0, 0lu);
236 // Test decode off end of array
237 testInt64LEBDecode({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff }, 2, false, 0x0, 0lu);
238}
239
240
241} // namespace TestWebKitAPI
242