• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpcpp/support/error_details.h>
20 #include <gtest/gtest.h>
21 
22 #include "src/proto/grpc/status/status.pb.h"
23 #include "src/proto/grpc/testing/echo_messages.pb.h"
24 
25 namespace grpc {
26 namespace {
27 
TEST(ExtractTest,Success)28 TEST(ExtractTest, Success) {
29   google::rpc::Status expected;
30   expected.set_code(13);  // INTERNAL
31   expected.set_message("I am an error message");
32   testing::EchoRequest expected_details;
33   expected_details.set_message(grpc::string(100, '\0'));
34   expected.add_details()->PackFrom(expected_details);
35 
36   google::rpc::Status to;
37   grpc::string error_details = expected.SerializeAsString();
38   Status from(static_cast<StatusCode>(expected.code()), expected.message(),
39               error_details);
40   EXPECT_TRUE(ExtractErrorDetails(from, &to).ok());
41   EXPECT_EQ(expected.code(), to.code());
42   EXPECT_EQ(expected.message(), to.message());
43   EXPECT_EQ(1, to.details_size());
44   testing::EchoRequest details;
45   to.details(0).UnpackTo(&details);
46   EXPECT_EQ(expected_details.message(), details.message());
47 }
48 
TEST(ExtractTest,NullInput)49 TEST(ExtractTest, NullInput) {
50   EXPECT_EQ(StatusCode::FAILED_PRECONDITION,
51             ExtractErrorDetails(Status(), nullptr).error_code());
52 }
53 
TEST(ExtractTest,Unparsable)54 TEST(ExtractTest, Unparsable) {
55   grpc::string error_details("I am not a status object");
56   Status from(StatusCode::INTERNAL, "", error_details);
57   google::rpc::Status to;
58   EXPECT_EQ(StatusCode::INVALID_ARGUMENT,
59             ExtractErrorDetails(from, &to).error_code());
60 }
61 
TEST(SetTest,Success)62 TEST(SetTest, Success) {
63   google::rpc::Status expected;
64   expected.set_code(13);  // INTERNAL
65   expected.set_message("I am an error message");
66   testing::EchoRequest expected_details;
67   expected_details.set_message(grpc::string(100, '\0'));
68   expected.add_details()->PackFrom(expected_details);
69 
70   Status to;
71   Status s = SetErrorDetails(expected, &to);
72   EXPECT_TRUE(s.ok());
73   EXPECT_EQ(expected.code(), to.error_code());
74   EXPECT_EQ(expected.message(), to.error_message());
75   EXPECT_EQ(expected.SerializeAsString(), to.error_details());
76 }
77 
TEST(SetTest,NullInput)78 TEST(SetTest, NullInput) {
79   EXPECT_EQ(StatusCode::FAILED_PRECONDITION,
80             SetErrorDetails(google::rpc::Status(), nullptr).error_code());
81 }
82 
TEST(SetTest,OutOfScopeErrorCode)83 TEST(SetTest, OutOfScopeErrorCode) {
84   google::rpc::Status expected;
85   expected.set_code(17);  // Out of scope (UNAUTHENTICATED is 16).
86   expected.set_message("I am an error message");
87   testing::EchoRequest expected_details;
88   expected_details.set_message(grpc::string(100, '\0'));
89   expected.add_details()->PackFrom(expected_details);
90 
91   Status to;
92   Status s = SetErrorDetails(expected, &to);
93   EXPECT_TRUE(s.ok());
94   EXPECT_EQ(StatusCode::UNKNOWN, to.error_code());
95   EXPECT_EQ(expected.message(), to.error_message());
96   EXPECT_EQ(expected.SerializeAsString(), to.error_details());
97 }
98 
TEST(SetTest,ValidScopeErrorCode)99 TEST(SetTest, ValidScopeErrorCode) {
100   for (int c = StatusCode::OK; c <= StatusCode::UNAUTHENTICATED; c++) {
101     google::rpc::Status expected;
102     expected.set_code(c);
103     expected.set_message("I am an error message");
104     testing::EchoRequest expected_details;
105     expected_details.set_message(grpc::string(100, '\0'));
106     expected.add_details()->PackFrom(expected_details);
107 
108     Status to;
109     Status s = SetErrorDetails(expected, &to);
110     EXPECT_TRUE(s.ok());
111     EXPECT_EQ(c, to.error_code());
112     EXPECT_EQ(expected.message(), to.error_message());
113     EXPECT_EQ(expected.SerializeAsString(), to.error_details());
114   }
115 }
116 
117 }  // namespace
118 }  // namespace grpc
119 
main(int argc,char ** argv)120 int main(int argc, char** argv) {
121   ::testing::InitGoogleTest(&argc, argv);
122   return RUN_ALL_TESTS();
123 }
124