• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 <brillo/errors/error.h>
6 
7 #include <gtest/gtest.h>
8 
9 using brillo::Error;
10 
11 namespace {
12 
GenerateNetworkError()13 brillo::ErrorPtr GenerateNetworkError() {
14   base::Location loc("GenerateNetworkError",
15                                 "error_unittest.cc",
16                                 15,
17                                 ::base::GetProgramCounter());
18   return Error::Create(loc, "network", "not_found", "Resource not found");
19 }
20 
GenerateHttpError()21 brillo::ErrorPtr GenerateHttpError() {
22   brillo::ErrorPtr inner = GenerateNetworkError();
23   return Error::Create(FROM_HERE, "HTTP", "404", "Not found", std::move(inner));
24 }
25 
26 }  // namespace
27 
TEST(Error,Single)28 TEST(Error, Single) {
29   brillo::ErrorPtr err = GenerateNetworkError();
30   EXPECT_EQ("network", err->GetDomain());
31   EXPECT_EQ("not_found", err->GetCode());
32   EXPECT_EQ("Resource not found", err->GetMessage());
33   EXPECT_EQ("GenerateNetworkError", err->GetLocation().function_name());
34   EXPECT_EQ("error_unittest.cc", err->GetLocation().file_name());
35   EXPECT_EQ(15, err->GetLocation().line_number());
36   EXPECT_EQ(nullptr, err->GetInnerError());
37   EXPECT_TRUE(err->HasDomain("network"));
38   EXPECT_FALSE(err->HasDomain("HTTP"));
39   EXPECT_FALSE(err->HasDomain("foo"));
40   EXPECT_TRUE(err->HasError("network", "not_found"));
41   EXPECT_FALSE(err->HasError("network", "404"));
42   EXPECT_FALSE(err->HasError("HTTP", "404"));
43   EXPECT_FALSE(err->HasError("HTTP", "not_found"));
44   EXPECT_FALSE(err->HasError("foo", "bar"));
45 }
46 
TEST(Error,Nested)47 TEST(Error, Nested) {
48   brillo::ErrorPtr err = GenerateHttpError();
49   EXPECT_EQ("HTTP", err->GetDomain());
50   EXPECT_EQ("404", err->GetCode());
51   EXPECT_EQ("Not found", err->GetMessage());
52   EXPECT_NE(nullptr, err->GetInnerError());
53   EXPECT_EQ("network", err->GetInnerError()->GetDomain());
54   EXPECT_TRUE(err->HasDomain("network"));
55   EXPECT_TRUE(err->HasDomain("HTTP"));
56   EXPECT_FALSE(err->HasDomain("foo"));
57   EXPECT_TRUE(err->HasError("network", "not_found"));
58   EXPECT_FALSE(err->HasError("network", "404"));
59   EXPECT_TRUE(err->HasError("HTTP", "404"));
60   EXPECT_FALSE(err->HasError("HTTP", "not_found"));
61   EXPECT_FALSE(err->HasError("foo", "bar"));
62 }
63 
TEST(Error,Clone)64 TEST(Error, Clone) {
65   brillo::ErrorPtr err = GenerateHttpError();
66   brillo::ErrorPtr clone = err->Clone();
67   const brillo::Error* error1 = err.get();
68   const brillo::Error* error2 = clone.get();
69   while (error1 && error2) {
70     EXPECT_NE(error1, error2);
71     EXPECT_EQ(error1->GetDomain(), error2->GetDomain());
72     EXPECT_EQ(error1->GetCode(), error2->GetCode());
73     EXPECT_EQ(error1->GetMessage(), error2->GetMessage());
74     EXPECT_EQ(error1->GetLocation().function_name(),
75               error2->GetLocation().function_name());
76     EXPECT_EQ(error1->GetLocation().file_name(), error2->GetLocation().file_name());
77     EXPECT_EQ(error1->GetLocation().line_number(),
78               error2->GetLocation().line_number());
79     error1 = error1->GetInnerError();
80     error2 = error2->GetInnerError();
81   }
82   EXPECT_EQ(error1, error2);
83 }
84