Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: l2tp_manager_test.cc

Issue 6508016: vpn-manager: Add l2tp/ipsec vpn manager (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vpn-manager.git@master
Patch Set: compute local address instead of taking option Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 std::string expected(kBaseExpected);
85 EXPECT_EQ(expected, l2tp_.FormatPppdConfiguration());
86 FLAGS_pppd_plugin = "myplugin";
87 expected.append("plugin myplugin\n");
88 EXPECT_EQ(expected, l2tp_.FormatPppdConfiguration());
89 }
90
91 TEST_F(L2tpManagerTest, Initiate) {
92 FLAGS_user = "me";
93 FLAGS_password = "password";
94 EXPECT_TRUE(l2tp_.Initiate());
95 ExpectFileEquals("c managed me password\n", control_path_.value().c_str());
96 FLAGS_pppd_plugin = "set";
97 EXPECT_TRUE(l2tp_.Initiate());
98 ExpectFileEquals("c managed\n", control_path_.value().c_str());
99 }
100
101 TEST_F(L2tpManagerTest, Terminate) {
102 EXPECT_TRUE(l2tp_.Terminate());
103 ExpectFileEquals("d managed\n", control_path_.value().c_str());
104 }
105
106 TEST_F(L2tpManagerTest, Start) {
107 InSequence unused;
108 const int kMockFd = 567;
109
110 EXPECT_CALL(*l2tpd_, Reset(0));
111 EXPECT_CALL(*l2tpd_, AddArg(L2TPD));
112 EXPECT_CALL(*l2tpd_, AddArg("-c"));
113 EXPECT_CALL(*l2tpd_, AddArg("test/l2tpd.conf"));
114 EXPECT_CALL(*l2tpd_, AddArg("-C"));
115 EXPECT_CALL(*l2tpd_, AddArg("test/l2tpd.control"));
116 EXPECT_CALL(*l2tpd_, AddArg("-D"));
117 EXPECT_CALL(*l2tpd_, RedirectUsingPipe(STDERR_FILENO, false));
118 EXPECT_CALL(*l2tpd_, Start());
119 EXPECT_CALL(*l2tpd_, GetPipe(STDERR_FILENO)).WillOnce(Return(kMockFd));
120
121 EXPECT_TRUE(l2tp_.Start());
122 EXPECT_EQ(kMockFd, l2tp_.output_fd());
123 EXPECT_FALSE(l2tp_.start_ticks_.is_null());
124 EXPECT_FALSE(l2tp_.was_initiated_);
125 }
126
127 TEST_F(L2tpManagerTest, PollWaitIfNotUpYet) {
128 l2tp_.start_ticks_ = base::TimeTicks::Now();
129 EXPECT_EQ(1000, l2tp_.Poll());
130 }
131
132 TEST_F(L2tpManagerTest, PollTimeoutWaitingForControl) {
133 l2tp_.start_ticks_ = base::TimeTicks::Now() -
134 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout + 1);
135 EXPECT_EQ(1000, l2tp_.Poll());
136 EXPECT_TRUE(FindLog("PPP setup timed out"));
137 EXPECT_TRUE(l2tp_.was_stopped());
138 EXPECT_FALSE(file_util::PathExists(control_path_));
139 }
140
141 TEST_F(L2tpManagerTest, PollTimeoutWaitingForUp) {
142 l2tp_.start_ticks_ = base::TimeTicks::Now() -
143 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout + 1);
144 l2tp_.was_initiated_ = true;
145 EXPECT_EQ(1000, l2tp_.Poll());
146 EXPECT_TRUE(FindLog("PPP setup timed out"));
147 EXPECT_TRUE(l2tp_.was_stopped());
148 ExpectFileEquals("d managed\n", control_path_.value().c_str());
149 }
150
151 TEST_F(L2tpManagerTest, PollInitiateConnection) {
152 l2tp_.start_ticks_ = base::TimeTicks::Now();
153 file_util::WriteFile(control_path_, "", 0);
154 EXPECT_FALSE(l2tp_.was_initiated_);
155 FLAGS_pppd_plugin = "set";
156 EXPECT_EQ(1000, l2tp_.Poll());
157 EXPECT_TRUE(l2tp_.was_initiated_);
158 ExpectFileEquals("c managed\n", control_path_.value().c_str());
159 }
160
161 TEST_F(L2tpManagerTest, PollTransitionToUp) {
162 l2tp_.start_ticks_ = base::TimeTicks::Now();
163 l2tp_.was_initiated_ = true;
164 EXPECT_FALSE(l2tp_.is_running());
165 file_util::WriteFile(ppp_interface_path_, "", 0);
166 EXPECT_EQ(-1, l2tp_.Poll());
167 EXPECT_TRUE(FindLog("L2TP connection now up"));
168 EXPECT_TRUE(l2tp_.is_running());
169 }
170
171 TEST_F(L2tpManagerTest, PollNothingIfRunning) {
172 l2tp_.is_running_ = true;
173 EXPECT_EQ(-1, l2tp_.Poll());
174 }
175
176 int main(int argc, char** argv) {
177 SetUpTests(&argc, argv, true);
178 return RUN_ALL_TESTS();
179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698