chiark / gitweb /
test-bus-gvariant: remove unused variable
[elogind.git] / src / libsystemd-bus / test-bus-gvariant.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #ifdef HAVE_GLIB
23 #include <glib.h>
24 #endif
25
26 #include "util.h"
27 #include "sd-bus.h"
28 #include "bus-gvariant.h"
29 #include "bus-util.h"
30 #include "bus-internal.h"
31 #include "bus-message.h"
32
33 static void test_bus_gvariant_is_fixed_size(void) {
34         assert(bus_gvariant_is_fixed_size("") > 0);
35         assert(bus_gvariant_is_fixed_size("y") > 0);
36         assert(bus_gvariant_is_fixed_size("u") > 0);
37         assert(bus_gvariant_is_fixed_size("b") > 0);
38         assert(bus_gvariant_is_fixed_size("n") > 0);
39         assert(bus_gvariant_is_fixed_size("q") > 0);
40         assert(bus_gvariant_is_fixed_size("i") > 0);
41         assert(bus_gvariant_is_fixed_size("t") > 0);
42         assert(bus_gvariant_is_fixed_size("d") > 0);
43         assert(bus_gvariant_is_fixed_size("s") == 0);
44         assert(bus_gvariant_is_fixed_size("o") == 0);
45         assert(bus_gvariant_is_fixed_size("g") == 0);
46         assert(bus_gvariant_is_fixed_size("h") > 0);
47         assert(bus_gvariant_is_fixed_size("ay") == 0);
48         assert(bus_gvariant_is_fixed_size("v") == 0);
49         assert(bus_gvariant_is_fixed_size("(u)") > 0);
50         assert(bus_gvariant_is_fixed_size("(uuuuy)") > 0);
51         assert(bus_gvariant_is_fixed_size("(uusuuy)") == 0);
52         assert(bus_gvariant_is_fixed_size("a{ss}") == 0);
53         assert(bus_gvariant_is_fixed_size("((u)yyy(b(iiii)))") > 0);
54         assert(bus_gvariant_is_fixed_size("((u)yyy(b(iiivi)))") == 0);
55 }
56
57 static void test_bus_gvariant_get_alignment(void) {
58         assert(bus_gvariant_get_alignment("") == 1);
59         assert(bus_gvariant_get_alignment("y") == 1);
60         assert(bus_gvariant_get_alignment("b") == 1);
61         assert(bus_gvariant_get_alignment("u") == 4);
62         assert(bus_gvariant_get_alignment("s") == 1);
63         assert(bus_gvariant_get_alignment("o") == 1);
64         assert(bus_gvariant_get_alignment("g") == 1);
65         assert(bus_gvariant_get_alignment("v") == 8);
66         assert(bus_gvariant_get_alignment("h") == 4);
67         assert(bus_gvariant_get_alignment("i") == 4);
68         assert(bus_gvariant_get_alignment("t") == 8);
69         assert(bus_gvariant_get_alignment("x") == 8);
70         assert(bus_gvariant_get_alignment("q") == 2);
71         assert(bus_gvariant_get_alignment("n") == 2);
72         assert(bus_gvariant_get_alignment("d") == 8);
73         assert(bus_gvariant_get_alignment("ay") == 1);
74         assert(bus_gvariant_get_alignment("as") == 1);
75         assert(bus_gvariant_get_alignment("au") == 4);
76         assert(bus_gvariant_get_alignment("an") == 2);
77         assert(bus_gvariant_get_alignment("ans") == 2);
78         assert(bus_gvariant_get_alignment("ant") == 8);
79         assert(bus_gvariant_get_alignment("(ss)") == 1);
80         assert(bus_gvariant_get_alignment("(ssu)") == 4);
81         assert(bus_gvariant_get_alignment("a(ssu)") == 4);
82 }
83
84 static void test_marshal(void) {
85         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
86         _cleanup_bus_unref_ sd_bus *bus = NULL;
87
88         assert_se(sd_bus_open_system(&bus) >= 0);
89         bus->use_gvariant = true; /* dirty hack */
90
91         assert_se(sd_bus_message_new_method_call(bus, "a.service.name", "/an/object/path/which/is/really/really/long/so/that/we/hit/the/eight/bit/boundary/by/quite/some/margin/to/test/this/stuff/that/it/really/works", "an.interface.name", "AMethodName", &m) >= 0);
92
93         /* assert_se(sd_bus_message_append(m, "ssy(sts)v", "first-string-parameter", "second-string-parameter", 9, "a", (uint64_t) 7777, "b", "(su)", "xxx", 4712) >= 0);  */
94         assert_se(sd_bus_message_append(m,
95                                         "a(usv)", 2,
96                                         4711, "first-string-parameter", "(st)", "X", (uint64_t) 1111,
97                                         4712, "second-string-parameter", "(a(si))", 2, "Y", 5, "Z", 6) >= 0);
98
99         assert_se(bus_message_seal(m, 4711) >= 0);
100
101 #ifdef HAVE_GLIB
102         {
103                 GVariant *v;
104                 char *t;
105
106 #if !defined(GLIB_VERSION_2_36)
107                 g_type_init();
108 #endif
109
110                 v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuuua(yv))"), m->header, sizeof(struct bus_header) + BUS_MESSAGE_FIELDS_SIZE(m), false, NULL, NULL);
111                 t = g_variant_print(v, TRUE);
112                 printf("%s\n", t);
113                 g_free(t);
114                 g_variant_unref(v);
115
116                 v = g_variant_new_from_data(G_VARIANT_TYPE("(a(usv))"), m->body.data, BUS_MESSAGE_BODY_SIZE(m), false, NULL, NULL);
117                 t = g_variant_print(v, TRUE);
118                 printf("%s\n", t);
119                 g_free(t);
120                 g_variant_unref(v);
121         }
122 #endif
123
124 }
125
126 int main(int argc, char *argv[]) {
127
128         test_bus_gvariant_is_fixed_size();
129         test_bus_gvariant_get_alignment();
130         test_marshal();
131
132         return 0;
133 }