blob: 1b1b8b8f9200716a780b74d0efefecb27b92a9eb [file] [log] [blame]
yang-g7c090f82015-12-07 15:46:04 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/grpc.h>
35#include <string.h>
36
37#include <grpc/support/alloc.h>
38#include <grpc/support/log.h>
39#include <grpc/support/string_util.h>
40#include "test/core/util/test_config.h"
41
42static char *g_last_log_error_message = NULL;
43static const char *g_file_name = "channel.c";
44
45static int ends_with(const char *src, const char *suffix) {
46 size_t src_len = strlen(src);
47 size_t suffix_len = strlen(suffix);
48 if (src_len < suffix_len) {
49 return 0;
50 }
51 return strcmp(src + src_len - suffix_len, suffix) == 0;
52}
53
54static void log_error_sink(gpr_log_func_args *args) {
55 if (args->severity == GPR_LOG_SEVERITY_ERROR &&
56 ends_with(args->file, g_file_name)) {
57 g_last_log_error_message = gpr_strdup(args->message);
58 }
59}
60
61static void verify_last_error(const char *message) {
62 if (message == NULL) {
63 GPR_ASSERT(g_last_log_error_message == NULL);
64 return;
65 }
66 GPR_ASSERT(strcmp(message, g_last_log_error_message) == 0);
67 gpr_free(g_last_log_error_message);
68 g_last_log_error_message = NULL;
69}
70
71static char *compose_error_string(const char *key, const char *message) {
72 char *ret;
73 gpr_asprintf(&ret, "%s%s", key, message);
74 return ret;
75}
76
77static void one_test(grpc_channel_args *args, char *expected_error_message) {
78 grpc_channel *chan =
79 grpc_insecure_channel_create("nonexistant:54321", args, NULL);
80 verify_last_error(expected_error_message);
81 gpr_free(expected_error_message);
82 grpc_channel_destroy(chan);
83}
84
yang-g03468832015-12-08 10:10:44 -080085static void test_no_error_message(void) { one_test(NULL, NULL); }
yang-g7c090f82015-12-07 15:46:04 -080086
yang-g03468832015-12-08 10:10:44 -080087static void test_max_message_length_type(void) {
yang-g7c090f82015-12-07 15:46:04 -080088 grpc_arg client_arg;
89 grpc_channel_args client_args;
90 char *expected_error_message;
91
92 client_arg.type = GRPC_ARG_STRING;
93 client_arg.key = GRPC_ARG_MAX_MESSAGE_LENGTH;
yang-g26fdaa52015-12-08 08:05:19 -080094 client_arg.value.string = NULL;
yang-g7c090f82015-12-07 15:46:04 -080095
96 client_args.num_args = 1;
97 client_args.args = &client_arg;
98 expected_error_message = compose_error_string(
99 GRPC_ARG_MAX_MESSAGE_LENGTH, " ignored: it must be an integer");
100 one_test(&client_args, expected_error_message);
101}
102
yang-g03468832015-12-08 10:10:44 -0800103static void test_max_message_length_negative(void) {
yang-g7c090f82015-12-07 15:46:04 -0800104 grpc_arg client_arg;
105 grpc_channel_args client_args;
106 char *expected_error_message;
107
108 client_arg.type = GRPC_ARG_INTEGER;
109 client_arg.key = GRPC_ARG_MAX_MESSAGE_LENGTH;
110 client_arg.value.integer = -1;
111
112 client_args.num_args = 1;
113 client_args.args = &client_arg;
114 expected_error_message = compose_error_string(GRPC_ARG_MAX_MESSAGE_LENGTH,
115 " ignored: it must be >= 0");
116 one_test(&client_args, expected_error_message);
117}
118
yang-g03468832015-12-08 10:10:44 -0800119static void test_default_authority_type(void) {
yang-g7c090f82015-12-07 15:46:04 -0800120 grpc_arg client_arg;
121 grpc_channel_args client_args;
122 char *expected_error_message;
123
124 client_arg.type = GRPC_ARG_INTEGER;
125 client_arg.key = GRPC_ARG_DEFAULT_AUTHORITY;
yang-g26fdaa52015-12-08 08:05:19 -0800126 client_arg.value.integer = 0;
yang-g7c090f82015-12-07 15:46:04 -0800127
128 client_args.num_args = 1;
129 client_args.args = &client_arg;
130 expected_error_message = compose_error_string(
131 GRPC_ARG_DEFAULT_AUTHORITY, " ignored: it must be a string");
132 one_test(&client_args, expected_error_message);
133}
134
yang-g03468832015-12-08 10:10:44 -0800135static void test_ssl_name_override_type(void) {
yang-g7c090f82015-12-07 15:46:04 -0800136 grpc_arg client_arg;
137 grpc_channel_args client_args;
138 char *expected_error_message;
139
140 client_arg.type = GRPC_ARG_INTEGER;
141 client_arg.key = GRPC_SSL_TARGET_NAME_OVERRIDE_ARG;
yang-g26fdaa52015-12-08 08:05:19 -0800142 client_arg.value.integer = 0;
yang-g7c090f82015-12-07 15:46:04 -0800143
144 client_args.num_args = 1;
145 client_args.args = &client_arg;
146 expected_error_message = compose_error_string(
147 GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, " ignored: it must be a string");
148 one_test(&client_args, expected_error_message);
149}
150
yang-g03468832015-12-08 10:10:44 -0800151static void test_ssl_name_override_failed(void) {
yang-g7c090f82015-12-07 15:46:04 -0800152 grpc_arg client_arg[2];
153 grpc_channel_args client_args;
154 char *expected_error_message;
155
156 client_arg[0].type = GRPC_ARG_STRING;
157 client_arg[0].key = GRPC_ARG_DEFAULT_AUTHORITY;
158 client_arg[0].value.string = "default";
159 client_arg[1].type = GRPC_ARG_STRING;
160 client_arg[1].key = GRPC_SSL_TARGET_NAME_OVERRIDE_ARG;
161 client_arg[1].value.string = "ssl";
162
163 client_args.num_args = 2;
164 client_args.args = client_arg;
165 expected_error_message =
166 compose_error_string(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG,
167 " ignored: default host already set some other way");
168 one_test(&client_args, expected_error_message);
169}
170
171int main(int argc, char **argv) {
172 grpc_test_init(argc, argv);
173 grpc_init();
174 gpr_set_log_function(log_error_sink);
175
176 test_no_error_message();
177 test_max_message_length_type();
178 test_max_message_length_negative();
179 test_default_authority_type();
180 test_ssl_name_override_type();
181 test_ssl_name_override_failed();
182
183 grpc_shutdown();
184
185 return 0;
186}