1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-marshal-byteswap-util.c  Would be in dbus-marshal-byteswap.c but tests/bus only
3 *
4 * Copyright (C) 2005 Red Hat, Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24#include <config.h>
25
26#ifdef DBUS_BUILD_TESTS
27#include "dbus-marshal-byteswap.h"
28#include "dbus-test.h"
29#include <stdio.h>
30
31static void
32do_byteswap_test (int byte_order)
33{
34  int sequence;
35  DBusString signature;
36  DBusString body;
37  int opposite_order;
38
39  if (!_dbus_string_init (&signature) || !_dbus_string_init (&body))
40    _dbus_assert_not_reached ("oom");
41
42  opposite_order = byte_order == DBUS_LITTLE_ENDIAN ? DBUS_BIG_ENDIAN : DBUS_LITTLE_ENDIAN;
43
44  sequence = 0;
45  while (dbus_internal_do_not_use_generate_bodies (sequence,
46                                                   byte_order,
47                                                   &signature, &body))
48    {
49      DBusString copy;
50      DBusTypeReader body_reader;
51      DBusTypeReader copy_reader;
52
53      if (!_dbus_string_init (&copy))
54        _dbus_assert_not_reached ("oom");
55
56      if (!_dbus_string_copy (&body, 0, &copy, 0))
57        _dbus_assert_not_reached ("oom");
58
59      _dbus_marshal_byteswap (&signature, 0,
60                              byte_order,
61                              opposite_order,
62                              &copy, 0);
63
64      _dbus_type_reader_init (&body_reader, byte_order, &signature, 0,
65                              &body, 0);
66      _dbus_type_reader_init (&copy_reader, opposite_order, &signature, 0,
67                              &copy, 0);
68
69      if (!_dbus_type_reader_equal_values (&body_reader, &copy_reader))
70        {
71          _dbus_verbose_bytes_of_string (&signature, 0,
72                                         _dbus_string_get_length (&signature));
73          _dbus_verbose_bytes_of_string (&body, 0,
74                                         _dbus_string_get_length (&body));
75          _dbus_verbose_bytes_of_string (&copy, 0,
76                                         _dbus_string_get_length (&copy));
77
78          _dbus_warn ("Byte-swapped data did not have same values as original data\n");
79          _dbus_assert_not_reached ("test failed");
80        }
81
82      _dbus_string_free (&copy);
83
84      _dbus_string_set_length (&signature, 0);
85      _dbus_string_set_length (&body, 0);
86      ++sequence;
87    }
88
89  _dbus_string_free (&signature);
90  _dbus_string_free (&body);
91
92  printf ("  %d blocks swapped from order '%c' to '%c'\n",
93          sequence, byte_order, opposite_order);
94}
95
96dbus_bool_t
97_dbus_marshal_byteswap_test (void)
98{
99  do_byteswap_test (DBUS_LITTLE_ENDIAN);
100  do_byteswap_test (DBUS_BIG_ENDIAN);
101
102  return TRUE;
103}
104
105#endif /* DBUS_BUILD_TESTS */
106