OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chromeos/process_mock.h" |
| 6 #include "chromeos/syslog_logging.h" |
| 7 #include "chromeos/test_helpers.h" |
| 8 #include "gflags/gflags.h" |
| 9 #include "gtest/gtest.h" |
| 10 |
| 11 #include "vpn-manager/l2tp_manager.h" |
| 12 |
| 13 DECLARE_string(password); |
| 14 DECLARE_int32(ppp_setup_timeout); |
| 15 DECLARE_string(pppd_plugin); |
| 16 DECLARE_string(user); |
| 17 |
| 18 using ::chromeos::FindLog; |
| 19 using ::chromeos::ProcessMock; |
| 20 using ::testing::_; |
| 21 using ::testing::InSequence; |
| 22 using ::testing::Return; |
| 23 |
| 24 class L2tpManagerTest : public ::testing::Test { |
| 25 public: |
| 26 void SetUp() { |
| 27 test_path_ = FilePath("test"); |
| 28 ServiceManager::temp_path_ = new FilePath(test_path_); |
| 29 file_util::Delete(test_path_, true); |
| 30 file_util::CreateDirectory(test_path_); |
| 31 remote_ = "1.2.3.4"; |
| 32 control_path_ = test_path_.Append("control"); |
| 33 pppd_config_path_ = test_path_.Append("pppd.config"); |
| 34 ppp_interface_path_ = test_path_.Append("ppp0"); |
| 35 l2tpd_ = new ProcessMock; |
| 36 l2tp_.l2tpd_.reset(l2tpd_); |
| 37 l2tp_.l2tpd_control_path_ = control_path_; |
| 38 l2tp_.ppp_interface_path_ = ppp_interface_path_; |
| 39 FLAGS_pppd_plugin = ""; |
| 40 FLAGS_user = "me"; |
| 41 EXPECT_TRUE(l2tp_.Initialize(remote_)); |
| 42 } |
| 43 |
| 44 protected: |
| 45 std::string remote_; |
| 46 FilePath test_path_; |
| 47 FilePath control_path_; |
| 48 FilePath pppd_config_path_; |
| 49 FilePath ppp_interface_path_; |
| 50 L2tpManager l2tp_; |
| 51 ProcessMock* l2tpd_; |
| 52 }; |
| 53 |
| 54 TEST_F(L2tpManagerTest, FormatL2tpdConfiguration) { |
| 55 static const char kBaseExpected[] = |
| 56 "[lac managed]\n" |
| 57 "lns = 1.2.3.4\n" |
| 58 "require chap = yes\n" |
| 59 "refuse pap = yes\n" |
| 60 "require authentication = yes\n" |
| 61 "name = me\n" |
| 62 "ppp debug = yes\n" |
| 63 "pppoptfile = test/pppd.config\n" |
| 64 "length bit = yes\n"; |
| 65 EXPECT_EQ(kBaseExpected, |
| 66 l2tp_.FormatL2tpdConfiguration(pppd_config_path_.value())); |
| 67 } |
| 68 |
| 69 TEST_F(L2tpManagerTest, FormatPppdConfiguration) { |
| 70 const char kBaseExpected[] = |
| 71 "ipcp-accept-local\n" |
| 72 "ipcp-accept-remote\n" |
| 73 "refuse-eap\n" |
| 74 "noccp\n" |
| 75 "noauth\n" |
| 76 "crtscts\n" |
| 77 "idle 1800\n" |
| 78 "mtu 1410\n" |
| 79 "mru 1410\n" |
| 80 "nodefaultroute\n" |
| 81 "debug\n" |
| 82 "lock\n" |
| 83 "connect-delay 5000\n" |
| 84 "usepeerdns\n"; |
| 85 std::string expected(kBaseExpected); |
| 86 EXPECT_EQ(expected, l2tp_.FormatPppdConfiguration()); |
| 87 FLAGS_pppd_plugin = "myplugin"; |
| 88 expected.append("plugin myplugin\n"); |
| 89 EXPECT_EQ(expected, l2tp_.FormatPppdConfiguration()); |
| 90 } |
| 91 |
| 92 TEST_F(L2tpManagerTest, Initiate) { |
| 93 FLAGS_user = "me"; |
| 94 FLAGS_password = "password"; |
| 95 EXPECT_TRUE(l2tp_.Initiate()); |
| 96 ExpectFileEquals("c managed me password\n", control_path_.value().c_str()); |
| 97 FLAGS_pppd_plugin = "set"; |
| 98 EXPECT_TRUE(l2tp_.Initiate()); |
| 99 ExpectFileEquals("c managed\n", control_path_.value().c_str()); |
| 100 } |
| 101 |
| 102 TEST_F(L2tpManagerTest, Terminate) { |
| 103 EXPECT_TRUE(l2tp_.Terminate()); |
| 104 ExpectFileEquals("d managed\n", control_path_.value().c_str()); |
| 105 } |
| 106 |
| 107 TEST_F(L2tpManagerTest, Start) { |
| 108 InSequence unused; |
| 109 const int kMockFd = 567; |
| 110 |
| 111 EXPECT_CALL(*l2tpd_, Reset(0)); |
| 112 EXPECT_CALL(*l2tpd_, AddArg(L2TPD)); |
| 113 EXPECT_CALL(*l2tpd_, AddArg("-c")); |
| 114 EXPECT_CALL(*l2tpd_, AddArg("test/l2tpd.conf")); |
| 115 EXPECT_CALL(*l2tpd_, AddArg("-C")); |
| 116 EXPECT_CALL(*l2tpd_, AddArg("test/l2tpd.control")); |
| 117 EXPECT_CALL(*l2tpd_, AddArg("-D")); |
| 118 EXPECT_CALL(*l2tpd_, RedirectUsingPipe(STDERR_FILENO, false)); |
| 119 EXPECT_CALL(*l2tpd_, Start()); |
| 120 EXPECT_CALL(*l2tpd_, GetPipe(STDERR_FILENO)).WillOnce(Return(kMockFd)); |
| 121 |
| 122 EXPECT_TRUE(l2tp_.Start()); |
| 123 EXPECT_EQ(kMockFd, l2tp_.output_fd()); |
| 124 EXPECT_FALSE(l2tp_.start_ticks_.is_null()); |
| 125 EXPECT_FALSE(l2tp_.was_initiated_); |
| 126 } |
| 127 |
| 128 TEST_F(L2tpManagerTest, PollWaitIfNotUpYet) { |
| 129 l2tp_.start_ticks_ = base::TimeTicks::Now(); |
| 130 EXPECT_EQ(1000, l2tp_.Poll()); |
| 131 } |
| 132 |
| 133 TEST_F(L2tpManagerTest, PollTimeoutWaitingForControl) { |
| 134 l2tp_.start_ticks_ = base::TimeTicks::Now() - |
| 135 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout + 1); |
| 136 EXPECT_EQ(1000, l2tp_.Poll()); |
| 137 EXPECT_TRUE(FindLog("PPP setup timed out")); |
| 138 EXPECT_TRUE(l2tp_.was_stopped()); |
| 139 EXPECT_FALSE(file_util::PathExists(control_path_)); |
| 140 } |
| 141 |
| 142 TEST_F(L2tpManagerTest, PollTimeoutWaitingForUp) { |
| 143 l2tp_.start_ticks_ = base::TimeTicks::Now() - |
| 144 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout + 1); |
| 145 l2tp_.was_initiated_ = true; |
| 146 EXPECT_EQ(1000, l2tp_.Poll()); |
| 147 EXPECT_TRUE(FindLog("PPP setup timed out")); |
| 148 EXPECT_TRUE(l2tp_.was_stopped()); |
| 149 ExpectFileEquals("d managed\n", control_path_.value().c_str()); |
| 150 } |
| 151 |
| 152 TEST_F(L2tpManagerTest, PollInitiateConnection) { |
| 153 l2tp_.start_ticks_ = base::TimeTicks::Now(); |
| 154 file_util::WriteFile(control_path_, "", 0); |
| 155 EXPECT_FALSE(l2tp_.was_initiated_); |
| 156 FLAGS_pppd_plugin = "set"; |
| 157 EXPECT_EQ(1000, l2tp_.Poll()); |
| 158 EXPECT_TRUE(l2tp_.was_initiated_); |
| 159 ExpectFileEquals("c managed\n", control_path_.value().c_str()); |
| 160 } |
| 161 |
| 162 TEST_F(L2tpManagerTest, PollTransitionToUp) { |
| 163 l2tp_.start_ticks_ = base::TimeTicks::Now(); |
| 164 l2tp_.was_initiated_ = true; |
| 165 EXPECT_FALSE(l2tp_.is_running()); |
| 166 file_util::WriteFile(ppp_interface_path_, "", 0); |
| 167 EXPECT_EQ(-1, l2tp_.Poll()); |
| 168 EXPECT_TRUE(FindLog("L2TP connection now up")); |
| 169 EXPECT_TRUE(l2tp_.is_running()); |
| 170 } |
| 171 |
| 172 TEST_F(L2tpManagerTest, PollNothingIfRunning) { |
| 173 l2tp_.is_running_ = true; |
| 174 EXPECT_EQ(-1, l2tp_.Poll()); |
| 175 } |
| 176 |
| 177 int main(int argc, char** argv) { |
| 178 SetUpTests(&argc, argv, true); |
| 179 return RUN_ALL_TESTS(); |
| 180 } |
OLD | NEW |