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
3 // found in the LICENSE file.
5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/path_service.h"
9 #include "remoting/host/json_host_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 const char* kTestConfig
=
17 " \"xmpp_login\" : \"test@gmail.com\",\n"
18 " \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n"
19 " \"host_id\" : \"TEST_HOST_ID\",\n"
20 " \"host_name\" : \"TEST_MACHINE_NAME\",\n"
21 " \"private_key\" : \"TEST_PRIVATE_KEY\"\n"
25 class JsonHostConfigTest
: public testing::Test
{
27 static void WriteTestFile(const base::FilePath
& filename
) {
28 base::WriteFile(filename
, kTestConfig
, std::strlen(kTestConfig
));
31 // The temporary directory used to contain the test operations.
32 base::ScopedTempDir test_dir_
;
35 TEST_F(JsonHostConfigTest
, InvalidFile
) {
36 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
37 base::FilePath non_existent_file
=
38 test_dir_
.path().AppendASCII("non_existent.json");
39 JsonHostConfig
target(non_existent_file
);
40 EXPECT_FALSE(target
.Read());
43 TEST_F(JsonHostConfigTest
, Read
) {
44 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
45 base::FilePath test_file
= test_dir_
.path().AppendASCII("read.json");
46 WriteTestFile(test_file
);
47 JsonHostConfig
target(test_file
);
48 ASSERT_TRUE(target
.Read());
51 EXPECT_TRUE(target
.GetString(kXmppLoginConfigPath
, &value
));
52 EXPECT_EQ("test@gmail.com", value
);
53 EXPECT_TRUE(target
.GetString(kXmppAuthTokenConfigPath
, &value
));
54 EXPECT_EQ("TEST_AUTH_TOKEN", value
);
55 EXPECT_TRUE(target
.GetString(kHostIdConfigPath
, &value
));
56 EXPECT_EQ("TEST_HOST_ID", value
);
57 EXPECT_TRUE(target
.GetString(kHostNameConfigPath
, &value
));
58 EXPECT_EQ("TEST_MACHINE_NAME", value
);
59 EXPECT_TRUE(target
.GetString(kPrivateKeyConfigPath
, &value
));
60 EXPECT_EQ("TEST_PRIVATE_KEY", value
);
62 EXPECT_FALSE(target
.GetString("non_existent_value", &value
));
65 TEST_F(JsonHostConfigTest
, Write
) {
66 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
68 base::FilePath test_file
= test_dir_
.path().AppendASCII("write.json");
69 WriteTestFile(test_file
);
70 JsonHostConfig
target(test_file
);
71 ASSERT_TRUE(target
.Read());
73 std::string new_auth_token_value
= "NEW_AUTH_TOKEN";
74 target
.SetString(kXmppAuthTokenConfigPath
, new_auth_token_value
);
75 ASSERT_TRUE(target
.Save());
77 // Now read the file again and check that the value has been written.
78 JsonHostConfig
reader(test_file
);
79 ASSERT_TRUE(reader
.Read());
82 EXPECT_TRUE(reader
.GetString(kXmppLoginConfigPath
, &value
));
83 EXPECT_EQ("test@gmail.com", value
);
84 EXPECT_TRUE(reader
.GetString(kXmppAuthTokenConfigPath
, &value
));
85 EXPECT_EQ(new_auth_token_value
, value
);
86 EXPECT_TRUE(reader
.GetString(kHostIdConfigPath
, &value
));
87 EXPECT_EQ("TEST_HOST_ID", value
);
88 EXPECT_TRUE(reader
.GetString(kHostNameConfigPath
, &value
));
89 EXPECT_EQ("TEST_MACHINE_NAME", value
);
90 EXPECT_TRUE(reader
.GetString(kPrivateKeyConfigPath
, &value
));
91 EXPECT_EQ("TEST_PRIVATE_KEY", value
);