Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 1 | // 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 | |
| 12 | using testing::Test; |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | class ErrorTest : public Test {}; |
| 17 | |
| 18 | TEST_F(ErrorTest, ConstructorDefault) { |
| 19 | Error e; |
| 20 | EXPECT_EQ(Error::kSuccess, e.type()); |
| 21 | EXPECT_EQ(Error::GetDefaultMessage(Error::kSuccess), e.message()); |
| 22 | } |
| 23 | |
| 24 | TEST_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 | |
| 30 | TEST_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 Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 37 | TEST_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 Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 44 | TEST_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 | |
| 51 | TEST_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 Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 59 | TEST_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 Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 67 | TEST_F(ErrorTest, ToDBusError) { |
| 68 | DBus::Error dbus_error; |
| 69 | ASSERT_FALSE(dbus_error.is_set()); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 70 | Error().ToDBusError(&dbus_error); |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 71 | ASSERT_FALSE(dbus_error.is_set()); |
| 72 | static const char kMessage[] = "Test error message"; |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 73 | Error(Error::kPermissionDenied, kMessage).ToDBusError(&dbus_error); |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 74 | 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 | |
| 79 | TEST_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 | |
| 86 | TEST_F(ErrorTest, GetName) { |
| 87 | EXPECT_EQ(SHILL_INTERFACE ".Error.NotFound", |
| 88 | Error::GetName(Error::kNotFound)); |
| 89 | } |
| 90 | |
| 91 | TEST_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 |