D-Bus  1.10.12
dbus-misc.c
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-misc.c  A few assorted public functions that don't fit elsewhere
00003  *
00004  * Copyright (C) 2006 Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  *
00022  */
00023 
00024 #include <config.h>
00025 #include "dbus-misc.h"
00026 #include "dbus-internals.h"
00027 #include "dbus-string.h"
00028 
00072 char*
00073 dbus_get_local_machine_id (void)
00074 {
00075   DBusString uuid;
00076   char *s;
00077 
00078   s = NULL;
00079 
00080   if (!_dbus_string_init (&uuid))
00081     return NULL;
00082 
00083   /* The documentation says dbus_get_local_machine_id() only fails on OOM;
00084    * this can actually also fail if the D-Bus installation is faulty
00085    * (no UUID) *and* reading a new random UUID fails, but we have no way
00086    * to report that */
00087   if (!_dbus_get_local_machine_uuid_encoded (&uuid, NULL) ||
00088       !_dbus_string_steal_data (&uuid, &s))
00089     {
00090       _dbus_string_free (&uuid);
00091       return NULL;
00092     }
00093   else
00094     {
00095       _dbus_string_free (&uuid);
00096       return s;
00097     }
00098 
00099 }
00100 
00164 void
00165 dbus_get_version (int *major_version_p,
00166                   int *minor_version_p,
00167                   int *micro_version_p)
00168 {
00169   if (major_version_p)
00170     *major_version_p = DBUS_MAJOR_VERSION;
00171   if (minor_version_p)
00172     *minor_version_p = DBUS_MINOR_VERSION;
00173   if (micro_version_p)
00174     *micro_version_p = DBUS_MICRO_VERSION;
00175 }
00176 
00177  /* End of public API */
00179 
00180 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
00181 
00182 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00183 
00184 #include "dbus-test.h"
00185 #include <stdlib.h>
00186 
00187 
00188 dbus_bool_t
00189 _dbus_misc_test (void)
00190 {
00191   int major, minor, micro;
00192   DBusString str;
00193 
00194   /* make sure we don't crash on NULL */
00195   dbus_get_version (NULL, NULL, NULL);
00196 
00197   /* Now verify that all the compile-time version stuff
00198    * is right and matches the runtime. These tests
00199    * are mostly intended to catch various kinds of
00200    * typo (mixing up major and minor, that sort of thing).
00201    */
00202   dbus_get_version (&major, &minor, &micro);
00203 
00204   _dbus_assert (major == DBUS_MAJOR_VERSION);
00205   _dbus_assert (minor == DBUS_MINOR_VERSION);
00206   _dbus_assert (micro == DBUS_MICRO_VERSION);
00207 
00208 #define MAKE_VERSION(x, y, z) (((x) << 16) | ((y) << 8) | (z))
00209 
00210   /* check that MAKE_VERSION works and produces the intended ordering */
00211   _dbus_assert (MAKE_VERSION (1, 0, 0) > MAKE_VERSION (0, 0, 0));
00212   _dbus_assert (MAKE_VERSION (1, 1, 0) > MAKE_VERSION (1, 0, 0));
00213   _dbus_assert (MAKE_VERSION (1, 1, 1) > MAKE_VERSION (1, 1, 0));
00214 
00215   _dbus_assert (MAKE_VERSION (2, 0, 0) > MAKE_VERSION (1, 1, 1));
00216   _dbus_assert (MAKE_VERSION (2, 1, 0) > MAKE_VERSION (1, 1, 1));
00217   _dbus_assert (MAKE_VERSION (2, 1, 1) > MAKE_VERSION (1, 1, 1));
00218 
00219   /* check DBUS_VERSION */
00220   _dbus_assert (MAKE_VERSION (major, minor, micro) == DBUS_VERSION);
00221 
00222   /* check that ordering works with DBUS_VERSION */
00223   _dbus_assert (MAKE_VERSION (major - 1, minor, micro) < DBUS_VERSION);
00224   _dbus_assert (MAKE_VERSION (major, minor - 1, micro) < DBUS_VERSION);
00225   _dbus_assert (MAKE_VERSION (major, minor, micro - 1) < DBUS_VERSION);
00226   
00227   _dbus_assert (MAKE_VERSION (major + 1, minor, micro) > DBUS_VERSION);
00228   _dbus_assert (MAKE_VERSION (major, minor + 1, micro) > DBUS_VERSION);
00229   _dbus_assert (MAKE_VERSION (major, minor, micro + 1) > DBUS_VERSION);
00230 
00231   /* Check DBUS_VERSION_STRING */
00232 
00233   if (!_dbus_string_init (&str))
00234     _dbus_assert_not_reached ("no memory");
00235 
00236   if (!(_dbus_string_append_int (&str, major) &&
00237         _dbus_string_append_byte (&str, '.') &&
00238         _dbus_string_append_int (&str, minor) &&
00239         _dbus_string_append_byte (&str, '.') &&
00240         _dbus_string_append_int (&str, micro)))
00241     _dbus_assert_not_reached ("no memory");
00242 
00243   _dbus_assert (_dbus_string_equal_c_str (&str, DBUS_VERSION_STRING));
00244 
00245   _dbus_string_free (&str);
00246    
00247   return TRUE;
00248 }
00249 
00250 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
00251 
00252 #endif