2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 18:08:23 +00:00
sudo/plugins/sudoers/regress/parser/check_base64.c

115 lines
3.2 KiB
C
Raw Normal View History

2013-04-17 12:54:33 -04:00
/*
2019-04-29 07:21:51 -06:00
* SPDX-License-Identifier: ISC
*
2018-05-19 19:03:47 -06:00
* Copyright (c) 2013-2018 Todd C. Miller <Todd.Miller@sudo.ws>
2013-04-17 12:54:33 -04:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-04-17 12:54:33 -04:00
#define SUDO_ERROR_WRAP 0
2014-07-22 14:25:16 -06:00
#include "sudo_compat.h"
2017-11-28 09:06:44 -07:00
#include "sudo_util.h"
2013-04-17 12:54:33 -04:00
2018-05-19 19:03:47 -06:00
/* From parse.h */
2013-04-17 12:54:33 -04:00
extern size_t base64_decode(const char *str, unsigned char *dst, size_t dsize);
2018-05-19 19:03:47 -06:00
extern size_t base64_encode(const unsigned char *in, size_t in_len, char *out, size_t out_len);
2013-04-17 12:54:33 -04:00
sudo_dso_public int main(int argc, char *argv[]);
2013-04-17 12:54:33 -04:00
2018-08-30 14:06:18 -06:00
static unsigned char bstring1[] = { 0xea, 0xb8, 0xa2, 0x71, 0xef, 0x67, 0xc1, 0xcd, 0x0d, 0xd9, 0xa6, 0xaa, 0xa8, 0x24, 0x77, 0x2a, 0xfc, 0x6f, 0x76, 0x37, 0x1b, 0xed, 0x9e, 0x1a, 0x90, 0x5f, 0xcf, 0xbc, 0x00 };
2013-04-17 12:54:33 -04:00
struct base64_test {
const char *ascii;
const char *encoded;
} test_strings[] = {
{
2018-08-30 14:06:18 -06:00
(char *)bstring1,
"6riice9nwc0N2aaqqCR3Kvxvdjcb7Z4akF/PvA=="
},
2013-04-17 12:54:33 -04:00
{
"any carnal pleasure.",
"YW55IGNhcm5hbCBwbGVhc3VyZS4="
},
{
"any carnal pleasure",
"YW55IGNhcm5hbCBwbGVhc3VyZQ=="
},
{
"any carnal pleasur",
"YW55IGNhcm5hbCBwbGVhc3Vy"
},
{
"any carnal pleasu",
"YW55IGNhcm5hbCBwbGVhc3U="
},
{
"any carnal pleas",
"YW55IGNhcm5hbCBwbGVhcw=="
}
};
int
main(int argc, char *argv[])
{
2018-05-19 19:03:47 -06:00
int ntests = nitems(test_strings);
2013-04-17 12:54:33 -04:00
int i, errors = 0;
2018-05-19 19:03:47 -06:00
unsigned char buf[64];
2013-04-17 12:54:33 -04:00
size_t len;
2017-11-28 09:06:44 -07:00
initprogname(argc > 0 ? argv[0] : "check_base64");
2013-04-17 12:54:33 -04:00
for (i = 0; i < ntests; i++) {
2018-05-19 19:03:47 -06:00
/* Test decode. */
2013-04-17 12:54:33 -04:00
len = base64_decode(test_strings[i].encoded, buf, sizeof(buf));
2018-05-19 19:03:47 -06:00
if (len == (size_t)-1) {
fprintf(stderr, "check_base64: failed to decode %s\n",
test_strings[i].encoded);
2013-04-17 12:54:33 -04:00
errors++;
2018-05-19 19:03:47 -06:00
} else {
buf[len] = '\0';
if (strcmp(test_strings[i].ascii, (char *)buf) != 0) {
fprintf(stderr, "check_base64: expected %s, got %s\n",
test_strings[i].ascii, buf);
errors++;
}
}
/* Test encode. */
2018-05-20 07:42:54 -06:00
len = base64_encode((unsigned char *)test_strings[i].ascii,
strlen(test_strings[i].ascii), (char *)buf, sizeof(buf));
2018-05-19 19:03:47 -06:00
if (len == (size_t)-1) {
fprintf(stderr, "check_base64: failed to encode %s\n",
test_strings[i].ascii);
errors++;
} else {
if (strcmp(test_strings[i].encoded, (char *)buf) != 0) {
fprintf(stderr, "check_base64: expected %s, got %s\n",
test_strings[i].encoded, buf);
errors++;
}
2013-04-17 12:54:33 -04:00
}
}
2018-05-19 19:03:47 -06:00
ntests *= 2; /* we test in both directions */
2013-04-17 12:54:33 -04:00
printf("check_base64: %d tests run, %d errors, %d%% success rate\n",
ntests, errors, (ntests - errors) * 100 / ntests);
exit(errors);
}