1 // 2 // Copyright (C) 2012 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/technology.h" 18 19 #include <gmock/gmock.h> 20 #include <gtest/gtest.h> 21 22 #include "shill/error.h" 23 24 using std::string; 25 using std::vector; 26 using testing::ElementsAre; 27 using testing::Test; 28 29 namespace shill { 30 31 class TechnologyTest : public Test {}; 32 33 TEST_F(TechnologyTest, IdentifierFromName) { 34 EXPECT_EQ(Technology::kEthernet, Technology::IdentifierFromName("ethernet")); 35 EXPECT_EQ(Technology::kEthernetEap, 36 Technology::IdentifierFromName("etherneteap")); 37 EXPECT_EQ(Technology::kWifi, Technology::IdentifierFromName("wifi")); 38 EXPECT_EQ(Technology::kWiMax, Technology::IdentifierFromName("wimax")); 39 EXPECT_EQ(Technology::kCellular, Technology::IdentifierFromName("cellular")); 40 EXPECT_EQ(Technology::kTunnel, Technology::IdentifierFromName("tunnel")); 41 EXPECT_EQ(Technology::kLoopback, Technology::IdentifierFromName("loopback")); 42 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromName("vpn")); 43 EXPECT_EQ(Technology::kPPP, Technology::IdentifierFromName("ppp")); 44 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("bluetooth")); 45 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("foo")); 46 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("")); 47 } 48 49 TEST_F(TechnologyTest, NameFromIdentifier) { 50 EXPECT_EQ("ethernet", Technology::NameFromIdentifier(Technology::kEthernet)); 51 EXPECT_EQ("etherneteap", 52 Technology::NameFromIdentifier(Technology::kEthernetEap)); 53 EXPECT_EQ("wifi", Technology::NameFromIdentifier(Technology::kWifi)); 54 EXPECT_EQ("wimax", Technology::NameFromIdentifier(Technology::kWiMax)); 55 EXPECT_EQ("cellular", Technology::NameFromIdentifier(Technology::kCellular)); 56 EXPECT_EQ("tunnel", Technology::NameFromIdentifier(Technology::kTunnel)); 57 EXPECT_EQ("loopback", Technology::NameFromIdentifier(Technology::kLoopback)); 58 EXPECT_EQ("vpn", Technology::NameFromIdentifier(Technology::kVPN)); 59 EXPECT_EQ("ppp", Technology::NameFromIdentifier(Technology::kPPP)); 60 EXPECT_EQ("pppoe", Technology::NameFromIdentifier(Technology::kPPPoE)); 61 EXPECT_EQ("unknown", Technology::NameFromIdentifier(Technology::kUnknown)); 62 } 63 64 TEST_F(TechnologyTest, IdentifierFromStorageGroup) { 65 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromStorageGroup("vpn")); 66 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromStorageGroup("vpn_a")); 67 EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromStorageGroup("vpn__a")); 68 EXPECT_EQ(Technology::kVPN, 69 Technology::IdentifierFromStorageGroup("vpn_a_1")); 70 EXPECT_EQ(Technology::kUnknown, 71 Technology::IdentifierFromStorageGroup("_vpn")); 72 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromStorageGroup("_")); 73 EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromStorageGroup("")); 74 } 75 76 TEST_F(TechnologyTest, GetTechnologyVectorFromStringWithValidTechnologyNames) { 77 vector<Technology::Identifier> technologies; 78 Error error; 79 80 EXPECT_TRUE(Technology::GetTechnologyVectorFromString( 81 "", &technologies, &error)); 82 EXPECT_THAT(technologies, ElementsAre()); 83 EXPECT_TRUE(error.IsSuccess()); 84 85 EXPECT_TRUE(Technology::GetTechnologyVectorFromString( 86 "ethernet", &technologies, &error)); 87 EXPECT_THAT(technologies, ElementsAre(Technology::kEthernet)); 88 EXPECT_TRUE(error.IsSuccess()); 89 90 EXPECT_TRUE(Technology::GetTechnologyVectorFromString( 91 "ethernet,vpn", &technologies, &error)); 92 EXPECT_THAT(technologies, ElementsAre(Technology::kEthernet, 93 Technology::kVPN)); 94 EXPECT_TRUE(error.IsSuccess()); 95 96 EXPECT_TRUE(Technology::GetTechnologyVectorFromString( 97 "wifi,ethernet,vpn", &technologies, &error)); 98 EXPECT_THAT(technologies, ElementsAre(Technology::kWifi, 99 Technology::kEthernet, 100 Technology::kVPN)); 101 EXPECT_TRUE(error.IsSuccess()); 102 } 103 104 TEST_F(TechnologyTest, 105 GetTechnologyVectorFromStringWithInvalidTechnologyNames) { 106 vector<Technology::Identifier> technologies; 107 Error error; 108 109 EXPECT_FALSE(Technology::GetTechnologyVectorFromString( 110 "foo", &technologies, &error)); 111 EXPECT_EQ(Error::kInvalidArguments, error.type()); 112 EXPECT_EQ("foo is an unknown technology name", error.message()); 113 114 EXPECT_FALSE(Technology::GetTechnologyVectorFromString( 115 "ethernet,bar", &technologies, &error)); 116 EXPECT_EQ(Error::kInvalidArguments, error.type()); 117 EXPECT_EQ("bar is an unknown technology name", error.message()); 118 119 EXPECT_FALSE(Technology::GetTechnologyVectorFromString( 120 "ethernet,foo,vpn", &technologies, &error)); 121 EXPECT_EQ(Error::kInvalidArguments, error.type()); 122 EXPECT_EQ("foo is an unknown technology name", error.message()); 123 } 124 125 TEST_F(TechnologyTest, 126 GetTechnologyVectorFromStringWithDuplicateTechnologyNames) { 127 vector<Technology::Identifier> technologies; 128 Error error; 129 130 EXPECT_FALSE(Technology::GetTechnologyVectorFromString( 131 "ethernet,vpn,ethernet", &technologies, &error)); 132 EXPECT_EQ(Error::kInvalidArguments, error.type()); 133 EXPECT_EQ("ethernet is duplicated in the list", error.message()); 134 } 135 136 } // namespace shill 137