| 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 "src/core/lib/security/security_connector/alts/alts_security_connector.h" |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <gtest/gtest.h> |
| 26 |
|
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
|
| 31 |
#include "src/core/lib/security/context/security_context.h" |
| 32 |
#include "src/core/lib/transport/transport.h" |
| 33 |
#include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h" |
| 34 |
#include "src/core/tsi/transport_security.h" |
| 35 |
|
| 36 |
using grpc_core::internal::grpc_alts_auth_context_from_tsi_peer; |
| 37 |
|
| 38 |
/* This file contains unit tests of grpc_alts_auth_context_from_tsi_peer(). */ |
| 39 |
TEST(AltsSecurityConnectorTest, InvalidInputFailure) { |
| 40 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 41 |
grpc_alts_auth_context_from_tsi_peer(nullptr); |
| 42 |
ASSERT_EQ(ctx, nullptr); |
| 43 |
} |
| 44 |
|
| 45 |
TEST(AltsSecurityConnectorTest, EmptyCertificateTypeFailure) { |
| 46 |
tsi_peer peer; |
| 47 |
ASSERT_EQ(tsi_construct_peer(0, &peer), TSI_OK); |
| 48 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 49 |
grpc_alts_auth_context_from_tsi_peer(&peer); |
| 50 |
ASSERT_EQ(ctx, nullptr); |
| 51 |
tsi_peer_destruct(&peer); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(AltsSecurityConnectorTest, EmptyPeerPropertyFailure) { |
| 55 |
tsi_peer peer; |
| 56 |
ASSERT_EQ(tsi_construct_peer(1, &peer), TSI_OK); |
| 57 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 58 |
TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_ALTS_CERTIFICATE_TYPE, |
| 59 |
&peer.properties[0]), |
| 60 |
TSI_OK); |
| 61 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 62 |
grpc_alts_auth_context_from_tsi_peer(&peer); |
| 63 |
ASSERT_EQ(ctx, nullptr); |
| 64 |
tsi_peer_destruct(&peer); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(AltsSecurityConnectorTest, MissingRpcProtocolVersionsPropertyFailure) { |
| 68 |
tsi_peer peer; |
| 69 |
ASSERT_EQ(tsi_construct_peer(kTsiAltsNumOfPeerProperties, &peer), TSI_OK); |
| 70 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 71 |
TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_ALTS_CERTIFICATE_TYPE, |
| 72 |
&peer.properties[0]), |
| 73 |
TSI_OK); |
| 74 |
ASSERT_EQ( |
| 75 |
tsi_construct_string_peer_property_from_cstring( |
| 76 |
TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY, "alice", &peer.properties[1]), |
| 77 |
TSI_OK); |
| 78 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 79 |
grpc_alts_auth_context_from_tsi_peer(&peer); |
| 80 |
ASSERT_EQ(ctx, nullptr); |
| 81 |
tsi_peer_destruct(&peer); |
| 82 |
} |
| 83 |
|
| 84 |
TEST(AltsSecurityConnectorTest, MissingSecurityLevelPropertyFailure) { |
| 85 |
tsi_peer peer; |
| 86 |
ASSERT_EQ(tsi_construct_peer(kTsiAltsNumOfPeerProperties, &peer), TSI_OK); |
| 87 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 88 |
TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_ALTS_CERTIFICATE_TYPE, |
| 89 |
&peer.properties[0]), |
| 90 |
TSI_OK); |
| 91 |
ASSERT_EQ( |
| 92 |
tsi_construct_string_peer_property_from_cstring( |
| 93 |
TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY, "alice", &peer.properties[1]), |
| 94 |
TSI_OK); |
| 95 |
grpc_gcp_rpc_protocol_versions peer_versions; |
| 96 |
grpc_gcp_rpc_protocol_versions_set_max(&peer_versions, |
| 97 |
GRPC_PROTOCOL_VERSION_MAX_MAJOR, |
| 98 |
GRPC_PROTOCOL_VERSION_MAX_MINOR); |
| 99 |
grpc_gcp_rpc_protocol_versions_set_min(&peer_versions, |
| 100 |
GRPC_PROTOCOL_VERSION_MIN_MAJOR, |
| 101 |
GRPC_PROTOCOL_VERSION_MIN_MINOR); |
| 102 |
grpc_slice serialized_peer_versions; |
| 103 |
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_encode(&peer_versions, |
| 104 |
&serialized_peer_versions)); |
| 105 |
|
| 106 |
ASSERT_EQ( |
| 107 |
tsi_construct_string_peer_property( |
| 108 |
TSI_ALTS_RPC_VERSIONS, |
| 109 |
reinterpret_cast<char*>( |
| 110 |
GRPC_SLICE_START_PTR(serialized_peer_versions)), |
| 111 |
GRPC_SLICE_LENGTH(serialized_peer_versions), &peer.properties[2]), |
| 112 |
TSI_OK); |
| 113 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 114 |
grpc_alts_auth_context_from_tsi_peer(&peer); |
| 115 |
ASSERT_EQ(ctx, nullptr); |
| 116 |
grpc_slice_unref(serialized_peer_versions); |
| 117 |
tsi_peer_destruct(&peer); |
| 118 |
} |
| 119 |
|
| 120 |
TEST(AltsSecurityConnectorTest, UnknownPeerPropertyFailure) { |
| 121 |
tsi_peer peer; |
| 122 |
ASSERT_EQ(tsi_construct_peer(kTsiAltsNumOfPeerProperties, &peer), TSI_OK); |
| 123 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 124 |
TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_ALTS_CERTIFICATE_TYPE, |
| 125 |
&peer.properties[0]), |
| 126 |
TSI_OK); |
| 127 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 128 |
"unknown", "alice", &peer.properties[1]), |
| 129 |
TSI_OK); |
| 130 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 131 |
grpc_alts_auth_context_from_tsi_peer(&peer); |
| 132 |
ASSERT_EQ(ctx, nullptr); |
| 133 |
tsi_peer_destruct(&peer); |
| 134 |
} |
| 135 |
|
| 136 |
static bool test_identity(const grpc_auth_context* ctx, |
| 137 |
const char* expected_property_name, |
| 138 |
const char* expected_identity) { |
| 139 |
grpc_auth_property_iterator it; |
| 140 |
const grpc_auth_property* prop; |
| 141 |
EXPECT_TRUE(grpc_auth_context_peer_is_authenticated(ctx)); |
| 142 |
it = grpc_auth_context_peer_identity(ctx); |
| 143 |
prop = grpc_auth_property_iterator_next(&it); |
| 144 |
EXPECT_NE(prop, nullptr); |
| 145 |
if (strcmp(prop->name, expected_property_name) != 0) { |
| 146 |
gpr_log(GPR_ERROR, "Expected peer identity property name %s and got %s.", |
| 147 |
expected_property_name, prop->name); |
| 148 |
return false; |
| 149 |
} |
| 150 |
if (strncmp(prop->value, expected_identity, prop->value_length) != 0) { |
| 151 |
gpr_log(GPR_ERROR, "Expected peer identity %s and got got %s.", |
| 152 |
expected_identity, prop->value); |
| 153 |
return false; |
| 154 |
} |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
TEST(AltsSecurityConnectorTest, AltsPeerToAuthContextSuccess) { |
| 159 |
tsi_peer peer; |
| 160 |
ASSERT_EQ(tsi_construct_peer(kTsiAltsNumOfPeerProperties, &peer), TSI_OK); |
| 161 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 162 |
TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_ALTS_CERTIFICATE_TYPE, |
| 163 |
&peer.properties[0]), |
| 164 |
TSI_OK); |
| 165 |
ASSERT_EQ( |
| 166 |
tsi_construct_string_peer_property_from_cstring( |
| 167 |
TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY, "alice", &peer.properties[1]), |
| 168 |
TSI_OK); |
| 169 |
grpc_gcp_rpc_protocol_versions peer_versions; |
| 170 |
grpc_gcp_rpc_protocol_versions_set_max(&peer_versions, |
| 171 |
GRPC_PROTOCOL_VERSION_MAX_MAJOR, |
| 172 |
GRPC_PROTOCOL_VERSION_MAX_MINOR); |
| 173 |
grpc_gcp_rpc_protocol_versions_set_min(&peer_versions, |
| 174 |
GRPC_PROTOCOL_VERSION_MIN_MAJOR, |
| 175 |
GRPC_PROTOCOL_VERSION_MIN_MINOR); |
| 176 |
grpc_slice serialized_peer_versions; |
| 177 |
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_encode(&peer_versions, |
| 178 |
&serialized_peer_versions)); |
| 179 |
ASSERT_EQ( |
| 180 |
tsi_construct_string_peer_property( |
| 181 |
TSI_ALTS_RPC_VERSIONS, |
| 182 |
reinterpret_cast<char*>( |
| 183 |
GRPC_SLICE_START_PTR(serialized_peer_versions)), |
| 184 |
GRPC_SLICE_LENGTH(serialized_peer_versions), &peer.properties[2]), |
| 185 |
TSI_OK); |
| 186 |
ASSERT_EQ(tsi_construct_string_peer_property_from_cstring( |
| 187 |
TSI_SECURITY_LEVEL_PEER_PROPERTY, |
| 188 |
tsi_security_level_to_string(TSI_PRIVACY_AND_INTEGRITY), |
| 189 |
&peer.properties[3]), |
| 190 |
TSI_OK); |
| 191 |
char test_ctx[] = "test serialized context"; |
| 192 |
grpc_slice serialized_alts_ctx = grpc_slice_from_copied_string(test_ctx); |
| 193 |
ASSERT_EQ( |
| 194 |
tsi_construct_string_peer_property( |
| 195 |
TSI_ALTS_CONTEXT, |
| 196 |
reinterpret_cast<char*>(GRPC_SLICE_START_PTR(serialized_alts_ctx)), |
| 197 |
GRPC_SLICE_LENGTH(serialized_alts_ctx), &peer.properties[4]), |
| 198 |
TSI_OK); |
| 199 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 200 |
grpc_alts_auth_context_from_tsi_peer(&peer); |
| 201 |
ASSERT_NE(ctx, nullptr); |
| 202 |
ASSERT_TRUE(test_identity(ctx.get(), TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY, |
| 203 |
"alice")); |
| 204 |
ctx.reset(DEBUG_LOCATION, "test"); |
| 205 |
grpc_slice_unref(serialized_peer_versions); |
| 206 |
grpc_slice_unref(serialized_alts_ctx); |
| 207 |
tsi_peer_destruct(&peer); |
| 208 |
} |
| 209 |
|
| 210 |
int main(int argc, char** argv) { |
| 211 |
::testing::InitGoogleTest(&argc, argv); |
| 212 |
return RUN_ALL_TESTS(); |
| 213 |
} |