Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API
3  * 
4  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #include <config.h>
26 #include "dbus-sysdeps.h"
27 #include "dbus-internals.h"
28 #include "dbus-string.h"
29 #include "dbus-test.h"
30
31 #include <stdlib.h>
32
33 #ifdef DBUS_WIN
34   /* do nothing, it's in stdlib.h */
35 #elif (defined __APPLE__)
36 # include <crt_externs.h>
37 # define environ (*_NSGetEnviron())
38 #else
39 extern char **environ;
40 #endif
41
42 /**
43  * Gets a #NULL-terminated list of key=value pairs from the
44  * environment. Use dbus_free_string_array to free it.
45  *
46  * @returns the environment or #NULL on OOM
47  */
48 char **
49 _dbus_get_environment (void)
50 {
51   int i, length;
52   char **environment;
53
54   _dbus_assert (environ != NULL);
55
56   for (length = 0; environ[length] != NULL; length++);
57
58   /* Add one for NULL */
59   length++;
60
61   environment = dbus_new0 (char *, length);
62
63   if (environment == NULL)
64     return NULL;
65
66   for (i = 0; environ[i] != NULL; i++)
67     {
68       environment[i] = _dbus_strdup (environ[i]);
69
70       if (environment[i] == NULL)
71         break;
72     }
73
74   if (environ[i] != NULL)
75     {
76       dbus_free_string_array (environment);
77       environment = NULL;
78     }
79
80   return environment;
81 }
82
83 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
84 static void
85 check_dirname (const char *filename,
86                const char *dirname)
87 {
88   DBusString f, d;
89   
90   _dbus_string_init_const (&f, filename);
91
92   if (!_dbus_string_init (&d))
93     _dbus_assert_not_reached ("no memory");
94
95   if (!_dbus_string_get_dirname (&f, &d))
96     _dbus_assert_not_reached ("no memory");
97
98   if (!_dbus_string_equal_c_str (&d, dirname))
99     {
100       _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n",
101                   filename,
102                   _dbus_string_get_const_data (&d),
103                   dirname);
104       exit (1);
105     }
106
107   _dbus_string_free (&d);
108 }
109
110 static void
111 check_path_absolute (const char *path,
112                      dbus_bool_t expected)
113 {
114   DBusString p;
115
116   _dbus_string_init_const (&p, path);
117
118   if (_dbus_path_is_absolute (&p) != expected)
119     {
120       _dbus_warn ("For path \"%s\" expected absolute = %d got %d\n",
121                   path, expected, _dbus_path_is_absolute (&p));
122       exit (1);
123     }
124 }
125
126 /**
127  * Unit test for dbus-sysdeps.c.
128  * 
129  * @returns #TRUE on success.
130  */
131 dbus_bool_t
132 _dbus_sysdeps_test (void)
133 {
134 #ifdef DBUS_WIN
135   check_dirname ("foo\\bar", "foo");
136   check_dirname ("foo\\\\bar", "foo");
137   check_dirname ("foo/\\/bar", "foo");
138   check_dirname ("foo\\bar/", "foo");
139   check_dirname ("foo//bar\\", "foo");
140   check_dirname ("foo\\bar/", "foo");
141   check_dirname ("foo/bar\\\\", "foo");
142   check_dirname ("\\foo", "\\");
143   check_dirname ("\\\\foo", "\\");
144   check_dirname ("\\", "\\");
145   check_dirname ("\\\\", "\\");
146   check_dirname ("\\/", "\\");
147   check_dirname ("/\\/", "/");
148   check_dirname ("c:\\foo\\bar", "c:\\foo");
149   check_dirname ("c:\\foo", "c:\\");
150   check_dirname ("c:/foo", "c:/");
151   check_dirname ("c:\\", "c:\\");
152   check_dirname ("c:/", "c:/");
153   check_dirname ("", ".");  
154 #else  
155   check_dirname ("foo", ".");
156   check_dirname ("foo/bar", "foo");
157   check_dirname ("foo//bar", "foo");
158   check_dirname ("foo///bar", "foo");
159   check_dirname ("foo/bar/", "foo");
160   check_dirname ("foo//bar/", "foo");
161   check_dirname ("foo///bar/", "foo");
162   check_dirname ("foo/bar//", "foo");
163   check_dirname ("foo//bar////", "foo");
164   check_dirname ("foo///bar///////", "foo");
165   check_dirname ("/foo", "/");
166   check_dirname ("////foo", "/");
167   check_dirname ("/foo/bar", "/foo");
168   check_dirname ("/foo//bar", "/foo");
169   check_dirname ("/foo///bar", "/foo");
170   check_dirname ("/", "/");
171   check_dirname ("///", "/");
172   check_dirname ("", ".");  
173 #endif
174
175 #ifdef DBUS_WIN
176   check_path_absolute ("c:/", TRUE);
177   check_path_absolute ("c:/foo", TRUE);
178   check_path_absolute ("", FALSE);
179   check_path_absolute ("foo", FALSE);
180   check_path_absolute ("foo/bar", FALSE);
181   check_path_absolute ("", FALSE);
182   check_path_absolute ("foo\\bar", FALSE);
183   check_path_absolute ("c:\\", TRUE);
184   check_path_absolute ("c:\\foo", TRUE);
185   check_path_absolute ("c:", TRUE);
186   check_path_absolute ("c:\\foo\\bar", TRUE);
187   check_path_absolute ("\\", TRUE);
188   check_path_absolute ("/", TRUE);
189 #else  
190   check_path_absolute ("/", TRUE);
191   check_path_absolute ("/foo", TRUE);
192   check_path_absolute ("", FALSE);
193   check_path_absolute ("foo", FALSE);
194   check_path_absolute ("foo/bar", FALSE);
195 #endif
196   
197   return TRUE;
198 }
199 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */