blob: 343f9d00bd9f936e4cd00cc0caa0d7a70d1d1601 [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>
James Hawkinseef069a2016-03-11 14:59:50 -080028#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080029
30using testing::UnorderedElementsAreArray;
31
32namespace {
33
34// Returns true if the time difference between |a| and |b| is no larger
35// than 10 seconds. This allow for a relatively large fuzz when comparing
36// two timestamps taken back-to-back.
37bool FuzzUptimeEquals(int32_t a, int32_t b) {
38 const int32_t FUZZ_SECONDS = 10;
39 return (abs(a - b) <= FUZZ_SECONDS);
40}
41
James Hawkinsabd73e62016-01-19 15:10:38 -080042// Recursively deletes the directory at |path|.
43void DeleteDirectory(const std::string& path) {
44 typedef std::unique_ptr<DIR, decltype(&closedir)> ScopedDIR;
45 ScopedDIR dir(opendir(path.c_str()), closedir);
46 ASSERT_NE(nullptr, dir.get());
47
48 struct dirent* entry;
49 while ((entry = readdir(dir.get())) != NULL) {
50 const std::string entry_name(entry->d_name);
51 if (entry_name == "." || entry_name == "..") {
52 continue;
53 }
54
55 const std::string entry_path = path + "/" + entry_name;
56 if (entry->d_type == DT_DIR) {
57 DeleteDirectory(entry_path);
James Hawkins6d0d12a2016-01-20 13:16:31 -080058 } else {
James Hawkinsabd73e62016-01-19 15:10:38 -080059 unlink(entry_path.c_str());
60 }
61 }
62
63 rmdir(path.c_str());
64}
65
66class BootEventRecordStoreTest : public ::testing::Test {
67 public:
68 BootEventRecordStoreTest() {
69 store_path_ = std::string(store_dir_.path) + "/";
70 }
71
72 const std::string& GetStorePathForTesting() const {
73 return store_path_;
74 }
75
76 private:
77 void TearDown() {
78 // This removes the record store temporary directory even though
79 // TemporaryDir should already take care of it, but this method cleans up
80 // the test files added to the directory which prevent TemporaryDir from
81 // being able to remove the directory.
82 DeleteDirectory(store_path_);
83 }
84
85 // A scoped temporary directory. Using this abstraction provides creation of
86 // the directory and the path to the directory, which is stored in
87 // |store_path_|.
88 TemporaryDir store_dir_;
89
90 // The path to the temporary directory used by the BootEventRecordStore to
91 // persist records. The directory is created and destroyed for each test.
92 std::string store_path_;
93
94 DISALLOW_COPY_AND_ASSIGN(BootEventRecordStoreTest);
95};
96
97} // namespace
98
99TEST_F(BootEventRecordStoreTest, AddSingleBootEvent) {
100 BootEventRecordStore store;
101 store.SetStorePath(GetStorePathForTesting());
102
James Hawkinseef069a2016-03-11 14:59:50 -0800103 time_t uptime = bootstat::ParseUptime();
James Hawkinsabd73e62016-01-19 15:10:38 -0800104 ASSERT_NE(-1, uptime);
105
106 store.AddBootEvent("cenozoic");
107
108 auto events = store.GetAllBootEvents();
109 ASSERT_EQ(1U, events.size());
110 EXPECT_EQ("cenozoic", events[0].first);
111 EXPECT_TRUE(FuzzUptimeEquals(uptime, events[0].second));
112}
113
114TEST_F(BootEventRecordStoreTest, AddMultipleBootEvents) {
115 BootEventRecordStore store;
116 store.SetStorePath(GetStorePathForTesting());
117
James Hawkinseef069a2016-03-11 14:59:50 -0800118 time_t uptime = bootstat::ParseUptime();
James Hawkinsabd73e62016-01-19 15:10:38 -0800119 ASSERT_NE(-1, uptime);
120
121 store.AddBootEvent("cretaceous");
122 store.AddBootEvent("jurassic");
123 store.AddBootEvent("triassic");
124
125 const std::string EXPECTED_NAMES[] = {
126 "cretaceous",
127 "jurassic",
128 "triassic",
129 };
130
131 auto events = store.GetAllBootEvents();
132 ASSERT_EQ(3U, events.size());
133
134 std::vector<std::string> names;
135 std::vector<int32_t> timestamps;
136 for (auto i = events.begin(); i != events.end(); ++i) {
137 names.push_back(i->first);
138 timestamps.push_back(i->second);
139 }
140
141 EXPECT_THAT(names, UnorderedElementsAreArray(EXPECTED_NAMES));
142
143 for (auto i = timestamps.cbegin(); i != timestamps.cend(); ++i) {
144 EXPECT_TRUE(FuzzUptimeEquals(uptime, *i));
145 }
146}
James Hawkins10f54be2016-02-09 15:32:38 -0800147
148TEST_F(BootEventRecordStoreTest, AddBootEventWithValue) {
149 BootEventRecordStore store;
150 store.SetStorePath(GetStorePathForTesting());
151
152 store.AddBootEventWithValue("permian", 42);
153
154 auto events = store.GetAllBootEvents();
155 ASSERT_EQ(1U, events.size());
156 EXPECT_EQ("permian", events[0].first);
157 EXPECT_EQ(42, events[0].second);
James Hawkins35349142016-02-16 15:05:54 -0800158}
159
160TEST_F(BootEventRecordStoreTest, GetBootEvent) {
161 BootEventRecordStore store;
162 store.SetStorePath(GetStorePathForTesting());
163
164 // Event does not exist.
165 BootEventRecordStore::BootEventRecord record;
166 bool result = store.GetBootEvent("nonexistent", &record);
167 EXPECT_EQ(false, result);
168
169 // Empty path.
170 EXPECT_DEATH(store.GetBootEvent(std::string(), &record), std::string());
171
172 // Success case.
173 store.AddBootEventWithValue("carboniferous", 314);
174 result = store.GetBootEvent("carboniferous", &record);
175 EXPECT_EQ(true, result);
176 EXPECT_EQ("carboniferous", record.first);
177 EXPECT_EQ(314, record.second);
178
179 // Null |record|.
180 EXPECT_DEATH(store.GetBootEvent("carboniferous", nullptr), std::string());
James Hawkins10f54be2016-02-09 15:32:38 -0800181}