#include "chrome/browser/profile_resetter/jtl_interpreter.h"
+#include <numeric>
+
#include "base/strings/string_util.h"
#include "base/test/values_test_util.h"
#include "chrome/browser/profile_resetter/jtl_foundation.h"
return jtl_foundation::Hasher(seed).GetHash(input);
}
+std::string EncodeUint32(uint32 value) {
+ std::string bytecode;
+ for (int i = 0; i < 4; ++i) {
+ bytecode.push_back(static_cast<char>(value & 0xFFu));
+ value >>= 8;
+ }
+ return bytecode;
+}
+
// escaped_json_param may contain ' characters that are replaced with ". This
// makes the code more readable because we need less escaping.
#define INIT_INTERPRETER(program_param, escaped_json_param) \
const char* escaped_json = escaped_json_param; \
std::string json; \
- ReplaceChars(escaped_json, "'", "\"", &json); \
- scoped_ptr<Value> json_value(ParseJson(json)); \
+ base::ReplaceChars(escaped_json, "'", "\"", &json); \
+ scoped_ptr<base::Value> json_value(ParseJson(json)); \
JtlInterpreter interpreter( \
seed, \
program_param, \
- static_cast<const DictionaryValue*>(json_value.get())); \
+ static_cast<const base::DictionaryValue*>(json_value.get())); \
interpreter.Execute()
using base::test::ParseJson;
}
}
+TEST(JtlInterpreter, CompareSubstring) {
+ struct TestCase {
+ std::string pattern;
+ const char* json;
+ bool expected_success;
+ } cases[] = {
+ { "abc", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", true },
+ { "xyz", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", true },
+ { "m", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", true },
+ { "abc", "{ 'KEY_HASH_1': 'abc' }", true },
+ { "cba", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", false },
+ { "acd", "{ 'KEY_HASH_1': 'abcdefghijklmnopqrstuvwxyz' }", false },
+ { "waaaaaaay_too_long", "{ 'KEY_HASH_1': 'abc' }", false },
+
+ { VALUE_HASH_1, "{ 'KEY_HASH_1': true }", false },
+ { VALUE_HASH_1, "{ 'KEY_HASH_1': 1 }", false },
+ { VALUE_HASH_1, "{ 'KEY_HASH_1': 1.1 }", false },
+ { VALUE_HASH_1, "{ 'KEY_HASH_1': [1] }", false },
+ { VALUE_HASH_1, "{ 'KEY_HASH_1': {'a': 'b'} }", false },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ SCOPED_TRACE(testing::Message() << "Iteration " << i);
+ std::string pattern = cases[i].pattern;
+ uint32 pattern_sum = std::accumulate(
+ pattern.begin(), pattern.end(), static_cast<uint32>(0u));
+ INIT_INTERPRETER(
+ OP_NAVIGATE(KEY_HASH_1) +
+ OP_COMPARE_NODE_SUBSTRING(GetHash(pattern),
+ EncodeUint32(pattern.size()),
+ EncodeUint32(pattern_sum)) +
+ OP_STORE_BOOL(VAR_HASH_1, VALUE_TRUE),
+ cases[i].json);
+ EXPECT_EQ(JtlInterpreter::OK, interpreter.result());
+ if (cases[i].expected_success) {
+ base::ExpectDictBooleanValue(
+ true, *interpreter.working_memory(), VAR_HASH_1);
+ } else {
+ EXPECT_FALSE(interpreter.working_memory()->HasKey(VAR_HASH_1));
+ }
+ }
+}
+
+TEST(JtlInterpreter, StoreNodeRegisterableDomainHash) {
+ struct TestCase {
+ std::string expected_value;
+ const char* json;
+ bool expected_success;
+ } cases[] = {
+ { GetHash("google"), "{ 'KEY_HASH_1': 'http://google.com/path' }", true },
+ { GetHash("google"), "{ 'KEY_HASH_1': 'http://mail.google.com/' }", true },
+ { GetHash("google"), "{ 'KEY_HASH_1': 'http://google.co.uk/' }", true },
+ { GetHash("google"), "{ 'KEY_HASH_1': 'http://google.com./' }", true },
+ { GetHash("google"), "{ 'KEY_HASH_1': 'http://..google.com/' }", true },
+
+ { GetHash("foo"), "{ 'KEY_HASH_1': 'http://foo.bar/path' }", true },
+ { GetHash("foo"), "{ 'KEY_HASH_1': 'http://sub.foo.bar' }", true },
+ { GetHash("foo"), "{ 'KEY_HASH_1': 'http://foo.appspot.com/' }", true },
+ { GetHash("foo"), "{ 'KEY_HASH_1': 'http://sub.foo.appspot.com' }", true },
+
+ { std::string(), "{ 'KEY_HASH_1': 'http://google.com../' }", false },
+
+ { std::string(), "{ 'KEY_HASH_1': 'http://bar/path' }", false },
+ { std::string(), "{ 'KEY_HASH_1': 'http://co.uk/path' }", false },
+ { std::string(), "{ 'KEY_HASH_1': 'http://appspot.com/path' }", false },
+ { std::string(), "{ 'KEY_HASH_1': 'http://127.0.0.1/path' }", false },
+ { std::string(), "{ 'KEY_HASH_1': 'file:///C:/bar.html' }", false },
+
+ { std::string(), "{ 'KEY_HASH_1': 1 }", false },
+ { std::string(), "{ 'KEY_HASH_1': 1.2 }", false },
+ { std::string(), "{ 'KEY_HASH_1': true }", false },
+ { std::string(), "{ 'KEY_HASH_1': [1] }", false },
+ { std::string(), "{ 'KEY_HASH_1': {'a': 'b'} }", false },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ SCOPED_TRACE(testing::Message() << "Iteration " << i);
+ INIT_INTERPRETER(
+ OP_NAVIGATE(KEY_HASH_1) +
+ OP_STORE_NODE_REGISTERABLE_DOMAIN_HASH(VAR_HASH_1) +
+ OP_STORE_BOOL(VAR_HASH_2, VALUE_TRUE),
+ cases[i].json);
+ EXPECT_EQ(JtlInterpreter::OK, interpreter.result());
+ if (cases[i].expected_success) {
+ base::ExpectDictStringValue(
+ cases[i].expected_value, *interpreter.working_memory(), VAR_HASH_1);
+ base::ExpectDictBooleanValue(
+ true, *interpreter.working_memory(), VAR_HASH_2);
+ } else {
+ EXPECT_FALSE(interpreter.working_memory()->HasKey(VAR_HASH_1));
+ EXPECT_FALSE(interpreter.working_memory()->HasKey(VAR_HASH_2));
+ }
+ }
+}
+
TEST(JtlInterpreter, Stop) {
INIT_INTERPRETER(
OP_NAVIGATE(KEY_HASH_1) +
EXPECT_FALSE(interpreter.GetOutputString("outputxx", &output2));
}
+TEST(JtlInterpreter, CalculateProgramChecksum) {
+ const char kTestSeed[] = "Irrelevant seed value.";
+ const char kTestProgram[] = "The quick brown fox jumps over the lazy dog.";
+ // This program is invalid, but we are not actually executing it.
+ base::DictionaryValue input;
+ JtlInterpreter interpreter(kTestSeed, kTestProgram, &input);
+ EXPECT_EQ(0xef537f, interpreter.CalculateProgramChecksum());
+}
+
} // namespace