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