• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/tsi/alts/handshaker/transport_security_common_api.h"
24 
25 const size_t kMaxRpcVersionMajor = 3;
26 const size_t kMaxRpcVersionMinor = 2;
27 const size_t kMinRpcVersionMajor = 2;
28 const size_t kMinRpcVersionMinor = 1;
29 
grpc_gcp_rpc_protocol_versions_equal(grpc_gcp_rpc_protocol_versions * l_versions,grpc_gcp_rpc_protocol_versions * r_versions)30 static bool grpc_gcp_rpc_protocol_versions_equal(
31     grpc_gcp_rpc_protocol_versions* l_versions,
32     grpc_gcp_rpc_protocol_versions* r_versions) {
33   GPR_ASSERT(l_versions != nullptr && r_versions != nullptr);
34   if ((l_versions->has_max_rpc_version ^ r_versions->has_max_rpc_version) |
35       (l_versions->has_min_rpc_version ^ r_versions->has_min_rpc_version)) {
36     return false;
37   }
38   if (l_versions->has_max_rpc_version) {
39     if ((l_versions->max_rpc_version.major !=
40          r_versions->max_rpc_version.major) ||
41         (l_versions->max_rpc_version.minor !=
42          r_versions->max_rpc_version.minor)) {
43       return false;
44     }
45   }
46   if (l_versions->has_min_rpc_version) {
47     if ((l_versions->min_rpc_version.major !=
48          r_versions->min_rpc_version.major) ||
49         (l_versions->min_rpc_version.minor !=
50          r_versions->min_rpc_version.minor)) {
51       return false;
52     }
53   }
54   return true;
55 }
56 
test_success()57 static void test_success() {
58   grpc_gcp_rpc_protocol_versions version;
59   grpc_gcp_rpc_protocol_versions decoded_version;
60   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(
61       &version, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
62   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(
63       &version, kMinRpcVersionMajor, kMinRpcVersionMinor));
64   /* Serializes to raw bytes. */
65   size_t encoded_length =
66       grpc_gcp_rpc_protocol_versions_encode_length(&version);
67   uint8_t* encoded_bytes = static_cast<uint8_t*>(gpr_malloc(encoded_length));
68   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
69       &version, encoded_bytes, encoded_length));
70   grpc_slice encoded_slice;
71   /* Serializes to grpc slice. */
72   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_encode(&version, &encoded_slice));
73   /* Checks serialized raw bytes and serialized grpc slice have same content. */
74   GPR_ASSERT(encoded_length == GRPC_SLICE_LENGTH(encoded_slice));
75   GPR_ASSERT(memcmp(encoded_bytes, GRPC_SLICE_START_PTR(encoded_slice),
76                     encoded_length) == 0);
77   /* Deserializes and compares with the original version. */
78   GPR_ASSERT(
79       grpc_gcp_rpc_protocol_versions_decode(encoded_slice, &decoded_version));
80   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_equal(&version, &decoded_version));
81   grpc_slice_unref(encoded_slice);
82   gpr_free(encoded_bytes);
83 }
84 
test_failure()85 static void test_failure() {
86   grpc_gcp_rpc_protocol_versions version, decoded_version;
87   grpc_slice encoded_slice;
88   /* Test for invalid arguments. */
89   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_set_max(
90       nullptr, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
91   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_set_min(
92       nullptr, kMinRpcVersionMajor, kMinRpcVersionMinor));
93   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_encode_length(nullptr) == 0);
94   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(
95       &version, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
96   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(
97       &version, kMinRpcVersionMajor, kMinRpcVersionMinor));
98   size_t encoded_length =
99       grpc_gcp_rpc_protocol_versions_encode_length(&version);
100   uint8_t* encoded_bytes = static_cast<uint8_t*>(gpr_malloc(encoded_length));
101   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
102       nullptr, encoded_bytes, encoded_length));
103   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
104       &version, nullptr, encoded_length));
105   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
106       &version, encoded_bytes, 0));
107   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode(nullptr, &encoded_slice));
108   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode(&version, nullptr));
109   GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_decode(encoded_slice, nullptr));
110   /* Test for nanopb decode. */
111   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_encode(&version, &encoded_slice));
112   grpc_slice bad_slice = grpc_slice_split_head(
113       &encoded_slice, GRPC_SLICE_LENGTH(encoded_slice) - 1);
114   grpc_slice_unref(encoded_slice);
115   GPR_ASSERT(
116       !grpc_gcp_rpc_protocol_versions_decode(bad_slice, &decoded_version));
117   grpc_slice_unref(bad_slice);
118   gpr_free(encoded_bytes);
119 }
120 
test_copy()121 static void test_copy() {
122   grpc_gcp_rpc_protocol_versions src;
123   grpc_gcp_rpc_protocol_versions des;
124   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&src, kMaxRpcVersionMajor,
125                                                     kMaxRpcVersionMinor));
126   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&src, kMinRpcVersionMajor,
127                                                     kMinRpcVersionMinor));
128   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_copy(&src, &des));
129   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_equal(&src, &des));
130 }
131 
test_check_success()132 static void test_check_success() {
133   grpc_gcp_rpc_protocol_versions v1;
134   grpc_gcp_rpc_protocol_versions v2;
135   grpc_gcp_rpc_protocol_versions_version highest_common_version;
136   /* test equality. */
137   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMaxRpcVersionMajor,
138                                                     kMaxRpcVersionMinor));
139   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMaxRpcVersionMajor,
140                                                     kMaxRpcVersionMinor));
141   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
142                                                     kMaxRpcVersionMinor));
143   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMaxRpcVersionMajor,
144                                                     kMaxRpcVersionMinor));
145   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_check(
146                  (const grpc_gcp_rpc_protocol_versions*)&v1,
147                  (const grpc_gcp_rpc_protocol_versions*)&v2,
148                  &highest_common_version) == 1);
149   GPR_ASSERT(grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
150                  &highest_common_version, &v1.max_rpc_version) == 0);
151 
152   /* test inequality. */
153   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMaxRpcVersionMajor,
154                                                     kMaxRpcVersionMinor));
155   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMinRpcVersionMinor,
156                                                     kMinRpcVersionMinor));
157   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
158                                                     kMinRpcVersionMinor));
159   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMinRpcVersionMajor,
160                                                     kMaxRpcVersionMinor));
161   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_check(
162                  (const grpc_gcp_rpc_protocol_versions*)&v1,
163                  (const grpc_gcp_rpc_protocol_versions*)&v2,
164                  &highest_common_version) == 1);
165   GPR_ASSERT(grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
166                  &highest_common_version, &v2.max_rpc_version) == 0);
167 }
168 
test_check_failure()169 static void test_check_failure() {
170   grpc_gcp_rpc_protocol_versions v1;
171   grpc_gcp_rpc_protocol_versions v2;
172   grpc_gcp_rpc_protocol_versions_version highest_common_version;
173 
174   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMinRpcVersionMajor,
175                                                     kMinRpcVersionMinor));
176   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMinRpcVersionMajor,
177                                                     kMinRpcVersionMinor));
178   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
179                                                     kMaxRpcVersionMinor));
180   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMaxRpcVersionMajor,
181                                                     kMaxRpcVersionMinor));
182   GPR_ASSERT(grpc_gcp_rpc_protocol_versions_check(
183                  (const grpc_gcp_rpc_protocol_versions*)&v1,
184                  (const grpc_gcp_rpc_protocol_versions*)&v2,
185                  &highest_common_version) == 0);
186 }
187 
main(int argc,char ** argv)188 int main(int argc, char** argv) {
189   /* Run tests. */
190   test_success();
191   test_failure();
192   test_copy();
193   test_check_success();
194   test_check_failure();
195   return 0;
196 }
197