1 /*
2  * Copyright 2020 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 "UidIoStatsCollector.h"
18 
19 #include <android-base/file.h>
20 #include <android-base/stringprintf.h>
21 #include <gmock/gmock.h>
22 
23 #include <unordered_map>
24 
25 namespace android {
26 namespace automotive {
27 namespace watchdog {
28 
29 using ::android::base::StringAppendF;
30 using ::android::base::WriteStringToFile;
31 using ::testing::UnorderedElementsAreArray;
32 
33 namespace {
34 
toString(std::unordered_map<uid_t,UidIoStats> uidIoStatsByUid)35 std::string toString(std::unordered_map<uid_t, UidIoStats> uidIoStatsByUid) {
36     std::string buffer;
37     for (const auto& [uid, stats] : uidIoStatsByUid) {
38         StringAppendF(&buffer, "{%d: %s}\n", uid, stats.toString().c_str());
39     }
40     return buffer;
41 }
42 
43 }  // namespace
44 
TEST(UidIoStatsCollectorTest,TestValidStatFile)45 TEST(UidIoStatsCollectorTest, TestValidStatFile) {
46     // Format: uid fgRdChar fgWrChar fgRdBytes fgWrBytes bgRdChar bgWrChar bgRdBytes bgWrBytes
47     // fgFsync bgFsync
48     constexpr char firstSnapshot[] = "1001234 5000 1000 3000 500 0 0 0 0 20 0\n"
49                                      "1005678 500 100 30 50 300 400 100 200 45 60\n"
50                                      "1009 0 0 0 0 40000 50000 20000 30000 0 300\n"
51                                      "1001000 4000 3000 2000 1000 400 300 200 100 50 10\n";
52     std::unordered_map<uid_t, UidIoStats> expectedFirstUsage =
53             {{1001234,
54               UidIoStats{/*fgRdBytes=*/3'000, /*bgRdBytes=*/0, /*fgWrBytes=*/500,
55                          /*bgWrBytes=*/0, /*fgFsync=*/20, /*bgFsync=*/0}},
56              {1005678,
57               UidIoStats{/*fgRdBytes=*/30, /*bgRdBytes=*/100, /*fgWrBytes=*/50, /*bgWrBytes=*/200,
58                          /*fgFsync=*/45, /*bgFsync=*/60}},
59              {1009,
60               UidIoStats{/*fgRdBytes=*/0, /*bgRdBytes=*/20'000, /*fgWrBytes=*/0,
61                          /*bgWrBytes=*/30'000,
62                          /*fgFsync=*/0, /*bgFsync=*/300}},
63              {1001000,
64               UidIoStats{/*fgRdBytes=*/2'000, /*bgRdBytes=*/200, /*fgWrBytes=*/1'000,
65                          /*bgWrBytes=*/100, /*fgFsync=*/50, /*bgFsync=*/10}}};
66     TemporaryFile tf;
67     ASSERT_NE(tf.fd, -1);
68     ASSERT_TRUE(WriteStringToFile(firstSnapshot, tf.path));
69 
70     UidIoStatsCollector collector(tf.path);
71     ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
72     ASSERT_RESULT_OK(collector.collect());
73 
74     const auto& actualFirstUsage = collector.deltaStats();
75     EXPECT_THAT(actualFirstUsage, UnorderedElementsAreArray(expectedFirstUsage))
76             << "Expected: " << toString(expectedFirstUsage)
77             << "Actual: " << toString(actualFirstUsage);
78 
79     constexpr char secondSnapshot[] = "1001234 10000 2000 7000 950 0 0 0 0 45 0\n"
80                                       "1005678 600 100 40 50 1000 1000 1000 600 50 70\n"
81                                       "1003456 300 500 200 300 0 0 0 0 50 0\n"
82                                       "1001000 400 300 200 100 40 30 20 10 5 1\n";
83     std::unordered_map<uid_t, UidIoStats> expectedSecondUsage =
84             {{1001234,
85               UidIoStats{/*fgRdBytes=*/4'000, /*bgRdBytes=*/0,
86                          /*fgWrBytes=*/450, /*bgWrBytes=*/0, /*fgFsync=*/25,
87                          /*bgFsync=*/0}},
88              {1005678,
89               UidIoStats{/*fgRdBytes=*/10, /*bgRdBytes=*/900, /*fgWrBytes=*/0, /*bgWrBytes=*/400,
90                          /*fgFsync=*/5, /*bgFsync=*/10}},
91              {1003456,
92               UidIoStats{/*fgRdBytes=*/200, /*bgRdBytes=*/0, /*fgWrBytes=*/300, /*bgWrBytes=*/0,
93                          /*fgFsync=*/50, /*bgFsync=*/0}}};
94     ASSERT_TRUE(WriteStringToFile(secondSnapshot, tf.path));
95     ASSERT_RESULT_OK(collector.collect());
96 
97     const auto& actualSecondUsage = collector.deltaStats();
98     EXPECT_THAT(actualSecondUsage, UnorderedElementsAreArray(expectedSecondUsage))
99             << "Expected: " << toString(expectedSecondUsage)
100             << "Actual: " << toString(actualSecondUsage);
101 }
102 
TEST(UidIoStatsCollectorTest,TestErrorOnInvalidStatFile)103 TEST(UidIoStatsCollectorTest, TestErrorOnInvalidStatFile) {
104     // Format: uid fgRdChar fgWrChar fgRdBytes fgWrBytes bgRdChar bgWrChar bgRdBytes bgWrBytes
105     // fgFsync bgFsync
106     constexpr char contents[] = "1001234 5000 1000 3000 500 0 0 0 0 20 0\n"
107                                 "1005678 500 100 30 50 300 400 100 200 45 60\n"
108                                 "1009012 0 0 0 0 40000 50000 20000 30000 0 300\n"
109                                 "1001000 4000 3000 2000 1000 CORRUPTED DATA\n";
110     TemporaryFile tf;
111     ASSERT_NE(tf.fd, -1);
112     ASSERT_TRUE(WriteStringToFile(contents, tf.path));
113 
114     UidIoStatsCollector collector(tf.path);
115     ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
116     EXPECT_FALSE(collector.collect().ok()) << "No error returned for invalid file";
117 }
118 
119 }  // namespace watchdog
120 }  // namespace automotive
121 }  // namespace android
122