aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/tsi/alts/handshaker/alts_handshaker_service_api_test.cc
blob: 3506264f5287d6ea3115e20c2ada1f058575c3fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"

int main(int argc, char** argv) {
  const char in_bytes[] = "HELLO GOOGLE!";
  const char out_frames[] = "HELLO WORLD!";
  const char key_data[] = "THIS IS KEY DATA.";
  const char details[] = "DETAILS NEED TO BE POPULATED";
  const uint32_t max_rpc_version_major = 3;
  const uint32_t max_rpc_version_minor = 2;
  const uint32_t min_rpc_version_major = 2;
  const uint32_t min_rpc_version_minor = 1;

  /* handshaker_req_next. */
  grpc_gcp_handshaker_req* req = grpc_gcp_handshaker_req_create(NEXT_REQ);
  grpc_gcp_handshaker_req* decoded_req =
      grpc_gcp_handshaker_decoded_req_create(NEXT_REQ);
  GPR_ASSERT(
      grpc_gcp_handshaker_req_set_in_bytes(req, in_bytes, strlen(in_bytes)));
  grpc_slice encoded_req;
  GPR_ASSERT(grpc_gcp_handshaker_req_encode(req, &encoded_req));
  GPR_ASSERT(grpc_gcp_handshaker_req_decode(encoded_req, decoded_req));
  GPR_ASSERT(grpc_gcp_handshaker_req_equals(req, decoded_req));
  grpc_gcp_handshaker_req_destroy(req);
  grpc_gcp_handshaker_req_destroy(decoded_req);
  grpc_slice_unref(encoded_req);

  /* handshaker_req_client_start. */
  req = grpc_gcp_handshaker_req_create(CLIENT_START_REQ);
  decoded_req = grpc_gcp_handshaker_decoded_req_create(CLIENT_START_REQ);
  GPR_ASSERT(grpc_gcp_handshaker_req_set_handshake_protocol(
      req, grpc_gcp_HandshakeProtocol_TLS));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_local_identity_hostname(
      req, "www.google.com"));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_local_endpoint(
      req, "2001:db8::8:800:200C:417a", 9876, grpc_gcp_NetworkProtocol_TCP));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_remote_endpoint(
      req, "2001:db8::bac5::fed0:84a2", 1234, grpc_gcp_NetworkProtocol_TCP));
  GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "grpc"));
  GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "http2"));
  GPR_ASSERT(
      grpc_gcp_handshaker_req_add_record_protocol(req, "ALTSRP_GCM_AES256"));
  GPR_ASSERT(
      grpc_gcp_handshaker_req_add_record_protocol(req, "ALTSRP_GCM_AES384"));
  GPR_ASSERT(grpc_gcp_handshaker_req_add_target_identity_service_account(
      req, "foo@google.com"));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_target_name(
      req, "google.example.library.service"));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_rpc_versions(
      req, max_rpc_version_major, max_rpc_version_minor, min_rpc_version_major,
      min_rpc_version_minor));
  GPR_ASSERT(grpc_gcp_handshaker_req_encode(req, &encoded_req));
  GPR_ASSERT(grpc_gcp_handshaker_req_decode(encoded_req, decoded_req));
  GPR_ASSERT(grpc_gcp_handshaker_req_equals(req, decoded_req));
  grpc_gcp_handshaker_req_destroy(req);
  grpc_gcp_handshaker_req_destroy(decoded_req);
  grpc_slice_unref(encoded_req);

  /* handshaker_req_server_start. */
  req = grpc_gcp_handshaker_req_create(SERVER_START_REQ);
  decoded_req = grpc_gcp_handshaker_decoded_req_create(SERVER_START_REQ);
  GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "grpc"));
  GPR_ASSERT(grpc_gcp_handshaker_req_add_application_protocol(req, "http2"));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_local_endpoint(
      req, "2001:db8::8:800:200C:417a", 9876, grpc_gcp_NetworkProtocol_TCP));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_remote_endpoint(
      req, "2001:db8::bac5::fed0:84a2", 1234, grpc_gcp_NetworkProtocol_UDP));
  GPR_ASSERT(
      grpc_gcp_handshaker_req_set_in_bytes(req, in_bytes, strlen(in_bytes)));
  GPR_ASSERT(grpc_gcp_handshaker_req_param_add_record_protocol(
      req, grpc_gcp_HandshakeProtocol_TLS, "ALTSRP_GCM_AES128"));
  GPR_ASSERT(grpc_gcp_handshaker_req_param_add_local_identity_service_account(
      req, grpc_gcp_HandshakeProtocol_TLS, "foo@google.com"));
  GPR_ASSERT(grpc_gcp_handshaker_req_param_add_local_identity_hostname(
      req, grpc_gcp_HandshakeProtocol_TLS, "yihuaz0.mtv.corp.google.com"));
  GPR_ASSERT(grpc_gcp_handshaker_req_param_add_record_protocol(
      req, grpc_gcp_HandshakeProtocol_ALTS, "ALTSRP_GCM_AES128"));
  GPR_ASSERT(grpc_gcp_handshaker_req_param_add_local_identity_hostname(
      req, grpc_gcp_HandshakeProtocol_ALTS, "www.amazon.com"));
  GPR_ASSERT(grpc_gcp_handshaker_req_set_rpc_versions(
      req, max_rpc_version_major, max_rpc_version_minor, min_rpc_version_major,
      min_rpc_version_minor));

  GPR_ASSERT(grpc_gcp_handshaker_req_encode(req, &encoded_req));
  GPR_ASSERT(grpc_gcp_handshaker_req_decode(encoded_req, decoded_req));
  GPR_ASSERT(grpc_gcp_handshaker_req_equals(req, decoded_req));
  grpc_gcp_handshaker_req_destroy(req);
  grpc_gcp_handshaker_req_destroy(decoded_req);
  grpc_slice_unref(encoded_req);

  /* handshaker_resp. */
  grpc_gcp_handshaker_resp* resp = grpc_gcp_handshaker_resp_create();
  grpc_gcp_handshaker_resp* decoded_resp = grpc_gcp_handshaker_resp_create();
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_out_frames(resp, out_frames,
                                                     strlen(out_frames)));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_bytes_consumed(resp, 1024));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_application_protocol(resp, "http"));
  GPR_ASSERT(
      grpc_gcp_handshaker_resp_set_record_protocol(resp, "ALTSRP_GCM_AES128"));
  GPR_ASSERT(
      grpc_gcp_handshaker_resp_set_key_data(resp, key_data, strlen(key_data)));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_local_identity_hostname(
      resp, "www.faceboook.com"));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_peer_identity_hostname(
      resp, "www.amazon.com"));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_channel_open(
      resp, false /* channel_open */));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_code(resp, 1023));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_details(resp, details));
  GPR_ASSERT(grpc_gcp_handshaker_resp_set_peer_rpc_versions(
      resp, max_rpc_version_major, max_rpc_version_minor, min_rpc_version_major,
      min_rpc_version_minor));
  grpc_slice encoded_resp;
  GPR_ASSERT(grpc_gcp_handshaker_resp_encode(resp, &encoded_resp));
  GPR_ASSERT(grpc_gcp_handshaker_resp_decode(encoded_resp, decoded_resp));
  GPR_ASSERT(grpc_gcp_handshaker_resp_equals(resp, decoded_resp));
  grpc_gcp_handshaker_resp_destroy(resp);
  grpc_gcp_handshaker_resp_destroy(decoded_resp);
  grpc_slice_unref(encoded_resp);
  /* Test invalid arguments. */
  GPR_ASSERT(!grpc_gcp_handshaker_req_set_in_bytes(nullptr, in_bytes,
                                                   strlen(in_bytes)));
  GPR_ASSERT(!grpc_gcp_handshaker_req_param_add_record_protocol(
      req, grpc_gcp_HandshakeProtocol_TLS, nullptr));
  GPR_ASSERT(!grpc_gcp_handshaker_req_param_add_local_identity_service_account(
      nullptr, grpc_gcp_HandshakeProtocol_TLS, nullptr));
  GPR_ASSERT(!grpc_gcp_handshaker_resp_set_record_protocol(nullptr, nullptr));
}