xref: /aosp_15_r20/external/grpc-grpc/test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"
20 
grpc_gcp_handshaker_resp_set_peer_rpc_versions(grpc_gcp_HandshakerResp * resp,upb_Arena * arena,uint32_t max_major,uint32_t max_minor,uint32_t min_major,uint32_t min_minor)21 bool grpc_gcp_handshaker_resp_set_peer_rpc_versions(
22     grpc_gcp_HandshakerResp* resp, upb_Arena* arena, uint32_t max_major,
23     uint32_t max_minor, uint32_t min_major, uint32_t min_minor) {
24   if (resp == nullptr) {
25     gpr_log(GPR_ERROR,
26             "Invalid nullptr argument to "
27             "grpc_gcp_handshaker_resp_set_peer_rpc_versions().");
28     return false;
29   }
30   grpc_gcp_rpc_protocol_versions versions;
31   versions.max_rpc_version.major = max_major;
32   versions.max_rpc_version.minor = max_minor;
33   versions.min_rpc_version.major = min_major;
34   versions.min_rpc_version.minor = min_minor;
35   grpc_gcp_HandshakerResult* result =
36       grpc_gcp_HandshakerResp_mutable_result(resp, arena);
37   grpc_gcp_RpcProtocolVersions* upb_versions =
38       grpc_gcp_HandshakerResult_mutable_peer_rpc_versions(result, arena);
39   grpc_gcp_RpcProtocolVersions_assign_from_struct(upb_versions, arena,
40                                                   &versions);
41   return true;
42 }
43 
grpc_gcp_handshaker_req_decode(grpc_slice slice,upb_Arena * arena)44 grpc_gcp_HandshakerReq* grpc_gcp_handshaker_req_decode(grpc_slice slice,
45                                                        upb_Arena* arena) {
46   size_t buf_size = GRPC_SLICE_LENGTH(slice);
47   void* buf = upb_Arena_Malloc(arena, buf_size);
48   memcpy(buf, reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)),
49          buf_size);
50   grpc_gcp_HandshakerReq* resp = grpc_gcp_HandshakerReq_parse(
51       reinterpret_cast<char*>(buf), buf_size, arena);
52   if (!resp) {
53     gpr_log(GPR_ERROR, "grpc_gcp_HandshakerReq decode error");
54     return nullptr;
55   }
56   return resp;
57 }
58 
59 // Check equality of a pair of grpc_gcp_identity fields.
handshaker_identity_equals(const grpc_gcp_Identity * l_id,const grpc_gcp_Identity * r_id)60 static bool handshaker_identity_equals(const grpc_gcp_Identity* l_id,
61                                        const grpc_gcp_Identity* r_id) {
62   if ((grpc_gcp_Identity_has_service_account(l_id) !=
63        grpc_gcp_Identity_has_service_account(r_id)) ||
64       (grpc_gcp_Identity_has_hostname(l_id) !=
65        grpc_gcp_Identity_has_hostname(r_id))) {
66     return false;
67   }
68 
69   if (grpc_gcp_Identity_has_service_account(l_id)) {
70     if (!upb_StringView_IsEqual(grpc_gcp_Identity_service_account(l_id),
71                                 grpc_gcp_Identity_service_account(r_id))) {
72       return false;
73     }
74   } else if (grpc_gcp_Identity_has_hostname(l_id)) {
75     if (!upb_StringView_IsEqual(grpc_gcp_Identity_hostname(l_id),
76                                 grpc_gcp_Identity_hostname(r_id))) {
77       return false;
78     }
79   }
80   return true;
81 }
82 
handshaker_rpc_versions_equals(const grpc_gcp_RpcProtocolVersions * l_version,const grpc_gcp_RpcProtocolVersions * r_version)83 static bool handshaker_rpc_versions_equals(
84     const grpc_gcp_RpcProtocolVersions* l_version,
85     const grpc_gcp_RpcProtocolVersions* r_version) {
86   const grpc_gcp_RpcProtocolVersions_Version* l_maxver =
87       grpc_gcp_RpcProtocolVersions_max_rpc_version(l_version);
88   const grpc_gcp_RpcProtocolVersions_Version* r_maxver =
89       grpc_gcp_RpcProtocolVersions_max_rpc_version(r_version);
90   const grpc_gcp_RpcProtocolVersions_Version* l_minver =
91       grpc_gcp_RpcProtocolVersions_min_rpc_version(l_version);
92   const grpc_gcp_RpcProtocolVersions_Version* r_minver =
93       grpc_gcp_RpcProtocolVersions_min_rpc_version(r_version);
94   return (grpc_gcp_RpcProtocolVersions_Version_major(l_maxver) ==
95           grpc_gcp_RpcProtocolVersions_Version_major(r_maxver)) &&
96          (grpc_gcp_RpcProtocolVersions_Version_minor(l_maxver) ==
97           grpc_gcp_RpcProtocolVersions_Version_minor(r_maxver)) &&
98          (grpc_gcp_RpcProtocolVersions_Version_major(l_minver) ==
99           grpc_gcp_RpcProtocolVersions_Version_major(r_minver)) &&
100          (grpc_gcp_RpcProtocolVersions_Version_minor(l_minver) ==
101           grpc_gcp_RpcProtocolVersions_Version_minor(r_minver));
102 }
103 
104 // Check equality of a pair of ALTS handshake responses.
grpc_gcp_handshaker_resp_equals(const grpc_gcp_HandshakerResp * l_resp,const grpc_gcp_HandshakerResp * r_resp)105 bool grpc_gcp_handshaker_resp_equals(const grpc_gcp_HandshakerResp* l_resp,
106                                      const grpc_gcp_HandshakerResp* r_resp) {
107   return upb_StringView_IsEqual(grpc_gcp_HandshakerResp_out_frames(l_resp),
108                                 grpc_gcp_HandshakerResp_out_frames(r_resp)) &&
109          (grpc_gcp_HandshakerResp_bytes_consumed(l_resp) ==
110           grpc_gcp_HandshakerResp_bytes_consumed(l_resp)) &&
111          grpc_gcp_handshaker_resp_result_equals(
112              grpc_gcp_HandshakerResp_result(l_resp),
113              grpc_gcp_HandshakerResp_result(r_resp)) &&
114          grpc_gcp_handshaker_resp_status_equals(
115              grpc_gcp_HandshakerResp_status(l_resp),
116              grpc_gcp_HandshakerResp_status(r_resp));
117 }
118 
119 // This method checks equality of two handshaker response results.
grpc_gcp_handshaker_resp_result_equals(const grpc_gcp_HandshakerResult * l_result,const grpc_gcp_HandshakerResult * r_result)120 bool grpc_gcp_handshaker_resp_result_equals(
121     const grpc_gcp_HandshakerResult* l_result,
122     const grpc_gcp_HandshakerResult* r_result) {
123   if (l_result == nullptr && r_result == nullptr) {
124     return true;
125   } else if ((l_result != nullptr && r_result == nullptr) ||
126              (l_result == nullptr && r_result != nullptr)) {
127     return false;
128   }
129   return upb_StringView_IsEqual(
130              grpc_gcp_HandshakerResult_application_protocol(l_result),
131              grpc_gcp_HandshakerResult_application_protocol(r_result)) &&
132          upb_StringView_IsEqual(
133              grpc_gcp_HandshakerResult_record_protocol(l_result),
134              grpc_gcp_HandshakerResult_record_protocol(r_result)) &&
135          upb_StringView_IsEqual(grpc_gcp_HandshakerResult_key_data(l_result),
136                                 grpc_gcp_HandshakerResult_key_data(r_result)) &&
137          handshaker_identity_equals(
138              grpc_gcp_HandshakerResult_peer_identity(l_result),
139              grpc_gcp_HandshakerResult_peer_identity(r_result)) &&
140          handshaker_identity_equals(
141              grpc_gcp_HandshakerResult_local_identity(l_result),
142              grpc_gcp_HandshakerResult_local_identity(r_result)) &&
143          (grpc_gcp_HandshakerResult_keep_channel_open(l_result) ==
144           grpc_gcp_HandshakerResult_keep_channel_open(r_result)) &&
145          handshaker_rpc_versions_equals(
146              grpc_gcp_HandshakerResult_peer_rpc_versions(l_result),
147              grpc_gcp_HandshakerResult_peer_rpc_versions(r_result));
148 }
149 
150 // This method checks equality of two handshaker response statuses.
grpc_gcp_handshaker_resp_status_equals(const grpc_gcp_HandshakerStatus * l_status,const grpc_gcp_HandshakerStatus * r_status)151 bool grpc_gcp_handshaker_resp_status_equals(
152     const grpc_gcp_HandshakerStatus* l_status,
153     const grpc_gcp_HandshakerStatus* r_status) {
154   if (l_status == nullptr && r_status == nullptr) {
155     return true;
156   } else if ((l_status != nullptr && r_status == nullptr) ||
157              (l_status == nullptr && r_status != nullptr)) {
158     return false;
159   }
160   return (grpc_gcp_HandshakerStatus_code(l_status) ==
161           grpc_gcp_HandshakerStatus_code(r_status)) &&
162          upb_StringView_IsEqual(grpc_gcp_HandshakerStatus_details(l_status),
163                                 grpc_gcp_HandshakerStatus_details(r_status));
164 }
165