"Fossies" - the Fresh Open Source Software Archive

Member "sudo-1.9.11p3/plugins/sudoers/regress/editor/check_editor.c" (12 Jun 2022, 4040 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_editor.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * SPDX-License-Identifier: ISC
    3  *
    4  * Copyright (c) 2021 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 "sudoers.h"
   28 #include <def_data.c>
   29 
   30 /* Note hard-coded array lengths. */
   31 struct test_data {
   32     char *editor_var;
   33     int nfiles;
   34     char *files[4];
   35     char *editor_path;
   36     int edit_argc;
   37     char *edit_argv[10];
   38 } test_data[] = {
   39     {
   40     /* Bug #942 */
   41     "SUDO_EDITOR=sh -c \"vi \\$1\"",
   42     1,
   43     { "/etc/motd", NULL },
   44     "/usr/bin/sh",
   45     5,
   46     { "sh", "-c", "vi $1", "--", "/etc/motd", NULL }
   47     },
   48     {
   49     /* Try connecting to the emacs server, falling back on plain emacs. */
   50     "VISUAL=sh -c \"emacsclient -a emacs -n \\\"\\$@\\\" || emacs \\\"\\$@\\\"\"",
   51     1,
   52     { "/etc/motd", NULL },
   53     "/usr/bin/sh",
   54     5,
   55     { "sh", "-c", "emacsclient -a emacs -n \"$@\" || emacs \"$@\"", "--", "/etc/motd", NULL }
   56     },
   57     {
   58     /* GitHub issue #99 */
   59     "EDITOR=/usr/bin/vi\\",
   60     1,
   61     { "/etc/hosts", "/bogus/file", NULL },
   62     "/usr/bin/vi\\",
   63     3,
   64     { "/usr/bin/vi\\", "--", "/etc/hosts", "/bogus/file", NULL }
   65     },
   66     { NULL }
   67 };
   68 
   69 sudo_dso_public int main(int argc, char *argv[]);
   70 
   71 /* STUB */
   72 int
   73 find_path(const char *infile, char **outfile, struct stat *sbp,
   74     const char *path, const char *runchroot, int ignore_dot,
   75     char * const *allowlist)
   76 {
   77     if (infile[0] == '/') {
   78     *outfile = strdup(infile);
   79     } else {
   80     if (asprintf(outfile, "/usr/bin/%s", infile) == -1)
   81         *outfile = NULL;
   82     }
   83     if (*outfile == NULL)
   84     return NOT_FOUND_ERROR;
   85     return FOUND;
   86 }
   87 
   88 int
   89 main(int argc, char *argv[])
   90 {
   91     struct test_data *data;
   92     int ntests = 0, errors = 0;
   93 
   94     initprogname(argc > 0 ? argv[0] : "check_editor");
   95 
   96     for (data = test_data; data->editor_var != NULL; data++) {
   97     const char *env_editor = NULL;
   98     char *cp, *editor_path, **edit_argv = NULL;
   99     int i, edit_argc = 0;
  100 
  101     /* clear existing editor environment vars */
  102     putenv("VISUAL=");
  103     putenv("EDITOR=");
  104     putenv("SUDO_EDITOR=");
  105 
  106     putenv(data->editor_var);
  107     editor_path = find_editor(data->nfiles, data->files, &edit_argc,
  108         &edit_argv, NULL, &env_editor);
  109     ntests++;
  110     if (strcmp(editor_path, data->editor_path) != 0) {
  111         sudo_warnx("test %d: editor_path: expected \"%s\", got \"%s\"",
  112         ntests, data->editor_path, editor_path);
  113         errors++;
  114     }
  115     ntests++;
  116     cp = strchr(data->editor_var, '=') + 1;
  117     if (strcmp(env_editor, cp) != 0) {
  118         sudo_warnx("test %d: env_editor: expected \"%s\", got \"%s\"",
  119         ntests, cp, env_editor ? env_editor : "(NULL)");
  120         errors++;
  121     }
  122     ntests++;
  123     if (edit_argc != data->edit_argc) {
  124         sudo_warnx("test %d: edit_argc: expected %d, got %d",
  125         ntests, data->edit_argc, edit_argc);
  126         errors++;
  127     } else {
  128         ntests++;
  129         for (i = 0; i < edit_argc; i++) {
  130         if (strcmp(edit_argv[i], data->edit_argv[i]) != 0) {
  131             sudo_warnx("test %d: edit_argv[%d]: expected \"%s\", got \"%s\"",
  132             ntests, i, data->edit_argv[i], edit_argv[i]);
  133             errors++;
  134             break;
  135         }
  136         }
  137     }
  138 
  139     free(editor_path);
  140     edit_argc -= data->nfiles + 1;
  141     for (i = 0; i < edit_argc; i++) {
  142         free(edit_argv[i]);
  143     }
  144     free(edit_argv);
  145     }
  146 
  147     if (ntests != 0) {
  148     printf("%s: %d tests run, %d errors, %d%% success rate\n",
  149         getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
  150     }
  151 
  152     exit(errors);
  153 }