summaryrefslogtreecommitdiffstats
path: root/remoting/host/json_host_config_unittest.cc
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-17 23:43:00 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-17 23:43:00 +0000
commit7620735be42ebc236d2da42d64f22dc5dedd01f9 (patch)
treebaf0e6311b84830cd76df764e73e9aacfeb65e3e /remoting/host/json_host_config_unittest.cc
parentf90ab58c11abf783c850c632f9b0467f968e4045 (diff)
downloadchromium_src-7620735be42ebc236d2da42d64f22dc5dedd01f9.zip
chromium_src-7620735be42ebc236d2da42d64f22dc5dedd01f9.tar.gz
chromium_src-7620735be42ebc236d2da42d64f22dc5dedd01f9.tar.bz2
JSON based host config storage implemented. Python script for host registration.
BUG=None TEST=None Review URL: http://codereview.chromium.org/2804007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50166 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/host/json_host_config_unittest.cc')
-rw-r--r--remoting/host/json_host_config_unittest.cc121
1 files changed, 121 insertions, 0 deletions
diff --git a/remoting/host/json_host_config_unittest.cc b/remoting/host/json_host_config_unittest.cc
new file mode 100644
index 0000000..ba6ce1b
--- /dev/null
+++ b/remoting/host/json_host_config_unittest.cc
@@ -0,0 +1,121 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/file_util.h"
+#include "base/message_loop.h"
+#include "base/message_loop_proxy.h"
+#include "base/path_service.h"
+#include "base/ref_counted.h"
+#include "base/scoped_temp_dir.h"
+#include "remoting/host/json_host_config.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace remoting {
+
+namespace {
+const char *kTestConfig =
+"{\n"
+" \"xmpp_login\" : \"test@gmail.com\",\n"
+" \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n"
+" \"host_id\" : \"TEST_HOST_ID\",\n"
+" \"host_name\" : \"TEST_MACHINE_NAME\",\n"
+" \"public_key\" : \"TEST_PUBLIC_KEY\"\n"
+"}\n";
+
+// Helper for Update test.
+class TestConfigUpdater : public base::RefCountedThreadSafe<TestConfigUpdater> {
+ public:
+ void DoUpdate(scoped_refptr<JsonHostConfig> target,
+ std::string new_auth_token_value) {
+ target->SetString(kXmppAuthTokenConfigPath, new_auth_token_value);
+ }
+};
+} // namespace
+
+class JsonHostConfigTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread();
+ }
+
+ static void WriteTestFile(const FilePath& filename) {
+ file_util::WriteFile(filename, kTestConfig, std::strlen(kTestConfig));
+ }
+
+ // The temporary directory used to contain the test operations.
+ ScopedTempDir test_dir_;
+ // A message loop that we can use as the file thread message loop.
+ MessageLoop message_loop_;
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
+};
+
+TEST_F(JsonHostConfigTest, InvalidFile) {
+ ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
+ FilePath non_existent_file =
+ test_dir_.path().AppendASCII("non_existent.json");
+ scoped_refptr<JsonHostConfig> target =
+ new JsonHostConfig(non_existent_file, message_loop_proxy_.get());
+ EXPECT_FALSE(target->Read());
+}
+
+TEST_F(JsonHostConfigTest, Read) {
+ ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
+ FilePath test_file = test_dir_.path().AppendASCII("read.json");
+ WriteTestFile(test_file);
+ scoped_refptr<JsonHostConfig> target =
+ new JsonHostConfig(test_file, message_loop_proxy_.get());
+ ASSERT_TRUE(target->Read());
+
+ std::string value;
+ EXPECT_TRUE(target->GetString(kXmppLoginConfigPath, &value));
+ EXPECT_EQ("test@gmail.com", value);
+ EXPECT_TRUE(target->GetString(kXmppAuthTokenConfigPath, &value));
+ EXPECT_EQ("TEST_AUTH_TOKEN", value);
+ EXPECT_TRUE(target->GetString(kHostIdConfigPath, &value));
+ EXPECT_EQ("TEST_HOST_ID", value);
+ EXPECT_TRUE(target->GetString(kHostNameConfigPath, &value));
+ EXPECT_EQ("TEST_MACHINE_NAME", value);
+ EXPECT_TRUE(target->GetString(kPublicKeyConfigPath, &value));
+ EXPECT_EQ("TEST_PUBLIC_KEY", value);
+
+ EXPECT_FALSE(target->GetString(L"non_existent_value", &value));
+}
+
+TEST_F(JsonHostConfigTest, Write) {
+ ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
+
+ FilePath test_file = test_dir_.path().AppendASCII("write.json");
+ WriteTestFile(test_file);
+ scoped_refptr<JsonHostConfig> target(
+ new JsonHostConfig(test_file, message_loop_proxy_.get()));
+ ASSERT_TRUE(target->Read());
+
+ std::string new_auth_token_value = "NEW_AUTH_TOKEN";
+ scoped_refptr<TestConfigUpdater> config_updater(new TestConfigUpdater());
+ target->Update(
+ NewRunnableMethod(config_updater.get(), &TestConfigUpdater::DoUpdate,
+ target, new_auth_token_value));
+
+ message_loop_.RunAllPending();
+
+ // Now read the file again and check that the value has been written.
+ scoped_refptr<JsonHostConfig> reader(
+ new JsonHostConfig(test_file, message_loop_proxy_.get()));
+
+ ASSERT_TRUE(reader->Read());
+
+ std::string value;
+ EXPECT_TRUE(reader->GetString(kXmppLoginConfigPath, &value));
+ EXPECT_EQ("test@gmail.com", value);
+ EXPECT_TRUE(reader->GetString(kXmppAuthTokenConfigPath, &value));
+ EXPECT_EQ(new_auth_token_value, value);
+ EXPECT_TRUE(reader->GetString(kHostIdConfigPath, &value));
+ EXPECT_EQ("TEST_HOST_ID", value);
+ EXPECT_TRUE(reader->GetString(kHostNameConfigPath, &value));
+ EXPECT_EQ("TEST_MACHINE_NAME", value);
+ EXPECT_TRUE(reader->GetString(kPublicKeyConfigPath, &value));
+ EXPECT_EQ("TEST_PUBLIC_KEY", value);
+}
+
+}