Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/pending_activation_store.h" |
| 6 | |
| 7 | #include <base/files/scoped_temp_dir.h> |
| 8 | #include <base/memory/scoped_ptr.h> |
| 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "shill/glib.h" |
| 13 | #include "shill/mock_store.h" |
| 14 | |
| 15 | using base::FilePath; |
| 16 | using ::testing::_; |
| 17 | using ::testing::DoAll; |
| 18 | using ::testing::Mock; |
| 19 | using ::testing::Return; |
| 20 | using ::testing::SetArgumentPointee; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | class PendingActivationStoreTest : public ::testing::Test { |
| 25 | public: |
| 26 | PendingActivationStoreTest() : mock_store_(new MockStore()) {} |
| 27 | |
| 28 | protected: |
| 29 | void SetMockStore() { |
| 30 | store_.storage_.reset(mock_store_.release()); |
| 31 | } |
| 32 | |
| 33 | GLib glib_; |
| 34 | scoped_ptr<MockStore> mock_store_; |
| 35 | PendingActivationStore store_; |
| 36 | }; |
| 37 | |
| 38 | TEST_F(PendingActivationStoreTest, FileInteractions) { |
| 39 | const char kEntry1[] = "1234"; |
| 40 | const char kEntry2[] = "4321"; |
| 41 | |
| 42 | base::ScopedTempDir temp_dir; |
| 43 | EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 44 | |
| 45 | EXPECT_TRUE(store_.InitStorage(&glib_, temp_dir.path())); |
| 46 | |
| 47 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 48 | store_.GetActivationState( |
| 49 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 50 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 51 | store_.GetActivationState( |
| 52 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 53 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 54 | store_.GetActivationState( |
| 55 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 56 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 57 | store_.GetActivationState( |
| 58 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 59 | |
| 60 | EXPECT_TRUE(store_.SetActivationState( |
| 61 | PendingActivationStore::kIdentifierICCID, |
| 62 | kEntry1, |
| 63 | PendingActivationStore::kStatePending)); |
| 64 | EXPECT_TRUE(store_.SetActivationState( |
| 65 | PendingActivationStore::kIdentifierICCID, |
| 66 | kEntry2, |
| 67 | PendingActivationStore::kStateActivated)); |
| 68 | |
| 69 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 70 | store_.GetActivationState( |
| 71 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 72 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 73 | store_.GetActivationState( |
| 74 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 75 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 76 | store_.GetActivationState( |
| 77 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 78 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 79 | store_.GetActivationState( |
| 80 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 81 | |
| 82 | EXPECT_TRUE(store_.SetActivationState( |
| 83 | PendingActivationStore::kIdentifierMEID, |
| 84 | kEntry1, |
| 85 | PendingActivationStore::kStateActivated)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 86 | |
| 87 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 88 | store_.GetActivationState( |
| 89 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 90 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 91 | store_.GetActivationState( |
| 92 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 93 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 94 | store_.GetActivationState( |
| 95 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 96 | |
| 97 | EXPECT_TRUE(store_.SetActivationState( |
| 98 | PendingActivationStore::kIdentifierICCID, |
| 99 | kEntry1, |
| 100 | PendingActivationStore::kStateActivated)); |
| 101 | EXPECT_TRUE(store_.SetActivationState( |
| 102 | PendingActivationStore::kIdentifierICCID, |
| 103 | kEntry2, |
| 104 | PendingActivationStore::kStatePending)); |
| 105 | |
| 106 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 107 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 108 | kEntry1)); |
| 109 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 110 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 111 | kEntry2)); |
| 112 | |
| 113 | // Close and reopen the file to verify that the entries persisted. |
| 114 | EXPECT_TRUE(store_.InitStorage(&glib_, temp_dir.path())); |
| 115 | |
| 116 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 117 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 118 | kEntry1)); |
| 119 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 120 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 121 | kEntry2)); |
| 122 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 123 | store_.GetActivationState( |
| 124 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 125 | |
| 126 | EXPECT_TRUE(store_.RemoveEntry( |
| 127 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 128 | EXPECT_TRUE(store_.RemoveEntry( |
| 129 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 130 | |
| 131 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 132 | store_.GetActivationState( |
| 133 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 134 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 135 | store_.GetActivationState( |
| 136 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 137 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 138 | store_.GetActivationState( |
| 139 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 140 | |
| 141 | EXPECT_TRUE(store_.RemoveEntry( |
| 142 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 143 | EXPECT_TRUE(store_.RemoveEntry( |
| 144 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 145 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 146 | store_.GetActivationState( |
| 147 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 148 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 149 | store_.GetActivationState( |
| 150 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 151 | |
| 152 | EXPECT_TRUE(store_.InitStorage(&glib_, temp_dir.path())); |
| 153 | |
| 154 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 155 | store_.GetActivationState( |
| 156 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 157 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 158 | store_.GetActivationState( |
| 159 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 160 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 161 | store_.GetActivationState( |
| 162 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 163 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 164 | store_.GetActivationState( |
| 165 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 166 | } |
| 167 | |
| 168 | TEST_F(PendingActivationStoreTest, GetActivationState) { |
| 169 | MockStore *mock_store = mock_store_.get(); |
| 170 | SetMockStore(); |
| 171 | |
| 172 | const char kEntry[] = "12345689"; |
| 173 | |
| 174 | // Value not found |
| 175 | EXPECT_CALL(*mock_store, GetInt(PendingActivationStore::kIccidGroupId, |
| 176 | kEntry, |
| 177 | _)) |
| 178 | .WillOnce(Return(false)); |
| 179 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 180 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 181 | kEntry)); |
| 182 | |
| 183 | // File contains invalid entry |
| 184 | EXPECT_CALL(*mock_store, |
| 185 | GetInt(PendingActivationStore::kMeidGroupId, kEntry, _)) |
Arman Uguray | 0a3e279 | 2013-01-17 16:31:50 -0800 | [diff] [blame] | 186 | .WillOnce(DoAll( |
| 187 | SetArgumentPointee<2>( |
| 188 | static_cast<int>(PendingActivationStore::kStateMax)), |
| 189 | Return(true))); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 190 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 191 | store_.GetActivationState(PendingActivationStore::kIdentifierMEID, |
| 192 | kEntry)); |
| 193 | EXPECT_CALL(*mock_store, |
| 194 | GetInt(PendingActivationStore::kMeidGroupId, kEntry, _)) |
| 195 | .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true))); |
| 196 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 197 | store_.GetActivationState(PendingActivationStore::kIdentifierMEID, |
| 198 | kEntry)); |
| 199 | Mock::VerifyAndClearExpectations(mock_store); |
| 200 | |
| 201 | // All enum values |
| 202 | EXPECT_CALL(*mock_store, |
| 203 | GetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 204 | .WillOnce(DoAll(SetArgumentPointee<2>(1), Return(true))); |
| 205 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 206 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 207 | kEntry)); |
| 208 | EXPECT_CALL(*mock_store, |
| 209 | GetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 210 | .WillOnce(DoAll(SetArgumentPointee<2>(2), Return(true))); |
| 211 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 212 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 213 | kEntry)); |
| 214 | Mock::VerifyAndClearExpectations(mock_store); |
| 215 | } |
| 216 | |
| 217 | TEST_F(PendingActivationStoreTest, SetActivationState) { |
| 218 | MockStore *mock_store = mock_store_.get(); |
| 219 | SetMockStore(); |
| 220 | |
| 221 | const char kEntry[] = "12345689"; |
| 222 | |
| 223 | EXPECT_CALL(*mock_store, Flush()).WillRepeatedly(Return(true)); |
| 224 | EXPECT_CALL(*mock_store, |
| 225 | SetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 226 | .WillOnce(Return(false)); |
| 227 | EXPECT_FALSE(store_.SetActivationState( |
| 228 | PendingActivationStore::kIdentifierICCID, |
| 229 | kEntry, |
| 230 | PendingActivationStore::kStateUnknown)); |
| 231 | EXPECT_FALSE(store_.SetActivationState( |
| 232 | PendingActivationStore::kIdentifierICCID, |
| 233 | kEntry, |
| 234 | PendingActivationStore::kStateUnknown)); |
| 235 | EXPECT_FALSE(store_.SetActivationState( |
| 236 | PendingActivationStore::kIdentifierICCID, |
| 237 | kEntry, |
| 238 | PendingActivationStore::kStatePending)); |
| 239 | |
| 240 | EXPECT_CALL(*mock_store, |
| 241 | SetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 242 | .WillRepeatedly(Return(true)); |
| 243 | EXPECT_FALSE(store_.SetActivationState( |
| 244 | PendingActivationStore::kIdentifierICCID, |
| 245 | kEntry, static_cast<PendingActivationStore::State>(-1))); |
| 246 | EXPECT_FALSE(store_.SetActivationState( |
| 247 | PendingActivationStore::kIdentifierICCID, |
Arman Uguray | 0a3e279 | 2013-01-17 16:31:50 -0800 | [diff] [blame] | 248 | kEntry, PendingActivationStore::kStateMax)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 249 | EXPECT_FALSE(store_.SetActivationState( |
| 250 | PendingActivationStore::kIdentifierICCID, |
| 251 | kEntry, PendingActivationStore::kStateUnknown)); |
| 252 | EXPECT_TRUE(store_.SetActivationState( |
| 253 | PendingActivationStore::kIdentifierICCID, |
| 254 | kEntry, |
| 255 | PendingActivationStore::kStatePending)); |
| 256 | EXPECT_TRUE(store_.SetActivationState( |
| 257 | PendingActivationStore::kIdentifierICCID, |
| 258 | kEntry, |
| 259 | PendingActivationStore::kStateActivated)); |
| 260 | } |
| 261 | |
| 262 | TEST_F(PendingActivationStoreTest, RemoveEntry) { |
| 263 | MockStore *mock_store = mock_store_.get(); |
| 264 | SetMockStore(); |
| 265 | |
| 266 | const char kEntry[] = "12345689"; |
| 267 | |
| 268 | EXPECT_CALL(*mock_store, Flush()).WillRepeatedly(Return(true)); |
| 269 | EXPECT_CALL(*mock_store, DeleteKey(PendingActivationStore::kIccidGroupId, |
| 270 | kEntry)) |
| 271 | .WillOnce(Return(false)); |
| 272 | EXPECT_FALSE(store_.RemoveEntry(PendingActivationStore::kIdentifierICCID, |
| 273 | kEntry)); |
| 274 | EXPECT_CALL(*mock_store, DeleteKey(PendingActivationStore::kIccidGroupId, |
| 275 | kEntry)) |
| 276 | .WillOnce(Return(true)); |
| 277 | EXPECT_TRUE(store_.RemoveEntry(PendingActivationStore::kIdentifierICCID, |
| 278 | kEntry)); |
| 279 | } |
| 280 | |
| 281 | } // namespace shill |