Arman Uguray | 26b7cf3 | 2013-02-17 13:56:12 -0800 | [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/activating_iccid_store.h" |
| 6 | |
| 7 | #include <base/file_util.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 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 15 | using base::FilePath; |
Arman Uguray | 26b7cf3 | 2013-02-17 13:56:12 -0800 | [diff] [blame] | 16 | using ::testing::_; |
| 17 | using ::testing::DoAll; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::SetArgumentPointee; |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | class ActivatingIccidStoreTest : public ::testing::Test { |
| 24 | public: |
| 25 | ActivatingIccidStoreTest() : mock_store_(new MockStore()) {} |
| 26 | |
| 27 | protected: |
| 28 | |
| 29 | void SetMockStore() { |
| 30 | iccid_store_.storage_.reset(mock_store_.release()); |
| 31 | } |
| 32 | |
| 33 | GLib glib_; |
| 34 | scoped_ptr<MockStore> mock_store_; |
| 35 | ActivatingIccidStore iccid_store_; |
| 36 | }; |
| 37 | |
| 38 | TEST_F(ActivatingIccidStoreTest, FileInteractions) { |
Arman Uguray | 26b7cf3 | 2013-02-17 13:56:12 -0800 | [diff] [blame] | 39 | const char kIccid1[] = "1234"; |
| 40 | const char kIccid2[] = "4321"; |
Prathmesh Prabhu | 97f317c | 2013-03-15 16:20:34 -0700 | [diff] [blame] | 41 | FilePath path; |
| 42 | FilePath::StringType prefix(""); //No prefix specified. |
| 43 | ASSERT_TRUE(file_util::CreateNewTempDirectory(prefix, &path)); |
Arman Uguray | 26b7cf3 | 2013-02-17 13:56:12 -0800 | [diff] [blame] | 44 | |
| 45 | EXPECT_TRUE(iccid_store_.InitStorage(&glib_, path)); |
| 46 | |
| 47 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 48 | iccid_store_.GetActivationState(kIccid1)); |
| 49 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 50 | iccid_store_.GetActivationState(kIccid2)); |
| 51 | |
| 52 | EXPECT_TRUE(iccid_store_.SetActivationState( |
| 53 | kIccid1, ActivatingIccidStore::kStatePending)); |
| 54 | EXPECT_TRUE(iccid_store_.SetActivationState( |
| 55 | kIccid2, ActivatingIccidStore::kStateActivated)); |
| 56 | |
| 57 | EXPECT_EQ(ActivatingIccidStore::kStatePending, |
| 58 | iccid_store_.GetActivationState(kIccid1)); |
| 59 | EXPECT_EQ(ActivatingIccidStore::kStateActivated, |
| 60 | iccid_store_.GetActivationState(kIccid2)); |
| 61 | |
| 62 | EXPECT_TRUE(iccid_store_.SetActivationState( |
| 63 | kIccid1, ActivatingIccidStore::kStateActivated)); |
| 64 | EXPECT_TRUE(iccid_store_.SetActivationState( |
| 65 | kIccid2, ActivatingIccidStore::kStatePending)); |
| 66 | |
| 67 | EXPECT_EQ(ActivatingIccidStore::kStateActivated, |
| 68 | iccid_store_.GetActivationState(kIccid1)); |
| 69 | EXPECT_EQ(ActivatingIccidStore::kStatePending, |
| 70 | iccid_store_.GetActivationState(kIccid2)); |
| 71 | |
| 72 | // Close and reopen the file to verify that the entries persisted. |
| 73 | EXPECT_TRUE(iccid_store_.InitStorage(&glib_, path)); |
| 74 | |
| 75 | EXPECT_EQ(ActivatingIccidStore::kStateActivated, |
| 76 | iccid_store_.GetActivationState(kIccid1)); |
| 77 | EXPECT_EQ(ActivatingIccidStore::kStatePending, |
| 78 | iccid_store_.GetActivationState(kIccid2)); |
| 79 | |
| 80 | EXPECT_TRUE(iccid_store_.RemoveEntry(kIccid1)); |
| 81 | EXPECT_TRUE(iccid_store_.RemoveEntry(kIccid2)); |
| 82 | |
| 83 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 84 | iccid_store_.GetActivationState(kIccid1)); |
| 85 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 86 | iccid_store_.GetActivationState(kIccid2)); |
| 87 | |
| 88 | EXPECT_TRUE(iccid_store_.InitStorage(&glib_, path)); |
| 89 | |
| 90 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 91 | iccid_store_.GetActivationState(kIccid1)); |
| 92 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 93 | iccid_store_.GetActivationState(kIccid2)); |
| 94 | |
Prathmesh Prabhu | 97f317c | 2013-03-15 16:20:34 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(file_util::Delete(path, true)); |
Arman Uguray | 26b7cf3 | 2013-02-17 13:56:12 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(ActivatingIccidStoreTest, GetActivationState) { |
| 99 | MockStore *mock_store = mock_store_.get(); |
| 100 | SetMockStore(); |
| 101 | |
| 102 | const char kIccid[] = "12345689"; |
| 103 | |
| 104 | // Value not found |
| 105 | EXPECT_CALL(*mock_store, GetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 106 | .WillOnce(Return(false)); |
| 107 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 108 | iccid_store_.GetActivationState(kIccid)); |
| 109 | |
| 110 | // File contains invalid entry |
| 111 | EXPECT_CALL(*mock_store, GetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 112 | .WillOnce(DoAll(SetArgumentPointee<2>(3), Return(true))); |
| 113 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 114 | iccid_store_.GetActivationState(kIccid)); |
| 115 | EXPECT_CALL(*mock_store, GetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 116 | .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true))); |
| 117 | EXPECT_EQ(ActivatingIccidStore::kStateUnknown, |
| 118 | iccid_store_.GetActivationState(kIccid)); |
| 119 | |
| 120 | // All enum values |
| 121 | EXPECT_CALL(*mock_store, GetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 122 | .WillOnce(DoAll(SetArgumentPointee<2>(1), Return(true))); |
| 123 | EXPECT_EQ(ActivatingIccidStore::kStatePending, |
| 124 | iccid_store_.GetActivationState(kIccid)); |
| 125 | EXPECT_CALL(*mock_store, GetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 126 | .WillOnce(DoAll(SetArgumentPointee<2>(2), Return(true))); |
| 127 | EXPECT_EQ(ActivatingIccidStore::kStateActivated, |
| 128 | iccid_store_.GetActivationState(kIccid)); |
| 129 | } |
| 130 | |
| 131 | TEST_F(ActivatingIccidStoreTest, SetActivationState) { |
| 132 | MockStore *mock_store = mock_store_.get(); |
| 133 | SetMockStore(); |
| 134 | |
| 135 | const char kIccid[] = "12345689"; |
| 136 | |
| 137 | EXPECT_CALL(*mock_store, Flush()).WillRepeatedly(Return(true)); |
| 138 | EXPECT_CALL(*mock_store, SetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 139 | .WillOnce(Return(false)); |
| 140 | EXPECT_FALSE(iccid_store_.SetActivationState( |
| 141 | kIccid, ActivatingIccidStore::kStateUnknown)); |
| 142 | EXPECT_FALSE(iccid_store_.SetActivationState( |
| 143 | kIccid, ActivatingIccidStore::kStateUnknown)); |
| 144 | EXPECT_FALSE(iccid_store_.SetActivationState( |
| 145 | kIccid, ActivatingIccidStore::kStatePending)); |
| 146 | |
| 147 | EXPECT_CALL(*mock_store, SetInt(ActivatingIccidStore::kGroupId, kIccid, _)) |
| 148 | .WillRepeatedly(Return(true)); |
| 149 | EXPECT_TRUE(iccid_store_.SetActivationState( |
| 150 | kIccid, ActivatingIccidStore::kStatePending)); |
| 151 | EXPECT_TRUE(iccid_store_.SetActivationState( |
| 152 | kIccid, ActivatingIccidStore::kStateActivated)); |
| 153 | } |
| 154 | |
| 155 | TEST_F(ActivatingIccidStoreTest, RemoveEntry) { |
| 156 | MockStore *mock_store = mock_store_.get(); |
| 157 | SetMockStore(); |
| 158 | |
| 159 | const char kIccid[] = "12345689"; |
| 160 | |
| 161 | EXPECT_CALL(*mock_store, Flush()).WillRepeatedly(Return(true)); |
| 162 | EXPECT_CALL(*mock_store, DeleteKey(ActivatingIccidStore::kGroupId, kIccid)) |
| 163 | .WillOnce(Return(false)); |
| 164 | EXPECT_FALSE(iccid_store_.RemoveEntry(kIccid)); |
| 165 | EXPECT_CALL(*mock_store, DeleteKey(ActivatingIccidStore::kGroupId, kIccid)) |
| 166 | .WillOnce(Return(true)); |
| 167 | EXPECT_TRUE(iccid_store_.RemoveEntry(kIccid)); |
| 168 | } |
| 169 | |
| 170 | } // namespace shill |