Imported Upstream version 1.29.0
[platform/upstream/grpc.git] / test / core / security / alts_credentials_fuzzer.cc
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 <string.h>
20
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_security.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/string_util.h>
26 #include "test/core/util/fuzzer_util.h"
27
28 #include "src/core/lib/gpr/env.h"
29 #include "src/core/lib/security/credentials/alts/alts_credentials.h"
30 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
31 #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
32
33 using grpc_core::testing::grpc_fuzzer_get_next_byte;
34 using grpc_core::testing::grpc_fuzzer_get_next_string;
35 using grpc_core::testing::input_stream;
36
37 // Logging
38 bool squelch = true;
39 bool leak_check = true;
40
41 static void dont_log(gpr_log_func_args* /*args*/) {}
42
43 // Add a random number of target service accounts to client options.
44 static void read_target_service_accounts(
45     input_stream* inp, grpc_alts_credentials_options* options) {
46   size_t n = grpc_fuzzer_get_next_byte(inp);
47   for (size_t i = 0; i < n; i++) {
48     char* service_account = grpc_fuzzer_get_next_string(inp, nullptr);
49     if (service_account != nullptr) {
50       grpc_alts_credentials_client_options_add_target_service_account(
51           options, service_account);
52       gpr_free(service_account);
53     }
54   }
55   // Added to improve code coverage.
56   grpc_alts_credentials_client_options_add_target_service_account(options,
57                                                                   nullptr);
58   grpc_alts_credentials_client_options_add_target_service_account(
59       nullptr, "this is service account");
60 }
61
62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
63   char* grpc_trace_fuzzer = gpr_getenv("GRPC_TRACE_FUZZER");
64   if (squelch && grpc_trace_fuzzer == nullptr) {
65     gpr_set_log_function(dont_log);
66   }
67   gpr_free(grpc_trace_fuzzer);
68   input_stream inp = {data, data + size};
69   grpc_init();
70   bool is_on_gcp = grpc_alts_is_running_on_gcp();
71   while (inp.cur != inp.end) {
72     bool enable_untrusted_alts = grpc_fuzzer_get_next_byte(&inp) & 0x01;
73     char* handshaker_service_url =
74         grpc_fuzzer_get_next_byte(&inp) & 0x01
75             ? grpc_fuzzer_get_next_string(&inp, nullptr)
76             : nullptr;
77     if (grpc_fuzzer_get_next_byte(&inp) & 0x01) {
78       // Test ALTS channel credentials.
79       grpc_alts_credentials_options* options =
80           grpc_alts_credentials_client_options_create();
81       read_target_service_accounts(&inp, options);
82       grpc_channel_credentials* cred = grpc_alts_credentials_create_customized(
83           options, handshaker_service_url, enable_untrusted_alts);
84       if (!enable_untrusted_alts && !is_on_gcp) {
85         GPR_ASSERT(cred == nullptr);
86       } else {
87         GPR_ASSERT(cred != nullptr);
88       }
89       grpc_channel_credentials_release(cred);
90       grpc_alts_credentials_options_destroy(options);
91     } else {
92       // Test ALTS server credentials.
93       grpc_alts_credentials_options* options =
94           grpc_alts_credentials_server_options_create();
95       grpc_server_credentials* cred =
96           grpc_alts_server_credentials_create_customized(
97               options, handshaker_service_url, enable_untrusted_alts);
98       if (!enable_untrusted_alts && !is_on_gcp) {
99         GPR_ASSERT(cred == nullptr);
100       } else {
101         GPR_ASSERT(cred != nullptr);
102       }
103       grpc_server_credentials_release(cred);
104       grpc_alts_credentials_options_destroy(options);
105     }
106     gpr_free(handshaker_service_url);
107   }
108   grpc_shutdown();
109   return 0;
110 }