| 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 <string.h> |
| 22 |
|
| 23 |
#include <gmock/gmock.h> |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/support/alloc.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
|
| 29 |
#include "test/core/util/test_config.h" |
| 30 |
|
| 31 |
TEST(ErrorTest, SetGetInt) { |
| 32 |
grpc_error_handle error = GRPC_ERROR_CREATE("Test"); |
| 33 |
EXPECT_NE(error, absl::OkStatus()); |
| 34 |
intptr_t i = 0; |
| 35 |
#ifndef NDEBUG |
| 36 |
// grpc_core::StatusIntProperty::kFileLine is for debug only |
| 37 |
EXPECT_TRUE( |
| 38 |
grpc_error_get_int(error, grpc_core::StatusIntProperty::kFileLine, &i)); |
| 39 |
EXPECT_TRUE(i); // line set will never be 0 |
| 40 |
#endif |
| 41 |
EXPECT_TRUE( |
| 42 |
!grpc_error_get_int(error, grpc_core::StatusIntProperty::kErrorNo, &i)); |
| 43 |
EXPECT_TRUE( |
| 44 |
!grpc_error_get_int(error, grpc_core::StatusIntProperty::kSize, &i)); |
| 45 |
|
| 46 |
intptr_t errnumber = 314; |
| 47 |
error = grpc_error_set_int(error, grpc_core::StatusIntProperty::kErrorNo, |
| 48 |
errnumber); |
| 49 |
EXPECT_TRUE( |
| 50 |
grpc_error_get_int(error, grpc_core::StatusIntProperty::kErrorNo, &i)); |
| 51 |
EXPECT_EQ(i, errnumber); |
| 52 |
|
| 53 |
intptr_t http = 2; |
| 54 |
error = grpc_error_set_int(error, grpc_core::StatusIntProperty::kHttp2Error, |
| 55 |
http); |
| 56 |
EXPECT_TRUE( |
| 57 |
grpc_error_get_int(error, grpc_core::StatusIntProperty::kHttp2Error, &i)); |
| 58 |
EXPECT_EQ(i, http); |
| 59 |
} |
| 60 |
|
| 61 |
TEST(ErrorTest, SetGetStr) { |
| 62 |
grpc_error_handle error = GRPC_ERROR_CREATE("Test"); |
| 63 |
|
| 64 |
std::string str; |
| 65 |
EXPECT_TRUE( |
| 66 |
!grpc_error_get_str(error, grpc_core::StatusStrProperty::kSyscall, &str)); |
| 67 |
EXPECT_TRUE(!grpc_error_get_str( |
| 68 |
error, grpc_core::StatusStrProperty::kTsiError, &str)); |
| 69 |
#ifndef NDEBUG |
| 70 |
// grpc_core::StatusStrProperty::kFile is for debug only |
| 71 |
EXPECT_TRUE( |
| 72 |
grpc_error_get_str(error, grpc_core::StatusStrProperty::kFile, &str)); |
| 73 |
EXPECT_THAT(str, testing::HasSubstr("error_test.c")); |
| 74 |
// __FILE__ expands differently on |
| 75 |
// Windows. All should at least |
| 76 |
// contain error_test.c |
| 77 |
#endif |
| 78 |
EXPECT_TRUE(grpc_error_get_str( |
| 79 |
error, grpc_core::StatusStrProperty::kDescription, &str)); |
| 80 |
EXPECT_EQ(str, "Test"); |
| 81 |
|
| 82 |
error = grpc_error_set_str(error, grpc_core::StatusStrProperty::kGrpcMessage, |
| 83 |
"longer message"); |
| 84 |
EXPECT_TRUE(grpc_error_get_str( |
| 85 |
error, grpc_core::StatusStrProperty::kGrpcMessage, &str)); |
| 86 |
EXPECT_EQ(str, "longer message"); |
| 87 |
} |
| 88 |
|
| 89 |
TEST(ErrorTest, CopyAndUnRef) { |
| 90 |
// error1 has one ref |
| 91 |
grpc_error_handle error1 = |
| 92 |
grpc_error_set_str(GRPC_ERROR_CREATE("Test"), |
| 93 |
grpc_core::StatusStrProperty::kGrpcMessage, "message"); |
| 94 |
std::string str; |
| 95 |
EXPECT_TRUE(grpc_error_get_str( |
| 96 |
error1, grpc_core::StatusStrProperty::kGrpcMessage, &str)); |
| 97 |
EXPECT_EQ(str, "message"); |
| 98 |
|
| 99 |
// this gives error3 a ref to the new error, and decrements error1 to one ref |
| 100 |
grpc_error_handle error3 = grpc_error_set_str( |
| 101 |
error1, grpc_core::StatusStrProperty::kSyscall, "syscall"); |
| 102 |
EXPECT_NE(error3, error1); // should not be the same because of extra ref |
| 103 |
EXPECT_TRUE(grpc_error_get_str( |
| 104 |
error3, grpc_core::StatusStrProperty::kGrpcMessage, &str)); |
| 105 |
EXPECT_EQ(str, "message"); |
| 106 |
|
| 107 |
// error 1 should not have a syscall but 3 should |
| 108 |
EXPECT_TRUE(!grpc_error_get_str( |
| 109 |
error1, grpc_core::StatusStrProperty::kSyscall, &str)); |
| 110 |
EXPECT_TRUE( |
| 111 |
grpc_error_get_str(error3, grpc_core::StatusStrProperty::kSyscall, &str)); |
| 112 |
EXPECT_EQ(str, "syscall"); |
| 113 |
} |
| 114 |
|
| 115 |
TEST(ErrorTest, CreateReferencing) { |
| 116 |
grpc_error_handle child = |
| 117 |
grpc_error_set_str(GRPC_ERROR_CREATE("Child"), |
| 118 |
grpc_core::StatusStrProperty::kGrpcMessage, "message"); |
| 119 |
grpc_error_handle parent = GRPC_ERROR_CREATE_REFERENCING("Parent", &child, 1); |
| 120 |
EXPECT_NE(parent, absl::OkStatus()); |
| 121 |
} |
| 122 |
|
| 123 |
TEST(ErrorTest, CreateReferencingMany) { |
| 124 |
grpc_error_handle children[3]; |
| 125 |
children[0] = |
| 126 |
grpc_error_set_str(GRPC_ERROR_CREATE("Child1"), |
| 127 |
grpc_core::StatusStrProperty::kGrpcMessage, "message"); |
| 128 |
children[1] = |
| 129 |
grpc_error_set_int(GRPC_ERROR_CREATE("Child2"), |
| 130 |
grpc_core::StatusIntProperty::kHttp2Error, 5); |
| 131 |
children[2] = grpc_error_set_str(GRPC_ERROR_CREATE("Child3"), |
| 132 |
grpc_core::StatusStrProperty::kGrpcMessage, |
| 133 |
"message 3"); |
| 134 |
|
| 135 |
grpc_error_handle parent = |
| 136 |
GRPC_ERROR_CREATE_REFERENCING("Parent", children, 3); |
| 137 |
EXPECT_NE(parent, absl::OkStatus()); |
| 138 |
|
| 139 |
for (size_t i = 0; i < 3; ++i) { |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
TEST(ErrorTest, PrintErrorString) { |
| 144 |
grpc_error_handle error = grpc_error_set_int( |
| 145 |
GRPC_ERROR_CREATE("Error"), grpc_core::StatusIntProperty::kRpcStatus, |
| 146 |
GRPC_STATUS_UNIMPLEMENTED); |
| 147 |
error = grpc_error_set_int(error, grpc_core::StatusIntProperty::kSize, 666); |
| 148 |
error = grpc_error_set_str(error, grpc_core::StatusStrProperty::kGrpcMessage, |
| 149 |
"message"); |
| 150 |
// gpr_log(GPR_DEBUG, "%s", grpc_core::StatusToString(error).c_str()); |
| 151 |
} |
| 152 |
|
| 153 |
TEST(ErrorTest, PrintErrorStringReference) { |
| 154 |
grpc_error_handle children[2]; |
| 155 |
children[0] = grpc_error_set_str( |
| 156 |
grpc_error_set_int(GRPC_ERROR_CREATE("1"), |
| 157 |
grpc_core::StatusIntProperty::kRpcStatus, |
| 158 |
GRPC_STATUS_UNIMPLEMENTED), |
| 159 |
grpc_core::StatusStrProperty::kGrpcMessage, "message for child 1"); |
| 160 |
children[1] = grpc_error_set_str( |
| 161 |
grpc_error_set_int(GRPC_ERROR_CREATE("2sd"), |
| 162 |
grpc_core::StatusIntProperty::kRpcStatus, |
| 163 |
GRPC_STATUS_INTERNAL), |
| 164 |
grpc_core::StatusStrProperty::kGrpcMessage, "message for child 2"); |
| 165 |
|
| 166 |
grpc_error_handle parent = |
| 167 |
GRPC_ERROR_CREATE_REFERENCING("Parent", children, 2); |
| 168 |
|
| 169 |
for (size_t i = 0; i < 2; ++i) { |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
TEST(ErrorTest, TestOsError) { |
| 174 |
int fake_errno = 5; |
| 175 |
const char* syscall = "syscall name"; |
| 176 |
grpc_error_handle error = GRPC_OS_ERROR(fake_errno, syscall); |
| 177 |
|
| 178 |
intptr_t i = 0; |
| 179 |
EXPECT_TRUE( |
| 180 |
grpc_error_get_int(error, grpc_core::StatusIntProperty::kErrorNo, &i)); |
| 181 |
EXPECT_EQ(i, fake_errno); |
| 182 |
|
| 183 |
std::string str; |
| 184 |
EXPECT_TRUE( |
| 185 |
grpc_error_get_str(error, grpc_core::StatusStrProperty::kSyscall, &str)); |
| 186 |
EXPECT_EQ(str, syscall); |
| 187 |
} |
| 188 |
|
| 189 |
int main(int argc, char** argv) { |
| 190 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 191 |
::testing::InitGoogleTest(&argc, argv); |
| 192 |
grpc_init(); |
| 193 |
int retval = RUN_ALL_TESTS(); |
| 194 |
grpc_shutdown(); |
| 195 |
return retval; |
| 196 |
} |