blob: 7736e38c91751fbb4e84ff3f72c2eb8f80f04f53 [file] [log] [blame]
Darin Petkove4c0ace2011-08-24 10:32:46 -07001// Copyright (c) 2011 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/error.h"
6
7#include <dbus-c++/error.h>
8#include <gtest/gtest.h>
9
10#include "shill/dbus_adaptor.h"
11
12using testing::Test;
13
14namespace shill {
15
16class ErrorTest : public Test {};
17
18TEST_F(ErrorTest, ConstructorDefault) {
19 Error e;
20 EXPECT_EQ(Error::kSuccess, e.type());
21 EXPECT_EQ(Error::GetDefaultMessage(Error::kSuccess), e.message());
22}
23
24TEST_F(ErrorTest, ConstructorDefaultMessage) {
25 Error e(Error::kAlreadyExists);
26 EXPECT_EQ(Error::kAlreadyExists, e.type());
27 EXPECT_EQ(Error::GetDefaultMessage(Error::kAlreadyExists), e.message());
28}
29
30TEST_F(ErrorTest, ConstructorCustomMessage) {
31 static const char kMessage[] = "Custom error message";
32 Error e(Error::kInProgress, kMessage);
33 EXPECT_EQ(Error::kInProgress, e.type());
34 EXPECT_EQ(kMessage, e.message());
35}
36
Gaurav Shah1b7a6162011-11-09 11:41:01 -080037TEST_F(ErrorTest, Reset) {
38 Error e(Error::kAlreadyExists);
39 e.Reset();
40 EXPECT_EQ(Error::kSuccess, e.type());
41 EXPECT_EQ(Error::GetDefaultMessage(Error::kSuccess), e.message());
42}
43
Darin Petkove4c0ace2011-08-24 10:32:46 -070044TEST_F(ErrorTest, PopulateDefaultMessage) {
45 Error e;
46 e.Populate(Error::kInternalError);
47 EXPECT_EQ(Error::kInternalError, e.type());
48 EXPECT_EQ(Error::GetDefaultMessage(Error::kInternalError), e.message());
49}
50
51TEST_F(ErrorTest, PopulateCustomMessage) {
52 static const char kMessage[] = "Another custom error message";
53 Error e;
54 e.Populate(Error::kInvalidArguments, kMessage);
55 EXPECT_EQ(Error::kInvalidArguments, e.type());
56 EXPECT_EQ(kMessage, e.message());
57}
58
Darin Petkove5bc2cb2011-12-07 14:47:32 +010059TEST_F(ErrorTest, CopyFrom) {
60 Error e(Error::kInvalidArguments, "Some message");
61 Error copy;
62 copy.CopyFrom(e);
63 EXPECT_EQ(e.type(), copy.type());
64 EXPECT_EQ(e.message(), copy.message());
65}
66
Darin Petkove4c0ace2011-08-24 10:32:46 -070067TEST_F(ErrorTest, ToDBusError) {
68 DBus::Error dbus_error;
69 ASSERT_FALSE(dbus_error.is_set());
Darin Petkovb100ae72011-08-24 16:19:45 -070070 Error().ToDBusError(&dbus_error);
Darin Petkove4c0ace2011-08-24 10:32:46 -070071 ASSERT_FALSE(dbus_error.is_set());
72 static const char kMessage[] = "Test error message";
Darin Petkovb100ae72011-08-24 16:19:45 -070073 Error(Error::kPermissionDenied, kMessage).ToDBusError(&dbus_error);
Darin Petkove4c0ace2011-08-24 10:32:46 -070074 ASSERT_TRUE(dbus_error.is_set());
75 EXPECT_EQ(Error::GetName(Error::kPermissionDenied), dbus_error.name());
76 EXPECT_STREQ(kMessage, dbus_error.message());
77}
78
79TEST_F(ErrorTest, IsSuccessFailure) {
80 EXPECT_TRUE(Error().IsSuccess());
81 EXPECT_FALSE(Error().IsFailure());
82 EXPECT_FALSE(Error(Error::kInvalidNetworkName).IsSuccess());
83 EXPECT_TRUE(Error(Error::kInvalidPassphrase).IsFailure());
84}
85
86TEST_F(ErrorTest, GetName) {
87 EXPECT_EQ(SHILL_INTERFACE ".Error.NotFound",
88 Error::GetName(Error::kNotFound));
89}
90
91TEST_F(ErrorTest, GetDefaultMessage) {
92 // Check the last error code to try to prevent off-by-one bugs when adding or
93 // removing error types.
94 ASSERT_EQ(Error::kPermissionDenied, Error::kNumErrors - 1);
95 EXPECT_EQ("Permission denied",
96 Error::GetDefaultMessage(Error::kPermissionDenied));
97}
98
99} // namespace shill