1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/ppp_device.h"
18
19 #include <map>
20 #include <string>
21
22 #include <gtest/gtest.h>
23
24 #include "shill/metrics.h"
25 #include "shill/mock_control.h"
26 #include "shill/mock_metrics.h"
27 #include "shill/mock_ppp_device.h"
28
29 using std::map;
30 using std::string;
31
32 namespace shill {
33
34 // TODO(quiche): Add test for UpdateIPConfigFromPPP. crbug.com/266404
35
TEST(PPPDeviceTest,GetInterfaceName)36 TEST(PPPDeviceTest, GetInterfaceName) {
37 map<string, string> config;
38 config[kPPPInterfaceName] = "ppp0";
39 config["foo"] = "bar";
40 EXPECT_EQ("ppp0", PPPDevice::GetInterfaceName(config));
41 }
42
TEST(PPPDeviceTest,ParseIPConfiguration)43 TEST(PPPDeviceTest, ParseIPConfiguration) {
44 MockControl control;
45 MockMetrics metrics(nullptr);
46 scoped_refptr<PPPDevice> device = new PPPDevice(&control, nullptr, &metrics,
47 nullptr, "test0", 0);
48
49 map<string, string> config;
50 config[kPPPInternalIP4Address] = "4.5.6.7";
51 config[kPPPExternalIP4Address] = "33.44.55.66";
52 config[kPPPGatewayAddress] = "192.168.1.1";
53 config[kPPPDNS1] = "1.1.1.1";
54 config[kPPPDNS2] = "2.2.2.2";
55 config[kPPPInterfaceName] = "ppp0";
56 config[kPPPLNSAddress] = "99.88.77.66";
57 config[kPPPMRU] = "1492";
58 config["foo"] = "bar"; // Unrecognized keys don't cause crash.
59 EXPECT_CALL(metrics, SendSparseToUMA(Metrics::kMetricPPPMTUValue, 1492));
60 IPConfig::Properties props = device->ParseIPConfiguration("in-test", config);
61 EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family);
62 EXPECT_EQ(IPAddress::GetMaxPrefixLength(IPAddress::kFamilyIPv4),
63 props.subnet_prefix);
64 EXPECT_EQ("4.5.6.7", props.address);
65 EXPECT_EQ("33.44.55.66", props.peer_address);
66 EXPECT_EQ("192.168.1.1", props.gateway);
67 ASSERT_EQ(2, props.dns_servers.size());
68 EXPECT_EQ("1.1.1.1", props.dns_servers[0]);
69 EXPECT_EQ("2.2.2.2", props.dns_servers[1]);
70 EXPECT_EQ("99.88.77.66/32", props.exclusion_list[0]);
71 EXPECT_EQ(1, props.exclusion_list.size());
72 EXPECT_EQ(1492, props.mtu);
73
74 // No gateway specified.
75 config.erase(kPPPGatewayAddress);
76 EXPECT_CALL(metrics, SendSparseToUMA(Metrics::kMetricPPPMTUValue, 1492));
77 IPConfig::Properties props2 = device->ParseIPConfiguration("in-test", config);
78 EXPECT_EQ("33.44.55.66", props2.gateway);
79 }
80
81 } // namespace shill
82