1 //
2 // Copyright (C) 2011 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/error.h"
18
19 #if defined(__ANDROID__)
20 #include <dbus/service_constants.h>
21 #else
22 #include <chromeos/dbus/service_constants.h>
23 #endif // __ANDROID__
24 #include <brillo/errors/error.h>
25 #include <brillo/errors/error_codes.h>
26 #include <gtest/gtest.h>
27
28 using testing::Test;
29
30 namespace shill {
31
32 class ErrorTest : public Test {};
33
TEST_F(ErrorTest,ConstructorDefault)34 TEST_F(ErrorTest, ConstructorDefault) {
35 Error e;
36 EXPECT_EQ(Error::kSuccess, e.type());
37 EXPECT_EQ(Error::GetDefaultMessage(Error::kSuccess), e.message());
38 }
39
TEST_F(ErrorTest,ConstructorDefaultMessage)40 TEST_F(ErrorTest, ConstructorDefaultMessage) {
41 Error e(Error::kAlreadyExists);
42 EXPECT_EQ(Error::kAlreadyExists, e.type());
43 EXPECT_EQ(Error::GetDefaultMessage(Error::kAlreadyExists), e.message());
44 }
45
TEST_F(ErrorTest,ConstructorCustomMessage)46 TEST_F(ErrorTest, ConstructorCustomMessage) {
47 static const char kMessage[] = "Custom error message";
48 Error e(Error::kInProgress, kMessage);
49 EXPECT_EQ(Error::kInProgress, e.type());
50 EXPECT_EQ(kMessage, e.message());
51 }
52
TEST_F(ErrorTest,Reset)53 TEST_F(ErrorTest, Reset) {
54 Error e(Error::kAlreadyExists);
55 e.Reset();
56 EXPECT_EQ(Error::kSuccess, e.type());
57 EXPECT_EQ(Error::GetDefaultMessage(Error::kSuccess), e.message());
58 }
59
TEST_F(ErrorTest,PopulateDefaultMessage)60 TEST_F(ErrorTest, PopulateDefaultMessage) {
61 Error e;
62 e.Populate(Error::kInternalError);
63 EXPECT_EQ(Error::kInternalError, e.type());
64 EXPECT_EQ(Error::GetDefaultMessage(Error::kInternalError), e.message());
65 }
66
TEST_F(ErrorTest,PopulateCustomMessage)67 TEST_F(ErrorTest, PopulateCustomMessage) {
68 static const char kMessage[] = "Another custom error message";
69 Error e;
70 e.Populate(Error::kInvalidArguments, kMessage);
71 EXPECT_EQ(Error::kInvalidArguments, e.type());
72 EXPECT_EQ(kMessage, e.message());
73 }
74
TEST_F(ErrorTest,CopyFrom)75 TEST_F(ErrorTest, CopyFrom) {
76 Error e(Error::kInvalidArguments, "Some message");
77 Error copy;
78 copy.CopyFrom(e);
79 EXPECT_EQ(e.type(), copy.type());
80 EXPECT_EQ(e.message(), copy.message());
81 }
82
TEST_F(ErrorTest,ToChromeosError)83 TEST_F(ErrorTest, ToChromeosError) {
84 brillo::ErrorPtr chromeos_error;
85 EXPECT_EQ(nullptr, chromeos_error.get());
86 Error().ToChromeosError(&chromeos_error);
87 EXPECT_EQ(nullptr, chromeos_error.get());
88 static const std::string kMessage = "Test error message";
89 Error(Error::kPermissionDenied, kMessage).ToChromeosError(&chromeos_error);
90 EXPECT_NE(nullptr, chromeos_error.get());
91 EXPECT_EQ(brillo::errors::dbus::kDomain, chromeos_error->GetDomain());
92 EXPECT_EQ(kErrorResultPermissionDenied, chromeos_error->GetCode());
93 EXPECT_EQ(kMessage, chromeos_error->GetMessage());
94 }
95
TEST_F(ErrorTest,IsSuccessFailure)96 TEST_F(ErrorTest, IsSuccessFailure) {
97 EXPECT_TRUE(Error().IsSuccess());
98 EXPECT_FALSE(Error().IsFailure());
99 EXPECT_FALSE(Error(Error::kInvalidNetworkName).IsSuccess());
100 EXPECT_TRUE(Error(Error::kInvalidPassphrase).IsFailure());
101 }
102
TEST_F(ErrorTest,GetDBusResult)103 TEST_F(ErrorTest, GetDBusResult) {
104 // Make sure the Error::Type enum matches up to the Error::Info array.
105 EXPECT_EQ(kErrorResultSuccess, Error::GetDBusResult(Error::kSuccess));
106 EXPECT_EQ(kErrorResultFailure, Error::GetDBusResult(Error::kOperationFailed));
107 EXPECT_EQ(kErrorResultAlreadyConnected,
108 Error::GetDBusResult(Error::kAlreadyConnected));
109 EXPECT_EQ(kErrorResultAlreadyExists,
110 Error::GetDBusResult(Error::kAlreadyExists));
111 EXPECT_EQ(kErrorResultIncorrectPin,
112 Error::GetDBusResult(Error::kIncorrectPin));
113 EXPECT_EQ(kErrorResultInProgress, Error::GetDBusResult(Error::kInProgress));
114 EXPECT_EQ(kErrorResultInternalError,
115 Error::GetDBusResult(Error::kInternalError));
116 EXPECT_EQ(kErrorResultInvalidApn, Error::GetDBusResult(Error::kInvalidApn));
117 EXPECT_EQ(kErrorResultInvalidArguments,
118 Error::GetDBusResult(Error::kInvalidArguments));
119 EXPECT_EQ(kErrorResultInvalidNetworkName,
120 Error::GetDBusResult(Error::kInvalidNetworkName));
121 EXPECT_EQ(kErrorResultInvalidPassphrase,
122 Error::GetDBusResult(Error::kInvalidPassphrase));
123 EXPECT_EQ(kErrorResultInvalidProperty,
124 Error::GetDBusResult(Error::kInvalidProperty));
125 EXPECT_EQ(kErrorResultNoCarrier, Error::GetDBusResult(Error::kNoCarrier));
126 EXPECT_EQ(kErrorResultNotConnected,
127 Error::GetDBusResult(Error::kNotConnected));
128 EXPECT_EQ(kErrorResultNotFound, Error::GetDBusResult(Error::kNotFound));
129 EXPECT_EQ(kErrorResultNotImplemented,
130 Error::GetDBusResult(Error::kNotImplemented));
131 EXPECT_EQ(kErrorResultNotOnHomeNetwork,
132 Error::GetDBusResult(Error::kNotOnHomeNetwork));
133 EXPECT_EQ(kErrorResultNotRegistered,
134 Error::GetDBusResult(Error::kNotRegistered));
135 EXPECT_EQ(kErrorResultNotSupported,
136 Error::GetDBusResult(Error::kNotSupported));
137 EXPECT_EQ(kErrorResultOperationAborted,
138 Error::GetDBusResult(Error::kOperationAborted));
139 EXPECT_EQ(kErrorResultOperationInitiated,
140 Error::GetDBusResult(Error::kOperationInitiated));
141 EXPECT_EQ(kErrorResultOperationTimeout,
142 Error::GetDBusResult(Error::kOperationTimeout));
143 EXPECT_EQ(kErrorResultPassphraseRequired,
144 Error::GetDBusResult(Error::kPassphraseRequired));
145 EXPECT_EQ(kErrorResultPermissionDenied,
146 Error::GetDBusResult(Error::kPermissionDenied));
147 EXPECT_EQ(kErrorResultPinBlocked, Error::GetDBusResult(Error::kPinBlocked));
148 EXPECT_EQ(kErrorResultPinRequired, Error::GetDBusResult(Error::kPinRequired));
149 EXPECT_EQ(kErrorResultWrongState, Error::GetDBusResult(Error::kWrongState));
150 }
151
TEST_F(ErrorTest,GetDefaultMessage)152 TEST_F(ErrorTest, GetDefaultMessage) {
153 // Check the last error code to try to prevent off-by-one bugs when adding or
154 // removing error types.
155 ASSERT_EQ(Error::kWrongState, Error::kNumErrors - 1);
156 EXPECT_EQ("Permission denied",
157 Error::GetDefaultMessage(Error::kPermissionDenied));
158 }
159
160 } // namespace shill
161