util/u_process: add util_get_process_exec_path
[mesa.git] / src / util / process_test.c
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 bool error = false;
35
36 static void
37 expect_equal_str(const char *expected, const char *actual, const char *test)
38 {
39 if (strcmp(expected, actual)) {
40 fprintf (stderr, "Error: Test '%s' failed:\n\t"
41 "Expected=\"%s\", Actual=\"%s\"\n",
42 test, expected, actual);
43 error = true;
44 }
45 }
46
47 static void
48 test_util_get_process_name (void)
49 {
50 #if !DETECT_OS_WINDOWS
51 const char* name = util_get_process_name();
52 expect_equal_str("process_test", name, "util_get_process_name");
53 #endif
54 }
55
56 /* This test gets the real path from Meson (BUILD_FULL_PATH env var),
57 * and compares it to the output of util_get_process_exec_path.
58 */
59 static void
60 test_util_get_process_exec_path (void)
61 {
62 char path[PATH_MAX];
63 if (util_get_process_exec_path(path, PATH_MAX) == 0) {
64 error = true;
65 return;
66 }
67 char* build_path = getenv("BUILD_FULL_PATH");
68 if (!build_path) {
69 fprintf(stderr, "BUILD_FULL_PATH environment variable should be set\n");
70 error = true;
71 return;
72 }
73 expect_equal_str(build_path, path, "util_get_process_name");
74 }
75
76 int
77 main (void)
78 {
79 test_util_get_process_name();
80 test_util_get_process_exec_path();
81
82 return error ? 1 : 0;
83 }