Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 1 | // 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 | |
| 12 | using std::string; |
| 13 | using std::vector; |
| 14 | using testing::ElementsAre; |
| 15 | using testing::Test; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | class TechnologyTest : public Test {}; |
| 20 | |
| 21 | TEST_F(TechnologyTest, IdentifierFromName) { |
| 22 | EXPECT_EQ(Technology::kEthernet, Technology::IdentifierFromName("ethernet")); |
| 23 | EXPECT_EQ(Technology::kWifi, Technology::IdentifierFromName("wifi")); |
Ben Chan | 46af127 | 2012-05-01 10:34:00 -0700 | [diff] [blame] | 24 | EXPECT_EQ(Technology::kWiMax, Technology::IdentifierFromName("wimax")); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 25 | EXPECT_EQ(Technology::kCellular, Technology::IdentifierFromName("cellular")); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 26 | EXPECT_EQ(Technology::kTunnel, Technology::IdentifierFromName("tunnel")); |
| 27 | EXPECT_EQ(Technology::kLoopback, Technology::IdentifierFromName("loopback")); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 28 | EXPECT_EQ(Technology::kVPN, Technology::IdentifierFromName("vpn")); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 29 | EXPECT_EQ(Technology::kPPP, Technology::IdentifierFromName("ppp")); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 30 | EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("bluetooth")); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 31 | EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("foo")); |
| 32 | EXPECT_EQ(Technology::kUnknown, Technology::IdentifierFromName("")); |
| 33 | } |
| 34 | |
| 35 | TEST_F(TechnologyTest, NameFromIdentifier) { |
| 36 | EXPECT_EQ("ethernet", Technology::NameFromIdentifier(Technology::kEthernet)); |
| 37 | EXPECT_EQ("wifi", Technology::NameFromIdentifier(Technology::kWifi)); |
Ben Chan | 46af127 | 2012-05-01 10:34:00 -0700 | [diff] [blame] | 38 | EXPECT_EQ("wimax", Technology::NameFromIdentifier(Technology::kWiMax)); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 39 | EXPECT_EQ("cellular", Technology::NameFromIdentifier(Technology::kCellular)); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 40 | EXPECT_EQ("tunnel", Technology::NameFromIdentifier(Technology::kTunnel)); |
| 41 | EXPECT_EQ("loopback", Technology::NameFromIdentifier(Technology::kLoopback)); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 42 | EXPECT_EQ("vpn", Technology::NameFromIdentifier(Technology::kVPN)); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 43 | EXPECT_EQ("ppp", Technology::NameFromIdentifier(Technology::kPPP)); |
| 44 | EXPECT_EQ("unknown", Technology::NameFromIdentifier(Technology::kUnknown)); |
Ben Chan | c12cf66 | 2012-04-05 14:47:18 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST_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 | |
| 59 | TEST_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 | |
| 87 | TEST_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 | |
| 108 | TEST_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 |