Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / tools / profile_reset / jtl_compiler_unittest.cc
blobc39e59a4ac2ad51166da796f46bb43e2298be25e
1 // Copyright 2013 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/tools/profile_reset/jtl_compiler.h"
7 #include <string>
9 #include "base/values.h"
10 #include "chrome/browser/profile_resetter/jtl_foundation.h"
11 #include "chrome/browser/profile_resetter/jtl_instructions.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace {
17 const char kTestHashSeed[] = "test-hash-seed";
19 // Helpers -------------------------------------------------------------------
21 std::string GetHash(const std::string& input) {
22 return jtl_foundation::Hasher(kTestHashSeed).GetHash(input);
25 static std::string EncodeUint32(uint32 value) {
26 std::string bytecode;
27 for (int i = 0; i < 4; ++i) {
28 bytecode.push_back(static_cast<char>(value & 0xFFu));
29 value >>= 8;
31 return bytecode;
34 // Tests ---------------------------------------------------------------------
36 // Note: Parsing and parsing-related errors are unit-tested separately in more
37 // detail in "jtl_parser_unittest.cc". Here, most of the time, we assume that
38 // creating the parse tree works.
40 TEST(JtlCompiler, CompileSingleInstructions) {
41 struct TestCase {
42 std::string source_code;
43 std::string expected_bytecode;
44 } cases[] = {
45 {"go(\"foo\").", OP_NAVIGATE(GetHash("foo"))},
46 {"go(\"has whitespace\t\").", OP_NAVIGATE(GetHash("has whitespace\t"))},
47 {"any.", OP_NAVIGATE_ANY},
48 {"back.", OP_NAVIGATE_BACK},
49 {"store_bool(\"name\", true).",
50 OP_STORE_BOOL(GetHash("name"), VALUE_TRUE)},
51 {"compare_stored_bool(\"name\", true, false).",
52 OP_COMPARE_STORED_BOOL(GetHash("name"), VALUE_TRUE, VALUE_FALSE)},
53 {"store_hash(\"name\", \"" + GetHash("value") + "\").",
54 OP_STORE_HASH(GetHash("name"), GetHash("value"))},
55 {"store_hashed(\"name\", \"value\").",
56 OP_STORE_HASH(GetHash("name"), GetHash("value"))},
57 {"store_node_bool(\"name\").", OP_STORE_NODE_BOOL(GetHash("name"))},
58 {"store_node_hash(\"name\").", OP_STORE_NODE_HASH(GetHash("name"))},
59 {"store_node_registerable_domain_hash(\"name\").",
60 OP_STORE_NODE_REGISTERABLE_DOMAIN_HASH(GetHash("name"))},
61 {"compare_stored_hashed(\"name\", \"value\", \"default\").",
62 OP_COMPARE_STORED_HASH(
63 GetHash("name"), GetHash("value"), GetHash("default"))},
64 {"compare_bool(false).", OP_COMPARE_NODE_BOOL(VALUE_FALSE)},
65 {"compare_bool(true).", OP_COMPARE_NODE_BOOL(VALUE_TRUE)},
66 {"compare_hashed(\"foo\").", OP_COMPARE_NODE_HASH(GetHash("foo"))},
67 {"compare_hashed_not(\"foo\").",
68 OP_COMPARE_NODE_HASH_NOT(GetHash("foo"))},
69 {"compare_to_stored_bool(\"name\").",
70 OP_COMPARE_NODE_TO_STORED_BOOL(GetHash("name"))},
71 {"compare_to_stored_hash(\"name\").",
72 OP_COMPARE_NODE_TO_STORED_HASH(GetHash("name"))},
73 {"compare_substring_hashed(\"pattern\").",
74 OP_COMPARE_NODE_SUBSTRING(
75 GetHash("pattern"), EncodeUint32(7), EncodeUint32(766))},
76 {"break.", OP_STOP_EXECUTING_SENTENCE},
77 {"break;", OP_STOP_EXECUTING_SENTENCE + OP_END_OF_SENTENCE}};
79 for (size_t i = 0; i < arraysize(cases); ++i) {
80 SCOPED_TRACE(cases[i].source_code);
81 std::string bytecode;
82 EXPECT_TRUE(JtlCompiler::Compile(
83 cases[i].source_code, kTestHashSeed, &bytecode, NULL));
84 EXPECT_EQ(cases[i].expected_bytecode, bytecode);
88 TEST(JtlCompiler, CompileEntireProgram) {
89 const char kSourceCode[] =
90 "// Store \"x\"=true if path is found.\n"
91 "go(\"foo\").go(\"bar\").store_bool(\"x\", true);\n"
92 "// ...\n"
93 "// Store \"y\"=\"1\" if above value is set.\n"
94 "compare_stored_bool(\"x\", true, false).store_hashed(\"y\", \"1\");\n";
96 std::string expected_bytecode =
97 OP_NAVIGATE(GetHash("foo")) +
98 OP_NAVIGATE(GetHash("bar")) +
99 OP_STORE_BOOL(GetHash("x"), VALUE_TRUE) + OP_END_OF_SENTENCE +
100 OP_COMPARE_STORED_BOOL(GetHash("x"), VALUE_TRUE, VALUE_FALSE) +
101 OP_STORE_HASH(GetHash("y"), GetHash("1")) + OP_END_OF_SENTENCE;
103 std::string bytecode;
104 EXPECT_TRUE(
105 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, NULL));
106 EXPECT_EQ(expected_bytecode, bytecode);
109 TEST(JtlCompiler, InvalidOperationName) {
110 const char kSourceCode[] = "any()\n.\nnon_existent_instruction\n(\n)\n;\n";
112 std::string bytecode;
113 JtlCompiler::CompileError error;
114 EXPECT_FALSE(
115 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
116 EXPECT_THAT(error.context,
117 testing::base::StartsWith("non_existent_instruction"));
118 EXPECT_EQ(2u, error.line_number);
119 EXPECT_EQ(JtlCompiler::CompileError::INVALID_OPERATION_NAME,
120 error.error_code);
123 TEST(JtlCompiler, InvalidArgumentsCount) {
124 const char* const kSourceCodes[] = {
125 "any().\nstore_bool(\"name\", true, \"superfluous argument\");\n",
126 "any().\nstore_bool(\"name\");"}; // missing argument
128 for (size_t i = 0; i < arraysize(kSourceCodes); ++i) {
129 SCOPED_TRACE(kSourceCodes[i]);
130 std::string bytecode;
131 JtlCompiler::CompileError error;
132 EXPECT_FALSE(JtlCompiler::Compile(
133 kSourceCodes[i], kTestHashSeed, &bytecode, &error));
134 EXPECT_THAT(error.context, testing::base::StartsWith("store_bool"));
135 EXPECT_EQ(1u, error.line_number);
136 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT,
137 error.error_code);
141 TEST(JtlCompiler, InvalidArgumentType) {
142 struct TestCase {
143 std::string expected_context_prefix;
144 std::string source_code;
145 } cases[] = {
146 {"compare_bool", "any()\n.\ncompare_bool(\"foo\");"},
147 {"compare_bool",
148 "any()\n.\ncompare_bool(\"01234567890123456789012345678901\");"},
149 {"compare_hashed", "any()\n.\ncompare_hashed(false);"},
150 {"store_hash", "any()\n.\nstore_hash(\"name\", false);"},
151 {"store_hash", "any()\n.\nstore_hash(\"name\", \"foo\");"},
152 {"compare_stored_bool",
153 "any()\n.\ncompare_stored_bool(\"name\", \"need a bool\", false);"},
154 {"compare_stored_bool",
155 "any()\n.\ncompare_stored_bool(\"name\", false, \"need a bool\");"},
156 {"compare_substring_hashed",
157 "any()\n.\ncompare_substring_hashed(true);"}};
159 for (size_t i = 0; i < arraysize(cases); ++i) {
160 SCOPED_TRACE(cases[i].source_code);
161 std::string bytecode;
162 JtlCompiler::CompileError error;
163 EXPECT_FALSE(JtlCompiler::Compile(
164 cases[i].source_code, kTestHashSeed, &bytecode, &error));
165 EXPECT_THAT(error.context,
166 testing::base::StartsWith(cases[i].expected_context_prefix));
167 EXPECT_EQ(2u, error.line_number);
168 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE,
169 error.error_code);
173 TEST(JtlCompiler, InvalidArgumentValue) {
174 struct TestCase {
175 std::string expected_context_prefix;
176 std::string source_code;
177 } cases[] = {
178 {"compare_substring_hashed", "compare_substring_hashed(\"\");"}};
180 for (size_t i = 0; i < arraysize(cases); ++i) {
181 SCOPED_TRACE(cases[i].source_code);
182 std::string bytecode;
183 JtlCompiler::CompileError error;
184 EXPECT_FALSE(JtlCompiler::Compile(
185 cases[i].source_code, kTestHashSeed, &bytecode, &error));
186 EXPECT_THAT(error.context,
187 testing::base::StartsWith(cases[i].expected_context_prefix));
188 EXPECT_EQ(0u, error.line_number);
189 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_VALUE,
190 error.error_code);
194 TEST(JtlCompiler, MistmatchedDoubleQuotes) {
195 const char kSourceCode[] = "any().\ngo(\"ok\", \"stray quote).break();";
197 std::string bytecode;
198 JtlCompiler::CompileError error;
199 EXPECT_FALSE(
200 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
201 EXPECT_EQ(1u, error.line_number);
202 EXPECT_EQ(JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES,
203 error.error_code);
206 TEST(JtlCompiler, ParsingError) {
207 const char kSourceCode[] = "any().\ngo()missing_separator();";
209 std::string bytecode;
210 JtlCompiler::CompileError error;
211 EXPECT_FALSE(
212 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
213 EXPECT_THAT(error.context, testing::base::StartsWith("go"));
214 EXPECT_EQ(1u, error.line_number);
215 EXPECT_EQ(JtlCompiler::CompileError::PARSING_ERROR, error.error_code);
218 } // namespace