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
34using namespace WebCore;
35
36namespace {
37
38IDBKeyPathElement 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
51void 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
72TEST(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
82TEST(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
95TEST(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
107TEST(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
116TEST(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
124TEST(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
132TEST(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
140TEST(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
148TEST(IDBKeyPathTest, InvalidKeyPath6)
149{
150    Vector<IDBKeyPathElement> expected;
151    String keyPath("+a[34].b[2].c");
152    checkKeyPath(keyPath, expected, 1);
153}
154
155TEST(IDBKeyPathTest, InvalidKeyPath7)
156{
157    Vector<IDBKeyPathElement> expected;
158    String keyPath("%a[34].b[2].c");
159    checkKeyPath(keyPath, expected, 1);
160}
161
162TEST(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
170TEST(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
178TEST(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
187TEST(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
195TEST(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