James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 24 | #include <android-base/file.h> |
| 25 | #include <android-base/test_utils.h> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <gmock/gmock.h> |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame^] | 28 | #include "uptime_parser.h" |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 29 | |
| 30 | using testing::UnorderedElementsAreArray; |
| 31 | |
| 32 | namespace { |
| 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. |
| 37 | bool FuzzUptimeEquals(int32_t a, int32_t b) { |
| 38 | const int32_t FUZZ_SECONDS = 10; |
| 39 | return (abs(a - b) <= FUZZ_SECONDS); |
| 40 | } |
| 41 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 42 | // Recursively deletes the directory at |path|. |
| 43 | void 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 Hawkins | 6d0d12a | 2016-01-20 13:16:31 -0800 | [diff] [blame] | 58 | } else { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 59 | unlink(entry_path.c_str()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | rmdir(path.c_str()); |
| 64 | } |
| 65 | |
| 66 | class 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 | |
| 99 | TEST_F(BootEventRecordStoreTest, AddSingleBootEvent) { |
| 100 | BootEventRecordStore store; |
| 101 | store.SetStorePath(GetStorePathForTesting()); |
| 102 | |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame^] | 103 | time_t uptime = bootstat::ParseUptime(); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 104 | 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 | |
| 114 | TEST_F(BootEventRecordStoreTest, AddMultipleBootEvents) { |
| 115 | BootEventRecordStore store; |
| 116 | store.SetStorePath(GetStorePathForTesting()); |
| 117 | |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame^] | 118 | time_t uptime = bootstat::ParseUptime(); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 119 | 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 Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 147 | |
| 148 | TEST_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 Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEST_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 Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 181 | } |