1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ih_util.h"
18
19 #include <android-base/file.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <string>
23
24 using namespace android::base;
25 using namespace std;
26 using ::testing::StrEq;
27
TEST(IhUtilTest,ParseHeader)28 TEST(IhUtilTest, ParseHeader) {
29 header_t result, expected;
30 result = parseHeader(" \t \t\t ");
31 EXPECT_EQ(expected, result);
32
33 result = parseHeader(" \t 100 00\tOpQ \t wqrw");
34 expected = { "100", "00", "opq", "wqrw" };
35 EXPECT_EQ(expected, result);
36
37 result = parseHeader(" \t 100 00\toooh \t wTF", "\t");
38 expected = { "100 00", "oooh", "wtf" };
39 EXPECT_EQ(expected, result);
40
41 result = parseHeader("123,456,78_9", ",");
42 expected = { "123", "456", "78_9" };
43 EXPECT_EQ(expected, result);
44 }
45
TEST(IhUtilTest,ParseRecord)46 TEST(IhUtilTest, ParseRecord) {
47 record_t result, expected;
48 result = parseRecord(" \t \t\t ");
49 EXPECT_EQ(expected, result);
50
51 result = parseRecord(" \t 100 00\toooh \t wqrw");
52 expected = { "100", "00", "oooh", "wqrw" };
53 EXPECT_EQ(expected, result);
54
55 result = parseRecord(" \t 100 00\toooh \t wqrw", "\t");
56 expected = { "100 00", "oooh", "wqrw" };
57 EXPECT_EQ(expected, result);
58
59 result = parseRecord("123,456,78_9", ",");
60 expected = { "123", "456", "78_9" };
61 EXPECT_EQ(expected, result);
62
63 result = parseRecord("", " ");
64 EXPECT_TRUE(result.empty());
65 }
66
TEST(IhUtilTest,ParseRecordByColumns)67 TEST(IhUtilTest, ParseRecordByColumns) {
68 record_t result, expected;
69 std::vector<int> indices = { 3, 10 };
70
71 result = parseRecordByColumns("12345", indices);
72 expected = {};
73 EXPECT_EQ(expected, result);
74
75 result = parseRecordByColumns("abc \t2345 6789 ", indices);
76 expected = { "abc", "2345 6789" };
77 EXPECT_EQ(expected, result);
78
79 std::string extraColumn1 = "abc \t23456789 bob";
80 std::string emptyMidColm = "abc \t bob";
81 std::string longFirstClm = "abcdefgt\t6789 bob";
82 std::string lngFrstEmpty = "abcdefgt\t bob";
83
84 result = parseRecordByColumns(extraColumn1, indices);
85 expected = { "abc", "23456789 bob" };
86 EXPECT_EQ(expected, result);
87
88 // 2nd column should be treated as an empty entry.
89 result = parseRecordByColumns(emptyMidColm, indices);
90 expected = { "abc", "bob" };
91 EXPECT_EQ(expected, result);
92
93 result = parseRecordByColumns(longFirstClm, indices);
94 expected = { "abcdefgt", "6789 bob" };
95 EXPECT_EQ(expected, result);
96
97 result = parseRecordByColumns(lngFrstEmpty, indices);
98 expected = { "abcdefgt", "bob" };
99 EXPECT_EQ(expected, result);
100 }
101
TEST(IhUtilTest,stripPrefix)102 TEST(IhUtilTest, stripPrefix) {
103 string data1 = "Swap: abc ";
104 EXPECT_TRUE(stripPrefix(&data1, "Swap:"));
105 EXPECT_THAT(data1, StrEq("abc"));
106
107 string data2 = "Swap: abc ";
108 EXPECT_FALSE(stripPrefix(&data2, "Total:"));
109 EXPECT_THAT(data2, StrEq("Swap: abc "));
110
111 string data3 = "Swap: abc ";
112 EXPECT_TRUE(stripPrefix(&data3, "Swa"));
113 EXPECT_THAT(data3, StrEq("p: abc"));
114
115 string data4 = "Swap: abc ";
116 EXPECT_FALSE(stripPrefix(&data4, "Swa", true));
117 EXPECT_THAT(data4, StrEq("Swap: abc "));
118 }
119
TEST(IhUtilTest,stripSuffix)120 TEST(IhUtilTest, stripSuffix) {
121 string data1 = " 243%abc";
122 EXPECT_TRUE(stripSuffix(&data1, "abc"));
123 EXPECT_THAT(data1, StrEq("243%"));
124
125 string data2 = " 243%abc";
126 EXPECT_FALSE(stripSuffix(&data2, "Not right"));
127 EXPECT_THAT(data2, StrEq(" 243%abc"));
128
129 string data3 = " 243%abc";
130 EXPECT_TRUE(stripSuffix(&data3, "bc"));
131 EXPECT_THAT(data3, StrEq("243%a"));
132
133 string data4 = " 243%abc";
134 EXPECT_FALSE(stripSuffix(&data4, "bc", true));
135 EXPECT_THAT(data4, StrEq(" 243%abc"));
136 }
137
TEST(IhUtilTest,behead)138 TEST(IhUtilTest, behead) {
139 string testcase1 = "81002 dropbox_file_copy (a)(b)";
140 EXPECT_THAT(behead(&testcase1, ' '), StrEq("81002"));
141 EXPECT_THAT(behead(&testcase1, ' '), StrEq("dropbox_file_copy"));
142 EXPECT_THAT(testcase1, "(a)(b)");
143
144 string testcase2 = "adbce,erwqr";
145 EXPECT_THAT(behead(&testcase2, ' '), StrEq("adbce,erwqr"));
146 EXPECT_THAT(testcase2, "");
147
148 string testcase3 = "first second";
149 EXPECT_THAT(behead(&testcase3, ' '), StrEq("first"));
150 EXPECT_THAT(behead(&testcase3, ' '), StrEq("second"));
151 EXPECT_THAT(testcase3, "");
152 }
153
TEST(IhUtilTest,Reader)154 TEST(IhUtilTest, Reader) {
155 TemporaryFile tf;
156 ASSERT_NE(tf.fd, -1);
157 ASSERT_TRUE(WriteStringToFile("test string\nsecond\nooo\n", tf.path));
158
159 Reader r(tf.fd);
160 string line;
161 ASSERT_TRUE(r.readLine(&line));
162 EXPECT_THAT(line, StrEq("test string"));
163 ASSERT_TRUE(r.readLine(&line));
164 EXPECT_THAT(line, StrEq("second"));
165 ASSERT_TRUE(r.readLine(&line));
166 EXPECT_THAT(line, StrEq("ooo"));
167 ASSERT_FALSE(r.readLine(&line));
168 ASSERT_TRUE(r.ok(&line));
169 }
170
TEST(IhUtilTest,ReaderEmpty)171 TEST(IhUtilTest, ReaderEmpty) {
172 TemporaryFile tf;
173 ASSERT_NE(tf.fd, -1);
174 ASSERT_TRUE(WriteStringToFile("", tf.path));
175
176 Reader r(tf.fd);
177 string line;
178 ASSERT_FALSE(r.readLine(&line));
179 EXPECT_THAT(line, StrEq(""));
180 ASSERT_TRUE(r.ok(&line));
181 }
182
TEST(IhUtilTest,ReaderMultipleEmptyLines)183 TEST(IhUtilTest, ReaderMultipleEmptyLines) {
184 TemporaryFile tf;
185 ASSERT_NE(tf.fd, -1);
186 ASSERT_TRUE(WriteStringToFile("\n\n", tf.path));
187
188 Reader r(tf.fd);
189 string line;
190 ASSERT_TRUE(r.readLine(&line));
191 EXPECT_THAT(line, StrEq(""));
192 ASSERT_TRUE(r.readLine(&line));
193 EXPECT_THAT(line, StrEq(""));
194 ASSERT_FALSE(r.readLine(&line));
195 EXPECT_THAT(line, StrEq(""));
196 ASSERT_TRUE(r.ok(&line));
197 }
198
TEST(IhUtilTest,ReaderFailedNegativeFd)199 TEST(IhUtilTest, ReaderFailedNegativeFd) {
200 Reader r(-123);
201 string line;
202 EXPECT_FALSE(r.readLine(&line));
203 EXPECT_FALSE(r.ok(&line));
204 EXPECT_THAT(line, StrEq("Invalid fd -123"));
205 }
206
TEST(IhUtilTest,ReaderFailedBadFd)207 TEST(IhUtilTest, ReaderFailedBadFd) {
208 Reader r(1231432);
209 string line;
210 EXPECT_FALSE(r.readLine(&line));
211 EXPECT_FALSE(r.ok(&line));
212 EXPECT_THAT(line, StrEq("Invalid fd 1231432"));
213 }
214