1 /*
2 * Copyright © 2020 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /* A collection of unit tests for u_process.c */
25
26 #include "util/detect_os.h"
27 #include "util/u_process.h"
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <stdlib.h>
33
34 #if DETECT_OS_WINDOWS && !defined(PATH_MAX)
35 #include <windows.h>
36 #define PATH_MAX MAX_PATH
37 #endif
38
39 static bool error = false;
40
41 static void
expect_equal_str(const char * expected,const char * actual,const char * test)42 expect_equal_str(const char *expected, const char *actual, const char *test)
43 {
44 if (strcmp(expected, actual)) {
45 fprintf (stderr, "Error: Test '%s' failed:\n\t"
46 "Expected=\"%s\", Actual=\"%s\"\n",
47 test, expected, actual);
48 error = true;
49 }
50 }
51
52 static void
test_util_get_process_name(void)53 test_util_get_process_name (void)
54 {
55 #if DETECT_OS_WINDOWS
56 const char *expected = "process_test.exe";
57 #else
58 const char *expected = "process_test";
59 #endif
60
61 const char *name_override = getenv("MESA_PROCESS_NAME");
62 if (name_override)
63 expected = name_override;
64
65 const char *name = util_get_process_name();
66 expect_equal_str(expected, name, "util_get_process_name");
67 }
68
posixify_path(char * path)69 static void posixify_path(char *path) {
70 /* Always using posix separator '/' to check path equal */
71 char *p = path;
72 for (; *p != '\0'; p += 1) {
73 if (*p == '\\') {
74 *p = '/';
75 }
76 }
77 }
78
79 /* This test gets the real path from Meson (BUILD_FULL_PATH env var),
80 * and compares it to the output of util_get_process_exec_path.
81 */
82 static void
test_util_get_process_exec_path(void)83 test_util_get_process_exec_path (void)
84 {
85 char path[PATH_MAX];
86 if (util_get_process_exec_path(path, PATH_MAX) == 0) {
87 error = true;
88 return;
89 }
90 posixify_path(path);
91 char* build_path = getenv("BUILD_FULL_PATH");
92 if (!build_path) {
93 fprintf(stderr, "BUILD_FULL_PATH environment variable should be set\n");
94 error = true;
95 return;
96 }
97 build_path = strdup(build_path);
98 posixify_path(build_path);
99 #ifdef __CYGWIN__
100 int i = strlen(build_path) - 4;
101 if ((i > 0) && (strcmp(&build_path[i], ".exe") == 0))
102 build_path[i] = 0;
103 #endif
104 expect_equal_str(build_path, path, "test_util_get_process_exec_path");
105 free(build_path);
106 }
107
108 int
main(void)109 main (void)
110 {
111 test_util_get_process_name();
112 test_util_get_process_exec_path();
113
114 return error ? 1 : 0;
115 }
116