• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/test_utils.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <string>
24 
25 using namespace android::base;
26 using namespace std;
27 using ::testing::StrEq;
28 
TEST(IhUtilTest,ParseHeader)29 TEST(IhUtilTest, ParseHeader) {
30     header_t result, expected;
31     result = parseHeader(" \t \t\t ");
32     EXPECT_EQ(expected, result);
33 
34     result = parseHeader(" \t 100 00\tOpQ \t wqrw");
35     expected = { "100", "00", "opq", "wqrw" };
36     EXPECT_EQ(expected, result);
37 
38     result = parseHeader(" \t 100 00\toooh \t wTF", "\t");
39     expected = { "100 00", "oooh", "wtf" };
40     EXPECT_EQ(expected, result);
41 
42     result = parseHeader("123,456,78_9", ",");
43     expected = { "123", "456", "78_9" };
44     EXPECT_EQ(expected, result);
45 }
46 
TEST(IhUtilTest,ParseRecord)47 TEST(IhUtilTest, ParseRecord) {
48     record_t result, expected;
49     result = parseRecord(" \t \t\t ");
50     EXPECT_EQ(expected, result);
51 
52     result = parseRecord(" \t 100 00\toooh \t wqrw");
53     expected = { "100", "00", "oooh", "wqrw" };
54     EXPECT_EQ(expected, result);
55 
56     result = parseRecord(" \t 100 00\toooh \t wqrw", "\t");
57     expected = { "100 00", "oooh", "wqrw" };
58     EXPECT_EQ(expected, result);
59 
60     result = parseRecord("123,456,78_9", ",");
61     expected = { "123", "456", "78_9" };
62     EXPECT_EQ(expected, result);
63 
64     result = parseRecord("", " ");
65     EXPECT_TRUE(result.empty());
66 }
67 
TEST(IhUtilTest,ParseRecordByColumns)68 TEST(IhUtilTest, ParseRecordByColumns) {
69     record_t result, expected;
70     std::vector<int> indices = { 3, 10 };
71 
72     result = parseRecordByColumns("12345", indices);
73     expected = {};
74     EXPECT_EQ(expected, result);
75 
76     result = parseRecordByColumns("abc \t2345  6789 ", indices);
77     expected = { "abc", "2345  6789" };
78     EXPECT_EQ(expected, result);
79 
80     std::string extraColumn1 = "abc \t23456789 bob";
81     std::string emptyMidColm = "abc \t         bob";
82     std::string longFirstClm = "abcdefgt\t6789 bob";
83     std::string lngFrstEmpty = "abcdefgt\t     bob";
84 
85     result = parseRecordByColumns(extraColumn1, indices);
86     expected = { "abc", "23456789 bob" };
87     EXPECT_EQ(expected, result);
88 
89     // 2nd column should be treated as an empty entry.
90     result = parseRecordByColumns(emptyMidColm, indices);
91     expected = { "abc", "bob" };
92     EXPECT_EQ(expected, result);
93 
94     result = parseRecordByColumns(longFirstClm, indices);
95     expected = { "abcdefgt", "6789 bob" };
96     EXPECT_EQ(expected, result);
97 
98     result = parseRecordByColumns(lngFrstEmpty, indices);
99     expected = { "abcdefgt", "bob" };
100     EXPECT_EQ(expected, result);
101 }
102 
TEST(IhUtilTest,stripPrefix)103 TEST(IhUtilTest, stripPrefix) {
104     string data1 = "Swap: abc ";
105     EXPECT_TRUE(stripPrefix(&data1, "Swap:"));
106     EXPECT_THAT(data1, StrEq("abc"));
107 
108     string data2 = "Swap: abc ";
109     EXPECT_FALSE(stripPrefix(&data2, "Total:"));
110     EXPECT_THAT(data2, StrEq("Swap: abc "));
111 
112     string data3 = "Swap: abc ";
113     EXPECT_TRUE(stripPrefix(&data3, "Swa"));
114     EXPECT_THAT(data3, StrEq("p: abc"));
115 
116     string data4 = "Swap: abc ";
117     EXPECT_FALSE(stripPrefix(&data4, "Swa", true));
118     EXPECT_THAT(data4, StrEq("Swap: abc "));
119 }
120 
TEST(IhUtilTest,stripSuffix)121 TEST(IhUtilTest, stripSuffix) {
122     string data1 = " 243%abc";
123     EXPECT_TRUE(stripSuffix(&data1, "abc"));
124     EXPECT_THAT(data1, StrEq("243%"));
125 
126     string data2 = " 243%abc";
127     EXPECT_FALSE(stripSuffix(&data2, "Not right"));
128     EXPECT_THAT(data2, StrEq(" 243%abc"));
129 
130     string data3 = " 243%abc";
131     EXPECT_TRUE(stripSuffix(&data3, "bc"));
132     EXPECT_THAT(data3, StrEq("243%a"));
133 
134     string data4 = " 243%abc";
135     EXPECT_FALSE(stripSuffix(&data4, "bc", true));
136     EXPECT_THAT(data4, StrEq(" 243%abc"));
137 }
138 
TEST(IhUtilTest,behead)139 TEST(IhUtilTest, behead) {
140     string testcase1 = "81002 dropbox_file_copy (a)(b)";
141     EXPECT_THAT(behead(&testcase1, ' '), StrEq("81002"));
142     EXPECT_THAT(behead(&testcase1, ' '), StrEq("dropbox_file_copy"));
143     EXPECT_THAT(testcase1, "(a)(b)");
144 
145     string testcase2 = "adbce,erwqr";
146     EXPECT_THAT(behead(&testcase2, ' '), StrEq("adbce,erwqr"));
147     EXPECT_THAT(testcase2, "");
148 
149     string testcase3 = "first second";
150     EXPECT_THAT(behead(&testcase3, ' '), StrEq("first"));
151     EXPECT_THAT(behead(&testcase3, ' '), StrEq("second"));
152     EXPECT_THAT(testcase3, "");
153 }
154 
TEST(IhUtilTest,Reader)155 TEST(IhUtilTest, Reader) {
156     TemporaryFile tf;
157     ASSERT_NE(tf.fd, -1);
158     ASSERT_TRUE(WriteStringToFile("test string\nsecond\nooo\n", tf.path));
159 
160     Reader r(tf.fd);
161     string line;
162     ASSERT_TRUE(r.readLine(&line));
163     EXPECT_THAT(line, StrEq("test string"));
164     ASSERT_TRUE(r.readLine(&line));
165     EXPECT_THAT(line, StrEq("second"));
166     ASSERT_TRUE(r.readLine(&line));
167     EXPECT_THAT(line, StrEq("ooo"));
168     ASSERT_FALSE(r.readLine(&line));
169     ASSERT_TRUE(r.ok(&line));
170 }
171 
TEST(IhUtilTest,ReaderEmpty)172 TEST(IhUtilTest, ReaderEmpty) {
173     TemporaryFile tf;
174     ASSERT_NE(tf.fd, -1);
175     ASSERT_TRUE(WriteStringToFile("", tf.path));
176 
177     Reader r(tf.fd);
178     string line;
179     ASSERT_FALSE(r.readLine(&line));
180     EXPECT_THAT(line, StrEq(""));
181     ASSERT_TRUE(r.ok(&line));
182 }
183 
TEST(IhUtilTest,ReaderMultipleEmptyLines)184 TEST(IhUtilTest, ReaderMultipleEmptyLines) {
185     TemporaryFile tf;
186     ASSERT_NE(tf.fd, -1);
187     ASSERT_TRUE(WriteStringToFile("\n\n", tf.path));
188 
189     Reader r(tf.fd);
190     string line;
191     ASSERT_TRUE(r.readLine(&line));
192     EXPECT_THAT(line, StrEq(""));
193     ASSERT_TRUE(r.readLine(&line));
194     EXPECT_THAT(line, StrEq(""));
195     ASSERT_FALSE(r.readLine(&line));
196     EXPECT_THAT(line, StrEq(""));
197     ASSERT_TRUE(r.ok(&line));
198 }
199 
TEST(IhUtilTest,ReaderFailedNegativeFd)200 TEST(IhUtilTest, ReaderFailedNegativeFd) {
201     Reader r(-123);
202     string line;
203     EXPECT_FALSE(r.readLine(&line));
204     EXPECT_FALSE(r.ok(&line));
205     EXPECT_THAT(line, StrEq("Invalid fd -123"));
206 }
207 
TEST(IhUtilTest,ReaderFailedBadFd)208 TEST(IhUtilTest, ReaderFailedBadFd) {
209     Reader r(1231432);
210     string line;
211     EXPECT_FALSE(r.readLine(&line));
212     EXPECT_FALSE(r.ok(&line));
213     EXPECT_THAT(line, StrEq("Invalid fd 1231432"));
214 }
215