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 "chrome/browser/chromeos/system/name_value_pairs_parser.h" | 5 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace chromeos { | 10 namespace chromeos { |
11 namespace system { | 11 namespace system { |
12 | 12 |
13 TEST(NameValuePairsParser, TestGetSingleValueFromTool) { | 13 TEST(NameValuePairsParser, TestGetSingleValueFromTool) { |
14 NameValuePairsParser::NameValueMap map; | 14 NameValuePairsParser::NameValueMap map; |
15 NameValuePairsParser parser(&map); | 15 NameValuePairsParser parser(&map); |
16 const char* command[] = { "/bin/echo", "Foo" }; | 16 const char* command[] = { "/bin/echo", "Foo" }; |
17 EXPECT_TRUE(parser.GetSingleValueFromTool(arraysize(command), command, | 17 EXPECT_TRUE(parser.GetSingleValueFromTool(arraysize(command), command, |
18 "foo")); | 18 "foo")); |
19 ASSERT_EQ(1U, map.size()); | 19 ASSERT_EQ(1U, map.size()); |
20 EXPECT_EQ("Foo", map["foo"]); | 20 EXPECT_EQ("Foo", map["foo"]); |
21 } | 21 } |
22 | 22 |
23 TEST(NameValuePairsParser, TestParseNameValuePairs) { | 23 TEST(NameValuePairsParser, TestParseNameValuePairs) { |
24 NameValuePairsParser::NameValueMap map; | 24 NameValuePairsParser::NameValueMap map; |
25 NameValuePairsParser parser(&map); | 25 NameValuePairsParser parser(&map); |
26 const std::string contents1 = "foo=Foo bar=Bar\nfoobar=FooBar\n"; | 26 const std::string contents1 = "foo=Foo bar=Bar\nfoobar=FooBar\n"; |
27 EXPECT_TRUE(parser.ParseNameValuePairs(contents1, "=", " \n")); | 27 EXPECT_TRUE(parser.ParseNameValuePairs(contents1, "=", " \n")); |
28 ASSERT_EQ(3U, map.size()); | 28 EXPECT_EQ(3U, map.size()); |
29 EXPECT_EQ("Foo", map["foo"]); | 29 EXPECT_EQ("Foo", map["foo"]); |
30 EXPECT_EQ("Bar", map["bar"]); | 30 EXPECT_EQ("Bar", map["bar"]); |
31 EXPECT_EQ("FooBar", map["foobar"]); | 31 EXPECT_EQ("FooBar", map["foobar"]); |
32 | 32 |
33 map.clear(); | 33 map.clear(); |
34 const std::string contents2 = "foo=Foo,bar=Bar"; | 34 const std::string contents2 = "foo=Foo,bar=Bar"; |
35 EXPECT_TRUE(parser.ParseNameValuePairs(contents2, "=", ",\n")); | 35 EXPECT_TRUE(parser.ParseNameValuePairs(contents2, "=", ",\n")); |
36 ASSERT_EQ(2U, map.size()); | 36 EXPECT_EQ(2U, map.size()); |
37 EXPECT_EQ("Foo", map["foo"]); | 37 EXPECT_EQ("Foo", map["foo"]); |
38 EXPECT_EQ("Bar", map["bar"]); | 38 EXPECT_EQ("Bar", map["bar"]); |
39 | 39 |
40 map.clear(); | 40 map.clear(); |
41 const std::string contents3 = "foo=Foo=foo,bar=Bar"; | 41 const std::string contents3 = "foo=Foo=foo,bar=Bar"; |
42 EXPECT_FALSE(parser.ParseNameValuePairs(contents3, "=", ",\n")); | 42 EXPECT_TRUE(parser.ParseNameValuePairs(contents3, "=", ",\n")); |
| 43 EXPECT_EQ(2U, map.size()); |
| 44 EXPECT_EQ("Foo=foo", map["foo"]); |
| 45 EXPECT_EQ("Bar", map["bar"]); |
43 | 46 |
44 map.clear(); | 47 map.clear(); |
45 const std::string contents4 = "foo=Foo,=Bar"; | 48 const std::string contents4 = "foo=Foo,=Bar"; |
46 EXPECT_FALSE(parser.ParseNameValuePairs(contents4, "=", ",\n")); | 49 EXPECT_FALSE(parser.ParseNameValuePairs(contents4, "=", ",\n")); |
| 50 EXPECT_EQ(1U, map.size()); |
| 51 EXPECT_EQ("Foo", map["foo"]); |
47 | 52 |
48 map.clear(); | 53 map.clear(); |
49 const std::string contents5 = | 54 const std::string contents5 = |
50 "\"initial_locale\"=\"ja\"\n" | 55 "\"initial_locale\"=\"ja\"\n" |
51 "\"initial_timezone\"=\"Asia/Tokyo\"\n" | 56 "\"initial_timezone\"=\"Asia/Tokyo\"\n" |
52 "\"keyboard_layout\"=\"mozc-jp\"\n"; | 57 "\"keyboard_layout\"=\"mozc-jp\"\n"; |
53 EXPECT_TRUE(parser.ParseNameValuePairs(contents5, "=", "\n")); | 58 EXPECT_TRUE(parser.ParseNameValuePairs(contents5, "=", "\n")); |
54 ASSERT_EQ(3U, map.size()); | 59 EXPECT_EQ(3U, map.size()); |
55 EXPECT_EQ("ja", map["initial_locale"]); | 60 EXPECT_EQ("ja", map["initial_locale"]); |
56 EXPECT_EQ("Asia/Tokyo", map["initial_timezone"]); | 61 EXPECT_EQ("Asia/Tokyo", map["initial_timezone"]); |
57 EXPECT_EQ("mozc-jp", map["keyboard_layout"]); | 62 EXPECT_EQ("mozc-jp", map["keyboard_layout"]); |
58 } | 63 } |
59 | 64 |
| 65 TEST(NameValuePairsParser, TestParseNameValuePairsWithComments) { |
| 66 NameValuePairsParser::NameValueMap map; |
| 67 NameValuePairsParser parser(&map); |
| 68 |
| 69 const std::string contents1 = "foo=Foo,bar=#Bar,baz= 0 #Baz"; |
| 70 EXPECT_TRUE(parser.ParseNameValuePairsWithComments( |
| 71 contents1, "=", ",\n", "#")); |
| 72 EXPECT_EQ(3U, map.size()); |
| 73 EXPECT_EQ("Foo", map["foo"]); |
| 74 EXPECT_EQ("", map["bar"]); |
| 75 EXPECT_EQ("0", map["baz"]); |
| 76 |
| 77 map.clear(); |
| 78 const std::string contents2 = "foo="; |
| 79 EXPECT_TRUE(parser.ParseNameValuePairsWithComments( |
| 80 contents2, "=", ",\n", "#")); |
| 81 EXPECT_EQ(1U, map.size()); |
| 82 EXPECT_EQ("", map["foo"]); |
| 83 |
| 84 map.clear(); |
| 85 const std::string contents3 = " \t ,,#all empty,"; |
| 86 EXPECT_FALSE(parser.ParseNameValuePairsWithComments( |
| 87 contents3, "=", ",\n", "#")); |
| 88 EXPECT_EQ(0U, map.size()); |
| 89 } |
| 90 |
60 TEST(NameValuePairsParser, TestParseNameValuePairsFromTool) { | 91 TEST(NameValuePairsParser, TestParseNameValuePairsFromTool) { |
61 // Sample output is taken from the /usr/bin/crosssytem tool. | 92 // Sample output is taken from the /usr/bin/crosssytem tool. |
62 const char* command[] = { "/bin/echo", | 93 const char* command[] = { "/bin/echo", |
63 "arch = x86 # Platform architecture\n" \ | 94 "arch = x86 # Platform architecture\n" \ |
64 "cros_debug = 1 # OS should allow debug\n" \ | 95 "cros_debug = 1 # OS should allow debug\n" \ |
65 "dbg_reset = (error) # Debug reset mode request\n" \ | 96 "dbg_reset = (error) # Debug reset mode request\n" \ |
66 "key#with_comment = some value # Multiple # comment # delims\n" \ | 97 "key#with_comment = some value # Multiple # comment # delims\n" \ |
67 "key = # No value." | 98 "key = # No value.\n" \ |
| 99 "vdat_timers = " \ |
| 100 "LFS=0,0 LF=1784220250,2971030570 LK=9064076660,9342689170 " \ |
| 101 "# Timer values from VbSharedData\n" |
68 }; | 102 }; |
69 | 103 |
70 NameValuePairsParser::NameValueMap map; | 104 NameValuePairsParser::NameValueMap map; |
71 NameValuePairsParser parser(&map); | 105 NameValuePairsParser parser(&map); |
72 parser.ParseNameValuePairsFromTool( | 106 parser.ParseNameValuePairsFromTool( |
73 arraysize(command), command, "=", "\n", "#"); | 107 arraysize(command), command, "=", "\n", "#"); |
| 108 EXPECT_EQ(6u, map.size()); |
74 EXPECT_EQ("x86", map["arch"]); | 109 EXPECT_EQ("x86", map["arch"]); |
75 EXPECT_EQ("1", map["cros_debug"]); | 110 EXPECT_EQ("1", map["cros_debug"]); |
76 EXPECT_EQ("(error)", map["dbg_reset"]); | 111 EXPECT_EQ("(error)", map["dbg_reset"]); |
77 EXPECT_EQ("some value", map["key#with_comment"]); | 112 EXPECT_EQ("some value", map["key#with_comment"]); |
78 EXPECT_EQ("", map["key"]); | 113 EXPECT_EQ("", map["key"]); |
79 EXPECT_EQ(5u, map.size()); | 114 EXPECT_EQ("LFS=0,0 LF=1784220250,2971030570 LK=9064076660,9342689170", |
| 115 map["vdat_timers"]); |
80 } | 116 } |
81 | 117 |
82 } // namespace system | 118 } // namespace system |
83 } // namespace chromeos | 119 } // namespace chromeos |
OLD | NEW |