Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 1 | // 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 | |
| 24 | namespace shill { |
| 25 | |
| 26 | using testing::_; |
| 27 | using testing::Invoke; |
| 28 | using testing::Return; |
| 29 | using testing::Test; |
| 30 | using testing::WithArg; |
| 31 | |
| 32 | class PPPDaemonTest : public Test, public RPCTaskDelegate { |
| 33 | public: |
| 34 | PPPDaemonTest() : weak_ptr_factory_(this) {} |
| 35 | virtual ~PPPDaemonTest() {} |
| 36 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 37 | std::unique_ptr<ExternalTask> Start(const PPPDaemon::Options& options, |
| 38 | const std::string& device, |
| 39 | Error* error) { |
Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 40 | 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 Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 46 | gboolean CaptureArgv(gchar** argv) { |
| 47 | for (gchar** i = argv; *i != nullptr; ++i) { |
Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 48 | argv_.push_back(*i); |
| 49 | } |
| 50 | return TRUE; |
| 51 | } |
| 52 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 53 | 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 Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 56 | |
| 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 | |
| 70 | TEST_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 Kelly | 1e8414c | 2015-01-30 10:56:59 -0500 | [diff] [blame] | 80 | EXPECT_EQ(argv_[i + 1], PPPDaemon::kShimPluginPath); |
Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | TEST_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 Kelly | 75fba81 | 2015-03-09 15:18:02 -0400 | [diff] [blame] | 93 | options.lcp_echo_interval = 1; |
| 94 | options.lcp_echo_failure = 1; |
Garret Kelly | b84e02c | 2015-03-26 15:54:51 -0400 | [diff] [blame] | 95 | options.max_fail = 1; |
Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 96 | |
| 97 | Error error; |
| 98 | std::unique_ptr<ExternalTask> task(Start(options, "eth0", &error)); |
| 99 | |
| 100 | std::set<std::string> expected_arguments = { |
Garret Kelly | 75fba81 | 2015-03-09 15:18:02 -0400 | [diff] [blame] | 101 | "nodetach", "nodefaultroute", "usepeerdns", "lcp-echo-interval", |
Garret Kelly | b84e02c | 2015-03-26 15:54:51 -0400 | [diff] [blame] | 102 | "lcp-echo-failure", "maxfail", |
Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 103 | }; |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 104 | for (const auto& argument : argv_) { |
Garret Kelly | cb832e8 | 2015-01-14 16:37:27 -0500 | [diff] [blame] | 105 | expected_arguments.erase(argument); |
| 106 | } |
| 107 | EXPECT_TRUE(expected_arguments.empty()); |
| 108 | } |
| 109 | |
| 110 | TEST_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 |