File: name_value_pairs_parser_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (179 lines) | stat: -rw-r--r-- 5,999 bytes parent folder | download | duplicates (6)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/system/name_value_pairs_parser.h"

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

namespace ash::system {

// A parameterized test class running tests for all the formats that are
// compatible with VPD dumps.
class VpdDumpNameValuePairsParserTest
    : public testing::TestWithParam<NameValuePairsFormat> {
 protected:
  NameValuePairsParser::NameValueMap map;
};

TEST_P(VpdDumpNameValuePairsParserTest, TestParseNameValuePairs) {
  const NameValuePairsFormat format = GetParam();
  NameValuePairsParser parser(&map);

  const std::string contents1 = R"(
"initial_locale"="ja"
"keyboard_layout"="mozc-jp"
"empty"=""
)";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents1, format));
  EXPECT_EQ(3U, map.size());
  EXPECT_EQ("ja", map["initial_locale"]);
  EXPECT_EQ("mozc-jp", map["keyboard_layout"]);
  EXPECT_EQ("", map["empty"]);

  map.clear();
  const std::string contents2 = R"(
"quoted"=""quote""
"serial_number"="BADBADBAD""
"model_name"="15" Chromebook"
)";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents2, format));
  EXPECT_EQ(3U, map.size());
  EXPECT_EQ("\"quote\"", map["quoted"]);
  EXPECT_EQ("BADBADBAD\"", map["serial_number"]);
  EXPECT_EQ("15\" Chromebook", map["model_name"]);

  map.clear();
  const std::string contents3 = R"(
"a"="
"a"=
"a=b"="c"
""="value"
)";
  EXPECT_FALSE(parser.ParseNameValuePairs(contents3, format));
  EXPECT_EQ(0U, map.size());
}

INSTANTIATE_TEST_SUITE_P(NameValuePairs,
                         VpdDumpNameValuePairsParserTest,
                         testing::Values(NameValuePairsFormat::kVpdDump,
                                         NameValuePairsFormat::kMachineInfo));

TEST(NameValuePairsParser, TestParseNameValuePairsInVpdDumpFormat) {
  constexpr NameValuePairsFormat format = NameValuePairsFormat::kVpdDump;
  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);

  // Names must be quoted in VPD dump format. Unquoted names are ignored.
  const std::string contents1 = R"(
"name1"="value1"
name2="value2"
"name3"="value3"
name4="value4"
)";
  EXPECT_FALSE(parser.ParseNameValuePairs(contents1, format));
  EXPECT_EQ(2U, map.size());
  EXPECT_EQ("value1", map["name1"]);
  EXPECT_EQ("value3", map["name3"]);
}

TEST(NameValuePairsParser, TestParseErrorInVpdDumpFormat) {
  constexpr NameValuePairsFormat format = NameValuePairsFormat::kVpdDump;
  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);

  // Names must be quoted in VPD dump format. Unquoted names are ignored.
  const std::string contents1 = R"(
"name1"="value1"
# RW_VPD execute error.
)";
  EXPECT_FALSE(parser.ParseNameValuePairs(contents1, format));
  EXPECT_EQ(1U, map.size());
  EXPECT_EQ("value1", map["name1"]);
}

TEST(NameValuePairsParser, TestParseNameValuePairsInMachineInfoFormat) {
  constexpr NameValuePairsFormat format = NameValuePairsFormat::kMachineInfo;
  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);

  // Names do not have to be quoted in machine info format.
  const std::string contents1 = R"(
"name1"="value1"
name2="value2"
"name3"="value3"
name4="value4"
)";
  EXPECT_TRUE(parser.ParseNameValuePairs(contents1, format));
  EXPECT_EQ(4U, map.size());
  EXPECT_EQ("value1", map["name1"]);
  EXPECT_EQ("value2", map["name2"]);
  EXPECT_EQ("value3", map["name3"]);
  EXPECT_EQ("value4", map["name4"]);

  map.clear();
  const std::string contents2 = R"(
="value"
)";
  EXPECT_FALSE(parser.ParseNameValuePairs(contents2, format));
  EXPECT_EQ(0U, map.size());
}

TEST(NameValuePairsParser, TestParseNameValuePairsFromCrossystemTool) {
  // Sample output is taken from the /usr/bin/crossystem tool.
  const base::CommandLine command(
      {"/bin/echo",
       // Below is single string argument.
       "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"
       "wpsw_cur               = 1             # Firmware hardware WP switch "
       "pos\n"});

  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);
  parser.ParseNameValuePairsFromTool(command,
                                     NameValuePairsFormat::kCrossystem);
  EXPECT_EQ(7u, 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"]);
  EXPECT_EQ("1", map["wpsw_cur"]);
}

TEST(NameValuePairsParser, TestParseNameValuePairsFromEmptyTool) {
  // Sample output is taken from the /usr/bin/crossystem tool.
  const base::CommandLine command(
      {"/bin/echo",
       // Use empty string argument to check that tool caller can handle it.
       ""});

  NameValuePairsParser::NameValueMap map;
  NameValuePairsParser parser(&map);
  parser.ParseNameValuePairsFromTool(command,
                                     NameValuePairsFormat::kCrossystem);
  EXPECT_TRUE(map.empty());
}

TEST(NameValuePairsParser, DeletePairsWithValue) {
  NameValuePairsParser::NameValueMap map = {
      {"foo", "good"}, {"bar", "bad"}, {"baz", "good"}, {"end", "bad"},
  };
  NameValuePairsParser parser(&map);
  parser.DeletePairsWithValue("bad");
  ASSERT_EQ(2u, map.size());
  EXPECT_EQ("good", map["foo"]);
  EXPECT_EQ("good", map["baz"]);
}

}  // namespace ash::system