1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <android-base/unique_fd.h>
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <stdio.h>
19 #include "src/storage/StorageManager.h"
20
21 #ifdef __ANDROID__
22
23 namespace android {
24 namespace os {
25 namespace statsd {
26
27 using namespace testing;
28 using std::make_shared;
29 using std::shared_ptr;
30 using std::vector;
31 using testing::Contains;
32
TEST(StorageManagerTest,TrainInfoReadWriteTest)33 TEST(StorageManagerTest, TrainInfoReadWriteTest) {
34 InstallTrainInfo trainInfo;
35 trainInfo.trainVersionCode = 12345;
36 trainInfo.trainName = "This is a train name #)$(&&$";
37 trainInfo.status = 1;
38 const char* expIds = "test_ids";
39 trainInfo.experimentIds.assign(expIds, expIds + strlen(expIds));
40
41 bool result;
42
43 result = StorageManager::writeTrainInfo(trainInfo);
44
45 EXPECT_TRUE(result);
46
47 InstallTrainInfo trainInfoResult;
48 result = StorageManager::readTrainInfo(trainInfo.trainName, trainInfoResult);
49 EXPECT_TRUE(result);
50
51 EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
52 ASSERT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
53 EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
54 EXPECT_EQ(trainInfo.status, trainInfoResult.status);
55 ASSERT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
56 EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
57 }
58
TEST(StorageManagerTest,TrainInfoReadWriteTrainNameSizeOneTest)59 TEST(StorageManagerTest, TrainInfoReadWriteTrainNameSizeOneTest) {
60 InstallTrainInfo trainInfo;
61 trainInfo.trainVersionCode = 12345;
62 trainInfo.trainName = "{";
63 trainInfo.status = 1;
64 const char* expIds = "test_ids";
65 trainInfo.experimentIds.assign(expIds, expIds + strlen(expIds));
66
67 bool result;
68
69 result = StorageManager::writeTrainInfo(trainInfo);
70
71 EXPECT_TRUE(result);
72
73 InstallTrainInfo trainInfoResult;
74 result = StorageManager::readTrainInfo(trainInfo.trainName, trainInfoResult);
75 EXPECT_TRUE(result);
76
77 EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
78 ASSERT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
79 EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
80 EXPECT_EQ(trainInfo.status, trainInfoResult.status);
81 ASSERT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
82 EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
83 }
84
TEST(StorageManagerTest,SortFileTest)85 TEST(StorageManagerTest, SortFileTest) {
86 vector<StorageManager::FileInfo> list;
87 // assume now sec is 500
88 list.emplace_back("200_5000_123454", false, 20, 300);
89 list.emplace_back("300_2000_123454_history", true, 30, 200);
90 list.emplace_back("400_100009_123454_history", true, 40, 100);
91 list.emplace_back("100_2000_123454", false, 50, 400);
92
93 StorageManager::sortFiles(&list);
94 EXPECT_EQ("200_5000_123454", list[0].mFileName);
95 EXPECT_EQ("100_2000_123454", list[1].mFileName);
96 EXPECT_EQ("400_100009_123454_history", list[2].mFileName);
97 EXPECT_EQ("300_2000_123454_history", list[3].mFileName);
98 }
99
100 const string testDir = "/data/misc/stats-data/";
101 const string file1 = testDir + "2557169347_1066_1";
102 const string file2 = testDir + "2557169349_1066_1";
103 const string file1_history = file1 + "_history";
104 const string file2_history = file2 + "_history";
105
prepareLocalHistoryTestFiles()106 bool prepareLocalHistoryTestFiles() {
107 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
108 open(file1.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)));
109 if (fd != -1) {
110 dprintf(fd, "content");
111 } else {
112 return false;
113 }
114
115 android::base::unique_fd fd2(TEMP_FAILURE_RETRY(
116 open(file2.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)));
117 if (fd2 != -1) {
118 dprintf(fd2, "content");
119 } else {
120 return false;
121 }
122 return true;
123 }
124
clearLocalHistoryTestFiles()125 void clearLocalHistoryTestFiles() {
126 TEMP_FAILURE_RETRY(remove(file1.c_str()));
127 TEMP_FAILURE_RETRY(remove(file2.c_str()));
128 TEMP_FAILURE_RETRY(remove(file1_history.c_str()));
129 TEMP_FAILURE_RETRY(remove(file2_history.c_str()));
130 }
131
fileExist(string name)132 bool fileExist(string name) {
133 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(name.c_str(), O_RDONLY | O_CLOEXEC)));
134 return fd != -1;
135 }
136
137 /* The following AppendConfigReportTests test the 4 combinations of [whether erase data] [whether
138 * the caller is adb] */
TEST(StorageManagerTest,AppendConfigReportTest1)139 TEST(StorageManagerTest, AppendConfigReportTest1) {
140 EXPECT_TRUE(prepareLocalHistoryTestFiles());
141
142 ProtoOutputStream out;
143 StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, false /*erase?*/,
144 false /*isAdb?*/);
145
146 EXPECT_FALSE(fileExist(file1));
147 EXPECT_FALSE(fileExist(file2));
148
149 EXPECT_TRUE(fileExist(file1_history));
150 EXPECT_TRUE(fileExist(file2_history));
151 clearLocalHistoryTestFiles();
152 }
153
TEST(StorageManagerTest,AppendConfigReportTest2)154 TEST(StorageManagerTest, AppendConfigReportTest2) {
155 EXPECT_TRUE(prepareLocalHistoryTestFiles());
156
157 ProtoOutputStream out;
158 StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, true /*erase?*/,
159 false /*isAdb?*/);
160
161 EXPECT_FALSE(fileExist(file1));
162 EXPECT_FALSE(fileExist(file2));
163 EXPECT_FALSE(fileExist(file1_history));
164 EXPECT_FALSE(fileExist(file2_history));
165
166 clearLocalHistoryTestFiles();
167 }
168
TEST(StorageManagerTest,AppendConfigReportTest3)169 TEST(StorageManagerTest, AppendConfigReportTest3) {
170 EXPECT_TRUE(prepareLocalHistoryTestFiles());
171
172 ProtoOutputStream out;
173 StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, false /*erase?*/,
174 true /*isAdb?*/);
175
176 EXPECT_TRUE(fileExist(file1));
177 EXPECT_TRUE(fileExist(file2));
178 EXPECT_FALSE(fileExist(file1_history));
179 EXPECT_FALSE(fileExist(file2_history));
180
181 clearLocalHistoryTestFiles();
182 }
183
TEST(StorageManagerTest,AppendConfigReportTest4)184 TEST(StorageManagerTest, AppendConfigReportTest4) {
185 EXPECT_TRUE(prepareLocalHistoryTestFiles());
186
187 ProtoOutputStream out;
188 StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, true /*erase?*/,
189 true /*isAdb?*/);
190
191 EXPECT_FALSE(fileExist(file1));
192 EXPECT_FALSE(fileExist(file2));
193 EXPECT_FALSE(fileExist(file1_history));
194 EXPECT_FALSE(fileExist(file2_history));
195
196 clearLocalHistoryTestFiles();
197 }
198
199 } // namespace statsd
200 } // namespace os
201 } // namespace android
202 #else
203 GTEST_LOG_(INFO) << "This test does nothing.\n";
204 #endif
205