blob: 384f84dc13b11960147b49ddf13bb475c826356c [file] [log] [blame]
James Hawkinsabd73e62016-01-19 15:10:38 -08001/*
2 * Copyright (C) 2016 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 "boot_event_record_store.h"
18
19#include <dirent.h>
20#include <sys/stat.h>
21#include <unistd.h>
22#include <cstdint>
23#include <cstdlib>
James Hawkinseabe08b2016-01-19 16:54:35 -080024#include <android-base/file.h>
25#include <android-base/test_utils.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080026#include <gtest/gtest.h>
27#include <gmock/gmock.h>
28
29using testing::UnorderedElementsAreArray;
30
31namespace {
32
33// Returns true if the time difference between |a| and |b| is no larger
34// than 10 seconds. This allow for a relatively large fuzz when comparing
35// two timestamps taken back-to-back.
36bool FuzzUptimeEquals(int32_t a, int32_t b) {
37 const int32_t FUZZ_SECONDS = 10;
38 return (abs(a - b) <= FUZZ_SECONDS);
39}
40
41// Returns the uptime as read from /proc/uptime, rounded down to an integer.
42int32_t ReadUptime() {
43 std::string uptime_str;
44 if (!android::base::ReadFileToString("/proc/uptime", &uptime_str)) {
45 return -1;
46 }
47
48 // Cast to int to round down.
49 return static_cast<int32_t>(strtod(uptime_str.c_str(), NULL));
50}
51
52// Recursively deletes the directory at |path|.
53void DeleteDirectory(const std::string& path) {
54 typedef std::unique_ptr<DIR, decltype(&closedir)> ScopedDIR;
55 ScopedDIR dir(opendir(path.c_str()), closedir);
56 ASSERT_NE(nullptr, dir.get());
57
58 struct dirent* entry;
59 while ((entry = readdir(dir.get())) != NULL) {
60 const std::string entry_name(entry->d_name);
61 if (entry_name == "." || entry_name == "..") {
62 continue;
63 }
64
65 const std::string entry_path = path + "/" + entry_name;
66 if (entry->d_type == DT_DIR) {
67 DeleteDirectory(entry_path);
James Hawkins6d0d12a2016-01-20 13:16:31 -080068 } else {
James Hawkinsabd73e62016-01-19 15:10:38 -080069 unlink(entry_path.c_str());
70 }
71 }
72
73 rmdir(path.c_str());
74}
75
76class BootEventRecordStoreTest : public ::testing::Test {
77 public:
78 BootEventRecordStoreTest() {
79 store_path_ = std::string(store_dir_.path) + "/";
80 }
81
82 const std::string& GetStorePathForTesting() const {
83 return store_path_;
84 }
85
86 private:
87 void TearDown() {
88 // This removes the record store temporary directory even though
89 // TemporaryDir should already take care of it, but this method cleans up
90 // the test files added to the directory which prevent TemporaryDir from
91 // being able to remove the directory.
92 DeleteDirectory(store_path_);
93 }
94
95 // A scoped temporary directory. Using this abstraction provides creation of
96 // the directory and the path to the directory, which is stored in
97 // |store_path_|.
98 TemporaryDir store_dir_;
99
100 // The path to the temporary directory used by the BootEventRecordStore to
101 // persist records. The directory is created and destroyed for each test.
102 std::string store_path_;
103
104 DISALLOW_COPY_AND_ASSIGN(BootEventRecordStoreTest);
105};
106
107} // namespace
108
109TEST_F(BootEventRecordStoreTest, AddSingleBootEvent) {
110 BootEventRecordStore store;
111 store.SetStorePath(GetStorePathForTesting());
112
113 int32_t uptime = ReadUptime();
114 ASSERT_NE(-1, uptime);
115
116 store.AddBootEvent("cenozoic");
117
118 auto events = store.GetAllBootEvents();
119 ASSERT_EQ(1U, events.size());
120 EXPECT_EQ("cenozoic", events[0].first);
121 EXPECT_TRUE(FuzzUptimeEquals(uptime, events[0].second));
122}
123
124TEST_F(BootEventRecordStoreTest, AddMultipleBootEvents) {
125 BootEventRecordStore store;
126 store.SetStorePath(GetStorePathForTesting());
127
128 int32_t uptime = ReadUptime();
129 ASSERT_NE(-1, uptime);
130
131 store.AddBootEvent("cretaceous");
132 store.AddBootEvent("jurassic");
133 store.AddBootEvent("triassic");
134
135 const std::string EXPECTED_NAMES[] = {
136 "cretaceous",
137 "jurassic",
138 "triassic",
139 };
140
141 auto events = store.GetAllBootEvents();
142 ASSERT_EQ(3U, events.size());
143
144 std::vector<std::string> names;
145 std::vector<int32_t> timestamps;
146 for (auto i = events.begin(); i != events.end(); ++i) {
147 names.push_back(i->first);
148 timestamps.push_back(i->second);
149 }
150
151 EXPECT_THAT(names, UnorderedElementsAreArray(EXPECTED_NAMES));
152
153 for (auto i = timestamps.cbegin(); i != timestamps.cend(); ++i) {
154 EXPECT_TRUE(FuzzUptimeEquals(uptime, *i));
155 }
156}