xref: /illumos-gate/usr/src/test/os-tests/tests/secure_getenv/checker.c (revision 35e0963c135e57db23f111dfe9dbf1dae97a9fc3)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2025 Oxide Computer Company
14  */
15 
16 /*
17  * This goes through and attempts to verify whether or not the "SECRET"
18  * environment variable is readable or not with getenv(3C) and
19  * secure_getenv(3C). It should always work with the former. It will only work
20  * with the latter depending on how we've been invoked. We know whether or not
21  * our caller expects this to pass depending on whether or not we have the -s
22  * argument.
23  */
24 
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <err.h>
29 
30 int
31 main(int argc, char *argv[])
32 {
33 	bool sec;
34 	const char *desc;
35 	int ret = EXIT_SUCCESS;
36 
37 	if (argc != 2 && argc != 3) {
38 		errx(EXIT_FAILURE, "INTERNAL TEST FAILURE: found %d args, but "
39 		    "expected 2 or 3", argc);
40 	}
41 
42 	desc = argv[1];
43 
44 	if (argc == 2) {
45 		sec = false;
46 	} else if (strcmp(argv[2], "secure") != 0) {
47 		errx(EXIT_FAILURE, "INTERNAL TEST FAILURE: argv[2] should "
48 		    "either be missing or 'secure', found %s", argv[2]);
49 	} else {
50 		sec = true;
51 	}
52 
53 	if (getenv("SECRET") == NULL) {
54 		warnx("TEST FAILED: %s: getenv(\"SECRET\") failed "
55 		    "unexpectedly", desc);
56 		ret = EXIT_FAILURE;
57 	}
58 
59 	if (sec) {
60 		if (secure_getenv("SECRET") != NULL) {
61 			warnx("TEST FAILED: %s: secure_getenv() returned a "
62 			    "value unexpectedly", desc);
63 			ret = EXIT_FAILURE;
64 		} else {
65 			(void) printf("TEST PASSED: %s: secure_getenv() "
66 			    "correctly failed to return 'SECRET'\n", desc);
67 		}
68 	} else {
69 		if (secure_getenv("SECRET") == NULL) {
70 			warnx("TEST FAILED: %s: secure_getenv() failed to "
71 			    "return a value unexpectedly", desc);
72 			ret = EXIT_FAILURE;
73 		} else {
74 			(void) printf("TEST PASSED: %s: secure_getenv() "
75 			    "correctly returned 'SECRET'\n", desc);
76 		}
77 	}
78 
79 	return (ret);
80 }
81