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