Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: remoting/host/json_host_config_unittest.cc

Issue 10007048: Cleanup and simplify HostConfig and JsonHostConfig. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/host/json_host_config.cc ('k') | remoting/host/plugin/daemon_controller_mac.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop.h"
8 #include "base/message_loop_proxy.h"
9 #include "base/path_service.h" 7 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
11 #include "remoting/host/json_host_config.h" 9 #include "remoting/host/json_host_config.h"
12 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
13 11
14 namespace remoting { 12 namespace remoting {
15 13
16 namespace { 14 namespace {
17 const char* kTestConfig = 15 const char* kTestConfig =
18 "{\n" 16 "{\n"
19 " \"xmpp_login\" : \"test@gmail.com\",\n" 17 " \"xmpp_login\" : \"test@gmail.com\",\n"
20 " \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n" 18 " \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n"
21 " \"host_id\" : \"TEST_HOST_ID\",\n" 19 " \"host_id\" : \"TEST_HOST_ID\",\n"
22 " \"host_name\" : \"TEST_MACHINE_NAME\",\n" 20 " \"host_name\" : \"TEST_MACHINE_NAME\",\n"
23 " \"private_key\" : \"TEST_PRIVATE_KEY\"\n" 21 " \"private_key\" : \"TEST_PRIVATE_KEY\"\n"
24 "}\n"; 22 "}\n";
25 } // namespace 23 } // namespace
26 24
27 class JsonHostConfigTest : public testing::Test { 25 class JsonHostConfigTest : public testing::Test {
28 protected: 26 protected:
29 virtual void SetUp() {
30 message_loop_proxy_ = base::MessageLoopProxy::current();
31 }
32
33 static void WriteTestFile(const FilePath& filename) { 27 static void WriteTestFile(const FilePath& filename) {
34 file_util::WriteFile(filename, kTestConfig, std::strlen(kTestConfig)); 28 file_util::WriteFile(filename, kTestConfig, std::strlen(kTestConfig));
35 } 29 }
36 30
37 // The temporary directory used to contain the test operations. 31 // The temporary directory used to contain the test operations.
38 ScopedTempDir test_dir_; 32 ScopedTempDir test_dir_;
39 // A message loop that we can use as the file thread message loop.
40 MessageLoop message_loop_;
41 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
42 }; 33 };
43 34
44 TEST_F(JsonHostConfigTest, InvalidFile) { 35 TEST_F(JsonHostConfigTest, InvalidFile) {
45 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 36 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
46 FilePath non_existent_file = 37 FilePath non_existent_file =
47 test_dir_.path().AppendASCII("non_existent.json"); 38 test_dir_.path().AppendASCII("non_existent.json");
48 scoped_refptr<JsonHostConfig> target( 39 JsonHostConfig target(non_existent_file);
49 new JsonHostConfig(non_existent_file, message_loop_proxy_.get())); 40 EXPECT_FALSE(target.Read());
50 EXPECT_FALSE(target->Read());
51 } 41 }
52 42
53 TEST_F(JsonHostConfigTest, Read) { 43 TEST_F(JsonHostConfigTest, Read) {
54 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 44 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
55 FilePath test_file = test_dir_.path().AppendASCII("read.json"); 45 FilePath test_file = test_dir_.path().AppendASCII("read.json");
56 WriteTestFile(test_file); 46 WriteTestFile(test_file);
57 scoped_refptr<JsonHostConfig> target( 47 JsonHostConfig target(test_file);
58 new JsonHostConfig(test_file, message_loop_proxy_.get())); 48 ASSERT_TRUE(target.Read());
59 ASSERT_TRUE(target->Read());
60 49
61 std::string value; 50 std::string value;
62 EXPECT_TRUE(target->GetString(kXmppLoginConfigPath, &value)); 51 EXPECT_TRUE(target.GetString(kXmppLoginConfigPath, &value));
63 EXPECT_EQ("test@gmail.com", value); 52 EXPECT_EQ("test@gmail.com", value);
64 EXPECT_TRUE(target->GetString(kXmppAuthTokenConfigPath, &value)); 53 EXPECT_TRUE(target.GetString(kXmppAuthTokenConfigPath, &value));
65 EXPECT_EQ("TEST_AUTH_TOKEN", value); 54 EXPECT_EQ("TEST_AUTH_TOKEN", value);
66 EXPECT_TRUE(target->GetString(kHostIdConfigPath, &value)); 55 EXPECT_TRUE(target.GetString(kHostIdConfigPath, &value));
67 EXPECT_EQ("TEST_HOST_ID", value); 56 EXPECT_EQ("TEST_HOST_ID", value);
68 EXPECT_TRUE(target->GetString(kHostNameConfigPath, &value)); 57 EXPECT_TRUE(target.GetString(kHostNameConfigPath, &value));
69 EXPECT_EQ("TEST_MACHINE_NAME", value); 58 EXPECT_EQ("TEST_MACHINE_NAME", value);
70 EXPECT_TRUE(target->GetString(kPrivateKeyConfigPath, &value)); 59 EXPECT_TRUE(target.GetString(kPrivateKeyConfigPath, &value));
71 EXPECT_EQ("TEST_PRIVATE_KEY", value); 60 EXPECT_EQ("TEST_PRIVATE_KEY", value);
72 61
73 EXPECT_FALSE(target->GetString("non_existent_value", &value)); 62 EXPECT_FALSE(target.GetString("non_existent_value", &value));
74 } 63 }
75 64
76 TEST_F(JsonHostConfigTest, Write) { 65 TEST_F(JsonHostConfigTest, Write) {
77 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 66 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
78 67
79 FilePath test_file = test_dir_.path().AppendASCII("write.json"); 68 FilePath test_file = test_dir_.path().AppendASCII("write.json");
80 WriteTestFile(test_file); 69 WriteTestFile(test_file);
81 scoped_refptr<JsonHostConfig> target( 70 JsonHostConfig target(test_file);
82 new JsonHostConfig(test_file, message_loop_proxy_.get())); 71 ASSERT_TRUE(target.Read());
83 ASSERT_TRUE(target->Read());
84 72
85 std::string new_auth_token_value = "NEW_AUTH_TOKEN"; 73 std::string new_auth_token_value = "NEW_AUTH_TOKEN";
86 target->SetString(kXmppAuthTokenConfigPath, new_auth_token_value); 74 target.SetString(kXmppAuthTokenConfigPath, new_auth_token_value);
87 target->Save(); 75 ASSERT_TRUE(target.Save());
88
89 message_loop_.RunAllPending();
90 76
91 // Now read the file again and check that the value has been written. 77 // Now read the file again and check that the value has been written.
92 scoped_refptr<JsonHostConfig> reader( 78 JsonHostConfig reader(test_file);
93 new JsonHostConfig(test_file, message_loop_proxy_.get())); 79 ASSERT_TRUE(reader.Read());
94
95 ASSERT_TRUE(reader->Read());
96 80
97 std::string value; 81 std::string value;
98 EXPECT_TRUE(reader->GetString(kXmppLoginConfigPath, &value)); 82 EXPECT_TRUE(reader.GetString(kXmppLoginConfigPath, &value));
99 EXPECT_EQ("test@gmail.com", value); 83 EXPECT_EQ("test@gmail.com", value);
100 EXPECT_TRUE(reader->GetString(kXmppAuthTokenConfigPath, &value)); 84 EXPECT_TRUE(reader.GetString(kXmppAuthTokenConfigPath, &value));
101 EXPECT_EQ(new_auth_token_value, value); 85 EXPECT_EQ(new_auth_token_value, value);
102 EXPECT_TRUE(reader->GetString(kHostIdConfigPath, &value)); 86 EXPECT_TRUE(reader.GetString(kHostIdConfigPath, &value));
103 EXPECT_EQ("TEST_HOST_ID", value); 87 EXPECT_EQ("TEST_HOST_ID", value);
104 EXPECT_TRUE(reader->GetString(kHostNameConfigPath, &value)); 88 EXPECT_TRUE(reader.GetString(kHostNameConfigPath, &value));
105 EXPECT_EQ("TEST_MACHINE_NAME", value); 89 EXPECT_EQ("TEST_MACHINE_NAME", value);
106 EXPECT_TRUE(reader->GetString(kPrivateKeyConfigPath, &value)); 90 EXPECT_TRUE(reader.GetString(kPrivateKeyConfigPath, &value));
107 EXPECT_EQ("TEST_PRIVATE_KEY", value); 91 EXPECT_EQ("TEST_PRIVATE_KEY", value);
108 } 92 }
109 93
110 } 94 }
OLDNEW
« no previous file with comments | « remoting/host/json_host_config.cc ('k') | remoting/host/plugin/daemon_controller_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698