"Fossies" - the Fresh Open Source Software Archive

Member "sudo-1.9.11p3/plugins/sudoers/regress/parser/check_base64.c" (12 Jun 2022, 3300 Bytes) of package /linux/misc/sudo-1.9.11p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "check_base64.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * SPDX-License-Identifier: ISC
    3  *
    4  * Copyright (c) 2013-2018 Todd C. Miller <Todd.Miller@sudo.ws>
    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 
   25 #define SUDO_ERROR_WRAP 0
   26 
   27 #include "sudo_compat.h"
   28 #include "sudo_util.h"
   29 
   30 /* From parse.h */
   31 extern size_t base64_decode(const char *str, unsigned char *dst, size_t dsize);
   32 extern size_t base64_encode(const unsigned char *in, size_t in_len, char *out, size_t out_len);
   33 
   34 sudo_dso_public int main(int argc, char *argv[]);
   35 
   36 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 };
   37 
   38 struct base64_test {
   39     const char *ascii;
   40     const char *encoded;
   41 } test_strings[] = {
   42     {
   43     (char *)bstring1,
   44     "6riice9nwc0N2aaqqCR3Kvxvdjcb7Z4akF/PvA=="
   45     },
   46     {
   47     "any carnal pleasure.",
   48     "YW55IGNhcm5hbCBwbGVhc3VyZS4="
   49     },
   50     {
   51     "any carnal pleasure",
   52     "YW55IGNhcm5hbCBwbGVhc3VyZQ=="
   53     },
   54     {
   55     "any carnal pleasur",
   56     "YW55IGNhcm5hbCBwbGVhc3Vy"
   57     },
   58     {
   59     "any carnal pleasu",
   60     "YW55IGNhcm5hbCBwbGVhc3U="
   61     },
   62     {
   63     "any carnal pleas",
   64     "YW55IGNhcm5hbCBwbGVhcw=="
   65     }
   66 };
   67 
   68 int
   69 main(int argc, char *argv[])
   70 {
   71     int ntests = nitems(test_strings);
   72     int i, errors = 0;
   73     unsigned char buf[64];
   74     size_t len;
   75 
   76     initprogname(argc > 0 ? argv[0] : "check_base64");
   77 
   78     for (i = 0; i < ntests; i++) {
   79     /* Test decode. */
   80     len = base64_decode(test_strings[i].encoded, buf, sizeof(buf));
   81     if (len == (size_t)-1) {
   82         fprintf(stderr, "check_base64: failed to decode %s\n",
   83         test_strings[i].encoded);
   84         errors++;
   85     } else {
   86         buf[len] = '\0';
   87         if (strcmp(test_strings[i].ascii, (char *)buf) != 0) {
   88         fprintf(stderr, "check_base64: expected %s, got %s\n",
   89             test_strings[i].ascii, buf);
   90         errors++;
   91         }
   92     }
   93 
   94     /* Test encode. */
   95     len = base64_encode((unsigned char *)test_strings[i].ascii,
   96         strlen(test_strings[i].ascii), (char *)buf, sizeof(buf));
   97     if (len == (size_t)-1) {
   98         fprintf(stderr, "check_base64: failed to encode %s\n",
   99         test_strings[i].ascii);
  100         errors++;
  101     } else {
  102         if (strcmp(test_strings[i].encoded, (char *)buf) != 0) {
  103         fprintf(stderr, "check_base64: expected %s, got %s\n",
  104             test_strings[i].encoded, buf);
  105         errors++;
  106         }
  107     }
  108     }
  109     ntests *= 2;    /* we test in both directions */
  110 
  111     if (ntests != 0) {
  112     printf("%s: %d tests run, %d errors, %d%% success rate\n",
  113         getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
  114     }
  115 
  116     exit(errors);
  117 }