blob: 65d0dba14eca6ae5a8144c03241f0bd2c6f67172 [file] [log] [blame]
Ben Chanc12cf662012-04-05 14:47:18 -07001// Copyright (c) 2012 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 "shill/technology.h"
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
10#include "shill/error.h"
11
12using std::string;
13using std::vector;
14using testing::ElementsAre;
15using testing::Test;
16
17namespace shill {
18
19class TechnologyTest : public Test {};
20
21TEST_F(TechnologyTest, IdentifierFromName) {
22 EXPECT_EQ(Technology::kEthernet, Technology::IdentifierFromName("ethernet"));
23 EXPECT_EQ(Technology::kWifi, Technology::IdentifierFromName("wifi"));
Ben Chan46af1272012-05-01 10:34:00 -070024 EXPECT_EQ(Technology::kWiMax, Technology::IdentifierFromName("wimax"));
Ben Chanc12cf662012-04-05 14:47:18 -070025 EXPECT_EQ(Technology::kCellular, Technology::IdentifierFromName("cellular"));
Paul Stewartca876ee2012-04-21 08:55:58 -070026 EXPECT_EQ(Technology::kTunnel, Technology::IdentifierFromName("tunnel"));
27 EXPECT_EQ(Technology::kLoopback, Technology::IdentifierFromName("loopback"));
Ben Chanc12cf662012-04-05 14:47:18 -070028 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromName("vpn"));
Paul Stewartca876ee2012-04-21 08:55:58 -070029 EXPECT_EQ(Technology::kPPP, Technology::IdentifierFromName("ppp"));
Ben Chanc12cf662012-04-05 14:47:18 -070030 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("bluetooth"));
Ben Chanc12cf662012-04-05 14:47:18 -070031 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("foo"));
32 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName(""));
33}
34
35TEST_F(TechnologyTest, NameFromIdentifier) {
36 EXPECT_EQ("ethernet", Technology::NameFromIdentifier(Technology::kEthernet));
37 EXPECT_EQ("wifi", Technology::NameFromIdentifier(Technology::kWifi));
Ben Chan46af1272012-05-01 10:34:00 -070038 EXPECT_EQ("wimax", Technology::NameFromIdentifier(Technology::kWiMax));
Ben Chanc12cf662012-04-05 14:47:18 -070039 EXPECT_EQ("cellular", Technology::NameFromIdentifier(Technology::kCellular));
Paul Stewartca876ee2012-04-21 08:55:58 -070040 EXPECT_EQ("tunnel", Technology::NameFromIdentifier(Technology::kTunnel));
41 EXPECT_EQ("loopback", Technology::NameFromIdentifier(Technology::kLoopback));
Ben Chanc12cf662012-04-05 14:47:18 -070042 EXPECT_EQ("vpn", Technology::NameFromIdentifier(Technology::kVPN));
Paul Stewartca876ee2012-04-21 08:55:58 -070043 EXPECT_EQ("ppp", Technology::NameFromIdentifier(Technology::kPPP));
44 EXPECT_EQ("unknown", Technology::NameFromIdentifier(Technology::kUnknown));
Ben Chanc12cf662012-04-05 14:47:18 -070045}
46
47TEST_F(TechnologyTest, IdentifierFromStorageGroup) {
48 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromStorageGroup("vpn"));
49 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromStorageGroup("vpn_a"));
50 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromStorageGroup("vpn__a"));
51 EXPECT_EQ(Technology::kVPN,
52 Technology::IdentifierFromStorageGroup("vpn_a_1"));
53 EXPECT_EQ(Technology::kUnknown,
54 Technology::IdentifierFromStorageGroup("_vpn"));
55 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromStorageGroup("_"));
56 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromStorageGroup(""));
57}
58
59TEST_F(TechnologyTest, GetTechnologyVectorFromStringWithValidTechnologyNames) {
60 vector<Technology::Identifier> technologies;
61 Error error;
62
63 EXPECT_TRUE(Technology::GetTechnologyVectorFromString(
64 "", &technologies, &error));
65 EXPECT_THAT(technologies, ElementsAre());
66 EXPECT_TRUE(error.IsSuccess());
67
68 EXPECT_TRUE(Technology::GetTechnologyVectorFromString(
69 "ethernet", &technologies, &error));
70 EXPECT_THAT(technologies, ElementsAre(Technology::kEthernet));
71 EXPECT_TRUE(error.IsSuccess());
72
73 EXPECT_TRUE(Technology::GetTechnologyVectorFromString(
74 "ethernet,vpn", &technologies, &error));
75 EXPECT_THAT(technologies, ElementsAre(Technology::kEthernet,
76 Technology::kVPN));
77 EXPECT_TRUE(error.IsSuccess());
78
79 EXPECT_TRUE(Technology::GetTechnologyVectorFromString(
80 "wifi,ethernet,vpn", &technologies, &error));
81 EXPECT_THAT(technologies, ElementsAre(Technology::kWifi,
82 Technology::kEthernet,
83 Technology::kVPN));
84 EXPECT_TRUE(error.IsSuccess());
85}
86
87TEST_F(TechnologyTest,
88 GetTechnologyVectorFromStringWithInvalidTechnologyNames) {
89 vector<Technology::Identifier> technologies;
90 Error error;
91
92 EXPECT_FALSE(Technology::GetTechnologyVectorFromString(
93 "foo", &technologies, &error));
94 EXPECT_EQ(Error::kInvalidArguments, error.type());
95 EXPECT_EQ("foo is an unknown technology name", error.message());
96
97 EXPECT_FALSE(Technology::GetTechnologyVectorFromString(
98 "ethernet,bar", &technologies, &error));
99 EXPECT_EQ(Error::kInvalidArguments, error.type());
100 EXPECT_EQ("bar is an unknown technology name", error.message());
101
102 EXPECT_FALSE(Technology::GetTechnologyVectorFromString(
103 "ethernet,foo,vpn", &technologies, &error));
104 EXPECT_EQ(Error::kInvalidArguments, error.type());
105 EXPECT_EQ("foo is an unknown technology name", error.message());
106}
107
108TEST_F(TechnologyTest,
109 GetTechnologyVectorFromStringWithDuplicateTechnologyNames) {
110 vector<Technology::Identifier> technologies;
111 Error error;
112
113 EXPECT_FALSE(Technology::GetTechnologyVectorFromString(
114 "ethernet,vpn,ethernet", &technologies, &error));
115 EXPECT_EQ(Error::kInvalidArguments, error.type());
116 EXPECT_EQ("ethernet is duplicated in the list", error.message());
117}
118
119} // namespace shill