1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/lib/iomgr/error.h"
20
21 #include <gmock/gmock.h>
22 #include <grpc/grpc.h>
23 #include <grpc/support/alloc.h>
24 #include <string.h>
25
26 #include "absl/log/log.h"
27 #include "absl/strings/str_cat.h"
28 #include "src/core/util/crash.h"
29 #include "src/core/util/strerror.h"
30 #include "test/core/test_util/test_config.h"
31
TEST(ErrorTest,SetGetInt)32 TEST(ErrorTest, SetGetInt) {
33 grpc_error_handle error = GRPC_ERROR_CREATE("Test");
34 EXPECT_NE(error, absl::OkStatus());
35 intptr_t i = 0;
36 #ifndef NDEBUG
37 // grpc_core::StatusIntProperty::kFileLine is for debug only
38 EXPECT_TRUE(
39 grpc_error_get_int(error, grpc_core::StatusIntProperty::kFileLine, &i));
40 EXPECT_TRUE(i); // line set will never be 0
41 #endif
42 EXPECT_TRUE(
43 !grpc_error_get_int(error, grpc_core::StatusIntProperty::kStreamId, &i));
44 EXPECT_TRUE(!grpc_error_get_int(
45 error, grpc_core::StatusIntProperty::kHttp2Error, &i));
46
47 intptr_t http = 2;
48 error = grpc_error_set_int(error, grpc_core::StatusIntProperty::kHttp2Error,
49 http);
50 EXPECT_TRUE(
51 grpc_error_get_int(error, grpc_core::StatusIntProperty::kHttp2Error, &i));
52 EXPECT_EQ(i, http);
53 }
54
TEST(ErrorTest,SetGetStr)55 TEST(ErrorTest, SetGetStr) {
56 grpc_error_handle error = GRPC_ERROR_CREATE("Test");
57
58 std::string str;
59 #ifndef NDEBUG
60 // grpc_core::StatusStrProperty::kFile is for debug only
61 EXPECT_TRUE(
62 grpc_error_get_str(error, grpc_core::StatusStrProperty::kFile, &str));
63 EXPECT_THAT(str, testing::HasSubstr("error_test.c"));
64 // __FILE__ expands differently on
65 // Windows. All should at least
66 // contain error_test.c
67 #endif
68 EXPECT_TRUE(grpc_error_get_str(
69 error, grpc_core::StatusStrProperty::kDescription, &str));
70 EXPECT_EQ(str, "Test");
71
72 error = grpc_error_set_str(error, grpc_core::StatusStrProperty::kGrpcMessage,
73 "longer message");
74 EXPECT_TRUE(grpc_error_get_str(
75 error, grpc_core::StatusStrProperty::kGrpcMessage, &str));
76 EXPECT_EQ(str, "longer message");
77 }
78
TEST(ErrorTest,CopyAndUnRef)79 TEST(ErrorTest, CopyAndUnRef) {
80 // error1 has one ref
81 grpc_error_handle error1 = grpc_error_set_int(
82 GRPC_ERROR_CREATE("Test"), grpc_core::StatusIntProperty::kStreamId, 1);
83 intptr_t i;
84 EXPECT_TRUE(
85 grpc_error_get_int(error1, grpc_core::StatusIntProperty::kStreamId, &i));
86 EXPECT_EQ(i, 1);
87
88 // this gives error3 a ref to the new error, and decrements error1 to one ref
89 grpc_error_handle error3 =
90 grpc_error_set_int(error1, grpc_core::StatusIntProperty::kHttp2Error, 2);
91 EXPECT_NE(error3, error1); // should not be the same because of extra ref
92 EXPECT_TRUE(grpc_error_get_int(
93 error3, grpc_core::StatusIntProperty::kHttp2Error, &i));
94 EXPECT_EQ(i, 2);
95
96 // error 1 should not have kHttp2Error
97 EXPECT_FALSE(grpc_error_get_int(
98 error1, grpc_core::StatusIntProperty::kHttp2Error, &i));
99 }
100
TEST(ErrorTest,CreateReferencing)101 TEST(ErrorTest, CreateReferencing) {
102 grpc_error_handle child =
103 grpc_error_set_str(GRPC_ERROR_CREATE("Child"),
104 grpc_core::StatusStrProperty::kGrpcMessage, "message");
105 grpc_error_handle parent = GRPC_ERROR_CREATE_REFERENCING("Parent", &child, 1);
106 EXPECT_NE(parent, absl::OkStatus());
107 }
108
TEST(ErrorTest,CreateReferencingMany)109 TEST(ErrorTest, CreateReferencingMany) {
110 grpc_error_handle children[3];
111 children[0] =
112 grpc_error_set_str(GRPC_ERROR_CREATE("Child1"),
113 grpc_core::StatusStrProperty::kGrpcMessage, "message");
114 children[1] =
115 grpc_error_set_int(GRPC_ERROR_CREATE("Child2"),
116 grpc_core::StatusIntProperty::kHttp2Error, 5);
117 children[2] = grpc_error_set_str(GRPC_ERROR_CREATE("Child3"),
118 grpc_core::StatusStrProperty::kGrpcMessage,
119 "message 3");
120
121 grpc_error_handle parent =
122 GRPC_ERROR_CREATE_REFERENCING("Parent", children, 3);
123 EXPECT_NE(parent, absl::OkStatus());
124
125 for (size_t i = 0; i < 3; ++i) {
126 }
127 }
128
TEST(ErrorTest,PrintErrorString)129 TEST(ErrorTest, PrintErrorString) {
130 grpc_error_handle error = grpc_error_set_int(
131 GRPC_ERROR_CREATE("Error"), grpc_core::StatusIntProperty::kRpcStatus,
132 GRPC_STATUS_UNIMPLEMENTED);
133 error =
134 grpc_error_set_int(error, grpc_core::StatusIntProperty::kHttp2Error, 666);
135 error = grpc_error_set_str(error, grpc_core::StatusStrProperty::kGrpcMessage,
136 "message");
137 // VLOG(2) << grpc_core::StatusToString(error);
138 }
139
TEST(ErrorTest,PrintErrorStringReference)140 TEST(ErrorTest, PrintErrorStringReference) {
141 grpc_error_handle children[2];
142 children[0] = grpc_error_set_str(
143 grpc_error_set_int(GRPC_ERROR_CREATE("1"),
144 grpc_core::StatusIntProperty::kRpcStatus,
145 GRPC_STATUS_UNIMPLEMENTED),
146 grpc_core::StatusStrProperty::kGrpcMessage, "message for child 1");
147 children[1] = grpc_error_set_str(
148 grpc_error_set_int(GRPC_ERROR_CREATE("2sd"),
149 grpc_core::StatusIntProperty::kRpcStatus,
150 GRPC_STATUS_INTERNAL),
151 grpc_core::StatusStrProperty::kGrpcMessage, "message for child 2");
152
153 grpc_error_handle parent =
154 GRPC_ERROR_CREATE_REFERENCING("Parent", children, 2);
155
156 for (size_t i = 0; i < 2; ++i) {
157 }
158 }
159
TEST(ErrorTest,TestOsError)160 TEST(ErrorTest, TestOsError) {
161 int fake_errno = 5;
162 const char* syscall = "syscall name";
163 grpc_error_handle error = GRPC_OS_ERROR(fake_errno, syscall);
164 EXPECT_EQ(error.message(),
165 absl::StrCat("syscall name: ", grpc_core::StrError(5), " (5)"));
166 }
167
main(int argc,char ** argv)168 int main(int argc, char** argv) {
169 grpc::testing::TestEnvironment env(&argc, argv);
170 ::testing::InitGoogleTest(&argc, argv);
171 grpc_init();
172 int retval = RUN_ALL_TESTS();
173 grpc_shutdown();
174 return retval;
175 }
176