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