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 #include "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"
21 
22 #define ALTS_TSI_UTILS_TEST_OUT_FRAME "Hello Google"
23 
convert_to_tsi_result_test()24 static void convert_to_tsi_result_test() {
25   GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OK) == TSI_OK);
26   GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_UNKNOWN) ==
27              TSI_UNKNOWN_ERROR);
28   GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(
29                  GRPC_STATUS_INVALID_ARGUMENT) == TSI_INVALID_ARGUMENT);
30   GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OUT_OF_RANGE) ==
31              TSI_UNKNOWN_ERROR);
32   GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_INTERNAL) ==
33              TSI_INTERNAL_ERROR);
34   GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_NOT_FOUND) ==
35              TSI_NOT_FOUND);
36 }
37 
deserialize_response_test()38 static void deserialize_response_test() {
39   grpc_gcp_handshaker_resp* resp = grpc_gcp_handshaker_resp_create();
40   GPR_ASSERT(grpc_gcp_handshaker_resp_set_out_frames(
41       resp, ALTS_TSI_UTILS_TEST_OUT_FRAME,
42       strlen(ALTS_TSI_UTILS_TEST_OUT_FRAME)));
43   grpc_slice slice;
44   GPR_ASSERT(grpc_gcp_handshaker_resp_encode(resp, &slice));
45 
46   /* Valid serialization. */
47   grpc_byte_buffer* buffer =
48       grpc_raw_byte_buffer_create(&slice, 1 /* number of slices */);
49   grpc_gcp_handshaker_resp* decoded_resp =
50       alts_tsi_utils_deserialize_response(buffer);
51   GPR_ASSERT(grpc_gcp_handshaker_resp_equals(resp, decoded_resp));
52   grpc_byte_buffer_destroy(buffer);
53 
54   /* Invalid serializaiton. */
55   grpc_slice bad_slice =
56       grpc_slice_split_head(&slice, GRPC_SLICE_LENGTH(slice) - 1);
57   buffer = grpc_raw_byte_buffer_create(&bad_slice, 1 /* number of slices */);
58   GPR_ASSERT(alts_tsi_utils_deserialize_response(buffer) == nullptr);
59 
60   /* Clean up. */
61   grpc_slice_unref(slice);
62   grpc_slice_unref(bad_slice);
63   grpc_byte_buffer_destroy(buffer);
64   grpc_gcp_handshaker_resp_destroy(resp);
65   grpc_gcp_handshaker_resp_destroy(decoded_resp);
66 }
67 
main(int argc,char ** argv)68 int main(int argc, char** argv) {
69   /* Tests. */
70   deserialize_response_test();
71   convert_to_tsi_result_test();
72   return 0;
73 }
74