1 /*
2 * Copyright (C) 2010 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 #include "IDBKeyPath.h"
28
29 #include <gtest/gtest.h>
30 #include <wtf/Vector.h>
31
32 #if ENABLE(INDEXED_DATABASE)
33
34 using namespace WebCore;
35
36 namespace {
37
ExpectedToken(const String & identifier,bool isIndexed,int index)38 IDBKeyPathElement ExpectedToken(const String& identifier, bool isIndexed, int index)
39 {
40 IDBKeyPathElement expected;
41 if (isIndexed) {
42 expected.type = IDBKeyPathElement::IsIndexed;
43 expected.index = index;
44 } else {
45 expected.type = IDBKeyPathElement::IsNamed;
46 expected.identifier = identifier;
47 }
48 return expected;
49 }
50
checkKeyPath(const String & keyPath,const Vector<IDBKeyPathElement> & expected,int parserError)51 void checkKeyPath(const String& keyPath, const Vector<IDBKeyPathElement>& expected, int parserError)
52 {
53
54 IDBKeyPathParseError error;
55 Vector<IDBKeyPathElement> idbKeyPathElements;
56 IDBParseKeyPath(keyPath, idbKeyPathElements, error);
57 ASSERT_EQ(parserError, error);
58 if (error != IDBKeyPathParseErrorNone)
59 return;
60 ASSERT_EQ(expected.size(), idbKeyPathElements.size());
61 for (size_t i = 0; i < expected.size(); ++i) {
62 ASSERT_TRUE(expected[i].type == idbKeyPathElements[i].type) << i;
63 if (expected[i].type == IDBKeyPathElement::IsIndexed)
64 ASSERT_EQ(expected[i].index, idbKeyPathElements[i].index) << i;
65 else if (expected[i].type == IDBKeyPathElement::IsNamed)
66 ASSERT_TRUE(expected[i].identifier == idbKeyPathElements[i].identifier) << i;
67 else
68 ASSERT_TRUE(false) << "Invalid IDBKeyPathElement type";
69 }
70 }
71
TEST(IDBKeyPathTest,ValidKeyPath0)72 TEST(IDBKeyPathTest, ValidKeyPath0)
73 {
74 Vector<IDBKeyPathElement> expected;
75 String keyPath("foo.bar.zoo");
76 expected.append(ExpectedToken("foo", false, 0));
77 expected.append(ExpectedToken("bar", false, 0));
78 expected.append(ExpectedToken("zoo", false, 0));
79 checkKeyPath(keyPath, expected, 0);
80 }
81
TEST(IDBKeyPathTest,ValidKeyPath1)82 TEST(IDBKeyPathTest, ValidKeyPath1)
83 {
84 Vector<IDBKeyPathElement> expected;
85 String keyPath("a[34][20].foo[2].bar");
86 expected.append(ExpectedToken("a", false, 0));
87 expected.append(ExpectedToken(String(), true, 34));
88 expected.append(ExpectedToken(String(), true, 20));
89 expected.append(ExpectedToken("foo", false, 0));
90 expected.append(ExpectedToken(String(), true, 2));
91 expected.append(ExpectedToken("bar", false, 0));
92 checkKeyPath(keyPath, expected, 0);
93 }
94
TEST(IDBKeyPathTest,ValidKeyPath2)95 TEST(IDBKeyPathTest, ValidKeyPath2)
96 {
97 Vector<IDBKeyPathElement> expected;
98 String keyPath("foo[ 34 ].Zoo_[00023]\t._c");
99 expected.append(ExpectedToken("foo", false, 0));
100 expected.append(ExpectedToken(String(), true, 34));
101 expected.append(ExpectedToken("Zoo_", false, 0));
102 expected.append(ExpectedToken(String(), true, 23));
103 expected.append(ExpectedToken("_c", false, 0));
104 checkKeyPath(keyPath, expected, 0);
105 }
106
TEST(IDBKeyPathTest,ValidKeyPath3)107 TEST(IDBKeyPathTest, ValidKeyPath3)
108 {
109 Vector<IDBKeyPathElement> expected;
110 String keyPath("foo[ 34 ]");
111 expected.append(ExpectedToken("foo", false, 0));
112 expected.append(ExpectedToken(String(), true, 34));
113 checkKeyPath(keyPath, expected, 0);
114 }
115
TEST(IDBKeyPathTest,ValidKeyPath4)116 TEST(IDBKeyPathTest, ValidKeyPath4)
117 {
118 Vector<IDBKeyPathElement> expected;
119 String keyPath("[ 34 ]");
120 expected.append(ExpectedToken(String(), true, 34));
121 checkKeyPath(keyPath, expected, 0);
122 }
123
TEST(IDBKeyPathTest,InvalidKeyPath2)124 TEST(IDBKeyPathTest, InvalidKeyPath2)
125 {
126 Vector<IDBKeyPathElement> expected;
127 String keyPath("a[[34]].b[2].c");
128 expected.append(ExpectedToken("a", false, 0));
129 checkKeyPath(keyPath, expected, 3);
130 }
131
TEST(IDBKeyPathTest,InvalidKeyPath3)132 TEST(IDBKeyPathTest, InvalidKeyPath3)
133 {
134 Vector<IDBKeyPathElement> expected;
135 String keyPath("a[[34].b[2].c");
136 expected.append(ExpectedToken("a", false, 0));
137 checkKeyPath(keyPath, expected, 3);
138 }
139
TEST(IDBKeyPathTest,InvalidKeyPath5)140 TEST(IDBKeyPathTest, InvalidKeyPath5)
141 {
142 Vector<IDBKeyPathElement> expected;
143 String keyPath("a[[34.b[2].c");
144 expected.append(ExpectedToken("a", false, 0));
145 checkKeyPath(keyPath, expected, 3);
146 }
147
TEST(IDBKeyPathTest,InvalidKeyPath6)148 TEST(IDBKeyPathTest, InvalidKeyPath6)
149 {
150 Vector<IDBKeyPathElement> expected;
151 String keyPath("+a[34].b[2].c");
152 checkKeyPath(keyPath, expected, 1);
153 }
154
TEST(IDBKeyPathTest,InvalidKeyPath7)155 TEST(IDBKeyPathTest, InvalidKeyPath7)
156 {
157 Vector<IDBKeyPathElement> expected;
158 String keyPath("%a[34].b[2].c");
159 checkKeyPath(keyPath, expected, 1);
160 }
161
TEST(IDBKeyPathTest,InvalidKeyPath8)162 TEST(IDBKeyPathTest, InvalidKeyPath8)
163 {
164 Vector<IDBKeyPathElement> expected;
165 String keyPath("a{[34]}.b[2].c");
166 expected.append(ExpectedToken("a", false, 0));
167 checkKeyPath(keyPath, expected, 2);
168 }
169
TEST(IDBKeyPathTest,InvalidKeyPath9)170 TEST(IDBKeyPathTest, InvalidKeyPath9)
171 {
172 Vector<IDBKeyPathElement> expected;
173 String keyPath("a..b[2].c");
174 expected.append(ExpectedToken("a", false, 0));
175 checkKeyPath(keyPath, expected, 5);
176 }
177
TEST(IDBKeyPathTest,InvalidKeyPath10)178 TEST(IDBKeyPathTest, InvalidKeyPath10)
179 {
180 Vector<IDBKeyPathElement> expected;
181 String keyPath("a[34]b.foo[2].bar");
182 expected.append(ExpectedToken("a", false, 0));
183 expected.append(ExpectedToken(String(), true, 34));
184 checkKeyPath(keyPath, expected, 4);
185 }
186
TEST(IDBKeyPathTest,InvalidKeyPath11)187 TEST(IDBKeyPathTest, InvalidKeyPath11)
188 {
189 Vector<IDBKeyPathElement> expected;
190 String keyPath("a[-1]");
191 expected.append(ExpectedToken("a", false, 0));
192 checkKeyPath(keyPath, expected, 3);
193 }
194
TEST(IDBKeyPathTest,InvalidKeyPath12)195 TEST(IDBKeyPathTest, InvalidKeyPath12)
196 {
197 Vector<IDBKeyPathElement> expected;
198 String keyPath("a[9999999999999999999999999999999999]");
199 expected.append(ExpectedToken("a", false, 0));
200 checkKeyPath(keyPath, expected, 3);
201 }
202
203 } // namespace
204
205 #endif // ENABLE(INDEXED_DATABASE)
206