| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "remoting/host/json_host_config.h" | 9 #include "remoting/host/json_host_config.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace remoting { | 12 namespace remoting { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 const char* kTestConfig = | 15 const char* kTestConfig = |
| 16 "{\n" | 16 "{\n" |
| 17 " \"xmpp_login\" : \"test@gmail.com\",\n" | 17 " \"xmpp_login\" : \"test@gmail.com\",\n" |
| 18 " \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n" | 18 " \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n" |
| 19 " \"host_id\" : \"TEST_HOST_ID\",\n" | 19 " \"host_id\" : \"TEST_HOST_ID\",\n" |
| 20 " \"host_name\" : \"TEST_MACHINE_NAME\",\n" | 20 " \"host_name\" : \"TEST_MACHINE_NAME\",\n" |
| 21 " \"private_key\" : \"TEST_PRIVATE_KEY\"\n" | 21 " \"private_key\" : \"TEST_PRIVATE_KEY\"\n" |
| 22 "}\n"; | 22 "}\n"; |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 class JsonHostConfigTest : public testing::Test { | 25 class JsonHostConfigTest : public testing::Test { |
| 26 protected: | 26 protected: |
| 27 static void WriteTestFile(const FilePath& filename) { | 27 static void WriteTestFile(const base::FilePath& filename) { |
| 28 file_util::WriteFile(filename, kTestConfig, std::strlen(kTestConfig)); | 28 file_util::WriteFile(filename, kTestConfig, std::strlen(kTestConfig)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // The temporary directory used to contain the test operations. | 31 // The temporary directory used to contain the test operations. |
| 32 base::ScopedTempDir test_dir_; | 32 base::ScopedTempDir test_dir_; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 TEST_F(JsonHostConfigTest, InvalidFile) { | 35 TEST_F(JsonHostConfigTest, InvalidFile) { |
| 36 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 36 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| 37 FilePath non_existent_file = | 37 base::FilePath non_existent_file = |
| 38 test_dir_.path().AppendASCII("non_existent.json"); | 38 test_dir_.path().AppendASCII("non_existent.json"); |
| 39 JsonHostConfig target(non_existent_file); | 39 JsonHostConfig target(non_existent_file); |
| 40 EXPECT_FALSE(target.Read()); | 40 EXPECT_FALSE(target.Read()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 TEST_F(JsonHostConfigTest, Read) { | 43 TEST_F(JsonHostConfigTest, Read) { |
| 44 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 44 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| 45 FilePath test_file = test_dir_.path().AppendASCII("read.json"); | 45 base::FilePath test_file = test_dir_.path().AppendASCII("read.json"); |
| 46 WriteTestFile(test_file); | 46 WriteTestFile(test_file); |
| 47 JsonHostConfig target(test_file); | 47 JsonHostConfig target(test_file); |
| 48 ASSERT_TRUE(target.Read()); | 48 ASSERT_TRUE(target.Read()); |
| 49 | 49 |
| 50 std::string value; | 50 std::string value; |
| 51 EXPECT_TRUE(target.GetString(kXmppLoginConfigPath, &value)); | 51 EXPECT_TRUE(target.GetString(kXmppLoginConfigPath, &value)); |
| 52 EXPECT_EQ("test@gmail.com", value); | 52 EXPECT_EQ("test@gmail.com", value); |
| 53 EXPECT_TRUE(target.GetString(kXmppAuthTokenConfigPath, &value)); | 53 EXPECT_TRUE(target.GetString(kXmppAuthTokenConfigPath, &value)); |
| 54 EXPECT_EQ("TEST_AUTH_TOKEN", value); | 54 EXPECT_EQ("TEST_AUTH_TOKEN", value); |
| 55 EXPECT_TRUE(target.GetString(kHostIdConfigPath, &value)); | 55 EXPECT_TRUE(target.GetString(kHostIdConfigPath, &value)); |
| 56 EXPECT_EQ("TEST_HOST_ID", value); | 56 EXPECT_EQ("TEST_HOST_ID", value); |
| 57 EXPECT_TRUE(target.GetString(kHostNameConfigPath, &value)); | 57 EXPECT_TRUE(target.GetString(kHostNameConfigPath, &value)); |
| 58 EXPECT_EQ("TEST_MACHINE_NAME", value); | 58 EXPECT_EQ("TEST_MACHINE_NAME", value); |
| 59 EXPECT_TRUE(target.GetString(kPrivateKeyConfigPath, &value)); | 59 EXPECT_TRUE(target.GetString(kPrivateKeyConfigPath, &value)); |
| 60 EXPECT_EQ("TEST_PRIVATE_KEY", value); | 60 EXPECT_EQ("TEST_PRIVATE_KEY", value); |
| 61 | 61 |
| 62 EXPECT_FALSE(target.GetString("non_existent_value", &value)); | 62 EXPECT_FALSE(target.GetString("non_existent_value", &value)); |
| 63 } | 63 } |
| 64 | 64 |
| 65 TEST_F(JsonHostConfigTest, Write) { | 65 TEST_F(JsonHostConfigTest, Write) { |
| 66 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 66 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| 67 | 67 |
| 68 FilePath test_file = test_dir_.path().AppendASCII("write.json"); | 68 base::FilePath test_file = test_dir_.path().AppendASCII("write.json"); |
| 69 WriteTestFile(test_file); | 69 WriteTestFile(test_file); |
| 70 JsonHostConfig target(test_file); | 70 JsonHostConfig target(test_file); |
| 71 ASSERT_TRUE(target.Read()); | 71 ASSERT_TRUE(target.Read()); |
| 72 | 72 |
| 73 std::string new_auth_token_value = "NEW_AUTH_TOKEN"; | 73 std::string new_auth_token_value = "NEW_AUTH_TOKEN"; |
| 74 target.SetString(kXmppAuthTokenConfigPath, new_auth_token_value); | 74 target.SetString(kXmppAuthTokenConfigPath, new_auth_token_value); |
| 75 ASSERT_TRUE(target.Save()); | 75 ASSERT_TRUE(target.Save()); |
| 76 | 76 |
| 77 // 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. |
| 78 JsonHostConfig reader(test_file); | 78 JsonHostConfig reader(test_file); |
| 79 ASSERT_TRUE(reader.Read()); | 79 ASSERT_TRUE(reader.Read()); |
| 80 | 80 |
| 81 std::string value; | 81 std::string value; |
| 82 EXPECT_TRUE(reader.GetString(kXmppLoginConfigPath, &value)); | 82 EXPECT_TRUE(reader.GetString(kXmppLoginConfigPath, &value)); |
| 83 EXPECT_EQ("test@gmail.com", value); | 83 EXPECT_EQ("test@gmail.com", value); |
| 84 EXPECT_TRUE(reader.GetString(kXmppAuthTokenConfigPath, &value)); | 84 EXPECT_TRUE(reader.GetString(kXmppAuthTokenConfigPath, &value)); |
| 85 EXPECT_EQ(new_auth_token_value, value); | 85 EXPECT_EQ(new_auth_token_value, value); |
| 86 EXPECT_TRUE(reader.GetString(kHostIdConfigPath, &value)); | 86 EXPECT_TRUE(reader.GetString(kHostIdConfigPath, &value)); |
| 87 EXPECT_EQ("TEST_HOST_ID", value); | 87 EXPECT_EQ("TEST_HOST_ID", value); |
| 88 EXPECT_TRUE(reader.GetString(kHostNameConfigPath, &value)); | 88 EXPECT_TRUE(reader.GetString(kHostNameConfigPath, &value)); |
| 89 EXPECT_EQ("TEST_MACHINE_NAME", value); | 89 EXPECT_EQ("TEST_MACHINE_NAME", value); |
| 90 EXPECT_TRUE(reader.GetString(kPrivateKeyConfigPath, &value)); | 90 EXPECT_TRUE(reader.GetString(kPrivateKeyConfigPath, &value)); |
| 91 EXPECT_EQ("TEST_PRIVATE_KEY", value); | 91 EXPECT_EQ("TEST_PRIVATE_KEY", value); |
| 92 } | 92 } |
| 93 | 93 |
| 94 } | 94 } |
| OLD | NEW |