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)); |
| 86 | EXPECT_TRUE(store_.SetActivationState( |
| 87 | PendingActivationStore::kIdentifierMEID, |
| 88 | kEntry2, |
| 89 | PendingActivationStore::kStatePendingTimeout)); |
| 90 | |
| 91 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 92 | store_.GetActivationState( |
| 93 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 94 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 95 | store_.GetActivationState( |
| 96 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 97 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 98 | store_.GetActivationState( |
| 99 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 100 | EXPECT_EQ(PendingActivationStore::kStatePendingTimeout, |
| 101 | store_.GetActivationState( |
| 102 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 103 | |
| 104 | EXPECT_TRUE(store_.SetActivationState( |
| 105 | PendingActivationStore::kIdentifierICCID, |
| 106 | kEntry1, |
| 107 | PendingActivationStore::kStateActivated)); |
| 108 | EXPECT_TRUE(store_.SetActivationState( |
| 109 | PendingActivationStore::kIdentifierICCID, |
| 110 | kEntry2, |
| 111 | PendingActivationStore::kStatePending)); |
| 112 | |
| 113 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 114 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 115 | kEntry1)); |
| 116 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 117 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 118 | kEntry2)); |
| 119 | |
| 120 | // Close and reopen the file to verify that the entries persisted. |
| 121 | EXPECT_TRUE(store_.InitStorage(&glib_, temp_dir.path())); |
| 122 | |
| 123 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 124 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 125 | kEntry1)); |
| 126 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 127 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 128 | kEntry2)); |
| 129 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 130 | store_.GetActivationState( |
| 131 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 132 | EXPECT_EQ(PendingActivationStore::kStatePendingTimeout, |
| 133 | store_.GetActivationState( |
| 134 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 135 | |
| 136 | EXPECT_TRUE(store_.RemoveEntry( |
| 137 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 138 | EXPECT_TRUE(store_.RemoveEntry( |
| 139 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 140 | |
| 141 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 142 | store_.GetActivationState( |
| 143 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 144 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 145 | store_.GetActivationState( |
| 146 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 147 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 148 | store_.GetActivationState( |
| 149 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 150 | EXPECT_EQ(PendingActivationStore::kStatePendingTimeout, |
| 151 | store_.GetActivationState( |
| 152 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 153 | |
| 154 | EXPECT_TRUE(store_.RemoveEntry( |
| 155 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 156 | EXPECT_TRUE(store_.RemoveEntry( |
| 157 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 158 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 159 | store_.GetActivationState( |
| 160 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 161 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 162 | store_.GetActivationState( |
| 163 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 164 | |
| 165 | EXPECT_TRUE(store_.InitStorage(&glib_, temp_dir.path())); |
| 166 | |
| 167 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 168 | store_.GetActivationState( |
| 169 | PendingActivationStore::kIdentifierMEID, kEntry1)); |
| 170 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 171 | store_.GetActivationState( |
| 172 | PendingActivationStore::kIdentifierICCID, kEntry2)); |
| 173 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 174 | store_.GetActivationState( |
| 175 | PendingActivationStore::kIdentifierICCID, kEntry1)); |
| 176 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 177 | store_.GetActivationState( |
| 178 | PendingActivationStore::kIdentifierMEID, kEntry2)); |
| 179 | } |
| 180 | |
| 181 | TEST_F(PendingActivationStoreTest, GetActivationState) { |
| 182 | MockStore *mock_store = mock_store_.get(); |
| 183 | SetMockStore(); |
| 184 | |
| 185 | const char kEntry[] = "12345689"; |
| 186 | |
| 187 | // Value not found |
| 188 | EXPECT_CALL(*mock_store, GetInt(PendingActivationStore::kIccidGroupId, |
| 189 | kEntry, |
| 190 | _)) |
| 191 | .WillOnce(Return(false)); |
| 192 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 193 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 194 | kEntry)); |
| 195 | |
| 196 | // File contains invalid entry |
| 197 | EXPECT_CALL(*mock_store, |
| 198 | GetInt(PendingActivationStore::kMeidGroupId, kEntry, _)) |
Arman Uguray | 0a3e279 | 2013-01-17 16:31:50 -0800 | [diff] [blame] | 199 | .WillOnce(DoAll( |
| 200 | SetArgumentPointee<2>( |
| 201 | static_cast<int>(PendingActivationStore::kStateMax)), |
| 202 | Return(true))); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 203 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 204 | store_.GetActivationState(PendingActivationStore::kIdentifierMEID, |
| 205 | kEntry)); |
| 206 | EXPECT_CALL(*mock_store, |
| 207 | GetInt(PendingActivationStore::kMeidGroupId, kEntry, _)) |
| 208 | .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true))); |
| 209 | EXPECT_EQ(PendingActivationStore::kStateUnknown, |
| 210 | store_.GetActivationState(PendingActivationStore::kIdentifierMEID, |
| 211 | kEntry)); |
| 212 | Mock::VerifyAndClearExpectations(mock_store); |
| 213 | |
| 214 | // All enum values |
| 215 | EXPECT_CALL(*mock_store, |
| 216 | GetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 217 | .WillOnce(DoAll(SetArgumentPointee<2>(1), Return(true))); |
| 218 | EXPECT_EQ(PendingActivationStore::kStatePending, |
| 219 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 220 | kEntry)); |
| 221 | EXPECT_CALL(*mock_store, |
| 222 | GetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 223 | .WillOnce(DoAll(SetArgumentPointee<2>(2), Return(true))); |
| 224 | EXPECT_EQ(PendingActivationStore::kStateActivated, |
| 225 | store_.GetActivationState(PendingActivationStore::kIdentifierICCID, |
| 226 | kEntry)); |
| 227 | Mock::VerifyAndClearExpectations(mock_store); |
| 228 | } |
| 229 | |
| 230 | TEST_F(PendingActivationStoreTest, SetActivationState) { |
| 231 | MockStore *mock_store = mock_store_.get(); |
| 232 | SetMockStore(); |
| 233 | |
| 234 | const char kEntry[] = "12345689"; |
| 235 | |
| 236 | EXPECT_CALL(*mock_store, Flush()).WillRepeatedly(Return(true)); |
| 237 | EXPECT_CALL(*mock_store, |
| 238 | SetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 239 | .WillOnce(Return(false)); |
| 240 | EXPECT_FALSE(store_.SetActivationState( |
| 241 | PendingActivationStore::kIdentifierICCID, |
| 242 | kEntry, |
| 243 | PendingActivationStore::kStateUnknown)); |
| 244 | EXPECT_FALSE(store_.SetActivationState( |
| 245 | PendingActivationStore::kIdentifierICCID, |
| 246 | kEntry, |
| 247 | PendingActivationStore::kStateUnknown)); |
| 248 | EXPECT_FALSE(store_.SetActivationState( |
| 249 | PendingActivationStore::kIdentifierICCID, |
| 250 | kEntry, |
| 251 | PendingActivationStore::kStatePending)); |
| 252 | |
| 253 | EXPECT_CALL(*mock_store, |
| 254 | SetInt(PendingActivationStore::kIccidGroupId, kEntry, _)) |
| 255 | .WillRepeatedly(Return(true)); |
| 256 | EXPECT_FALSE(store_.SetActivationState( |
| 257 | PendingActivationStore::kIdentifierICCID, |
| 258 | kEntry, static_cast<PendingActivationStore::State>(-1))); |
| 259 | EXPECT_FALSE(store_.SetActivationState( |
| 260 | PendingActivationStore::kIdentifierICCID, |
Arman Uguray | 0a3e279 | 2013-01-17 16:31:50 -0800 | [diff] [blame] | 261 | kEntry, PendingActivationStore::kStateMax)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 262 | EXPECT_FALSE(store_.SetActivationState( |
| 263 | PendingActivationStore::kIdentifierICCID, |
| 264 | kEntry, PendingActivationStore::kStateUnknown)); |
| 265 | EXPECT_TRUE(store_.SetActivationState( |
| 266 | PendingActivationStore::kIdentifierICCID, |
| 267 | kEntry, |
| 268 | PendingActivationStore::kStatePending)); |
| 269 | EXPECT_TRUE(store_.SetActivationState( |
| 270 | PendingActivationStore::kIdentifierICCID, |
| 271 | kEntry, |
| 272 | PendingActivationStore::kStateActivated)); |
| 273 | } |
| 274 | |
| 275 | TEST_F(PendingActivationStoreTest, RemoveEntry) { |
| 276 | MockStore *mock_store = mock_store_.get(); |
| 277 | SetMockStore(); |
| 278 | |
| 279 | const char kEntry[] = "12345689"; |
| 280 | |
| 281 | EXPECT_CALL(*mock_store, Flush()).WillRepeatedly(Return(true)); |
| 282 | EXPECT_CALL(*mock_store, DeleteKey(PendingActivationStore::kIccidGroupId, |
| 283 | kEntry)) |
| 284 | .WillOnce(Return(false)); |
| 285 | EXPECT_FALSE(store_.RemoveEntry(PendingActivationStore::kIdentifierICCID, |
| 286 | kEntry)); |
| 287 | EXPECT_CALL(*mock_store, DeleteKey(PendingActivationStore::kIccidGroupId, |
| 288 | kEntry)) |
| 289 | .WillOnce(Return(true)); |
| 290 | EXPECT_TRUE(store_.RemoveEntry(PendingActivationStore::kIdentifierICCID, |
| 291 | kEntry)); |
| 292 | } |
| 293 | |
| 294 | } // namespace shill |