1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 *
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
9 *
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
22 *
23 * Authors: Colin Walters <walters@verbum.org>
24 */
25
26 #include "config.h"
27 #include <glib.h>
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 static void
34 test_platform_argv0 (void)
35 {
36 GOptionContext *context;
37 gboolean arg;
38 GOptionEntry entries [] =
39 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
40 G_OPTION_ENTRY_NULL };
41 const gchar * const expected_prgnames[] =
42 {
43 "option-argv0",
44 #ifdef G_OS_WIN32
45 "option-argv0.exe",
46 #endif
47 NULL,
48 };
49 gboolean retval;
50 gboolean fatal_errors = TRUE;
51
52 /* This test must pass on platforms where platform_get_argv0()
53 * is implemented. At the moment that means Linux/Cygwin,
54 * (which uses /proc/self/cmdline) or OpenBSD (which uses
55 * sysctl and KERN_PROC_ARGS) or Windows (which uses
56 * GetCommandlineW ()). On other platforms the test
57 * is not expected to pass, but we'd still want to know
58 * how it does (the test code itself doesn't use any platform-specific
59 * functionality, the difference is internal to glib, so it's quite
60 * possible to run this test everywhere - it just won't pass on some
61 * platforms). Make errors non-fatal on these other platforms,
62 * to prevent them from crashing hard on failed assertions,
63 * and make them call g_test_skip() instead.
64 */
65 #if !defined HAVE_PROC_SELF_CMDLINE && \
66 !defined __OpenBSD__ && \
67 !defined __linux && \
68 !defined G_OS_WIN32
69 fatal_errors = FALSE;
70 #endif
71
72 context = g_option_context_new (NULL);
73 g_option_context_add_main_entries (context, entries, NULL);
74
75 retval = g_option_context_parse (context, NULL, NULL, NULL);
76 if (fatal_errors)
77 {
78 g_assert_true (retval);
79 g_assert_true (g_strv_contains (expected_prgnames, g_get_prgname ()));
80 }
81 else
82 {
83 gboolean failed = FALSE;
84
85 if (!retval)
86 {
87 g_print ("g_option_context_parse() failed\n");
88 failed = TRUE;
89 }
90 else if (!g_strv_contains (expected_prgnames, g_get_prgname ()))
91 {
92 g_print ("program name `%s' is neither `option-argv0', nor `lt-option-argv0'\n", g_get_prgname());
93 failed = TRUE;
94 }
95 else
96 g_print ("The test unexpectedly passed\n");
97 if (failed)
98 g_test_skip ("platform_get_argv0() is not implemented [correctly?] on this platform");
99 }
100
101 g_option_context_free (context);
102 }
103
104 int
105 main (int argc,
106 char *argv[])
107 {
108 g_test_init (&argc, &argv, "no_g_set_prgname", NULL);
109
110 g_test_add_func ("/option/argv0", test_platform_argv0);
111
112 return g_test_run ();
113 }