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 "remoting/host/host_config.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 const char* kTestConfig
=
19 " \"xmpp_login\" : \"test@gmail.com\",\n"
20 " \"xmpp_auth_token\" : \"TEST_AUTH_TOKEN\",\n"
21 " \"host_id\" : \"TEST_HOST_ID\",\n"
22 " \"host_name\" : \"TEST_MACHINE_NAME\",\n"
23 " \"private_key\" : \"TEST_PRIVATE_KEY\"\n"
28 class HostConfigTest
: public testing::Test
{
32 static void WriteTestFile(const base::FilePath
& filename
) {
33 base::WriteFile(filename
, kTestConfig
, std::strlen(kTestConfig
));
36 // The temporary directory used to contain the test operations.
37 base::ScopedTempDir test_dir_
;
40 DISALLOW_COPY_AND_ASSIGN(HostConfigTest
);
43 TEST_F(HostConfigTest
, InvalidFile
) {
44 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
45 base::FilePath non_existent_file
=
46 test_dir_
.path().AppendASCII("non_existent.json");
47 EXPECT_FALSE(HostConfigFromJsonFile(non_existent_file
));
50 TEST_F(HostConfigTest
, Read
) {
51 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
52 base::FilePath test_file
= test_dir_
.path().AppendASCII("read.json");
53 WriteTestFile(test_file
);
54 scoped_ptr
<base::DictionaryValue
> target(HostConfigFromJsonFile(test_file
));
58 EXPECT_TRUE(target
->GetString(kXmppLoginConfigPath
, &value
));
59 EXPECT_EQ("test@gmail.com", value
);
60 EXPECT_TRUE(target
->GetString(kXmppAuthTokenConfigPath
, &value
));
61 EXPECT_EQ("TEST_AUTH_TOKEN", value
);
62 EXPECT_TRUE(target
->GetString(kHostIdConfigPath
, &value
));
63 EXPECT_EQ("TEST_HOST_ID", value
);
64 EXPECT_TRUE(target
->GetString(kHostNameConfigPath
, &value
));
65 EXPECT_EQ("TEST_MACHINE_NAME", value
);
66 EXPECT_TRUE(target
->GetString(kPrivateKeyConfigPath
, &value
));
67 EXPECT_EQ("TEST_PRIVATE_KEY", value
);
69 EXPECT_FALSE(target
->GetString("non_existent_value", &value
));
72 TEST_F(HostConfigTest
, Write
) {
73 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
75 base::FilePath test_file
= test_dir_
.path().AppendASCII("write.json");
76 WriteTestFile(test_file
);
77 scoped_ptr
<base::DictionaryValue
> target(HostConfigFromJsonFile(test_file
));
80 std::string new_auth_token_value
= "NEW_AUTH_TOKEN";
81 target
->SetString(kXmppAuthTokenConfigPath
, new_auth_token_value
);
82 ASSERT_TRUE(HostConfigToJsonFile(*target
, test_file
));
84 // Now read the file again and check that the value has been written.
85 scoped_ptr
<base::DictionaryValue
> reader(HostConfigFromJsonFile(test_file
));
89 EXPECT_TRUE(reader
->GetString(kXmppLoginConfigPath
, &value
));
90 EXPECT_EQ("test@gmail.com", value
);
91 EXPECT_TRUE(reader
->GetString(kXmppAuthTokenConfigPath
, &value
));
92 EXPECT_EQ(new_auth_token_value
, value
);
93 EXPECT_TRUE(reader
->GetString(kHostIdConfigPath
, &value
));
94 EXPECT_EQ("TEST_HOST_ID", value
);
95 EXPECT_TRUE(reader
->GetString(kHostNameConfigPath
, &value
));
96 EXPECT_EQ("TEST_MACHINE_NAME", value
);
97 EXPECT_TRUE(reader
->GetString(kPrivateKeyConfigPath
, &value
));
98 EXPECT_EQ("TEST_PRIVATE_KEY", value
);
101 } // namespace remoting