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 void TearDown() { |
| 44 ServiceManager::DeleteTemp(); |
| 45 } |
| 46 |
| 47 protected: |
| 48 std::string remote_; |
| 49 FilePath test_path_; |
| 50 FilePath control_path_; |
| 51 FilePath pppd_config_path_; |
| 52 FilePath ppp_interface_path_; |
| 53 L2tpManager l2tp_; |
| 54 ProcessMock* l2tpd_; |
| 55 }; |
| 56 |
| 57 TEST_F(L2tpManagerTest, FormatL2tpdConfiguration) { |
| 58 static const char kBaseExpected[] = |
| 59 "[lac managed]\n" |
| 60 "lns = 1.2.3.4\n" |
| 61 "require chap = yes\n" |
| 62 "refuse pap = yes\n" |
| 63 "require authentication = yes\n" |
| 64 "name = me\n" |
| 65 "ppp debug = yes\n" |
| 66 "pppoptfile = test/pppd.config\n" |
| 67 "length bit = yes\n"; |
| 68 EXPECT_EQ(kBaseExpected, |
| 69 l2tp_.FormatL2tpdConfiguration(pppd_config_path_.value())); |
| 70 } |
| 71 |
| 72 TEST_F(L2tpManagerTest, FormatPppdConfiguration) { |
| 73 const char kBaseExpected[] = |
| 74 "ipcp-accept-local\n" |
| 75 "ipcp-accept-remote\n" |
| 76 "refuse-eap\n" |
| 77 "noccp\n" |
| 78 "noauth\n" |
| 79 "crtscts\n" |
| 80 "idle 1800\n" |
| 81 "mtu 1410\n" |
| 82 "mru 1410\n" |
| 83 "nodefaultroute\n" |
| 84 "debug\n" |
| 85 "lock\n" |
| 86 "connect-delay 5000\n"; |
| 87 std::string expected(kBaseExpected); |
| 88 EXPECT_EQ(expected, l2tp_.FormatPppdConfiguration()); |
| 89 FLAGS_pppd_plugin = "myplugin"; |
| 90 expected.append("plugin myplugin\n"); |
| 91 EXPECT_EQ(expected, l2tp_.FormatPppdConfiguration()); |
| 92 } |
| 93 |
| 94 TEST_F(L2tpManagerTest, Initiate) { |
| 95 FLAGS_user = "me"; |
| 96 FLAGS_password = "password"; |
| 97 EXPECT_TRUE(l2tp_.Initiate()); |
| 98 ExpectFileEquals("c managed me password\n", control_path_.value().c_str()); |
| 99 FLAGS_pppd_plugin = "set"; |
| 100 EXPECT_TRUE(l2tp_.Initiate()); |
| 101 ExpectFileEquals("c managed\n", control_path_.value().c_str()); |
| 102 } |
| 103 |
| 104 TEST_F(L2tpManagerTest, Terminate) { |
| 105 EXPECT_TRUE(l2tp_.Terminate()); |
| 106 ExpectFileEquals("d managed\n", control_path_.value().c_str()); |
| 107 } |
| 108 |
| 109 TEST_F(L2tpManagerTest, Start) { |
| 110 InSequence unused; |
| 111 const int kMockFd = 567; |
| 112 |
| 113 EXPECT_CALL(*l2tpd_, Reset(0)); |
| 114 EXPECT_CALL(*l2tpd_, AddArg(L2TPD)); |
| 115 EXPECT_CALL(*l2tpd_, AddArg("-c")); |
| 116 EXPECT_CALL(*l2tpd_, AddArg("test/l2tpd.conf")); |
| 117 EXPECT_CALL(*l2tpd_, AddArg("-C")); |
| 118 EXPECT_CALL(*l2tpd_, AddArg("test/l2tpd.control")); |
| 119 EXPECT_CALL(*l2tpd_, AddArg("-D")); |
| 120 EXPECT_CALL(*l2tpd_, RedirectUsingPipe(STDERR_FILENO, false)); |
| 121 EXPECT_CALL(*l2tpd_, Start()); |
| 122 EXPECT_CALL(*l2tpd_, GetPipe(STDERR_FILENO)).WillOnce(Return(kMockFd)); |
| 123 |
| 124 EXPECT_TRUE(l2tp_.Start()); |
| 125 EXPECT_EQ(kMockFd, l2tp_.output_fd()); |
| 126 EXPECT_FALSE(l2tp_.start_ticks_.is_null()); |
| 127 EXPECT_FALSE(l2tp_.was_initiated_); |
| 128 } |
| 129 |
| 130 TEST_F(L2tpManagerTest, PollWaitIfNotUpYet) { |
| 131 l2tp_.start_ticks_ = base::TimeTicks::Now(); |
| 132 EXPECT_EQ(1000, l2tp_.Poll()); |
| 133 } |
| 134 |
| 135 TEST_F(L2tpManagerTest, PollTimeoutWaitingForControl) { |
| 136 l2tp_.start_ticks_ = base::TimeTicks::Now() - |
| 137 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout + 1); |
| 138 EXPECT_EQ(1000, l2tp_.Poll()); |
| 139 EXPECT_TRUE(FindLog("PPP setup timed out")); |
| 140 EXPECT_TRUE(l2tp_.was_stopped_); |
| 141 EXPECT_FALSE(file_util::PathExists(control_path_)); |
| 142 } |
| 143 |
| 144 TEST_F(L2tpManagerTest, PollTimeoutWaitingForUp) { |
| 145 l2tp_.start_ticks_ = base::TimeTicks::Now() - |
| 146 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout + 1); |
| 147 l2tp_.was_initiated_ = true; |
| 148 EXPECT_EQ(1000, l2tp_.Poll()); |
| 149 EXPECT_TRUE(FindLog("PPP setup timed out")); |
| 150 EXPECT_TRUE(l2tp_.was_stopped_); |
| 151 ExpectFileEquals("d managed\n", control_path_.value().c_str()); |
| 152 } |
| 153 |
| 154 TEST_F(L2tpManagerTest, PollInitiateConnection) { |
| 155 l2tp_.start_ticks_ = base::TimeTicks::Now(); |
| 156 file_util::WriteFile(control_path_, "", 0); |
| 157 EXPECT_FALSE(l2tp_.was_initiated_); |
| 158 FLAGS_pppd_plugin = "set"; |
| 159 EXPECT_EQ(1000, l2tp_.Poll()); |
| 160 EXPECT_TRUE(l2tp_.was_initiated_); |
| 161 ExpectFileEquals("c managed\n", control_path_.value().c_str()); |
| 162 } |
| 163 |
| 164 TEST_F(L2tpManagerTest, PollTransitionToUp) { |
| 165 l2tp_.start_ticks_ = base::TimeTicks::Now(); |
| 166 l2tp_.was_initiated_ = true; |
| 167 EXPECT_FALSE(l2tp_.is_running_); |
| 168 file_util::WriteFile(ppp_interface_path_, "", 0); |
| 169 EXPECT_EQ(-1, l2tp_.Poll()); |
| 170 EXPECT_TRUE(FindLog("L2TP connection now up")); |
| 171 EXPECT_TRUE(l2tp_.is_running_); |
| 172 } |
| 173 |
| 174 TEST_F(L2tpManagerTest, PollNothingIfRunning) { |
| 175 l2tp_.is_running_ = true; |
| 176 EXPECT_EQ(-1, l2tp_.Poll()); |
| 177 } |
| 178 |
| 179 int main(int argc, char** argv) { |
| 180 SetUpTests(&argc, argv, true); |
| 181 return RUN_ALL_TESTS(); |
| 182 } |
OLD | NEW |