1 // Copyright 2013 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.h>
39
40 #include "client/linux/minidump_writer/proc_cpuinfo_reader.h"
41 #include "breakpad_googletest_includes.h"
42 #include "common/linux/scoped_tmpfile.h"
43
44 using namespace google_breakpad;
45
46 namespace {
47
48 typedef testing::Test ProcCpuInfoReaderTest;
49
50 }
51
TEST(ProcCpuInfoReaderTest,EmptyFile)52 TEST(ProcCpuInfoReaderTest, EmptyFile) {
53 ScopedTmpFile file;
54 ASSERT_TRUE(file.InitString(""));
55 ProcCpuInfoReader reader(file.GetFd());
56
57 const char* field;
58 ASSERT_FALSE(reader.GetNextField(&field));
59 }
60
TEST(ProcCpuInfoReaderTest,OneLineTerminated)61 TEST(ProcCpuInfoReaderTest, OneLineTerminated) {
62 ScopedTmpFile file;
63 ASSERT_TRUE(file.InitString("foo : bar\n"));
64 ProcCpuInfoReader reader(file.GetFd());
65
66 const char* field;
67 ASSERT_TRUE(reader.GetNextField(&field));
68 ASSERT_STREQ("foo", field);
69 ASSERT_STREQ("bar", reader.GetValue());
70
71 ASSERT_FALSE(reader.GetNextField(&field));
72 }
73
TEST(ProcCpuInfoReaderTest,OneLine)74 TEST(ProcCpuInfoReaderTest, OneLine) {
75 ScopedTmpFile file;
76 ASSERT_TRUE(file.InitString("foo : bar"));
77 ProcCpuInfoReader reader(file.GetFd());
78
79 const char* field;
80 size_t value_len;
81 ASSERT_TRUE(reader.GetNextField(&field));
82 ASSERT_STREQ("foo", field);
83 ASSERT_STREQ("bar", reader.GetValueAndLen(&value_len));
84 ASSERT_EQ(3U, value_len);
85
86 ASSERT_FALSE(reader.GetNextField(&field));
87 }
88
TEST(ProcCpuInfoReaderTest,TwoLinesTerminated)89 TEST(ProcCpuInfoReaderTest, TwoLinesTerminated) {
90 ScopedTmpFile file;
91 ASSERT_TRUE(file.InitString("foo : bar\nzoo : tut\n"));
92 ProcCpuInfoReader reader(file.GetFd());
93
94 const char* field;
95 ASSERT_TRUE(reader.GetNextField(&field));
96 ASSERT_STREQ("foo", field);
97 ASSERT_STREQ("bar", reader.GetValue());
98
99 ASSERT_TRUE(reader.GetNextField(&field));
100 ASSERT_STREQ("zoo", field);
101 ASSERT_STREQ("tut", reader.GetValue());
102
103 ASSERT_FALSE(reader.GetNextField(&field));
104 }
105
TEST(ProcCpuInfoReaderTest,SkipMalformedLine)106 TEST(ProcCpuInfoReaderTest, SkipMalformedLine) {
107 ScopedTmpFile file;
108 ASSERT_TRUE(file.InitString("this line should have a column\nfoo : bar\n"));
109 ProcCpuInfoReader reader(file.GetFd());
110
111 const char* field;
112 ASSERT_TRUE(reader.GetNextField(&field));
113 ASSERT_STREQ("foo", field);
114 ASSERT_STREQ("bar", reader.GetValue());
115
116 ASSERT_FALSE(reader.GetNextField(&field));
117 }
118
TEST(ProcCpuInfoReaderTest,SkipOneEmptyLine)119 TEST(ProcCpuInfoReaderTest, SkipOneEmptyLine) {
120 ScopedTmpFile file;
121 ASSERT_TRUE(file.InitString("\n\nfoo : bar\n"));
122 ProcCpuInfoReader reader(file.GetFd());
123
124 const char* field;
125 ASSERT_TRUE(reader.GetNextField(&field));
126 ASSERT_STREQ("foo", field);
127 ASSERT_STREQ("bar", reader.GetValue());
128
129 ASSERT_FALSE(reader.GetNextField(&field));
130 }
131
TEST(ProcCpuInfoReaderTest,SkipEmptyField)132 TEST(ProcCpuInfoReaderTest, SkipEmptyField) {
133 ScopedTmpFile file;
134 ASSERT_TRUE(file.InitString(" : bar\nzoo : tut\n"));
135 ProcCpuInfoReader reader(file.GetFd());
136
137 const char* field;
138 ASSERT_TRUE(reader.GetNextField(&field));
139 ASSERT_STREQ("zoo", field);
140 ASSERT_STREQ("tut", reader.GetValue());
141
142 ASSERT_FALSE(reader.GetNextField(&field));
143 }
144
TEST(ProcCpuInfoReaderTest,SkipTwoEmptyLines)145 TEST(ProcCpuInfoReaderTest, SkipTwoEmptyLines) {
146 ScopedTmpFile file;
147 ASSERT_TRUE(file.InitString("foo : bar\n\n\nfoo : bar\n"));
148 ProcCpuInfoReader reader(file.GetFd());
149
150 const char* field;
151 ASSERT_TRUE(reader.GetNextField(&field));
152 ASSERT_STREQ("foo", field);
153 ASSERT_STREQ("bar", reader.GetValue());
154
155 ASSERT_TRUE(reader.GetNextField(&field));
156 ASSERT_STREQ("foo", field);
157 ASSERT_STREQ("bar", reader.GetValue());
158
159 ASSERT_FALSE(reader.GetNextField(&field));
160 }
161
TEST(ProcCpuInfoReaderTest,FieldWithSpaces)162 TEST(ProcCpuInfoReaderTest, FieldWithSpaces) {
163 ScopedTmpFile file;
164 ASSERT_TRUE(file.InitString("foo bar : zoo\n"));
165 ProcCpuInfoReader reader(file.GetFd());
166
167 const char* field;
168 ASSERT_TRUE(reader.GetNextField(&field));
169 ASSERT_STREQ("foo bar", field);
170 ASSERT_STREQ("zoo", reader.GetValue());
171
172 ASSERT_FALSE(reader.GetNextField(&field));
173 }
174
TEST(ProcCpuInfoReaderTest,EmptyValue)175 TEST(ProcCpuInfoReaderTest, EmptyValue) {
176 ScopedTmpFile file;
177 ASSERT_TRUE(file.InitString("foo :\n"));
178 ProcCpuInfoReader reader(file.GetFd());
179
180 const char* field;
181 ASSERT_TRUE(reader.GetNextField(&field));
182 ASSERT_STREQ("foo", field);
183 size_t value_len;
184 ASSERT_STREQ("", reader.GetValueAndLen(&value_len));
185 ASSERT_EQ(0U, value_len);
186
187 ASSERT_FALSE(reader.GetNextField(&field));
188 }
189