1 //
2 //
3 // Copyright 2018 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/tsi/alts/handshaker/alts_tsi_utils.h"
20
21 #include <gtest/gtest.h>
22
23 #include "upb/mem/arena.hpp"
24
25 #include "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"
26 #include "test/core/util/test_config.h"
27
28 #define ALTS_TSI_UTILS_TEST_OUT_FRAME "Hello Google"
29
TEST(AltsTsiUtilsTest,ConvertToTsiResultTest)30 TEST(AltsTsiUtilsTest, ConvertToTsiResultTest) {
31 ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OK), TSI_OK);
32 ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_UNKNOWN),
33 TSI_UNKNOWN_ERROR);
34 ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_INVALID_ARGUMENT),
35 TSI_INVALID_ARGUMENT);
36 ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OUT_OF_RANGE),
37 TSI_UNKNOWN_ERROR);
38 ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_INTERNAL),
39 TSI_INTERNAL_ERROR);
40 ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_NOT_FOUND),
41 TSI_NOT_FOUND);
42 }
43
TEST(AltsTsiUtilsTest,DeserializeResponseTest)44 TEST(AltsTsiUtilsTest, DeserializeResponseTest) {
45 upb::Arena arena;
46 grpc_gcp_HandshakerResp* resp = grpc_gcp_HandshakerResp_new(arena.ptr());
47 grpc_gcp_HandshakerResp_set_out_frames(
48 resp, upb_StringView_FromString(ALTS_TSI_UTILS_TEST_OUT_FRAME));
49 size_t buf_len;
50 char* buf = grpc_gcp_HandshakerResp_serialize(resp, arena.ptr(), &buf_len);
51 grpc_slice slice = grpc_slice_from_copied_buffer(buf, buf_len);
52
53 // Valid serialization.
54 upb::Arena arena2;
55 grpc_byte_buffer* buffer =
56 grpc_raw_byte_buffer_create(&slice, 1 /* number of slices */);
57 grpc_gcp_HandshakerResp* decoded_resp =
58 alts_tsi_utils_deserialize_response(buffer, arena2.ptr());
59 ASSERT_TRUE(grpc_gcp_handshaker_resp_equals(resp, decoded_resp));
60 grpc_byte_buffer_destroy(buffer);
61
62 // Invalid serialization.
63 grpc_slice bad_slice =
64 grpc_slice_split_head(&slice, GRPC_SLICE_LENGTH(slice) - 1);
65 buffer = grpc_raw_byte_buffer_create(&bad_slice, 1 /* number of slices */);
66 ASSERT_EQ(alts_tsi_utils_deserialize_response(buffer, arena2.ptr()), nullptr);
67
68 // Clean up.
69 grpc_slice_unref(slice);
70 grpc_slice_unref(bad_slice);
71 grpc_byte_buffer_destroy(buffer);
72 }
73
main(int argc,char ** argv)74 int main(int argc, char** argv) {
75 grpc::testing::TestEnvironment env(&argc, argv);
76 ::testing::InitGoogleTest(&argc, argv);
77 grpc::testing::TestGrpcScope grpc_scope;
78 return RUN_ALL_TESTS();
79 }
80