| 1 |
/* |
| 2 |
* SPDX-License-Identifier: ISC |
| 3 |
* |
| 4 |
* Copyright (c) 2019-2020 Todd C. Miller <[email protected]> |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and distribute this software for any |
| 7 |
* purpose with or without fee is hereby granted, provided that the above |
| 8 |
* copyright notice and this permission notice appear in all copies. |
| 9 |
* |
| 10 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#ifdef HAVE_STDBOOL_H |
| 25 |
# include <stdbool.h> |
| 26 |
#else |
| 27 |
# include <compat/stdbool.h> |
| 28 |
#endif |
| 29 |
#include <time.h> |
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
#include <sudo_compat.h> |
| 33 |
#include <sudo_fatal.h> |
| 34 |
#include <sudo_iolog.h> |
| 35 |
#include <sudo_util.h> |
| 36 |
|
| 37 |
sudo_dso_public int main(int argc, char *argv[]); |
| 38 |
|
| 39 |
/* |
| 40 |
* Test that iolog_parse_host_port() works as expected. |
| 41 |
*/ |
| 42 |
|
| 43 |
struct host_port_test { |
| 44 |
const char *str; /* input string */ |
| 45 |
const char *host; /* parsed host */ |
| 46 |
const char *port; /* parsed port */ |
| 47 |
bool tls; /* parsed TLS flag */ |
| 48 |
const char *defport; /* default port */ |
| 49 |
const char *defport_tls; /* default port */ |
| 50 |
bool ret; /* return value */ |
| 51 |
}; |
| 52 |
|
| 53 |
static struct host_port_test test_data[] = { |
| 54 |
/* No TLS */ |
| 55 |
{ "xerxes", "xerxes", "12345", false, "12345", NULL, true }, |
| 56 |
{ "xerxes:12345", "xerxes", "12345", false, "67890", NULL, true }, |
| 57 |
{ "127.0.0.1", "127.0.0.1", "12345", false, "12345", NULL, true }, |
| 58 |
{ "127.0.0.1:12345", "127.0.0.1", "12345", false, "67890", NULL, true }, |
| 59 |
{ "[::1]", "::1", "12345", false, "12345", NULL, true }, |
| 60 |
{ "[::1]:12345", "::1", "12345", false, "67890", NULL, true }, |
| 61 |
|
| 62 |
/* With TLS */ |
| 63 |
{ "xerxes(tls)", "xerxes", "12345", true, "5678", "12345", true }, |
| 64 |
{ "xerxes:12345(tls)", "xerxes", "12345", true, "5678", "67890", true }, |
| 65 |
{ "127.0.0.1(tls)", "127.0.0.1", "12345", true, "5678", "12345", true }, |
| 66 |
{ "127.0.0.1:12345(tls)", "127.0.0.1", "12345", true, "5678", "67890", true }, |
| 67 |
{ "[::1](tls)", "::1", "12345", true, "5678", "12345", true }, |
| 68 |
{ "[::1]:12345(tls)", "::1", "12345", true, "5678", "67890", true }, |
| 69 |
|
| 70 |
/* Errors */ |
| 71 |
{ "xerxes:", NULL, NULL, false, "12345", NULL, false }, /* missing port */ |
| 72 |
{ "127.0.0.1:", NULL, NULL, false, "12345", NULL, false }, /* missing port */ |
| 73 |
{ "[::1:12345", NULL, NULL, false, "67890", NULL, false }, /* missing bracket */ |
| 74 |
{ "[::1]:", NULL, NULL, false, "12345", NULL, false }, /* missing port */ |
| 75 |
{ NULL } |
| 76 |
}; |
| 77 |
|
| 78 |
int |
| 79 |
main(int argc, char *argv[]) |
| 80 |
{ |
| 81 |
int ch, i, errors = 0, ntests = 0; |
| 82 |
char *host, *port, *copy = NULL; |
| 83 |
bool ret, tls; |
| 84 |
|
| 85 |
initprogname(argc > 0 ? argv[0] : "host_port_test"); |
| 86 |
|
| 87 |
while ((ch = getopt(argc, argv, "v")) != -1) { |
| 88 |
switch (ch) { |
| 89 |
case 'v': |
| 90 |
/* ignore */ |
| 91 |
break; |
| 92 |
default: |
| 93 |
fprintf(stderr, "usage: %s [-v]\n", getprogname()); |
| 94 |
return EXIT_FAILURE; |
| 95 |
} |
| 96 |
} |
| 97 |
argc -= optind; |
| 98 |
argv += optind; |
| 99 |
|
| 100 |
for (i = 0; test_data[i].str != NULL; i++) { |
| 101 |
host = port = NULL; |
| 102 |
tls = false; |
| 103 |
free(copy); |
| 104 |
if ((copy = strdup(test_data[i].str)) == NULL) |
| 105 |
sudo_fatal_nodebug(NULL); |
| 106 |
|
| 107 |
ntests++; |
| 108 |
ret = iolog_parse_host_port(copy, &host, &port, &tls, |
| 109 |
test_data[i].defport, test_data[i].defport_tls); |
| 110 |
if (ret != test_data[i].ret) { |
| 111 |
sudo_warnx_nodebug("test #%d: %s: returned %s, expected %s", |
| 112 |
ntests, test_data[i].str, ret ? "true" : "false", |
| 113 |
test_data[i].ret ? "true" : "false"); |
| 114 |
errors++; |
| 115 |
continue; |
| 116 |
} |
| 117 |
if (!ret) |
| 118 |
continue; |
| 119 |
|
| 120 |
if (host == NULL) { |
| 121 |
sudo_warnx_nodebug("test #%d: %s: NULL host", |
| 122 |
ntests, test_data[i].str); |
| 123 |
errors++; |
| 124 |
continue; |
| 125 |
} |
| 126 |
if (strcmp(host, test_data[i].host) != 0) { |
| 127 |
sudo_warnx_nodebug("test #%d: %s: bad host, expected %s, got %s", |
| 128 |
ntests, test_data[i].str, test_data[i].host, host); |
| 129 |
errors++; |
| 130 |
continue; |
| 131 |
} |
| 132 |
if (port == NULL) { |
| 133 |
sudo_warnx_nodebug("test #%d: %s: NULL port", |
| 134 |
ntests, test_data[i].str); |
| 135 |
errors++; |
| 136 |
continue; |
| 137 |
} |
| 138 |
if (strcmp(port, test_data[i].port) != 0) { |
| 139 |
sudo_warnx_nodebug("test #%d: %s: bad port, expected %s, got %s", |
| 140 |
ntests, test_data[i].str, test_data[i].port, port); |
| 141 |
errors++; |
| 142 |
continue; |
| 143 |
} |
| 144 |
if (tls != test_data[i].tls) { |
| 145 |
sudo_warnx_nodebug("test #%d: %s: bad tls, expected %s, got %s", |
| 146 |
ntests, test_data[i].str, test_data[i].tls ? "true" : "false", |
| 147 |
tls ? "true" : "false"); |
| 148 |
errors++; |
| 149 |
continue; |
| 150 |
} |
| 151 |
} |
| 152 |
free(copy); |
| 153 |
if (ntests != 0) { |
| 154 |
printf("%s: %d tests run, %d errors, %d%% success rate\n", |
| 155 |
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); |
| 156 |
} |
| 157 |
return errors; |
| 158 |
} |