D-Bus  1.13.7
dbus-marshal-byteswap-util.c
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_ENABLE_EMBEDDED_TESTS
27 #include "dbus-marshal-byteswap.h"
28 #include "dbus-test.h"
29 #include <dbus/dbus-test-tap.h>
30 #include <stdio.h>
31 
32 static void
33 do_byteswap_test (int byte_order)
34 {
35  int sequence;
36  DBusString signature;
37  DBusString body;
38  int opposite_order;
39 
40  if (!_dbus_string_init (&signature) || !_dbus_string_init (&body))
41  _dbus_test_fatal ("oom");
42 
43  opposite_order = byte_order == DBUS_LITTLE_ENDIAN ? DBUS_BIG_ENDIAN : DBUS_LITTLE_ENDIAN;
44 
45  sequence = 0;
46  while (_dbus_test_generate_bodies (sequence, byte_order, &signature, &body))
47  {
48  DBusString copy;
49  DBusTypeReader body_reader;
50  DBusTypeReader copy_reader;
51 
52  if (!_dbus_string_init (&copy))
53  _dbus_test_fatal ("oom");
54 
55  if (!_dbus_string_copy (&body, 0, &copy, 0))
56  _dbus_test_fatal ("oom");
57 
58  _dbus_marshal_byteswap (&signature, 0,
59  byte_order,
60  opposite_order,
61  &copy, 0);
62 
63  _dbus_type_reader_init (&body_reader, byte_order, &signature, 0,
64  &body, 0);
65  _dbus_type_reader_init (&copy_reader, opposite_order, &signature, 0,
66  &copy, 0);
67 
68  if (!_dbus_type_reader_equal_values (&body_reader, &copy_reader))
69  {
70  _dbus_verbose_bytes_of_string (&signature, 0,
71  _dbus_string_get_length (&signature));
73  _dbus_string_get_length (&body));
75  _dbus_string_get_length (&copy));
76 
77  _dbus_test_fatal ("Byte-swapped data did not have same values as original data");
78  }
79 
80  _dbus_string_free (&copy);
81 
82  _dbus_string_set_length (&signature, 0);
83  _dbus_string_set_length (&body, 0);
84  ++sequence;
85  }
86 
87  _dbus_string_free (&signature);
88  _dbus_string_free (&body);
89 
90  _dbus_test_diag (" %d blocks swapped from order '%c' to '%c'",
91  sequence, byte_order, opposite_order);
92 }
93 
95 _dbus_marshal_byteswap_test (void)
96 {
97  do_byteswap_test (DBUS_LITTLE_ENDIAN);
98  do_byteswap_test (DBUS_BIG_ENDIAN);
99 
100  return TRUE;
101 }
102 
103 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1300
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
#define DBUS_BIG_ENDIAN
Code marking MSB-first byte order in the wire protocol.
Definition: dbus-protocol.h:54
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:264
The type reader is an iterator for reading values from a block of values.
#define TRUE
Expands to "1".
void _dbus_type_reader_init(DBusTypeReader *reader, int byte_order, const DBusString *type_str, int type_pos, const DBusString *value_str, int value_pos)
Initializes a type reader.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:819
DBUS_PRIVATE_EXPORT void _dbus_verbose_bytes_of_string(const DBusString *str, int start, int len)
Dump the given part of the string to verbose log.
void _dbus_marshal_byteswap(const DBusString *signature, int signature_start, int old_byte_order, int new_byte_order, DBusString *value_str, int value_pos)
Byteswaps the marshaled data in the given value_str.
#define DBUS_LITTLE_ENDIAN
Code marking LSB-first byte order in the wire protocol.
Definition: dbus-protocol.h:53