blob: 09db6ebdfd0c5144d9bf11ff337b457878b54318 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
2// Copyright (C) 2012 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//
Darin Petkova9b1fed2012-02-29 11:49:05 +010016
17#include "shill/rpc_task.h"
18
19#include <gtest/gtest.h>
20
Darin Petkova9b1fed2012-02-29 11:49:05 +010021#include "shill/mock_adaptors.h"
Alex Vakulenkoa41ab512014-07-23 14:24:23 -070022#include "shill/nice_mock_control.h"
Darin Petkova9b1fed2012-02-29 11:49:05 +010023
24using std::map;
25using std::string;
mukesh agrawalae30e9e2013-05-28 14:09:16 -070026using std::vector;
Darin Petkova9b1fed2012-02-29 11:49:05 +010027
28namespace shill {
29
30class RPCTaskTest : public testing::Test,
31 public RPCTaskDelegate {
32 public:
Darin Petkov209e6292012-04-20 11:33:32 +020033 RPCTaskTest()
34 : get_login_calls_(0),
35 notify_calls_(0),
36 task_(&control_, this) {}
Darin Petkova9b1fed2012-02-29 11:49:05 +010037
38 // Inherited from RPCTaskDelegate.
Paul Stewart3b30ca52015-06-16 13:13:10 -070039 virtual void GetLogin(string* user, string* password);
40 virtual void Notify(const string& reason, const map<string, string>& dict);
Darin Petkova9b1fed2012-02-29 11:49:05 +010041
42 protected:
Darin Petkov209e6292012-04-20 11:33:32 +020043 int get_login_calls_;
Darin Petkova9b1fed2012-02-29 11:49:05 +010044 int notify_calls_;
Paul Stewart3b30ca52015-06-16 13:13:10 -070045 string* last_user_;
46 string* last_password_;
Darin Petkova9b1fed2012-02-29 11:49:05 +010047 string last_notify_reason_;
48 map<string, string> last_notify_dict_;
49 NiceMockControl control_;
50 RPCTask task_;
51};
52
Paul Stewart3b30ca52015-06-16 13:13:10 -070053void RPCTaskTest::GetLogin(string* user, string* password) {
Darin Petkov209e6292012-04-20 11:33:32 +020054 get_login_calls_++;
55 last_user_ = user;
56 last_password_ = password;
57}
58
Paul Stewart3b30ca52015-06-16 13:13:10 -070059void RPCTaskTest::Notify(const string& reason,
60 const map<string, string>& dict) {
Darin Petkova9b1fed2012-02-29 11:49:05 +010061 notify_calls_++;
62 last_notify_reason_ = reason;
63 last_notify_dict_ = dict;
64}
65
mukesh agrawalae30e9e2013-05-28 14:09:16 -070066TEST_F(RPCTaskTest, GetEnvironment) {
Peter Qiua24480a2015-08-11 23:09:54 -070067 map<string, string> env = task_.GetEnvironment();
mukesh agrawalae30e9e2013-05-28 14:09:16 -070068 ASSERT_EQ(2, env.size());
Peter Qiua24480a2015-08-11 23:09:54 -070069 EXPECT_EQ(env[kRPCTaskServiceVariable], RPCTaskMockAdaptor::kRpcConnId);
70 EXPECT_EQ(env[kRPCTaskPathVariable], RPCTaskMockAdaptor::kRpcId);
mukesh agrawalae30e9e2013-05-28 14:09:16 -070071}
72
Darin Petkova9b1fed2012-02-29 11:49:05 +010073TEST_F(RPCTaskTest, GetRpcIdentifiers) {
74 EXPECT_EQ(RPCTaskMockAdaptor::kRpcId, task_.GetRpcIdentifier());
Darin Petkova9b1fed2012-02-29 11:49:05 +010075 EXPECT_EQ(RPCTaskMockAdaptor::kRpcConnId, task_.GetRpcConnectionIdentifier());
76}
77
Darin Petkov209e6292012-04-20 11:33:32 +020078TEST_F(RPCTaskTest, GetLogin) {
79 string user, password;
80 task_.GetLogin(&user, &password);
81 EXPECT_EQ(1, get_login_calls_);
82 EXPECT_EQ(&user, last_user_);
83 EXPECT_EQ(&password, last_password_);
84}
85
Darin Petkova9b1fed2012-02-29 11:49:05 +010086TEST_F(RPCTaskTest, Notify) {
87 static const char kReason[] = "up";
88 map<string, string> dict;
89 dict["foo"] = "bar";
90 task_.Notify(kReason, dict);
91 EXPECT_EQ(1, notify_calls_);
92 EXPECT_EQ(kReason, last_notify_reason_);
93 EXPECT_EQ("bar", last_notify_dict_["foo"]);
94}
95
96} // namespace shill