dbus-marshal-byteswap-util.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-marshal-byteswap-util.c  Would be in dbus-marshal-byteswap.c but tests/bus only
00003  *
00004  * Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include <config.h>
00025 
00026 #ifdef DBUS_BUILD_TESTS 
00027 #include "dbus-marshal-byteswap.h"
00028 #include "dbus-test.h"
00029 #include <stdio.h>
00030 
00031 static void
00032 do_byteswap_test (int byte_order)
00033 {
00034   int sequence;
00035   DBusString signature;
00036   DBusString body;
00037   int opposite_order;
00038 
00039   if (!_dbus_string_init (&signature) || !_dbus_string_init (&body))
00040     _dbus_assert_not_reached ("oom");
00041 
00042   opposite_order = byte_order == DBUS_LITTLE_ENDIAN ? DBUS_BIG_ENDIAN : DBUS_LITTLE_ENDIAN;
00043   
00044   sequence = 0;
00045   while (dbus_internal_do_not_use_generate_bodies (sequence,
00046                                                    byte_order,
00047                                                    &signature, &body))
00048     {
00049       DBusString copy;
00050       DBusTypeReader body_reader;
00051       DBusTypeReader copy_reader;
00052 
00053       if (!_dbus_string_init (&copy))
00054         _dbus_assert_not_reached ("oom");
00055 
00056       if (!_dbus_string_copy (&body, 0, &copy, 0))
00057         _dbus_assert_not_reached ("oom");
00058 
00059       _dbus_marshal_byteswap (&signature, 0,
00060                               byte_order,
00061                               opposite_order,
00062                               &copy, 0);
00063 
00064       _dbus_type_reader_init (&body_reader, byte_order, &signature, 0,
00065                               &body, 0);
00066       _dbus_type_reader_init (&copy_reader, opposite_order, &signature, 0,
00067                               &copy, 0);
00068       
00069       if (!_dbus_type_reader_equal_values (&body_reader, &copy_reader))
00070         {
00071           _dbus_verbose_bytes_of_string (&signature, 0,
00072                                          _dbus_string_get_length (&signature));
00073           _dbus_verbose_bytes_of_string (&body, 0,
00074                                          _dbus_string_get_length (&body));
00075           _dbus_verbose_bytes_of_string (&copy, 0,
00076                                          _dbus_string_get_length (&copy));
00077 
00078           _dbus_warn ("Byte-swapped data did not have same values as original data\n");
00079           _dbus_assert_not_reached ("test failed");
00080         }
00081       
00082       _dbus_string_free (&copy);
00083       
00084       _dbus_string_set_length (&signature, 0);
00085       _dbus_string_set_length (&body, 0);
00086       ++sequence;
00087     }
00088 
00089   _dbus_string_free (&signature);
00090   _dbus_string_free (&body);
00091 
00092   printf ("  %d blocks swapped from order '%c' to '%c'\n",
00093           sequence, byte_order, opposite_order);
00094 }
00095 
00096 dbus_bool_t
00097 _dbus_marshal_byteswap_test (void)
00098 {
00099   do_byteswap_test (DBUS_LITTLE_ENDIAN);
00100   do_byteswap_test (DBUS_BIG_ENDIAN);
00101 
00102   return TRUE;
00103 }
00104 
00105 #endif /* DBUS_BUILD_TESTS */

Generated on Fri Sep 21 18:12:12 2007 for D-Bus by  doxygen 1.5.1