Home | History | Annotate | Download | only in storage
      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 <gmock/gmock.h>
     16 #include <gtest/gtest.h>
     17 #include <stdio.h>
     18 #include "src/storage/StorageManager.h"
     19 
     20 #ifdef __ANDROID__
     21 
     22 namespace android {
     23 namespace os {
     24 namespace statsd {
     25 
     26 using namespace testing;
     27 using std::make_shared;
     28 using std::shared_ptr;
     29 using std::vector;
     30 using testing::Contains;
     31 
     32 TEST(StorageManagerTest, TrainInfoReadWriteTest) {
     33     InstallTrainInfo trainInfo;
     34     trainInfo.trainVersionCode = 12345;
     35     trainInfo.trainName = "This is a train name #)$(&&$";
     36     trainInfo.status = 1;
     37     const char* expIds = "test_ids";
     38     trainInfo.experimentIds.assign(expIds, expIds + strlen(expIds));
     39 
     40     bool result;
     41 
     42     result = StorageManager::writeTrainInfo(trainInfo.trainVersionCode, trainInfo.trainName,
     43                                             trainInfo.status, trainInfo.experimentIds);
     44 
     45     EXPECT_TRUE(result);
     46 
     47     InstallTrainInfo trainInfoResult;
     48     result = StorageManager::readTrainInfo(trainInfoResult);
     49     EXPECT_TRUE(result);
     50 
     51     EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
     52     EXPECT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
     53     EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
     54     EXPECT_EQ(trainInfo.status, trainInfoResult.status);
     55     EXPECT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
     56     EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
     57 }
     58 
     59 TEST(StorageManagerTest, TrainInfoReadWriteEmptyTrainNameTest) {
     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.trainVersionCode, trainInfo.trainName,
     70                                             trainInfo.status, trainInfo.experimentIds);
     71 
     72     EXPECT_TRUE(result);
     73 
     74     InstallTrainInfo trainInfoResult;
     75     result = StorageManager::readTrainInfo(trainInfoResult);
     76     EXPECT_TRUE(result);
     77 
     78     EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
     79     EXPECT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
     80     EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
     81     EXPECT_EQ(trainInfo.status, trainInfoResult.status);
     82     EXPECT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
     83     EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
     84 }
     85 
     86 TEST(StorageManagerTest, TrainInfoReadWriteTrainNameSizeOneTest) {
     87     InstallTrainInfo trainInfo;
     88     trainInfo.trainVersionCode = 12345;
     89     trainInfo.trainName = "{";
     90     trainInfo.status = 1;
     91     const char* expIds = "test_ids";
     92     trainInfo.experimentIds.assign(expIds, expIds + strlen(expIds));
     93 
     94     bool result;
     95 
     96     result = StorageManager::writeTrainInfo(trainInfo.trainVersionCode, trainInfo.trainName,
     97                                             trainInfo.status, trainInfo.experimentIds);
     98 
     99     EXPECT_TRUE(result);
    100 
    101     InstallTrainInfo trainInfoResult;
    102     result = StorageManager::readTrainInfo(trainInfoResult);
    103     EXPECT_TRUE(result);
    104 
    105     EXPECT_EQ(trainInfo.trainVersionCode, trainInfoResult.trainVersionCode);
    106     EXPECT_EQ(trainInfo.trainName.size(), trainInfoResult.trainName.size());
    107     EXPECT_EQ(trainInfo.trainName, trainInfoResult.trainName);
    108     EXPECT_EQ(trainInfo.status, trainInfoResult.status);
    109     EXPECT_EQ(trainInfo.experimentIds.size(), trainInfoResult.experimentIds.size());
    110     EXPECT_EQ(trainInfo.experimentIds, trainInfoResult.experimentIds);
    111 }
    112 
    113 TEST(StorageManagerTest, SortFileTest) {
    114     vector<StorageManager::FileInfo> list;
    115     // assume now sec is 500
    116     list.emplace_back("200_5000_123454", false, 20, 300);
    117     list.emplace_back("300_2000_123454_history", true, 30, 200);
    118     list.emplace_back("400_100009_123454_history", true, 40, 100);
    119     list.emplace_back("100_2000_123454", false, 50, 400);
    120 
    121     StorageManager::sortFiles(&list);
    122     EXPECT_EQ("200_5000_123454", list[0].mFileName);
    123     EXPECT_EQ("100_2000_123454", list[1].mFileName);
    124     EXPECT_EQ("400_100009_123454_history", list[2].mFileName);
    125     EXPECT_EQ("300_2000_123454_history", list[3].mFileName);
    126 }
    127 
    128 const string testDir = "/data/misc/stats-data/";
    129 const string file1 = testDir + "2557169347_1066_1";
    130 const string file2 = testDir + "2557169349_1066_1";
    131 const string file1_history = file1 + "_history";
    132 const string file2_history = file2 + "_history";
    133 
    134 bool prepareLocalHistoryTestFiles() {
    135     android::base::unique_fd fd(TEMP_FAILURE_RETRY(
    136             open(file1.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)));
    137     if (fd != -1) {
    138         dprintf(fd, "content");
    139     } else {
    140         return false;
    141     }
    142 
    143     android::base::unique_fd fd2(TEMP_FAILURE_RETRY(
    144             open(file2.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR)));
    145     if (fd2 != -1) {
    146         dprintf(fd2, "content");
    147     } else {
    148         return false;
    149     }
    150     return true;
    151 }
    152 
    153 void clearLocalHistoryTestFiles() {
    154     TEMP_FAILURE_RETRY(remove(file1.c_str()));
    155     TEMP_FAILURE_RETRY(remove(file2.c_str()));
    156     TEMP_FAILURE_RETRY(remove(file1_history.c_str()));
    157     TEMP_FAILURE_RETRY(remove(file2_history.c_str()));
    158 }
    159 
    160 bool fileExist(string name) {
    161     android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(name.c_str(), O_RDONLY | O_CLOEXEC)));
    162     return fd != -1;
    163 }
    164 
    165 /* The following AppendConfigReportTests test the 4 combinations of [whether erase data] [whether
    166  * the caller is adb] */
    167 TEST(StorageManagerTest, AppendConfigReportTest1) {
    168     EXPECT_TRUE(prepareLocalHistoryTestFiles());
    169 
    170     ProtoOutputStream out;
    171     StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, false /*erase?*/,
    172                                               false /*isAdb?*/);
    173 
    174     EXPECT_FALSE(fileExist(file1));
    175     EXPECT_FALSE(fileExist(file2));
    176 
    177     EXPECT_TRUE(fileExist(file1_history));
    178     EXPECT_TRUE(fileExist(file2_history));
    179     clearLocalHistoryTestFiles();
    180 }
    181 
    182 TEST(StorageManagerTest, AppendConfigReportTest2) {
    183     EXPECT_TRUE(prepareLocalHistoryTestFiles());
    184 
    185     ProtoOutputStream out;
    186     StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, true /*erase?*/,
    187                                               false /*isAdb?*/);
    188 
    189     EXPECT_FALSE(fileExist(file1));
    190     EXPECT_FALSE(fileExist(file2));
    191     EXPECT_FALSE(fileExist(file1_history));
    192     EXPECT_FALSE(fileExist(file2_history));
    193 
    194     clearLocalHistoryTestFiles();
    195 }
    196 
    197 TEST(StorageManagerTest, AppendConfigReportTest3) {
    198     EXPECT_TRUE(prepareLocalHistoryTestFiles());
    199 
    200     ProtoOutputStream out;
    201     StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, false /*erase?*/,
    202                                               true /*isAdb?*/);
    203 
    204     EXPECT_TRUE(fileExist(file1));
    205     EXPECT_TRUE(fileExist(file2));
    206     EXPECT_FALSE(fileExist(file1_history));
    207     EXPECT_FALSE(fileExist(file2_history));
    208 
    209     clearLocalHistoryTestFiles();
    210 }
    211 
    212 TEST(StorageManagerTest, AppendConfigReportTest4) {
    213     EXPECT_TRUE(prepareLocalHistoryTestFiles());
    214 
    215     ProtoOutputStream out;
    216     StorageManager::appendConfigMetricsReport(ConfigKey(1066, 1), &out, true /*erase?*/,
    217                                               true /*isAdb?*/);
    218 
    219     EXPECT_FALSE(fileExist(file1));
    220     EXPECT_FALSE(fileExist(file2));
    221     EXPECT_FALSE(fileExist(file1_history));
    222     EXPECT_FALSE(fileExist(file2_history));
    223 
    224     clearLocalHistoryTestFiles();
    225 }
    226 
    227 }  // namespace statsd
    228 }  // namespace os
    229 }  // namespace android
    230 #else
    231 GTEST_LOG_(INFO) << "This test does nothing.\n";
    232 #endif
    233