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