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 <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"
24
main(int argc,char ** argv)25 int main(int argc, char** argv) {
26 const char in_bytes[] = "HELLO GOOGLE!";
27 const char out_frames[] = "HELLO WORLD!";
28 const char key_data[] = "THIS IS KEY DATA.";
29 const char details[] = "DETAILS NEED TO BE POPULATED";
30 const uint32_t max_rpc_version_major = 3;
31 const uint32_t max_rpc_version_minor = 2;
32 const uint32_t min_rpc_version_major = 2;
33 const uint32_t min_rpc_version_minor = 1;
34
35 /* handshaker_req_next. */
36 grpc_gcp_handshaker_req* req = grpc_gcp_handshaker_req_create(NEXT_REQ);
37 grpc_gcp_handshaker_req* decoded_req =
38 grpc_gcp_handshaker_decoded_req_create(NEXT_REQ);
39 GPR_ASSERT(
40 grpc_gcp_handshaker_req_set_in_bytes(req, in_bytes, strlen(in_bytes)));
41 grpc_slice encoded_req;
42 GPR_ASSERT(grpc_gcp_handshaker_req_encode(req, &encoded_req));
43 GPR_ASSERT(grpc_gcp_handshaker_req_decode(encoded_req, decoded_req));
44 GPR_ASSERT(grpc_gcp_handshaker_req_equals(req, decoded_req));
45 grpc_gcp_handshaker_req_destroy(req);
46 grpc_gcp_handshaker_req_destroy(decoded_req);
47 grpc_slice_unref(encoded_req);
48
49 /* handshaker_req_client_start. */
50 req = grpc_gcp_handshaker_req_create(CLIENT_START_REQ);
51 decoded_req = grpc_gcp_handshaker_decoded_req_create(CLIENT_START_REQ);
52 GPR_ASSERT(grpc_gcp_handshaker_req_set_handshake_protocol(
53 req, grpc_gcp_HandshakeProtocol_TLS));
54 GPR_ASSERT(grpc_gcp_handshaker_req_set_local_identity_hostname(
55 req, "www.google.com"));
56 GPR_ASSERT(grpc_gcp_handshaker_req_set_local_endpoint(
57 req, "2001:db8::8:800:200C:417a", 9876, grpc_gcp_NetworkProtocol_TCP));
58 GPR_ASSERT(grpc_gcp_handshaker_req_set_remote_endpoint(
59 req, "2001:db8::bac5::fed0:84a2", 1234, grpc_gcp_NetworkProtocol_TCP));
60 GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "grpc"));
61 GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "http2"));
62 GPR_ASSERT(
63 grpc_gcp_handshaker_req_add_record_protocol(req, "ALTSRP_GCM_AES256"));
64 GPR_ASSERT(
65 grpc_gcp_handshaker_req_add_record_protocol(req, "ALTSRP_GCM_AES384"));
66 GPR_ASSERT(grpc_gcp_handshaker_req_add_target_identity_service_account(
67 req, "foo@google.com"));
68 GPR_ASSERT(grpc_gcp_handshaker_req_set_target_name(
69 req, "google.example.library.service"));
70 GPR_ASSERT(grpc_gcp_handshaker_req_set_rpc_versions(
71 req, max_rpc_version_major, max_rpc_version_minor, min_rpc_version_major,
72 min_rpc_version_minor));
73 GPR_ASSERT(grpc_gcp_handshaker_req_encode(req, &encoded_req));
74 GPR_ASSERT(grpc_gcp_handshaker_req_decode(encoded_req, decoded_req));
75 GPR_ASSERT(grpc_gcp_handshaker_req_equals(req, decoded_req));
76 grpc_gcp_handshaker_req_destroy(req);
77 grpc_gcp_handshaker_req_destroy(decoded_req);
78 grpc_slice_unref(encoded_req);
79
80 /* handshaker_req_server_start. */
81 req = grpc_gcp_handshaker_req_create(SERVER_START_REQ);
82 decoded_req = grpc_gcp_handshaker_decoded_req_create(SERVER_START_REQ);
83 GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "grpc"));
84 GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "http2"));
85 GPR_ASSERT(grpc_gcp_handshaker_req_set_local_endpoint(
86 req, "2001:db8::8:800:200C:417a", 9876, grpc_gcp_NetworkProtocol_TCP));
87 GPR_ASSERT(grpc_gcp_handshaker_req_set_remote_endpoint(
88 req, "2001:db8::bac5::fed0:84a2", 1234, grpc_gcp_NetworkProtocol_UDP));
89 GPR_ASSERT(
90 grpc_gcp_handshaker_req_set_in_bytes(req, in_bytes, strlen(in_bytes)));
91 GPR_ASSERT(grpc_gcp_handshaker_req_param_add_record_protocol(
92 req, grpc_gcp_HandshakeProtocol_TLS, "ALTSRP_GCM_AES128"));
93 GPR_ASSERT(grpc_gcp_handshaker_req_param_add_local_identity_service_account(
94 req, grpc_gcp_HandshakeProtocol_TLS, "foo@google.com"));
95 GPR_ASSERT(grpc_gcp_handshaker_req_param_add_local_identity_hostname(
96 req, grpc_gcp_HandshakeProtocol_TLS, "yihuaz0.mtv.corp.google.com"));
97 GPR_ASSERT(grpc_gcp_handshaker_req_param_add_record_protocol(
98 req, grpc_gcp_HandshakeProtocol_ALTS, "ALTSRP_GCM_AES128"));
99 GPR_ASSERT(grpc_gcp_handshaker_req_param_add_local_identity_hostname(
100 req, grpc_gcp_HandshakeProtocol_ALTS, "www.amazon.com"));
101 GPR_ASSERT(grpc_gcp_handshaker_req_set_rpc_versions(
102 req, max_rpc_version_major, max_rpc_version_minor, min_rpc_version_major,
103 min_rpc_version_minor));
104
105 GPR_ASSERT(grpc_gcp_handshaker_req_encode(req, &encoded_req));
106 GPR_ASSERT(grpc_gcp_handshaker_req_decode(encoded_req, decoded_req));
107 GPR_ASSERT(grpc_gcp_handshaker_req_equals(req, decoded_req));
108 grpc_gcp_handshaker_req_destroy(req);
109 grpc_gcp_handshaker_req_destroy(decoded_req);
110 grpc_slice_unref(encoded_req);
111
112 /* handshaker_resp. */
113 grpc_gcp_handshaker_resp* resp = grpc_gcp_handshaker_resp_create();
114 grpc_gcp_handshaker_resp* decoded_resp = grpc_gcp_handshaker_resp_create();
115 GPR_ASSERT(grpc_gcp_handshaker_resp_set_out_frames(resp, out_frames,
116 strlen(out_frames)));
117 GPR_ASSERT(grpc_gcp_handshaker_resp_set_bytes_consumed(resp, 1024));
118 GPR_ASSERT(grpc_gcp_handshaker_resp_set_application_protocol(resp, "http"));
119 GPR_ASSERT(
120 grpc_gcp_handshaker_resp_set_record_protocol(resp, "ALTSRP_GCM_AES128"));
121 GPR_ASSERT(
122 grpc_gcp_handshaker_resp_set_key_data(resp, key_data, strlen(key_data)));
123 GPR_ASSERT(grpc_gcp_handshaker_resp_set_local_identity_hostname(
124 resp, "www.faceboook.com"));
125 GPR_ASSERT(grpc_gcp_handshaker_resp_set_peer_identity_hostname(
126 resp, "www.amazon.com"));
127 GPR_ASSERT(grpc_gcp_handshaker_resp_set_channel_open(
128 resp, false /* channel_open */));
129 GPR_ASSERT(grpc_gcp_handshaker_resp_set_code(resp, 1023));
130 GPR_ASSERT(grpc_gcp_handshaker_resp_set_details(resp, details));
131 GPR_ASSERT(grpc_gcp_handshaker_resp_set_peer_rpc_versions(
132 resp, max_rpc_version_major, max_rpc_version_minor, min_rpc_version_major,
133 min_rpc_version_minor));
134 grpc_slice encoded_resp;
135 GPR_ASSERT(grpc_gcp_handshaker_resp_encode(resp, &encoded_resp));
136 GPR_ASSERT(grpc_gcp_handshaker_resp_decode(encoded_resp, decoded_resp));
137 GPR_ASSERT(grpc_gcp_handshaker_resp_equals(resp, decoded_resp));
138 grpc_gcp_handshaker_resp_destroy(resp);
139 grpc_gcp_handshaker_resp_destroy(decoded_resp);
140 grpc_slice_unref(encoded_resp);
141 /* Test invalid arguments. */
142 GPR_ASSERT(!grpc_gcp_handshaker_req_set_in_bytes(nullptr, in_bytes,
143 strlen(in_bytes)));
144 GPR_ASSERT(!grpc_gcp_handshaker_req_param_add_record_protocol(
145 req, grpc_gcp_HandshakeProtocol_TLS, nullptr));
146 GPR_ASSERT(!grpc_gcp_handshaker_req_param_add_local_identity_service_account(
147 nullptr, grpc_gcp_HandshakeProtocol_TLS, nullptr));
148 GPR_ASSERT(!grpc_gcp_handshaker_resp_set_record_protocol(nullptr, nullptr));
149 }
150