blob: b403e183867e44dbced0b1cc15a0dda2cb8a2ca8 [file] [log] [blame]
Darin Petkova9b1fed2012-02-29 11:49:05 +01001// Copyright (c) 2012 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/rpc_task.h"
6
7#include <gtest/gtest.h>
8
Darin Petkova9b1fed2012-02-29 11:49:05 +01009#include "shill/mock_adaptors.h"
Alex Vakulenkoa41ab512014-07-23 14:24:23 -070010#include "shill/nice_mock_control.h"
Darin Petkova9b1fed2012-02-29 11:49:05 +010011
12using std::map;
13using std::string;
mukesh agrawalae30e9e2013-05-28 14:09:16 -070014using std::vector;
Darin Petkova9b1fed2012-02-29 11:49:05 +010015
16namespace shill {
17
18class RPCTaskTest : public testing::Test,
19 public RPCTaskDelegate {
20 public:
Darin Petkov209e6292012-04-20 11:33:32 +020021 RPCTaskTest()
22 : get_login_calls_(0),
23 notify_calls_(0),
24 task_(&control_, this) {}
Darin Petkova9b1fed2012-02-29 11:49:05 +010025
26 // Inherited from RPCTaskDelegate.
Darin Petkov209e6292012-04-20 11:33:32 +020027 virtual void GetLogin(string *user, string *password);
Darin Petkova9b1fed2012-02-29 11:49:05 +010028 virtual void Notify(const string &reason, const map<string, string> &dict);
29
30 protected:
Darin Petkov209e6292012-04-20 11:33:32 +020031 int get_login_calls_;
Darin Petkova9b1fed2012-02-29 11:49:05 +010032 int notify_calls_;
Darin Petkov209e6292012-04-20 11:33:32 +020033 string *last_user_;
34 string *last_password_;
Darin Petkova9b1fed2012-02-29 11:49:05 +010035 string last_notify_reason_;
36 map<string, string> last_notify_dict_;
37 NiceMockControl control_;
38 RPCTask task_;
39};
40
Darin Petkov209e6292012-04-20 11:33:32 +020041void RPCTaskTest::GetLogin(string *user, string *password) {
42 get_login_calls_++;
43 last_user_ = user;
44 last_password_ = password;
45}
46
Darin Petkova9b1fed2012-02-29 11:49:05 +010047void RPCTaskTest::Notify(const string &reason,
48 const map<string, string> &dict) {
Darin Petkova9b1fed2012-02-29 11:49:05 +010049 notify_calls_++;
50 last_notify_reason_ = reason;
51 last_notify_dict_ = dict;
52}
53
mukesh agrawalae30e9e2013-05-28 14:09:16 -070054TEST_F(RPCTaskTest, GetEnvironment) {
55 vector<string> env = task_.GetEnvironment();
56 ASSERT_EQ(2, env.size());
57 EXPECT_EQ(
58 string(kRPCTaskServiceVariable) + "=" + RPCTaskMockAdaptor::kRpcConnId,
59 env[0]);
60 EXPECT_EQ(string(kRPCTaskPathVariable) + "=" + RPCTaskMockAdaptor::kRpcId,
61 env[1]);
62}
63
Darin Petkova9b1fed2012-02-29 11:49:05 +010064TEST_F(RPCTaskTest, GetRpcIdentifiers) {
65 EXPECT_EQ(RPCTaskMockAdaptor::kRpcId, task_.GetRpcIdentifier());
Darin Petkova9b1fed2012-02-29 11:49:05 +010066 EXPECT_EQ(RPCTaskMockAdaptor::kRpcConnId, task_.GetRpcConnectionIdentifier());
67}
68
Darin Petkov209e6292012-04-20 11:33:32 +020069TEST_F(RPCTaskTest, GetLogin) {
70 string user, password;
71 task_.GetLogin(&user, &password);
72 EXPECT_EQ(1, get_login_calls_);
73 EXPECT_EQ(&user, last_user_);
74 EXPECT_EQ(&password, last_password_);
75}
76
Darin Petkova9b1fed2012-02-29 11:49:05 +010077TEST_F(RPCTaskTest, Notify) {
78 static const char kReason[] = "up";
79 map<string, string> dict;
80 dict["foo"] = "bar";
81 task_.Notify(kReason, dict);
82 EXPECT_EQ(1, notify_calls_);
83 EXPECT_EQ(kReason, last_notify_reason_);
84 EXPECT_EQ("bar", last_notify_dict_["foo"]);
85}
86
87} // namespace shill