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