blob: 74eafbf56d6673a21114594b1dfffea577264bfe [file] [log] [blame]
Chenjie Yu6b1667c2019-01-18 10:09:33 -08001// 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
Ruchir Rastogi4e65e382020-01-22 17:29:14 -080015#include <android-base/unique_fd.h>
Chenjie Yu6b1667c2019-01-18 10:09:33 -080016#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18#include <stdio.h>
19#include "src/storage/StorageManager.h"
20
21#ifdef __ANDROID__
22
23namespace android {
24namespace os {
25namespace statsd {
26
27using namespace testing;
28using std::make_shared;
29using std::shared_ptr;
30using std::vector;
31using testing::Contains;
32
33TEST(StorageManagerTest, TrainInfoReadWriteTest) {
Chenjie Yu97dbb202019-02-13 16:42:04 -080034 InstallTrainInfo trainInfo;
Chenjie Yu6b1667c2019-01-18 10:09:33 -080035 trainInfo.trainVersionCode = 12345;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080036 trainInfo.trainName = "This is a train name #)$(&&$";
37 trainInfo.status = 1;
Chenjie Yu6b1667c2019-01-18 10:09:33 -080038 const char* expIds = "test_ids";
39 trainInfo.experimentIds.assign(expIds, expIds + strlen(expIds));
40
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080041 bool result;
Chenjie Yu6b1667c2019-01-18 10:09:33 -080042
Jonathan Nguyen703c42f2020-02-04 15:54:26 -080043 result = StorageManager::writeTrainInfo(trainInfo);
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080044
45 EXPECT_TRUE(result);
46
47 InstallTrainInfo trainInfoResult;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -080048 result = StorageManager::readTrainInfo(trainInfo.trainName, trainInfoResult);
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080049 EXPECT_TRUE(result);
50
51 EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
Muhammad Qureshidff78d62020-05-11 13:37:43 -070052 ASSERT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080053 EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
54 EXPECT_EQ(trainInfo.status, trainInfoResult.status);
Muhammad Qureshidff78d62020-05-11 13:37:43 -070055 ASSERT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080056 EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
57}
58
59TEST(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
Jonathan Nguyen703c42f2020-02-04 15:54:26 -080069 result = StorageManager::writeTrainInfo(trainInfo);
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080070
71 EXPECT_TRUE(result);
72
73 InstallTrainInfo trainInfoResult;
Jonathan Nguyen703c42f2020-02-04 15:54:26 -080074 result = StorageManager::readTrainInfo(trainInfo.trainName, trainInfoResult);
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080075 EXPECT_TRUE(result);
76
77 EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
Muhammad Qureshidff78d62020-05-11 13:37:43 -070078 ASSERT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080079 EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
80 EXPECT_EQ(trainInfo.status, trainInfoResult.status);
Muhammad Qureshidff78d62020-05-11 13:37:43 -070081 ASSERT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080082 EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
Chenjie Yu6b1667c2019-01-18 10:09:33 -080083}
84
Yao Chen9a43b4f2019-04-10 10:43:20 -070085TEST(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
Yao Chen5c10cb42019-06-03 14:45:54 -0700100const string testDir = "/data/misc/stats-data/";
101const string file1 = testDir + "2557169347_1066_1";
102const string file2 = testDir + "2557169349_1066_1";
103const string file1_history = file1 + "_history";
104const string file2_history = file2 + "_history";
105
106bool 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
125void 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
132bool 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] */
139TEST(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
154TEST(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
169TEST(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
184TEST(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
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800199} // namespace statsd
200} // namespace os
201} // namespace android
202#else
203GTEST_LOG_(INFO) << "This test does nothing.\n";
204#endif