dbus-sysdeps-util.c 5.29 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
/* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 
 * Copyright (C) 2002, 2003, 2004, 2005  Red Hat, Inc.
 * Copyright (C) 2003 CodeFactory AB
 *
 * Licensed under the Academic Free License version 2.1
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * 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 27 28 29
#include "dbus-sysdeps.h"
#include "dbus-internals.h"
#include "dbus-string.h"
#include "dbus-test.h"
30
#include <dbus/dbus-test-tap.h>
31 32

#include <stdlib.h>
33 34 35 36 37 38

#ifdef DBUS_WIN
  /* do nothing, it's in stdlib.h */
#elif (defined __APPLE__)
# include <crt_externs.h>
# define environ (*_NSGetEnviron())
39 40
#elif HAVE_DECL_ENVIRON && defined(HAVE_UNISTD_H)
# include <unistd.h>
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#else
extern char **environ;
#endif

/**
 * Gets a #NULL-terminated list of key=value pairs from the
 * environment. Use dbus_free_string_array to free it.
 *
 * @returns the environment or #NULL on OOM
 */
char **
_dbus_get_environment (void)
{
  int i, length;
  char **environment;

  _dbus_assert (environ != NULL);

  for (length = 0; environ[length] != NULL; length++);

  /* Add one for NULL */
  length++;

  environment = dbus_new0 (char *, length);

  if (environment == NULL)
    return NULL;

  for (i = 0; environ[i] != NULL; i++)
    {
      environment[i] = _dbus_strdup (environ[i]);

      if (environment[i] == NULL)
        break;
    }

  if (environ[i] != NULL)
    {
      dbus_free_string_array (environment);
      environment = NULL;
    }

  return environment;
}

86
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
87 88 89 90 91 92 93 94 95
static void
check_dirname (const char *filename,
               const char *dirname)
{
  DBusString f, d;
  
  _dbus_string_init_const (&f, filename);

  if (!_dbus_string_init (&d))
96
    _dbus_test_fatal ("no memory");
97 98

  if (!_dbus_string_get_dirname (&f, &d))
99
    _dbus_test_fatal ("no memory");
100 101 102

  if (!_dbus_string_equal_c_str (&d, dirname))
    {
103
      _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"",
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                  filename,
                  _dbus_string_get_const_data (&d),
                  dirname);
      exit (1);
    }

  _dbus_string_free (&d);
}

static void
check_path_absolute (const char *path,
                     dbus_bool_t expected)
{
  DBusString p;

  _dbus_string_init_const (&p, path);

  if (_dbus_path_is_absolute (&p) != expected)
    {
123
      _dbus_warn ("For path \"%s\" expected absolute = %d got %d",
124 125 126 127 128 129 130 131 132 133 134 135 136
                  path, expected, _dbus_path_is_absolute (&p));
      exit (1);
    }
}

/**
 * Unit test for dbus-sysdeps.c.
 * 
 * @returns #TRUE on success.
 */
dbus_bool_t
_dbus_sysdeps_test (void)
{
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#ifdef DBUS_WIN
  check_dirname ("foo\\bar", "foo");
  check_dirname ("foo\\\\bar", "foo");
  check_dirname ("foo/\\/bar", "foo");
  check_dirname ("foo\\bar/", "foo");
  check_dirname ("foo//bar\\", "foo");
  check_dirname ("foo\\bar/", "foo");
  check_dirname ("foo/bar\\\\", "foo");
  check_dirname ("\\foo", "\\");
  check_dirname ("\\\\foo", "\\");
  check_dirname ("\\", "\\");
  check_dirname ("\\\\", "\\");
  check_dirname ("\\/", "\\");
  check_dirname ("/\\/", "/");
  check_dirname ("c:\\foo\\bar", "c:\\foo");
  check_dirname ("c:\\foo", "c:\\");
  check_dirname ("c:/foo", "c:/");
  check_dirname ("c:\\", "c:\\");
  check_dirname ("c:/", "c:/");
  check_dirname ("", ".");  
#else  
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  check_dirname ("foo", ".");
  check_dirname ("foo/bar", "foo");
  check_dirname ("foo//bar", "foo");
  check_dirname ("foo///bar", "foo");
  check_dirname ("foo/bar/", "foo");
  check_dirname ("foo//bar/", "foo");
  check_dirname ("foo///bar/", "foo");
  check_dirname ("foo/bar//", "foo");
  check_dirname ("foo//bar////", "foo");
  check_dirname ("foo///bar///////", "foo");
  check_dirname ("/foo", "/");
  check_dirname ("////foo", "/");
  check_dirname ("/foo/bar", "/foo");
  check_dirname ("/foo//bar", "/foo");
  check_dirname ("/foo///bar", "/foo");
  check_dirname ("/", "/");
  check_dirname ("///", "/");
  check_dirname ("", ".");  
176
#endif
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#ifdef DBUS_WIN
  check_path_absolute ("c:/", TRUE);
  check_path_absolute ("c:/foo", TRUE);
  check_path_absolute ("", FALSE);
  check_path_absolute ("foo", FALSE);
  check_path_absolute ("foo/bar", FALSE);
  check_path_absolute ("", FALSE);
  check_path_absolute ("foo\\bar", FALSE);
  check_path_absolute ("c:\\", TRUE);
  check_path_absolute ("c:\\foo", TRUE);
  check_path_absolute ("c:", TRUE);
  check_path_absolute ("c:\\foo\\bar", TRUE);
  check_path_absolute ("\\", TRUE);
  check_path_absolute ("/", TRUE);
#else  
193 194 195 196 197
  check_path_absolute ("/", TRUE);
  check_path_absolute ("/foo", TRUE);
  check_path_absolute ("", FALSE);
  check_path_absolute ("foo", FALSE);
  check_path_absolute ("foo/bar", FALSE);
198
#endif
199 200 201
  
  return TRUE;
}
202
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */