17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2020 Advanced Micro Devices, Inc.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg/* A collection of unit tests for u_process.c */
257ec681f3Smrg
267ec681f3Smrg#include "util/detect_os.h"
277ec681f3Smrg#include "util/u_process.h"
287ec681f3Smrg#include <stdio.h>
297ec681f3Smrg#include <stdbool.h>
307ec681f3Smrg#include <string.h>
317ec681f3Smrg#include <limits.h>
327ec681f3Smrg#include <stdlib.h>
337ec681f3Smrg
347ec681f3Smrg#if DETECT_OS_WINDOWS && !defined(PATH_MAX)
357ec681f3Smrg#include <windows.h>
367ec681f3Smrg#define PATH_MAX MAX_PATH
377ec681f3Smrg#endif
387ec681f3Smrg
397ec681f3Smrgbool error = false;
407ec681f3Smrg
417ec681f3Smrgstatic void
427ec681f3Smrgexpect_equal_str(const char *expected, const char *actual, const char *test)
437ec681f3Smrg{
447ec681f3Smrg   if (strcmp(expected, actual)) {
457ec681f3Smrg      fprintf (stderr, "Error: Test '%s' failed:\n\t"
467ec681f3Smrg               "Expected=\"%s\", Actual=\"%s\"\n",
477ec681f3Smrg               test, expected, actual);
487ec681f3Smrg      error = true;
497ec681f3Smrg   }
507ec681f3Smrg}
517ec681f3Smrg
527ec681f3Smrgstatic void
537ec681f3Smrgtest_util_get_process_name (void)
547ec681f3Smrg{
557ec681f3Smrg#if DETECT_OS_WINDOWS
567ec681f3Smrg   const char *expected = "process_test.exe";
577ec681f3Smrg#else
587ec681f3Smrg   const char *expected = "process_test";
597ec681f3Smrg#endif
607ec681f3Smrg
617ec681f3Smrg   const char *name = util_get_process_name();
627ec681f3Smrg   expect_equal_str(expected, name, "util_get_process_name");
637ec681f3Smrg}
647ec681f3Smrg
657ec681f3Smrg/* This test gets the real path from Meson (BUILD_FULL_PATH env var),
667ec681f3Smrg * and compares it to the output of util_get_process_exec_path.
677ec681f3Smrg */
687ec681f3Smrgstatic void
697ec681f3Smrgtest_util_get_process_exec_path (void)
707ec681f3Smrg{
717ec681f3Smrg   char path[PATH_MAX];
727ec681f3Smrg   if (util_get_process_exec_path(path, PATH_MAX) == 0) {
737ec681f3Smrg      error = true;
747ec681f3Smrg      return;
757ec681f3Smrg   }
767ec681f3Smrg   char* build_path = getenv("BUILD_FULL_PATH");
777ec681f3Smrg   if (!build_path) {
787ec681f3Smrg      fprintf(stderr, "BUILD_FULL_PATH environment variable should be set\n");
797ec681f3Smrg      error = true;
807ec681f3Smrg      return;
817ec681f3Smrg   }
827ec681f3Smrg#ifdef __CYGWIN__
837ec681f3Smrg   int i = strlen(build_path) - 4;
847ec681f3Smrg   if ((i > 0) && (strcmp(&build_path[i], ".exe") == 0))
857ec681f3Smrg      build_path[i] = 0;
867ec681f3Smrg#endif
877ec681f3Smrg   expect_equal_str(build_path, path, "util_get_process_name");
887ec681f3Smrg}
897ec681f3Smrg
907ec681f3Smrgint
917ec681f3Smrgmain (void)
927ec681f3Smrg{
937ec681f3Smrg   test_util_get_process_name();
947ec681f3Smrg   test_util_get_process_exec_path();
957ec681f3Smrg
967ec681f3Smrg   return error ? 1 : 0;
977ec681f3Smrg}
98