Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 1 | // Copyright 2014 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/device_claimer.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | #include "shill/error.h" |
| 12 | #include "shill/mock_dbus_manager.h" |
| 13 | #include "shill/mock_device_info.h" |
| 14 | |
| 15 | using std::string; |
| 16 | using ::testing::_; |
| 17 | using ::testing::Mock; |
| 18 | using ::testing::Return; |
| 19 | |
| 20 | namespace shill { |
| 21 | |
| 22 | const char kDBusServiceName[] = "org.chromium.TestService"; |
| 23 | const char kTestDevice1Name[] = "test_device1"; |
| 24 | const char kTestDevice2Name[] = "test_device2"; |
| 25 | |
| 26 | class DeviceClaimerTest : public testing::Test { |
| 27 | public: |
| 28 | DeviceClaimerTest() |
| 29 | : device_info_(nullptr, nullptr, nullptr, nullptr), |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 30 | device_claimer_(kDBusServiceName, &device_info_, false) {} |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 31 | |
| 32 | protected: |
| 33 | MockDeviceInfo device_info_; |
| 34 | DeviceClaimer device_claimer_; |
| 35 | }; |
| 36 | |
| 37 | TEST_F(DeviceClaimerTest, StartDBusNameWatcher) { |
| 38 | // Setup DBus name watcher. |
| 39 | MockDBusManager dbus_manager; |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 40 | DBusNameWatcher* name_watcher = |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 41 | new DBusNameWatcher(&dbus_manager, |
| 42 | kDBusServiceName, |
| 43 | DBusNameWatcher::NameAppearedCallback(), |
| 44 | DBusNameWatcher::NameVanishedCallback()); |
| 45 | |
| 46 | // Start DBus name watcher. |
| 47 | EXPECT_CALL(dbus_manager, CreateNameWatcher(kDBusServiceName, _, _)) |
| 48 | .WillOnce(Return(name_watcher)); |
| 49 | EXPECT_TRUE(device_claimer_.StartDBusNameWatcher( |
| 50 | &dbus_manager, |
| 51 | DBusNameWatcher::NameAppearedCallback(), |
| 52 | DBusNameWatcher::NameVanishedCallback())); |
| 53 | Mock::VerifyAndClearExpectations(&dbus_manager); |
| 54 | |
| 55 | // Start DBus name watcher again, should fail since name watcher already |
| 56 | // started. |
| 57 | EXPECT_CALL(dbus_manager, CreateNameWatcher(_, _, _)).Times(0); |
| 58 | EXPECT_FALSE(device_claimer_.StartDBusNameWatcher( |
| 59 | &dbus_manager, |
| 60 | DBusNameWatcher::NameAppearedCallback(), |
| 61 | DBusNameWatcher::NameVanishedCallback())); |
| 62 | Mock::VerifyAndClearExpectations(&dbus_manager); |
| 63 | } |
| 64 | |
| 65 | TEST_F(DeviceClaimerTest, ClaimAndReleaseDevices) { |
| 66 | // Should not have any device claimed initially. |
| 67 | EXPECT_FALSE(device_claimer_.DevicesClaimed()); |
| 68 | |
| 69 | // Claim device 1. |
| 70 | Error error; |
| 71 | EXPECT_CALL(device_info_, AddDeviceToBlackList(kTestDevice1Name)).Times(1); |
| 72 | EXPECT_TRUE(device_claimer_.Claim(kTestDevice1Name, &error)); |
| 73 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 74 | EXPECT_TRUE(device_claimer_.DevicesClaimed()); |
| 75 | Mock::VerifyAndClearExpectations(&device_info_); |
| 76 | |
| 77 | // Claim device 2. |
| 78 | error.Reset(); |
| 79 | EXPECT_CALL(device_info_, AddDeviceToBlackList(kTestDevice2Name)).Times(1); |
| 80 | EXPECT_TRUE(device_claimer_.Claim(kTestDevice2Name, &error)); |
| 81 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 82 | EXPECT_TRUE(device_claimer_.DevicesClaimed()); |
| 83 | Mock::VerifyAndClearExpectations(&device_info_); |
| 84 | |
| 85 | // Claim device 1 again, should fail since it is already claimed. |
| 86 | const char kDuplicateDevice1Error[] = |
| 87 | "Device test_device1 had already been claimed"; |
| 88 | error.Reset(); |
| 89 | EXPECT_CALL(device_info_, AddDeviceToBlackList(_)).Times(0); |
| 90 | EXPECT_FALSE(device_claimer_.Claim(kTestDevice1Name, &error)); |
| 91 | EXPECT_EQ(string(kDuplicateDevice1Error), error.message()); |
| 92 | Mock::VerifyAndClearExpectations(&device_info_); |
| 93 | |
| 94 | // Release device 1. |
| 95 | error.Reset(); |
| 96 | EXPECT_CALL(device_info_, |
| 97 | RemoveDeviceFromBlackList(kTestDevice1Name)).Times(1); |
| 98 | EXPECT_TRUE(device_claimer_.Release(kTestDevice1Name, &error)); |
| 99 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 100 | // Should still have one device claimed. |
| 101 | EXPECT_TRUE(device_claimer_.DevicesClaimed()); |
| 102 | Mock::VerifyAndClearExpectations(&device_info_); |
| 103 | |
| 104 | // Release device 1 again, should fail since device 1 is not currently |
| 105 | // claimed. |
| 106 | const char kDevice1NotClaimedError[] = |
| 107 | "Device test_device1 have not been claimed"; |
| 108 | error.Reset(); |
| 109 | EXPECT_CALL(device_info_, RemoveDeviceFromBlackList(_)).Times(0); |
| 110 | EXPECT_FALSE(device_claimer_.Release(kTestDevice1Name, &error)); |
| 111 | EXPECT_EQ(string(kDevice1NotClaimedError), error.message()); |
| 112 | // Should still have one device claimed. |
| 113 | EXPECT_TRUE(device_claimer_.DevicesClaimed()); |
| 114 | Mock::VerifyAndClearExpectations(&device_info_); |
| 115 | |
| 116 | // Release device 2 |
| 117 | error.Reset(); |
| 118 | EXPECT_CALL(device_info_, |
| 119 | RemoveDeviceFromBlackList(kTestDevice2Name)).Times(1); |
| 120 | EXPECT_TRUE(device_claimer_.Release(kTestDevice2Name, &error)); |
| 121 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 122 | Mock::VerifyAndClearExpectations(&device_info_); |
| 123 | |
| 124 | // Should not have any claimed devices. |
| 125 | EXPECT_FALSE(device_claimer_.DevicesClaimed()); |
| 126 | } |
| 127 | |
| 128 | } // namespace shill |