1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/profile_resetter/jtl_interpreter.h"
9 #include "base/strings/string_util.h"
10 #include "base/test/values_test_util.h"
11 #include "chrome/browser/profile_resetter/jtl_foundation.h"
12 #include "chrome/browser/profile_resetter/jtl_instructions.h"
13 #include "crypto/hmac.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 const char seed
[] = "foobar";
20 #define KEY_HASH_1 GetHash("KEY_HASH_1")
21 #define KEY_HASH_2 GetHash("KEY_HASH_2")
22 #define KEY_HASH_3 GetHash("KEY_HASH_3")
23 #define KEY_HASH_4 GetHash("KEY_HASH_4")
25 #define VALUE_HASH_1 GetHash("VALUE_HASH_1")
26 #define VALUE_HASH_2 GetHash("VALUE_HASH_2")
28 #define VAR_HASH_1 "01234567890123456789012345678901"
29 #define VAR_HASH_2 "12345678901234567890123456789012"
31 std::string
GetHash(const std::string
& input
) {
32 return jtl_foundation::Hasher(seed
).GetHash(input
);
35 std::string
EncodeUint32(uint32 value
) {
37 for (int i
= 0; i
< 4; ++i
) {
38 bytecode
.push_back(static_cast<char>(value
& 0xFFu
));
44 // escaped_json_param may contain ' characters that are replaced with ". This
45 // makes the code more readable because we need less escaping.
46 #define INIT_INTERPRETER(program_param, escaped_json_param) \
47 const char* escaped_json = escaped_json_param; \
49 base::ReplaceChars(escaped_json, "'", "\"", &json); \
50 scoped_ptr<base::Value> json_value(ParseJson(json)); \
51 JtlInterpreter interpreter( \
54 static_cast<const base::DictionaryValue*>(json_value.get())); \
57 using base::test::ParseJson
;
59 TEST(JtlInterpreter
, Store
) {
61 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
62 "{ 'KEY_HASH_1': 'VALUE_HASH_1' }");
63 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
64 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
67 TEST(JtlInterpreter
, NavigateAndStore
) {
69 OP_NAVIGATE(KEY_HASH_1
) +
70 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
71 "{ 'KEY_HASH_1': 'VALUE_HASH_1' }");
72 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
73 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
76 TEST(JtlInterpreter
, FailNavigate
) {
78 OP_NAVIGATE(KEY_HASH_2
) +
79 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
80 "{ 'KEY_HASH_1': 'VALUE_HASH_1' }");
81 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
82 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
85 TEST(JtlInterpreter
, ConsecutiveNavigate
) {
87 OP_NAVIGATE(KEY_HASH_1
) +
88 OP_NAVIGATE(KEY_HASH_2
) +
89 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
90 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
91 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
92 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
95 TEST(JtlInterpreter
, FailConsecutiveNavigate
) {
97 OP_NAVIGATE(KEY_HASH_1
) +
98 OP_NAVIGATE(KEY_HASH_2
) +
99 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
100 "{ 'KEY_HASH_1': 'foo' }");
101 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
102 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
105 TEST(JtlInterpreter
, NavigateAnyInDictionary
) {
107 OP_NAVIGATE(KEY_HASH_1
) +
109 OP_NAVIGATE(KEY_HASH_4
) +
110 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
112 " { 'KEY_HASH_2': {'KEY_HASH_3': 'VALUE_HASH_1' },"
113 " 'KEY_HASH_3': {'KEY_HASH_4': 'VALUE_HASH_1' }"
115 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
116 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
119 TEST(JtlInterpreter
, NavigateAnyInList
) {
121 OP_NAVIGATE(KEY_HASH_1
) +
123 OP_NAVIGATE(KEY_HASH_4
) +
124 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
126 " [ {'KEY_HASH_3': 'VALUE_HASH_1' },"
127 " {'KEY_HASH_4': 'VALUE_HASH_1' }"
129 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
130 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
133 TEST(JtlInterpreter
, NavigateBack
) {
135 OP_NAVIGATE(KEY_HASH_1
) +
136 OP_NAVIGATE(KEY_HASH_2
) +
138 OP_NAVIGATE(KEY_HASH_3
) +
139 OP_NAVIGATE(KEY_HASH_4
) +
140 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
142 " { 'KEY_HASH_2': {'KEY_HASH_3': 'VALUE_HASH_1' },"
143 " 'KEY_HASH_3': {'KEY_HASH_4': 'VALUE_HASH_1' }"
145 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
146 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
149 TEST(JtlInterpreter
, StoreTwoValues
) {
151 OP_NAVIGATE(KEY_HASH_1
) +
152 OP_NAVIGATE(KEY_HASH_2
) +
153 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
) +
154 OP_STORE_HASH(VAR_HASH_2
, VALUE_HASH_1
),
155 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
156 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
157 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
158 base::ExpectDictStringValue(VALUE_HASH_1
, *interpreter
.working_memory(),
162 TEST(JtlInterpreter
, CompareStoredMatch
) {
164 OP_NAVIGATE(KEY_HASH_1
) +
165 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
) +
166 OP_NAVIGATE(KEY_HASH_2
) +
167 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, VALUE_TRUE
, VALUE_FALSE
) +
168 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
169 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
170 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
171 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
172 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_2
);
175 TEST(JtlInterpreter
, CompareStoredMismatch
) {
177 OP_NAVIGATE(KEY_HASH_1
) +
178 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
) +
179 OP_NAVIGATE(KEY_HASH_2
) +
180 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, VALUE_FALSE
, VALUE_TRUE
) +
181 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
182 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
183 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
184 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
185 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
188 TEST(JtlInterpreter
, CompareStoredNoValueMatchingDefault
) {
190 OP_NAVIGATE(KEY_HASH_1
) +
191 OP_NAVIGATE(KEY_HASH_2
) +
192 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, VALUE_TRUE
, VALUE_TRUE
) +
193 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
194 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
195 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
196 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_2
);
199 TEST(JtlInterpreter
, CompareStoredNoValueMismatchingDefault
) {
201 OP_NAVIGATE(KEY_HASH_1
) +
202 OP_NAVIGATE(KEY_HASH_2
) +
203 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, VALUE_TRUE
, VALUE_FALSE
) +
204 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
205 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
206 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
207 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
210 TEST(JtlInterpreter
, CompareBool
) {
212 std::string expected_value
;
214 bool expected_success
;
216 { VALUE_TRUE
, "{ 'KEY_HASH_1': true }", true },
217 { VALUE_FALSE
, "{ 'KEY_HASH_1': false }", true },
218 { VALUE_TRUE
, "{ 'KEY_HASH_1': false }", false },
219 { VALUE_TRUE
, "{ 'KEY_HASH_1': 'abc' }", false },
220 { VALUE_TRUE
, "{ 'KEY_HASH_1': 1 }", false },
221 { VALUE_TRUE
, "{ 'KEY_HASH_1': 1.2 }", false },
222 { VALUE_TRUE
, "{ 'KEY_HASH_1': [1] }", false },
223 { VALUE_TRUE
, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
226 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
227 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
229 OP_NAVIGATE(KEY_HASH_1
) +
230 OP_COMPARE_NODE_BOOL(cases
[i
].expected_value
) +
231 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
233 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
234 if (cases
[i
].expected_success
) {
235 base::ExpectDictBooleanValue(
236 true, *interpreter
.working_memory(), VAR_HASH_1
);
238 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
243 TEST(JtlInterpreter
, CompareHashString
) {
245 std::string expected_value
;
247 bool expected_success
;
249 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", true },
250 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 'VALUE_HASH_2' }", false },
251 { VALUE_HASH_1
, "{ 'KEY_HASH_1': true }", false },
252 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 1 }", false },
253 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 1.1 }", false },
254 { VALUE_HASH_1
, "{ 'KEY_HASH_1': [1] }", false },
255 { VALUE_HASH_1
, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
257 { GetHash("1.2"), "{ 'KEY_HASH_1': 1.2 }", true },
258 { GetHash("1.2"), "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
259 { GetHash("1.2"), "{ 'KEY_HASH_1': true }", false },
260 { GetHash("1.2"), "{ 'KEY_HASH_1': 1 }", false },
261 { GetHash("1.2"), "{ 'KEY_HASH_1': 1.3 }", false },
262 { GetHash("1.2"), "{ 'KEY_HASH_1': [1] }", false },
263 { GetHash("1.2"), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
265 { GetHash("1"), "{ 'KEY_HASH_1': 1 }", true },
266 { GetHash("1"), "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
267 { GetHash("1"), "{ 'KEY_HASH_1': true }", false },
268 { GetHash("1"), "{ 'KEY_HASH_1': 2 }", false },
269 { GetHash("1"), "{ 'KEY_HASH_1': 1.1 }", false },
270 { GetHash("1"), "{ 'KEY_HASH_1': [1] }", false },
271 { GetHash("1"), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
274 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
275 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
277 OP_NAVIGATE(KEY_HASH_1
) +
278 OP_COMPARE_NODE_HASH(cases
[i
].expected_value
) +
279 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
281 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
282 if (cases
[i
].expected_success
) {
283 base::ExpectDictBooleanValue(
284 true, *interpreter
.working_memory(), VAR_HASH_1
);
286 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
290 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
291 SCOPED_TRACE(testing::Message() << "Negated, Iteration " << i
);
293 OP_NAVIGATE(KEY_HASH_1
) +
294 OP_COMPARE_NODE_HASH_NOT(cases
[i
].expected_value
) +
295 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
297 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
298 if (!cases
[i
].expected_success
) {
299 base::ExpectDictBooleanValue(
300 true, *interpreter
.working_memory(), VAR_HASH_1
);
302 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
307 TEST(JtlInterpreter
, StoreNodeBool
) {
311 bool expected_success
;
313 { true, "{ 'KEY_HASH_1': true }", true },
314 { false, "{ 'KEY_HASH_1': false }", true },
315 { false, "{ 'KEY_HASH_1': 'abc' }", false },
316 { false, "{ 'KEY_HASH_1': 1 }", false },
317 { false, "{ 'KEY_HASH_1': 1.2 }", false },
318 { false, "{ 'KEY_HASH_1': [1] }", false },
319 { false, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
322 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
323 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
325 OP_NAVIGATE(KEY_HASH_1
) +
326 OP_STORE_NODE_BOOL(VAR_HASH_1
) +
327 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
329 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
330 if (cases
[i
].expected_success
) {
331 base::ExpectDictBooleanValue(
332 cases
[i
].expected_value
, *interpreter
.working_memory(), VAR_HASH_1
);
333 base::ExpectDictBooleanValue(
334 true, *interpreter
.working_memory(), VAR_HASH_2
);
336 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
337 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
342 TEST(JtlInterpreter
, CompareNodeToStoredBool
) {
344 std::string stored_value
;
346 bool expected_success
;
348 { VALUE_TRUE
, "{ 'KEY_HASH_1': true }", true },
349 { VALUE_FALSE
, "{ 'KEY_HASH_1': false }", true },
350 { VALUE_FALSE
, "{ 'KEY_HASH_1': true }", false },
351 { std::string(), "{ 'KEY_HASH_1': true }", false },
353 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
354 { GetHash("1"), "{ 'KEY_HASH_1': 1 }", false },
355 { GetHash("1.2"), "{ 'KEY_HASH_1': 1.2 }", false },
357 { VALUE_HASH_1
, "{ 'KEY_HASH_1': true }", false },
358 { GetHash("1"), "{ 'KEY_HASH_1': true }", false },
359 { GetHash("1.2"), "{ 'KEY_HASH_1': true }", false },
361 { VALUE_TRUE
, "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
362 { VALUE_TRUE
, "{ 'KEY_HASH_1': 1 }", false },
363 { VALUE_TRUE
, "{ 'KEY_HASH_1': 1.2 }", false },
364 { VALUE_TRUE
, "{ 'KEY_HASH_1': [1] }", false },
365 { VALUE_TRUE
, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
368 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
369 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
370 std::string store_op
;
371 if (cases
[i
].stored_value
== VALUE_TRUE
||
372 cases
[i
].stored_value
== VALUE_FALSE
)
373 store_op
= OP_STORE_BOOL(VAR_HASH_1
, cases
[i
].stored_value
);
374 else if (!cases
[i
].stored_value
.empty())
375 store_op
= OP_STORE_HASH(VAR_HASH_1
, cases
[i
].stored_value
);
378 OP_NAVIGATE(KEY_HASH_1
) +
379 OP_COMPARE_NODE_TO_STORED_BOOL(VAR_HASH_1
) +
380 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
382 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
383 if (cases
[i
].expected_success
) {
384 base::ExpectDictBooleanValue(
385 true, *interpreter
.working_memory(), VAR_HASH_2
);
387 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
392 TEST(JtlInterpreter
, StoreNodeHash
) {
394 std::string expected_value
;
396 bool expected_success
;
398 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", true },
399 { VALUE_HASH_2
, "{ 'KEY_HASH_1': 'VALUE_HASH_2' }", true },
400 { GetHash("1"), "{ 'KEY_HASH_1': 1 }", true },
401 { GetHash("1.2"), "{ 'KEY_HASH_1': 1.2 }", true },
402 { std::string(), "{ 'KEY_HASH_1': true }", false },
403 { std::string(), "{ 'KEY_HASH_1': [1] }", false },
404 { std::string(), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
407 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
408 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
410 OP_NAVIGATE(KEY_HASH_1
) +
411 OP_STORE_NODE_HASH(VAR_HASH_1
) +
412 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
414 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
415 if (cases
[i
].expected_success
) {
416 base::ExpectDictStringValue(
417 cases
[i
].expected_value
, *interpreter
.working_memory(), VAR_HASH_1
);
418 base::ExpectDictBooleanValue(
419 true, *interpreter
.working_memory(), VAR_HASH_2
);
421 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
422 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
427 TEST(JtlInterpreter
, CompareNodeToStoredHash
) {
429 std::string stored_value
;
431 bool expected_success
;
433 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", true },
434 { VALUE_HASH_2
, "{ 'KEY_HASH_1': 'VALUE_HASH_2' }", true },
435 { std::string(), "{ 'KEY_HASH_1': 'VALUE_HASH_2' }", false },
436 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 'VALUE_HASH_2' }", false },
437 { VALUE_HASH_1
, "{ 'KEY_HASH_1': true }", false },
438 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 1 }", false },
439 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 1.1 }", false },
440 { VALUE_HASH_1
, "{ 'KEY_HASH_1': [1] }", false },
441 { VALUE_HASH_1
, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
443 { GetHash("1.2"), "{ 'KEY_HASH_1': 1.2 }", true },
444 { GetHash("1.3"), "{ 'KEY_HASH_1': 1.3 }", true },
445 { std::string(), "{ 'KEY_HASH_1': 1.2 }", false },
446 { GetHash("1.2"), "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
447 { GetHash("1.2"), "{ 'KEY_HASH_1': true }", false },
448 { GetHash("1.2"), "{ 'KEY_HASH_1': 1 }", false },
449 { GetHash("1.2"), "{ 'KEY_HASH_1': 1.3 }", false },
450 { GetHash("1.2"), "{ 'KEY_HASH_1': [1] }", false },
451 { GetHash("1.2"), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
453 { GetHash("1"), "{ 'KEY_HASH_1': 1 }", true },
454 { GetHash("2"), "{ 'KEY_HASH_1': 2 }", true },
455 { std::string(), "{ 'KEY_HASH_1': 2 }", false },
456 { GetHash("1"), "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
457 { GetHash("1"), "{ 'KEY_HASH_1': true }", false },
458 { GetHash("1"), "{ 'KEY_HASH_1': 2 }", false },
459 { GetHash("1"), "{ 'KEY_HASH_1': 1.1 }", false },
460 { GetHash("1"), "{ 'KEY_HASH_1': [1] }", false },
461 { GetHash("1"), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
463 { VALUE_TRUE
, "{ 'KEY_HASH_1': 'VALUE_HASH_1' }", false },
464 { VALUE_TRUE
, "{ 'KEY_HASH_1': 1 }", false },
465 { VALUE_TRUE
, "{ 'KEY_HASH_1': 1.3 }", false },
466 { VALUE_TRUE
, "{ 'KEY_HASH_1': [1] }", false },
467 { VALUE_TRUE
, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
469 { VALUE_TRUE
, "{ 'KEY_HASH_1': true }", false },
470 { VALUE_FALSE
, "{ 'KEY_HASH_1': false }", false },
473 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
474 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
475 std::string store_op
;
476 if (cases
[i
].stored_value
== VALUE_TRUE
||
477 cases
[i
].stored_value
== VALUE_FALSE
)
478 store_op
= OP_STORE_BOOL(VAR_HASH_1
, cases
[i
].stored_value
);
479 else if (!cases
[i
].stored_value
.empty())
480 store_op
= OP_STORE_HASH(VAR_HASH_1
, cases
[i
].stored_value
);
483 OP_NAVIGATE(KEY_HASH_1
) +
484 OP_COMPARE_NODE_TO_STORED_HASH(VAR_HASH_1
) +
485 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
487 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
488 if (cases
[i
].expected_success
) {
489 base::ExpectDictBooleanValue(
490 true, *interpreter
.working_memory(), VAR_HASH_2
);
492 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
497 TEST(JtlInterpreter
, CompareSubstring
) {
501 bool expected_success
;
503 { "abc", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", true },
504 { "xyz", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", true },
505 { "m", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", true },
506 { "abc", "{ 'KEY_HASH_1': 'abc' }", true },
507 { "cba", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", false },
508 { "acd", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", false },
509 { "waaaaaaay_too_long", "{ 'KEY_HASH_1': 'abc' }", false },
511 { VALUE_HASH_1
, "{ 'KEY_HASH_1': true }", false },
512 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 1 }", false },
513 { VALUE_HASH_1
, "{ 'KEY_HASH_1': 1.1 }", false },
514 { VALUE_HASH_1
, "{ 'KEY_HASH_1': [1] }", false },
515 { VALUE_HASH_1
, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
518 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
519 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
520 std::string pattern
= cases
[i
].pattern
;
521 uint32 pattern_sum
= std::accumulate(
522 pattern
.begin(), pattern
.end(), static_cast<uint32
>(0u));
524 OP_NAVIGATE(KEY_HASH_1
) +
525 OP_COMPARE_NODE_SUBSTRING(GetHash(pattern
),
526 EncodeUint32(pattern
.size()),
527 EncodeUint32(pattern_sum
)) +
528 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
),
530 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
531 if (cases
[i
].expected_success
) {
532 base::ExpectDictBooleanValue(
533 true, *interpreter
.working_memory(), VAR_HASH_1
);
535 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
540 TEST(JtlInterpreter
, StoreNodeEffectiveSLDHash
) {
542 std::string expected_value
;
544 bool expected_success
;
546 { GetHash("google"), "{ 'KEY_HASH_1': 'http://google.com/path' }", true },
547 { GetHash("google"), "{ 'KEY_HASH_1': 'http://mail.google.com/' }", true },
548 { GetHash("google"), "{ 'KEY_HASH_1': 'http://google.co.uk/' }", true },
549 { GetHash("google"), "{ 'KEY_HASH_1': 'http://google.com./' }", true },
550 { GetHash("google"), "{ 'KEY_HASH_1': 'http://..google.com/' }", true },
552 { std::string(), "{ 'KEY_HASH_1': 'http://google.com../' }", false },
553 { std::string(), "{ 'KEY_HASH_1': 'http://foo.bar/path' }", false },
554 { std::string(), "{ 'KEY_HASH_1': 'http://bar/path' }", false },
555 { std::string(), "{ 'KEY_HASH_1': 'http://co.uk/path' }", false },
556 { std::string(), "{ 'KEY_HASH_1': 'http://127.0.0.1/path' }", false },
557 { std::string(), "{ 'KEY_HASH_1': 'file:///C:/bar.html' }", false },
559 { std::string(), "{ 'KEY_HASH_1': 1 }", false },
560 { std::string(), "{ 'KEY_HASH_1': 1.2 }", false },
561 { std::string(), "{ 'KEY_HASH_1': true }", false },
562 { std::string(), "{ 'KEY_HASH_1': [1] }", false },
563 { std::string(), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
566 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
567 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
569 OP_NAVIGATE(KEY_HASH_1
) +
570 OP_STORE_NODE_EFFECTIVE_SLD_HASH(VAR_HASH_1
) +
571 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
573 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
574 if (cases
[i
].expected_success
) {
575 base::ExpectDictStringValue(
576 cases
[i
].expected_value
, *interpreter
.working_memory(), VAR_HASH_1
);
577 base::ExpectDictBooleanValue(
578 true, *interpreter
.working_memory(), VAR_HASH_2
);
580 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_1
));
581 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
586 TEST(JtlInterpreter
, Stop
) {
588 OP_NAVIGATE(KEY_HASH_1
) +
589 OP_NAVIGATE(KEY_HASH_2
) +
590 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
) +
591 OP_STOP_EXECUTING_SENTENCE
+
592 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
593 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
594 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
595 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
596 EXPECT_FALSE(interpreter
.working_memory()->HasKey(VAR_HASH_2
));
599 TEST(JtlInterpreter
, EndOfSentence
) {
601 OP_NAVIGATE(KEY_HASH_1
) +
602 OP_NAVIGATE(KEY_HASH_2
) +
603 OP_STORE_BOOL(VAR_HASH_1
, VALUE_TRUE
) +
605 OP_STORE_BOOL(VAR_HASH_2
, VALUE_TRUE
),
606 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
607 EXPECT_EQ(JtlInterpreter::OK
, interpreter
.result());
608 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_1
);
609 base::ExpectDictBooleanValue(true, *interpreter
.working_memory(), VAR_HASH_2
);
612 TEST(JtlInterpreter
, InvalidBack
) {
614 OP_NAVIGATE(KEY_HASH_1
) +
617 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
618 EXPECT_EQ(JtlInterpreter::RUNTIME_ERROR
, interpreter
.result());
621 TEST(JtlInterpreter
, IncorrectPrograms
) {
622 std::string missing_hash
;
623 std::string missing_bool
;
624 std::string
invalid_hash("123");
625 std::string
invalid_bool("\x02", 1);
626 std::string
invalid_operation("\x99", 1);
627 std::string programs
[] = {
628 OP_NAVIGATE(missing_hash
),
629 OP_NAVIGATE(invalid_hash
),
630 OP_STORE_BOOL(VAR_HASH_1
, invalid_bool
),
631 OP_STORE_BOOL(missing_hash
, VALUE_TRUE
),
632 OP_STORE_BOOL(invalid_hash
, VALUE_TRUE
),
633 OP_COMPARE_STORED_BOOL(invalid_hash
, VALUE_TRUE
, VALUE_TRUE
),
634 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, invalid_bool
, VALUE_TRUE
),
635 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, VALUE_TRUE
, invalid_bool
),
636 OP_COMPARE_STORED_BOOL(VAR_HASH_1
, VALUE_TRUE
, missing_bool
),
637 OP_STORE_NODE_BOOL(missing_hash
),
638 OP_STORE_NODE_BOOL(invalid_hash
),
639 OP_STORE_NODE_HASH(missing_hash
),
640 OP_STORE_NODE_HASH(invalid_hash
),
641 OP_COMPARE_NODE_BOOL(missing_bool
),
642 OP_COMPARE_NODE_BOOL(invalid_bool
),
643 OP_COMPARE_NODE_HASH(missing_hash
),
644 OP_COMPARE_NODE_HASH(invalid_hash
),
645 OP_COMPARE_NODE_TO_STORED_BOOL(missing_hash
),
646 OP_COMPARE_NODE_TO_STORED_BOOL(invalid_hash
),
647 OP_COMPARE_NODE_TO_STORED_HASH(missing_hash
),
648 OP_COMPARE_NODE_TO_STORED_HASH(invalid_hash
),
651 for (size_t i
= 0; i
< arraysize(programs
); ++i
) {
652 SCOPED_TRACE(testing::Message() << "Iteration " << i
);
653 INIT_INTERPRETER(programs
[i
],
654 "{ 'KEY_HASH_1': { 'KEY_HASH_2': 'VALUE_HASH_1' } }");
655 EXPECT_EQ(JtlInterpreter::PARSE_ERROR
, interpreter
.result());
659 TEST(JtlInterpreter
, GetOutput
) {
661 OP_STORE_BOOL(GetHash("output1"), VALUE_TRUE
) +
662 OP_STORE_HASH(GetHash("output2"), VALUE_HASH_1
),
664 bool output1
= false;
666 EXPECT_TRUE(interpreter
.GetOutputBoolean("output1", &output1
));
667 EXPECT_EQ(true, output1
);
668 EXPECT_TRUE(interpreter
.GetOutputString("output2", &output2
));
669 EXPECT_EQ(VALUE_HASH_1
, output2
);
670 EXPECT_FALSE(interpreter
.GetOutputBoolean("outputxx", &output1
));
671 EXPECT_FALSE(interpreter
.GetOutputString("outputxx", &output2
));