blob: f98a1fe192705a42dc5630d1c7ef0071950a4987 [file] [log] [blame]
Garret Kellycb832e82015-01-14 16:37:27 -05001// Copyright 2015 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 <map>
6#include <memory>
7#include <set>
8#include <string>
9#include <vector>
10
11#include <base/bind.h>
12#include <base/bind_helpers.h>
13#include <base/memory/weak_ptr.h>
14#include <gtest/gtest.h>
15
16#include "shill/error.h"
17#include "shill/external_task.h"
18#include "shill/glib.h"
19#include "shill/mock_control.h"
20#include "shill/mock_glib.h"
21#include "shill/ppp_daemon.h"
22#include "shill/rpc_task.h"
23
24namespace shill {
25
26using testing::_;
27using testing::Invoke;
28using testing::Return;
29using testing::Test;
30using testing::WithArg;
31
32class PPPDaemonTest : public Test, public RPCTaskDelegate {
33 public:
34 PPPDaemonTest() : weak_ptr_factory_(this) {}
35 virtual ~PPPDaemonTest() {}
36
Paul Stewart3b30ca52015-06-16 13:13:10 -070037 std::unique_ptr<ExternalTask> Start(const PPPDaemon::Options& options,
38 const std::string& device,
39 Error* error) {
Garret Kellycb832e82015-01-14 16:37:27 -050040 PPPDaemon::DeathCallback callback(base::Bind(&PPPDaemonTest::DeathCallback,
41 base::Unretained(this)));
42 return PPPDaemon::Start(&control_, &glib_, weak_ptr_factory_.GetWeakPtr(),
43 options, device, callback, error);
44 }
45
Paul Stewart3b30ca52015-06-16 13:13:10 -070046 gboolean CaptureArgv(gchar** argv) {
47 for (gchar** i = argv; *i != nullptr; ++i) {
Garret Kellycb832e82015-01-14 16:37:27 -050048 argv_.push_back(*i);
49 }
50 return TRUE;
51 }
52
Paul Stewart3b30ca52015-06-16 13:13:10 -070053 MOCK_METHOD2(GetLogin, void(std::string* user, std::string* password));
54 MOCK_METHOD2(Notify, void(const std::string& reason,
55 const std::map<std::string, std::string>& dict));
Garret Kellycb832e82015-01-14 16:37:27 -050056
57 protected:
58 MockControl control_;
59 MockGLib glib_;
60
61 std::vector<std::string> argv_;
62 base::WeakPtrFactory<PPPDaemonTest> weak_ptr_factory_;
63
64 MOCK_METHOD2(DeathCallback, void(pid_t pid, int status));
65
66 private:
67 DISALLOW_COPY_AND_ASSIGN(PPPDaemonTest);
68};
69
70TEST_F(PPPDaemonTest, PluginUsed) {
71 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
72 .WillOnce(WithArg<1>(Invoke(this, &PPPDaemonTest::CaptureArgv)));
73
74 Error error;
75 PPPDaemon::Options options;
76 std::unique_ptr<ExternalTask> task(Start(options, "eth0", &error));
77
78 for (size_t i = 0; i < argv_.size(); ++i) {
79 if (argv_[i] == "plugin") {
Garret Kelly1e8414c2015-01-30 10:56:59 -050080 EXPECT_EQ(argv_[i + 1], PPPDaemon::kShimPluginPath);
Garret Kellycb832e82015-01-14 16:37:27 -050081 }
82 }
83}
84
85TEST_F(PPPDaemonTest, OptionsConverted) {
86 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
87 .WillOnce(WithArg<1>(Invoke(this, &PPPDaemonTest::CaptureArgv)));
88
89 PPPDaemon::Options options;
90 options.no_detach = true;
91 options.no_default_route = true;
92 options.use_peer_dns = true;
Garret Kelly75fba812015-03-09 15:18:02 -040093 options.lcp_echo_interval = 1;
94 options.lcp_echo_failure = 1;
Garret Kellyb84e02c2015-03-26 15:54:51 -040095 options.max_fail = 1;
Garret Kellycb832e82015-01-14 16:37:27 -050096
97 Error error;
98 std::unique_ptr<ExternalTask> task(Start(options, "eth0", &error));
99
100 std::set<std::string> expected_arguments = {
Garret Kelly75fba812015-03-09 15:18:02 -0400101 "nodetach", "nodefaultroute", "usepeerdns", "lcp-echo-interval",
Garret Kellyb84e02c2015-03-26 15:54:51 -0400102 "lcp-echo-failure", "maxfail",
Garret Kellycb832e82015-01-14 16:37:27 -0500103 };
Paul Stewart3b30ca52015-06-16 13:13:10 -0700104 for (const auto& argument : argv_) {
Garret Kellycb832e82015-01-14 16:37:27 -0500105 expected_arguments.erase(argument);
106 }
107 EXPECT_TRUE(expected_arguments.empty());
108}
109
110TEST_F(PPPDaemonTest, ErrorPropagated) {
111 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
112 .WillOnce(Return(false));
113
114 PPPDaemon::Options options;
115 Error error;
116 std::unique_ptr<ExternalTask> task(Start(options, "eth0", &error));
117
118 EXPECT_NE(error.type(), Error::kSuccess);
119 EXPECT_EQ(task.get(), nullptr);
120}
121
122} // namespace shill