summaryrefslogtreecommitdiffstats
path: root/chromeos/system/name_value_pairs_parser_unittest.cc
blob: e6275211ca0b645f6b8ba68b7318df60f7fef822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/system/name_value_pairs_parser.h"

#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {
namespace system {

TEST(NameValuePairsParser, TestGetSingleValueFromTool) {
  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);
  const char* command[] = { "/bin/echo", "Foo" };
  EXPECT_TRUE(parser.GetSingleValueFromTool(arraysize(command), command,
                                            "foo"));
  ASSERT_EQ(1U, map.size());
  EXPECT_EQ("Foo", map["foo"]);
}

TEST(NameValuePairsParser, TestParseNameValuePairs) {
  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);
  const std::string contents1 = "foo=Foo bar=Bar\nfoobar=FooBar\n";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents1, "=", " \n"));
  EXPECT_EQ(3U, map.size());
  EXPECT_EQ("Foo", map["foo"]);
  EXPECT_EQ("Bar", map["bar"]);
  EXPECT_EQ("FooBar", map["foobar"]);

  map.clear();
  const std::string contents2 = "foo=Foo,bar=Bar";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents2, "=", ",\n"));
  EXPECT_EQ(2U, map.size());
  EXPECT_EQ("Foo", map["foo"]);
  EXPECT_EQ("Bar", map["bar"]);

  map.clear();
  const std::string contents3 = "foo=Foo=foo,bar=Bar";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents3, "=", ",\n"));
  EXPECT_EQ(2U, map.size());
  EXPECT_EQ("Foo=foo", map["foo"]);
  EXPECT_EQ("Bar", map["bar"]);

  map.clear();
  const std::string contents4 = "foo=Foo,=Bar";
  EXPECT_FALSE(parser.ParseNameValuePairs(contents4, "=", ",\n"));
  EXPECT_EQ(1U, map.size());
  EXPECT_EQ("Foo", map["foo"]);

  map.clear();
  const std::string contents5 =
      "\"initial_locale\"=\"ja\"\n"
      "\"initial_timezone\"=\"Asia/Tokyo\"\n"
      "\"keyboard_layout\"=\"mozc-jp\"\n";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents5, "=", "\n"));
  EXPECT_EQ(3U, map.size());
  EXPECT_EQ("ja", map["initial_locale"]);
  EXPECT_EQ("Asia/Tokyo", map["initial_timezone"]);
  EXPECT_EQ("mozc-jp", map["keyboard_layout"]);
}

TEST(NameValuePairsParser, TestParseNameValuePairsWithComments) {
  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);

  const std::string contents1 = "foo=Foo,bar=#Bar,baz= 0 #Baz";
  EXPECT_TRUE(parser.ParseNameValuePairsWithComments(
      contents1, "=", ",\n", "#"));
  EXPECT_EQ(3U, map.size());
  EXPECT_EQ("Foo", map["foo"]);
  EXPECT_EQ("", map["bar"]);
  EXPECT_EQ("0", map["baz"]);

  map.clear();
  const std::string contents2 = "foo=";
  EXPECT_TRUE(parser.ParseNameValuePairsWithComments(
      contents2, "=", ",\n", "#"));
  EXPECT_EQ(1U, map.size());
  EXPECT_EQ("", map["foo"]);

  map.clear();
  const std::string contents3 = " \t ,,#all empty,";
  EXPECT_FALSE(parser.ParseNameValuePairsWithComments(
      contents3, "=", ",\n", "#"));
  EXPECT_EQ(0U, map.size());
}

TEST(NameValuePairsParser, TestParseNameValuePairsFromTool) {
  // Sample output is taken from the /usr/bin/crosssytem tool.
  const char* command[] = { "/bin/echo",
    "arch                   = x86           # Platform architecture\n" \
    "cros_debug             = 1             # OS should allow debug\n" \
    "dbg_reset              = (error)       # Debug reset mode request\n" \
    "key#with_comment       = some value    # Multiple # comment # delims\n" \
    "key                    =               # No value.\n" \
    "vdat_timers            = " \
        "LFS=0,0 LF=1784220250,2971030570 LK=9064076660,9342689170 " \
        "# Timer values from VbSharedData\n"
  };

  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);
  parser.ParseNameValuePairsFromTool(
      arraysize(command), command, "=", "\n", "#");
  EXPECT_EQ(6u, map.size());
  EXPECT_EQ("x86", map["arch"]);
  EXPECT_EQ("1", map["cros_debug"]);
  EXPECT_EQ("(error)", map["dbg_reset"]);
  EXPECT_EQ("some value", map["key#with_comment"]);
  EXPECT_EQ("", map["key"]);
  EXPECT_EQ("LFS=0,0 LF=1784220250,2971030570 LK=9064076660,9342689170",
            map["vdat_timers"]);
}

}  // namespace system
}  // namespace chromeos