D-Bus 1.6.12

dbus-sysdeps-util.c

00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API
00003  * 
00004  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat, Inc.
00005  * Copyright (C) 2003 CodeFactory AB
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-sysdeps.h"
00027 #include "dbus-internals.h"
00028 #include "dbus-string.h"
00029 #include "dbus-test.h"
00030 
00031 #include <stdlib.h>
00032 
00033 #ifdef DBUS_WIN
00034   /* do nothing, it's in stdlib.h */
00035 #elif (defined __APPLE__)
00036 # include <crt_externs.h>
00037 # define environ (*_NSGetEnviron())
00038 #else
00039 extern char **environ;
00040 #endif
00041 
00048 char **
00049 _dbus_get_environment (void)
00050 {
00051   int i, length;
00052   char **environment;
00053 
00054   _dbus_assert (environ != NULL);
00055 
00056   for (length = 0; environ[length] != NULL; length++);
00057 
00058   /* Add one for NULL */
00059   length++;
00060 
00061   environment = dbus_new0 (char *, length);
00062 
00063   if (environment == NULL)
00064     return NULL;
00065 
00066   for (i = 0; environ[i] != NULL; i++)
00067     {
00068       environment[i] = _dbus_strdup (environ[i]);
00069 
00070       if (environment[i] == NULL)
00071         break;
00072     }
00073 
00074   if (environ[i] != NULL)
00075     {
00076       dbus_free_string_array (environment);
00077       environment = NULL;
00078     }
00079 
00080   return environment;
00081 }
00082 
00083 #ifdef DBUS_BUILD_TESTS
00084 static void
00085 check_dirname (const char *filename,
00086                const char *dirname)
00087 {
00088   DBusString f, d;
00089   
00090   _dbus_string_init_const (&f, filename);
00091 
00092   if (!_dbus_string_init (&d))
00093     _dbus_assert_not_reached ("no memory");
00094 
00095   if (!_dbus_string_get_dirname (&f, &d))
00096     _dbus_assert_not_reached ("no memory");
00097 
00098   if (!_dbus_string_equal_c_str (&d, dirname))
00099     {
00100       _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n",
00101                   filename,
00102                   _dbus_string_get_const_data (&d),
00103                   dirname);
00104       exit (1);
00105     }
00106 
00107   _dbus_string_free (&d);
00108 }
00109 
00110 static void
00111 check_path_absolute (const char *path,
00112                      dbus_bool_t expected)
00113 {
00114   DBusString p;
00115 
00116   _dbus_string_init_const (&p, path);
00117 
00118   if (_dbus_path_is_absolute (&p) != expected)
00119     {
00120       _dbus_warn ("For path \"%s\" expected absolute = %d got %d\n",
00121                   path, expected, _dbus_path_is_absolute (&p));
00122       exit (1);
00123     }
00124 }
00125 
00131 dbus_bool_t
00132 _dbus_sysdeps_test (void)
00133 {
00134 #ifdef DBUS_WIN
00135   check_dirname ("foo\\bar", "foo");
00136   check_dirname ("foo\\\\bar", "foo");
00137   check_dirname ("foo/\\/bar", "foo");
00138   check_dirname ("foo\\bar/", "foo");
00139   check_dirname ("foo//bar\\", "foo");
00140   check_dirname ("foo\\bar/", "foo");
00141   check_dirname ("foo/bar\\\\", "foo");
00142   check_dirname ("\\foo", "\\");
00143   check_dirname ("\\\\foo", "\\");
00144   check_dirname ("\\", "\\");
00145   check_dirname ("\\\\", "\\");
00146   check_dirname ("\\/", "\\");
00147   check_dirname ("/\\/", "/");
00148   check_dirname ("c:\\foo\\bar", "c:\\foo");
00149   check_dirname ("c:\\foo", "c:\\");
00150   check_dirname ("c:/foo", "c:/");
00151   check_dirname ("c:\\", "c:\\");
00152   check_dirname ("c:/", "c:/");
00153   check_dirname ("", ".");  
00154 #else  
00155   check_dirname ("foo", ".");
00156   check_dirname ("foo/bar", "foo");
00157   check_dirname ("foo//bar", "foo");
00158   check_dirname ("foo///bar", "foo");
00159   check_dirname ("foo/bar/", "foo");
00160   check_dirname ("foo//bar/", "foo");
00161   check_dirname ("foo///bar/", "foo");
00162   check_dirname ("foo/bar//", "foo");
00163   check_dirname ("foo//bar////", "foo");
00164   check_dirname ("foo///bar///////", "foo");
00165   check_dirname ("/foo", "/");
00166   check_dirname ("////foo", "/");
00167   check_dirname ("/foo/bar", "/foo");
00168   check_dirname ("/foo//bar", "/foo");
00169   check_dirname ("/foo///bar", "/foo");
00170   check_dirname ("/", "/");
00171   check_dirname ("///", "/");
00172   check_dirname ("", ".");  
00173 #endif
00174 
00175 #ifdef DBUS_WIN
00176   check_path_absolute ("c:/", TRUE);
00177   check_path_absolute ("c:/foo", TRUE);
00178   check_path_absolute ("", FALSE);
00179   check_path_absolute ("foo", FALSE);
00180   check_path_absolute ("foo/bar", FALSE);
00181   check_path_absolute ("", FALSE);
00182   check_path_absolute ("foo\\bar", FALSE);
00183   check_path_absolute ("c:\\", TRUE);
00184   check_path_absolute ("c:\\foo", TRUE);
00185   check_path_absolute ("c:", TRUE);
00186   check_path_absolute ("c:\\foo\\bar", TRUE);
00187   check_path_absolute ("\\", TRUE);
00188   check_path_absolute ("/", TRUE);
00189 #else  
00190   check_path_absolute ("/", TRUE);
00191   check_path_absolute ("/foo", TRUE);
00192   check_path_absolute ("", FALSE);
00193   check_path_absolute ("foo", FALSE);
00194   check_path_absolute ("foo/bar", FALSE);
00195 #endif
00196   
00197   return TRUE;
00198 }
00199 #endif /* DBUS_BUILD_TESTS */