Merge lp:~attente/glib/gicon-upstream into lp:ubuntu/saucy/glib2.0
- gicon-upstream
- Merge into saucy
Proposed by
William Hua
Status: | Merged |
---|---|
Merge reported by: | Sebastien Bacher |
Merged at revision: | not available |
Proposed branch: | lp:~attente/glib/gicon-upstream |
Merge into: | lp:ubuntu/saucy/glib2.0 |
Diff against target: |
27569 lines (+26956/-46) 51 files modified
.pc/applied-patches (+3/-0) .pc/g-variant-get-data-as-bytes.patch/glib/gvariant-core.c (+1104/-0) .pc/g-variant-get-data-as-bytes.patch/glib/tests/gvariant.c (+4339/-0) .pc/g-variant-new-take-string.patch/docs/reference/glib/glib-sections.txt (+3227/-0) .pc/g-variant-new-take-string.patch/glib/gvariant.c (+5328/-0) .pc/g-variant-new-take-string.patch/glib/gvariant.h (+395/-0) .pc/gicon-serialization-support.patch/docs/reference/gio/gio-docs.xml (+306/-0) .pc/gicon-serialization-support.patch/docs/reference/gio/gio-sections.txt (+4024/-0) .pc/gicon-serialization-support.patch/gio/Makefile.am (+786/-0) .pc/gicon-serialization-support.patch/gio/gemblem.c (+359/-0) .pc/gicon-serialization-support.patch/gio/gemblemedicon.c (+423/-0) .pc/gicon-serialization-support.patch/gio/gfileicon.c (+340/-0) .pc/gicon-serialization-support.patch/gio/gicon.c (+449/-0) .pc/gicon-serialization-support.patch/gio/gicon.h (+96/-0) .pc/gicon-serialization-support.patch/gio/gio.h (+164/-0) .pc/gicon-serialization-support.patch/gio/giotypes.h (+473/-0) .pc/gicon-serialization-support.patch/gio/gmenu.c (+1336/-0) .pc/gicon-serialization-support.patch/gio/gmenu.h (+177/-0) .pc/gicon-serialization-support.patch/gio/gmenumodel.h (+276/-0) .pc/gicon-serialization-support.patch/gio/gthemedicon.c (+522/-0) .pc/gicon-serialization-support.patch/gio/gvfs.h (+133/-0) .pc/gicon-serialization-support.patch/gio/tests/g-icon.c (+382/-0) debian/changelog (+11/-0) debian/libglib2.0-0.symbols (+7/-0) debian/patches/g-variant-get-data-as-bytes.patch (+58/-0) debian/patches/g-variant-new-take-string.patch (+66/-0) debian/patches/gicon-serialization-support.patch (+1205/-0) debian/patches/series (+3/-0) docs/reference/gio/gio-docs.xml (+1/-0) docs/reference/gio/gio-sections.txt (+19/-1) docs/reference/glib/glib-sections.txt (+1/-0) gio/Makefile.am (+2/-0) gio/gbytesicon.c (+264/-0) gio/gbytesicon.h (+54/-0) gio/gemblem.c (+21/-0) gio/gemblemedicon.c (+49/-0) gio/gfileicon.c (+9/-0) gio/gicon.c (+277/-39) gio/gicon.h (+7/-0) gio/gio.h (+1/-0) gio/giotypes.h (+1/-0) gio/gmenu.c (+41/-0) gio/gmenu.h (+4/-0) gio/gmenumodel.h (+15/-0) gio/gthemedicon.c (+9/-0) gio/gvfs.h (+2/-1) gio/tests/g-icon.c (+121/-1) glib/gvariant-core.c (+14/-1) glib/gvariant.c (+36/-0) glib/gvariant.h (+2/-0) glib/tests/gvariant.c (+14/-3) |
To merge this branch: | bzr merge lp:~attente/glib/gicon-upstream |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ubuntu branches | Pending | ||
Review via email: mp+162430@code.launchpad.net |
Commit message
* debian/
- Add upstream's g_variant_
* debian/
- Add upstream's GVariant bytes serialization bug fix.
* debian/
- Add upstream's improved GIcon serialization support.
Description of the change
Patch GLib with upstream bug fixes to GVariant and changes to GIcon interface.
To post a comment you must log in.
Revision history for this message
Sebastien Bacher (seb128) wrote : | # |
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file '.pc/applied-patches' | |||
2 | --- .pc/applied-patches 2013-04-10 11:43:24 +0000 | |||
3 | +++ .pc/applied-patches 2013-05-03 17:57:29 +0000 | |||
4 | @@ -13,3 +13,6 @@ | |||
5 | 13 | 17_check_abis_mips_symbols.patch | 13 | 17_check_abis_mips_symbols.patch |
6 | 14 | 61_glib-compile-binaries-path.patch | 14 | 61_glib-compile-binaries-path.patch |
7 | 15 | 90_gio-modules-multiarch-compat.patch | 15 | 90_gio-modules-multiarch-compat.patch |
8 | 16 | g-variant-new-take-string.patch | ||
9 | 17 | g-variant-get-data-as-bytes.patch | ||
10 | 18 | gicon-serialization-support.patch | ||
11 | 16 | 19 | ||
12 | === added directory '.pc/g-variant-get-data-as-bytes.patch' | |||
13 | === added directory '.pc/g-variant-get-data-as-bytes.patch/glib' | |||
14 | === added file '.pc/g-variant-get-data-as-bytes.patch/glib/gvariant-core.c' | |||
15 | --- .pc/g-variant-get-data-as-bytes.patch/glib/gvariant-core.c 1970-01-01 00:00:00 +0000 | |||
16 | +++ .pc/g-variant-get-data-as-bytes.patch/glib/gvariant-core.c 2013-05-03 17:57:29 +0000 | |||
17 | @@ -0,0 +1,1104 @@ | |||
18 | 1 | /* | ||
19 | 2 | * Copyright © 2007, 2008 Ryan Lortie | ||
20 | 3 | * Copyright © 2010 Codethink Limited | ||
21 | 4 | * | ||
22 | 5 | * This library is free software; you can redistribute it and/or | ||
23 | 6 | * modify it under the terms of the GNU Lesser General Public | ||
24 | 7 | * License as published by the Free Software Foundation; either | ||
25 | 8 | * version 2 of the License, or (at your option) any later version. | ||
26 | 9 | * | ||
27 | 10 | * This library is distributed in the hope that it will be useful, | ||
28 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
29 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
30 | 13 | * Lesser General Public License for more details. | ||
31 | 14 | * | ||
32 | 15 | * You should have received a copy of the GNU Lesser General Public | ||
33 | 16 | * License along with this library; if not, write to the | ||
34 | 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
35 | 18 | * Boston, MA 02111-1307, USA. | ||
36 | 19 | */ | ||
37 | 20 | |||
38 | 21 | #include "config.h" | ||
39 | 22 | |||
40 | 23 | #include <glib/gvariant-core.h> | ||
41 | 24 | |||
42 | 25 | #include <glib/gvariant-serialiser.h> | ||
43 | 26 | #include <glib/gtestutils.h> | ||
44 | 27 | #include <glib/gbitlock.h> | ||
45 | 28 | #include <glib/gatomic.h> | ||
46 | 29 | #include <glib/gbytes.h> | ||
47 | 30 | #include <glib/gslice.h> | ||
48 | 31 | #include <glib/gmem.h> | ||
49 | 32 | #include <string.h> | ||
50 | 33 | |||
51 | 34 | |||
52 | 35 | /* | ||
53 | 36 | * This file includes the structure definition for GVariant and a small | ||
54 | 37 | * set of functions that are allowed to access the structure directly. | ||
55 | 38 | * | ||
56 | 39 | * This minimises the amount of code that can possibly touch a GVariant | ||
57 | 40 | * structure directly to a few simple fundamental operations. These few | ||
58 | 41 | * operations are written to be completely threadsafe with respect to | ||
59 | 42 | * all possible outside access. This means that we only need to be | ||
60 | 43 | * concerned about thread safety issues in this one small file. | ||
61 | 44 | * | ||
62 | 45 | * Most GVariant API functions are in gvariant.c. | ||
63 | 46 | */ | ||
64 | 47 | |||
65 | 48 | /** | ||
66 | 49 | * GVariant: | ||
67 | 50 | * | ||
68 | 51 | * #GVariant is an opaque data structure and can only be accessed | ||
69 | 52 | * using the following functions. | ||
70 | 53 | * | ||
71 | 54 | * Since: 2.24 | ||
72 | 55 | **/ | ||
73 | 56 | struct _GVariant | ||
74 | 57 | /* see below for field member documentation */ | ||
75 | 58 | { | ||
76 | 59 | GVariantTypeInfo *type_info; | ||
77 | 60 | gsize size; | ||
78 | 61 | |||
79 | 62 | union | ||
80 | 63 | { | ||
81 | 64 | struct | ||
82 | 65 | { | ||
83 | 66 | GBytes *bytes; | ||
84 | 67 | gconstpointer data; | ||
85 | 68 | } serialised; | ||
86 | 69 | |||
87 | 70 | struct | ||
88 | 71 | { | ||
89 | 72 | GVariant **children; | ||
90 | 73 | gsize n_children; | ||
91 | 74 | } tree; | ||
92 | 75 | } contents; | ||
93 | 76 | |||
94 | 77 | gint state; | ||
95 | 78 | gint ref_count; | ||
96 | 79 | }; | ||
97 | 80 | |||
98 | 81 | /* struct GVariant: | ||
99 | 82 | * | ||
100 | 83 | * There are two primary forms of GVariant instances: "serialised form" | ||
101 | 84 | * and "tree form". | ||
102 | 85 | * | ||
103 | 86 | * "serialised form": A serialised GVariant instance stores its value in | ||
104 | 87 | * the GVariant serialisation format. All | ||
105 | 88 | * basic-typed instances (ie: non-containers) are in | ||
106 | 89 | * serialised format, as are some containers. | ||
107 | 90 | * | ||
108 | 91 | * "tree form": Some containers are in "tree form". In this case, | ||
109 | 92 | * instead of containing the serialised data for the | ||
110 | 93 | * container, the instance contains an array of pointers to | ||
111 | 94 | * the child values of the container (thus forming a tree). | ||
112 | 95 | * | ||
113 | 96 | * It is possible for an instance to transition from tree form to | ||
114 | 97 | * serialised form. This happens, implicitly, if the serialised data is | ||
115 | 98 | * requested (eg: via g_variant_get_data()). Serialised form instances | ||
116 | 99 | * never transition into tree form. | ||
117 | 100 | * | ||
118 | 101 | * | ||
119 | 102 | * The fields of the structure are documented here: | ||
120 | 103 | * | ||
121 | 104 | * type_info: this is a reference to a GVariantTypeInfo describing the | ||
122 | 105 | * type of the instance. When the instance is freed, this | ||
123 | 106 | * reference must be released with g_variant_type_info_unref(). | ||
124 | 107 | * | ||
125 | 108 | * The type_info field never changes during the life of the | ||
126 | 109 | * instance, so it can be accessed without a lock. | ||
127 | 110 | * | ||
128 | 111 | * size: this is the size of the serialised form for the instance, if it | ||
129 | 112 | * is known. If the instance is in serialised form then it is, by | ||
130 | 113 | * definition, known. If the instance is in tree form then it may | ||
131 | 114 | * be unknown (in which case it is -1). It is possible for the | ||
132 | 115 | * size to be known when in tree form if, for example, the user | ||
133 | 116 | * has called g_variant_get_size() without calling | ||
134 | 117 | * g_variant_get_data(). Additionally, even when the user calls | ||
135 | 118 | * g_variant_get_data() the size of the data must first be | ||
136 | 119 | * determined so that a large enough buffer can be allocated for | ||
137 | 120 | * the data. | ||
138 | 121 | * | ||
139 | 122 | * Once the size is known, it can never become unknown again. | ||
140 | 123 | * g_variant_ensure_size() is used to ensure that the size is in | ||
141 | 124 | * the known state -- it calculates the size if needed. After | ||
142 | 125 | * that, the size field can be accessed without a lock. | ||
143 | 126 | * | ||
144 | 127 | * contents: a union containing either the information associated with | ||
145 | 128 | * holding a value in serialised form or holding a value in | ||
146 | 129 | * tree form. | ||
147 | 130 | * | ||
148 | 131 | * .serialised: Only valid when the instance is in serialised form. | ||
149 | 132 | * | ||
150 | 133 | * Since an instance can never transition away from | ||
151 | 134 | * serialised form, once these fields are set, they will | ||
152 | 135 | * never be changed. It is therefore valid to access | ||
153 | 136 | * them without holding a lock. | ||
154 | 137 | * | ||
155 | 138 | * .bytes: the #GBytes that contains the memory pointed to by | ||
156 | 139 | * .data, or %NULL if .data is %NULL. In the event that | ||
157 | 140 | * the instance was deserialised from another instance, | ||
158 | 141 | * then the bytes will be shared by both of them. When | ||
159 | 142 | * the instance is freed, this reference must be released | ||
160 | 143 | * with g_bytes_unref(). | ||
161 | 144 | * | ||
162 | 145 | * .data: the serialised data (of size 'size') of the instance. | ||
163 | 146 | * This pointer should not be freed or modified in any way. | ||
164 | 147 | * #GBytes is responsible for memory management. | ||
165 | 148 | * | ||
166 | 149 | * This pointer may be %NULL in two cases: | ||
167 | 150 | * | ||
168 | 151 | * - if the serialised size of the instance is 0 | ||
169 | 152 | * | ||
170 | 153 | * - if the instance is of a fixed-sized type and was | ||
171 | 154 | * deserialised out of a corrupted container such that | ||
172 | 155 | * the container contains too few bytes to point to the | ||
173 | 156 | * entire proper fixed-size of this instance. In this | ||
174 | 157 | * case, 'size' will still be equal to the proper fixed | ||
175 | 158 | * size, but this pointer will be %NULL. This is exactly | ||
176 | 159 | * the reason that g_variant_get_data() sometimes returns | ||
177 | 160 | * %NULL. For all other calls, the effect should be as | ||
178 | 161 | * if .data pointed to the appropriate number of nul | ||
179 | 162 | * bytes. | ||
180 | 163 | * | ||
181 | 164 | * .tree: Only valid when the instance is in tree form. | ||
182 | 165 | * | ||
183 | 166 | * Note that accesses from other threads could result in | ||
184 | 167 | * conversion of the instance from tree form to serialised form | ||
185 | 168 | * at any time. For this reason, the instance lock must always | ||
186 | 169 | * be held while performing any operations on 'contents.tree'. | ||
187 | 170 | * | ||
188 | 171 | * .children: the array of the child instances of this instance. | ||
189 | 172 | * When the instance is freed (or converted to serialised | ||
190 | 173 | * form) then each child must have g_variant_unref() | ||
191 | 174 | * called on it and the array must be freed using | ||
192 | 175 | * g_free(). | ||
193 | 176 | * | ||
194 | 177 | * .n_children: the number of items in the .children array. | ||
195 | 178 | * | ||
196 | 179 | * state: a bitfield describing the state of the instance. It is a | ||
197 | 180 | * bitwise-or of the following STATE_* constants: | ||
198 | 181 | * | ||
199 | 182 | * STATE_LOCKED: the instance lock is held. This is the bit used by | ||
200 | 183 | * g_bit_lock(). | ||
201 | 184 | * | ||
202 | 185 | * STATE_SERIALISED: the instance is in serialised form. If this | ||
203 | 186 | * flag is not set then the instance is in tree | ||
204 | 187 | * form. | ||
205 | 188 | * | ||
206 | 189 | * STATE_TRUSTED: for serialised form instances, this means that the | ||
207 | 190 | * serialised data is known to be in normal form (ie: | ||
208 | 191 | * not corrupted). | ||
209 | 192 | * | ||
210 | 193 | * For tree form instances, this means that all of the | ||
211 | 194 | * child instances in the contents.tree.children array | ||
212 | 195 | * are trusted. This means that if the container is | ||
213 | 196 | * serialised then the resulting data will be in | ||
214 | 197 | * normal form. | ||
215 | 198 | * | ||
216 | 199 | * If this flag is unset it does not imply that the | ||
217 | 200 | * data is corrupted. It merely means that we're not | ||
218 | 201 | * sure that it's valid. See g_variant_is_trusted(). | ||
219 | 202 | * | ||
220 | 203 | * STATE_FLOATING: if this flag is set then the object has a floating | ||
221 | 204 | * reference. See g_variant_ref_sink(). | ||
222 | 205 | * | ||
223 | 206 | * ref_count: the reference count of the instance | ||
224 | 207 | */ | ||
225 | 208 | #define STATE_LOCKED 1 | ||
226 | 209 | #define STATE_SERIALISED 2 | ||
227 | 210 | #define STATE_TRUSTED 4 | ||
228 | 211 | #define STATE_FLOATING 8 | ||
229 | 212 | |||
230 | 213 | /* -- private -- */ | ||
231 | 214 | /* < private > | ||
232 | 215 | * g_variant_lock: | ||
233 | 216 | * @value: a #GVariant | ||
234 | 217 | * | ||
235 | 218 | * Locks @value for performing sensitive operations. | ||
236 | 219 | */ | ||
237 | 220 | static void | ||
238 | 221 | g_variant_lock (GVariant *value) | ||
239 | 222 | { | ||
240 | 223 | g_bit_lock (&value->state, 0); | ||
241 | 224 | } | ||
242 | 225 | |||
243 | 226 | /* < private > | ||
244 | 227 | * g_variant_unlock: | ||
245 | 228 | * @value: a #GVariant | ||
246 | 229 | * | ||
247 | 230 | * Unlocks @value after performing sensitive operations. | ||
248 | 231 | */ | ||
249 | 232 | static void | ||
250 | 233 | g_variant_unlock (GVariant *value) | ||
251 | 234 | { | ||
252 | 235 | g_bit_unlock (&value->state, 0); | ||
253 | 236 | } | ||
254 | 237 | |||
255 | 238 | /* < private > | ||
256 | 239 | * g_variant_release_children: | ||
257 | 240 | * @value: a #GVariant | ||
258 | 241 | * | ||
259 | 242 | * Releases the reference held on each child in the 'children' array of | ||
260 | 243 | * @value and frees the array itself. @value must be in tree form. | ||
261 | 244 | * | ||
262 | 245 | * This is done when freeing a tree-form instance or converting it to | ||
263 | 246 | * serialised form. | ||
264 | 247 | * | ||
265 | 248 | * The current thread must hold the lock on @value. | ||
266 | 249 | */ | ||
267 | 250 | static void | ||
268 | 251 | g_variant_release_children (GVariant *value) | ||
269 | 252 | { | ||
270 | 253 | gsize i; | ||
271 | 254 | |||
272 | 255 | g_assert (value->state & STATE_LOCKED); | ||
273 | 256 | g_assert (~value->state & STATE_SERIALISED); | ||
274 | 257 | |||
275 | 258 | for (i = 0; i < value->contents.tree.n_children; i++) | ||
276 | 259 | g_variant_unref (value->contents.tree.children[i]); | ||
277 | 260 | |||
278 | 261 | g_free (value->contents.tree.children); | ||
279 | 262 | } | ||
280 | 263 | |||
281 | 264 | /* This begins the main body of the recursive serialiser. | ||
282 | 265 | * | ||
283 | 266 | * There are 3 functions here that work as a team with the serialiser to | ||
284 | 267 | * get things done. g_variant_store() has a trivial role, but as a | ||
285 | 268 | * public API function, it has its definition elsewhere. | ||
286 | 269 | * | ||
287 | 270 | * Note that "serialisation" of an instance does not mean that the | ||
288 | 271 | * instance is converted to serialised form -- it means that the | ||
289 | 272 | * serialised form of an instance is written to an external buffer. | ||
290 | 273 | * g_variant_ensure_serialised() (which is not part of this set of | ||
291 | 274 | * functions) is the function that is responsible for converting an | ||
292 | 275 | * instance to serialised form. | ||
293 | 276 | * | ||
294 | 277 | * We are only concerned here with container types since non-container | ||
295 | 278 | * instances are always in serialised form. For these instances, | ||
296 | 279 | * storing their serialised form merely involves a memcpy(). | ||
297 | 280 | * | ||
298 | 281 | * Serialisation is a two-step process. First, the size of the | ||
299 | 282 | * serialised data must be calculated so that an appropriately-sized | ||
300 | 283 | * buffer can be allocated. Second, the data is written into the | ||
301 | 284 | * buffer. | ||
302 | 285 | * | ||
303 | 286 | * Determining the size: | ||
304 | 287 | * The process of determining the size is triggered by a call to | ||
305 | 288 | * g_variant_ensure_size() on a container. This invokes the | ||
306 | 289 | * serialiser code to determine the size. The serialiser is passed | ||
307 | 290 | * g_variant_fill_gvs() as a callback. | ||
308 | 291 | * | ||
309 | 292 | * g_variant_fill_gvs() is called by the serialiser on each child of | ||
310 | 293 | * the container which, in turn, calls g_variant_ensure_size() on | ||
311 | 294 | * itself and fills in the result of its own size calculation. | ||
312 | 295 | * | ||
313 | 296 | * The serialiser uses the size information from the children to | ||
314 | 297 | * calculate the size needed for the entire container. | ||
315 | 298 | * | ||
316 | 299 | * Writing the data: | ||
317 | 300 | * After the buffer has been allocated, g_variant_serialise() is | ||
318 | 301 | * called on the container. This invokes the serialiser code to write | ||
319 | 302 | * the bytes to the container. The serialiser is, again, passed | ||
320 | 303 | * g_variant_fill_gvs() as a callback. | ||
321 | 304 | * | ||
322 | 305 | * This time, when g_variant_fill_gvs() is called for each child, the | ||
323 | 306 | * child is given a pointer to a sub-region of the allocated buffer | ||
324 | 307 | * where it should write its data. This is done by calling | ||
325 | 308 | * g_variant_store(). In the event that the instance is in serialised | ||
326 | 309 | * form this means a memcpy() of the serialised data into the | ||
327 | 310 | * allocated buffer. In the event that the instance is in tree form | ||
328 | 311 | * this means a recursive call back into g_variant_serialise(). | ||
329 | 312 | * | ||
330 | 313 | * | ||
331 | 314 | * The forward declaration here allows corecursion via callback: | ||
332 | 315 | */ | ||
333 | 316 | static void g_variant_fill_gvs (GVariantSerialised *, gpointer); | ||
334 | 317 | |||
335 | 318 | /* < private > | ||
336 | 319 | * g_variant_ensure_size: | ||
337 | 320 | * @value: a #GVariant | ||
338 | 321 | * | ||
339 | 322 | * Ensures that the ->size field of @value is filled in properly. This | ||
340 | 323 | * must be done as a precursor to any serialisation of the value in | ||
341 | 324 | * order to know how large of a buffer is needed to store the data. | ||
342 | 325 | * | ||
343 | 326 | * The current thread must hold the lock on @value. | ||
344 | 327 | */ | ||
345 | 328 | static void | ||
346 | 329 | g_variant_ensure_size (GVariant *value) | ||
347 | 330 | { | ||
348 | 331 | g_assert (value->state & STATE_LOCKED); | ||
349 | 332 | |||
350 | 333 | if (value->size == (gssize) -1) | ||
351 | 334 | { | ||
352 | 335 | gpointer *children; | ||
353 | 336 | gsize n_children; | ||
354 | 337 | |||
355 | 338 | children = (gpointer *) value->contents.tree.children; | ||
356 | 339 | n_children = value->contents.tree.n_children; | ||
357 | 340 | value->size = g_variant_serialiser_needed_size (value->type_info, | ||
358 | 341 | g_variant_fill_gvs, | ||
359 | 342 | children, n_children); | ||
360 | 343 | } | ||
361 | 344 | } | ||
362 | 345 | |||
363 | 346 | /* < private > | ||
364 | 347 | * g_variant_serialise: | ||
365 | 348 | * @value: a #GVariant | ||
366 | 349 | * @data: an appropriately-sized buffer | ||
367 | 350 | * | ||
368 | 351 | * Serialises @value into @data. @value must be in tree form. | ||
369 | 352 | * | ||
370 | 353 | * No change is made to @value. | ||
371 | 354 | * | ||
372 | 355 | * The current thread must hold the lock on @value. | ||
373 | 356 | */ | ||
374 | 357 | static void | ||
375 | 358 | g_variant_serialise (GVariant *value, | ||
376 | 359 | gpointer data) | ||
377 | 360 | { | ||
378 | 361 | GVariantSerialised serialised = { 0, }; | ||
379 | 362 | gpointer *children; | ||
380 | 363 | gsize n_children; | ||
381 | 364 | |||
382 | 365 | g_assert (~value->state & STATE_SERIALISED); | ||
383 | 366 | g_assert (value->state & STATE_LOCKED); | ||
384 | 367 | |||
385 | 368 | serialised.type_info = value->type_info; | ||
386 | 369 | serialised.size = value->size; | ||
387 | 370 | serialised.data = data; | ||
388 | 371 | |||
389 | 372 | children = (gpointer *) value->contents.tree.children; | ||
390 | 373 | n_children = value->contents.tree.n_children; | ||
391 | 374 | |||
392 | 375 | g_variant_serialiser_serialise (serialised, g_variant_fill_gvs, | ||
393 | 376 | children, n_children); | ||
394 | 377 | } | ||
395 | 378 | |||
396 | 379 | /* < private > | ||
397 | 380 | * g_variant_fill_gvs: | ||
398 | 381 | * @serialised: a pointer to a #GVariantSerialised | ||
399 | 382 | * @data: a #GVariant instance | ||
400 | 383 | * | ||
401 | 384 | * This is the callback that is passed by a tree-form container instance | ||
402 | 385 | * to the serialiser. This callback gets called on each child of the | ||
403 | 386 | * container. Each child is responsible for performing the following | ||
404 | 387 | * actions: | ||
405 | 388 | * | ||
406 | 389 | * - reporting its type | ||
407 | 390 | * | ||
408 | 391 | * - reporting its serialised size (requires knowing the size first) | ||
409 | 392 | * | ||
410 | 393 | * - possibly storing its serialised form into the provided buffer | ||
411 | 394 | */ | ||
412 | 395 | static void | ||
413 | 396 | g_variant_fill_gvs (GVariantSerialised *serialised, | ||
414 | 397 | gpointer data) | ||
415 | 398 | { | ||
416 | 399 | GVariant *value = data; | ||
417 | 400 | |||
418 | 401 | g_variant_lock (value); | ||
419 | 402 | g_variant_ensure_size (value); | ||
420 | 403 | g_variant_unlock (value); | ||
421 | 404 | |||
422 | 405 | if (serialised->type_info == NULL) | ||
423 | 406 | serialised->type_info = value->type_info; | ||
424 | 407 | g_assert (serialised->type_info == value->type_info); | ||
425 | 408 | |||
426 | 409 | if (serialised->size == 0) | ||
427 | 410 | serialised->size = value->size; | ||
428 | 411 | g_assert (serialised->size == value->size); | ||
429 | 412 | |||
430 | 413 | if (serialised->data) | ||
431 | 414 | /* g_variant_store() is a public API, so it | ||
432 | 415 | * it will reacquire the lock if it needs to. | ||
433 | 416 | */ | ||
434 | 417 | g_variant_store (value, serialised->data); | ||
435 | 418 | } | ||
436 | 419 | |||
437 | 420 | /* this ends the main body of the recursive serialiser */ | ||
438 | 421 | |||
439 | 422 | /* < private > | ||
440 | 423 | * g_variant_ensure_serialised: | ||
441 | 424 | * @value: a #GVariant | ||
442 | 425 | * | ||
443 | 426 | * Ensures that @value is in serialised form. | ||
444 | 427 | * | ||
445 | 428 | * If @value is in tree form then this function ensures that the | ||
446 | 429 | * serialised size is known and then allocates a buffer of that size and | ||
447 | 430 | * serialises the instance into the buffer. The 'children' array is | ||
448 | 431 | * then released and the instance is set to serialised form based on the | ||
449 | 432 | * contents of the buffer. | ||
450 | 433 | * | ||
451 | 434 | * The current thread must hold the lock on @value. | ||
452 | 435 | */ | ||
453 | 436 | static void | ||
454 | 437 | g_variant_ensure_serialised (GVariant *value) | ||
455 | 438 | { | ||
456 | 439 | g_assert (value->state & STATE_LOCKED); | ||
457 | 440 | |||
458 | 441 | if (~value->state & STATE_SERIALISED) | ||
459 | 442 | { | ||
460 | 443 | GBytes *bytes; | ||
461 | 444 | gpointer data; | ||
462 | 445 | |||
463 | 446 | g_variant_ensure_size (value); | ||
464 | 447 | data = g_malloc (value->size); | ||
465 | 448 | g_variant_serialise (value, data); | ||
466 | 449 | |||
467 | 450 | g_variant_release_children (value); | ||
468 | 451 | |||
469 | 452 | bytes = g_bytes_new_take (data, value->size); | ||
470 | 453 | value->contents.serialised.data = g_bytes_get_data (bytes, NULL); | ||
471 | 454 | value->contents.serialised.bytes = bytes; | ||
472 | 455 | value->state |= STATE_SERIALISED; | ||
473 | 456 | } | ||
474 | 457 | } | ||
475 | 458 | |||
476 | 459 | /* < private > | ||
477 | 460 | * g_variant_alloc: | ||
478 | 461 | * @type: the type of the new instance | ||
479 | 462 | * @serialised: if the instance will be in serialised form | ||
480 | 463 | * @trusted: if the instance will be trusted | ||
481 | 464 | * | ||
482 | 465 | * Allocates a #GVariant instance and does some common work (such as | ||
483 | 466 | * looking up and filling in the type info), setting the state field, | ||
484 | 467 | * and setting the ref_count to 1. | ||
485 | 468 | * | ||
486 | 469 | * Returns: a new #GVariant with a floating reference | ||
487 | 470 | */ | ||
488 | 471 | static GVariant * | ||
489 | 472 | g_variant_alloc (const GVariantType *type, | ||
490 | 473 | gboolean serialised, | ||
491 | 474 | gboolean trusted) | ||
492 | 475 | { | ||
493 | 476 | GVariant *value; | ||
494 | 477 | |||
495 | 478 | value = g_slice_new (GVariant); | ||
496 | 479 | value->type_info = g_variant_type_info_get (type); | ||
497 | 480 | value->state = (serialised ? STATE_SERIALISED : 0) | | ||
498 | 481 | (trusted ? STATE_TRUSTED : 0) | | ||
499 | 482 | STATE_FLOATING; | ||
500 | 483 | value->size = (gssize) -1; | ||
501 | 484 | value->ref_count = 1; | ||
502 | 485 | |||
503 | 486 | return value; | ||
504 | 487 | } | ||
505 | 488 | |||
506 | 489 | /** | ||
507 | 490 | * g_variant_new_from_bytes: | ||
508 | 491 | * @type: a #GVariantType | ||
509 | 492 | * @bytes: a #GBytes | ||
510 | 493 | * @trusted: if the contents of @bytes are trusted | ||
511 | 494 | * | ||
512 | 495 | * Constructs a new serialised-mode #GVariant instance. This is the | ||
513 | 496 | * inner interface for creation of new serialised values that gets | ||
514 | 497 | * called from various functions in gvariant.c. | ||
515 | 498 | * | ||
516 | 499 | * A reference is taken on @bytes. | ||
517 | 500 | * | ||
518 | 501 | * Returns: a new #GVariant with a floating reference | ||
519 | 502 | * | ||
520 | 503 | * Since: 2.36 | ||
521 | 504 | */ | ||
522 | 505 | GVariant * | ||
523 | 506 | g_variant_new_from_bytes (const GVariantType *type, | ||
524 | 507 | GBytes *bytes, | ||
525 | 508 | gboolean trusted) | ||
526 | 509 | { | ||
527 | 510 | GVariant *value; | ||
528 | 511 | guint alignment; | ||
529 | 512 | gsize size; | ||
530 | 513 | |||
531 | 514 | value = g_variant_alloc (type, TRUE, trusted); | ||
532 | 515 | |||
533 | 516 | value->contents.serialised.bytes = g_bytes_ref (bytes); | ||
534 | 517 | |||
535 | 518 | g_variant_type_info_query (value->type_info, | ||
536 | 519 | &alignment, &size); | ||
537 | 520 | |||
538 | 521 | if (size && g_bytes_get_size (bytes) != size) | ||
539 | 522 | { | ||
540 | 523 | /* Creating a fixed-sized GVariant with a bytes of the wrong | ||
541 | 524 | * size. | ||
542 | 525 | * | ||
543 | 526 | * We should do the equivalent of pulling a fixed-sized child out | ||
544 | 527 | * of a brozen container (ie: data is NULL size is equal to the correct | ||
545 | 528 | * fixed size). | ||
546 | 529 | */ | ||
547 | 530 | value->contents.serialised.data = NULL; | ||
548 | 531 | value->size = size; | ||
549 | 532 | } | ||
550 | 533 | else | ||
551 | 534 | { | ||
552 | 535 | value->contents.serialised.data = g_bytes_get_data (bytes, &value->size); | ||
553 | 536 | } | ||
554 | 537 | |||
555 | 538 | return value; | ||
556 | 539 | } | ||
557 | 540 | |||
558 | 541 | /* -- internal -- */ | ||
559 | 542 | |||
560 | 543 | /* < internal > | ||
561 | 544 | * g_variant_new_from_children: | ||
562 | 545 | * @type: a #GVariantType | ||
563 | 546 | * @children: an array of #GVariant pointers. Consumed. | ||
564 | 547 | * @n_children: the length of @children | ||
565 | 548 | * @trusted: %TRUE if every child in @children in trusted | ||
566 | 549 | * | ||
567 | 550 | * Constructs a new tree-mode #GVariant instance. This is the inner | ||
568 | 551 | * interface for creation of new serialised values that gets called from | ||
569 | 552 | * various functions in gvariant.c. | ||
570 | 553 | * | ||
571 | 554 | * @children is consumed by this function. g_free() will be called on | ||
572 | 555 | * it some time later. | ||
573 | 556 | * | ||
574 | 557 | * Returns: a new #GVariant with a floating reference | ||
575 | 558 | */ | ||
576 | 559 | GVariant * | ||
577 | 560 | g_variant_new_from_children (const GVariantType *type, | ||
578 | 561 | GVariant **children, | ||
579 | 562 | gsize n_children, | ||
580 | 563 | gboolean trusted) | ||
581 | 564 | { | ||
582 | 565 | GVariant *value; | ||
583 | 566 | |||
584 | 567 | value = g_variant_alloc (type, FALSE, trusted); | ||
585 | 568 | value->contents.tree.children = children; | ||
586 | 569 | value->contents.tree.n_children = n_children; | ||
587 | 570 | |||
588 | 571 | return value; | ||
589 | 572 | } | ||
590 | 573 | |||
591 | 574 | /* < internal > | ||
592 | 575 | * g_variant_get_type_info: | ||
593 | 576 | * @value: a #GVariant | ||
594 | 577 | * | ||
595 | 578 | * Returns the #GVariantTypeInfo corresponding to the type of @value. A | ||
596 | 579 | * reference is not added, so the return value is only good for the | ||
597 | 580 | * duration of the life of @value. | ||
598 | 581 | * | ||
599 | 582 | * Returns: the #GVariantTypeInfo for @value | ||
600 | 583 | */ | ||
601 | 584 | GVariantTypeInfo * | ||
602 | 585 | g_variant_get_type_info (GVariant *value) | ||
603 | 586 | { | ||
604 | 587 | return value->type_info; | ||
605 | 588 | } | ||
606 | 589 | |||
607 | 590 | /* < internal > | ||
608 | 591 | * g_variant_is_trusted: | ||
609 | 592 | * @value: a #GVariant | ||
610 | 593 | * | ||
611 | 594 | * Determines if @value is trusted by #GVariant to contain only | ||
612 | 595 | * fully-valid data. All values constructed solely via #GVariant APIs | ||
613 | 596 | * are trusted, but values containing data read in from other sources | ||
614 | 597 | * are usually not trusted. | ||
615 | 598 | * | ||
616 | 599 | * The main advantage of trusted data is that certain checks can be | ||
617 | 600 | * skipped. For example, we don't need to check that a string is | ||
618 | 601 | * properly nul-terminated or that an object path is actually a | ||
619 | 602 | * properly-formatted object path. | ||
620 | 603 | * | ||
621 | 604 | * Returns: if @value is trusted | ||
622 | 605 | */ | ||
623 | 606 | gboolean | ||
624 | 607 | g_variant_is_trusted (GVariant *value) | ||
625 | 608 | { | ||
626 | 609 | return (value->state & STATE_TRUSTED) != 0; | ||
627 | 610 | } | ||
628 | 611 | |||
629 | 612 | /* -- public -- */ | ||
630 | 613 | |||
631 | 614 | /** | ||
632 | 615 | * g_variant_unref: | ||
633 | 616 | * @value: a #GVariant | ||
634 | 617 | * | ||
635 | 618 | * Decreases the reference count of @value. When its reference count | ||
636 | 619 | * drops to 0, the memory used by the variant is freed. | ||
637 | 620 | * | ||
638 | 621 | * Since: 2.24 | ||
639 | 622 | **/ | ||
640 | 623 | void | ||
641 | 624 | g_variant_unref (GVariant *value) | ||
642 | 625 | { | ||
643 | 626 | g_return_if_fail (value != NULL); | ||
644 | 627 | g_return_if_fail (value->ref_count > 0); | ||
645 | 628 | |||
646 | 629 | if (g_atomic_int_dec_and_test (&value->ref_count)) | ||
647 | 630 | { | ||
648 | 631 | if G_UNLIKELY (value->state & STATE_LOCKED) | ||
649 | 632 | g_critical ("attempting to free a locked GVariant instance. " | ||
650 | 633 | "This should never happen."); | ||
651 | 634 | |||
652 | 635 | value->state |= STATE_LOCKED; | ||
653 | 636 | |||
654 | 637 | g_variant_type_info_unref (value->type_info); | ||
655 | 638 | |||
656 | 639 | if (value->state & STATE_SERIALISED) | ||
657 | 640 | g_bytes_unref (value->contents.serialised.bytes); | ||
658 | 641 | else | ||
659 | 642 | g_variant_release_children (value); | ||
660 | 643 | |||
661 | 644 | memset (value, 0, sizeof (GVariant)); | ||
662 | 645 | g_slice_free (GVariant, value); | ||
663 | 646 | } | ||
664 | 647 | } | ||
665 | 648 | |||
666 | 649 | /** | ||
667 | 650 | * g_variant_ref: | ||
668 | 651 | * @value: a #GVariant | ||
669 | 652 | * | ||
670 | 653 | * Increases the reference count of @value. | ||
671 | 654 | * | ||
672 | 655 | * Returns: the same @value | ||
673 | 656 | * | ||
674 | 657 | * Since: 2.24 | ||
675 | 658 | **/ | ||
676 | 659 | GVariant * | ||
677 | 660 | g_variant_ref (GVariant *value) | ||
678 | 661 | { | ||
679 | 662 | g_return_val_if_fail (value != NULL, NULL); | ||
680 | 663 | g_return_val_if_fail (value->ref_count > 0, NULL); | ||
681 | 664 | |||
682 | 665 | g_atomic_int_inc (&value->ref_count); | ||
683 | 666 | |||
684 | 667 | return value; | ||
685 | 668 | } | ||
686 | 669 | |||
687 | 670 | /** | ||
688 | 671 | * g_variant_ref_sink: | ||
689 | 672 | * @value: a #GVariant | ||
690 | 673 | * | ||
691 | 674 | * #GVariant uses a floating reference count system. All functions with | ||
692 | 675 | * names starting with <literal>g_variant_new_</literal> return floating | ||
693 | 676 | * references. | ||
694 | 677 | * | ||
695 | 678 | * Calling g_variant_ref_sink() on a #GVariant with a floating reference | ||
696 | 679 | * will convert the floating reference into a full reference. Calling | ||
697 | 680 | * g_variant_ref_sink() on a non-floating #GVariant results in an | ||
698 | 681 | * additional normal reference being added. | ||
699 | 682 | * | ||
700 | 683 | * In other words, if the @value is floating, then this call "assumes | ||
701 | 684 | * ownership" of the floating reference, converting it to a normal | ||
702 | 685 | * reference. If the @value is not floating, then this call adds a | ||
703 | 686 | * new normal reference increasing the reference count by one. | ||
704 | 687 | * | ||
705 | 688 | * All calls that result in a #GVariant instance being inserted into a | ||
706 | 689 | * container will call g_variant_ref_sink() on the instance. This means | ||
707 | 690 | * that if the value was just created (and has only its floating | ||
708 | 691 | * reference) then the container will assume sole ownership of the value | ||
709 | 692 | * at that point and the caller will not need to unreference it. This | ||
710 | 693 | * makes certain common styles of programming much easier while still | ||
711 | 694 | * maintaining normal refcounting semantics in situations where values | ||
712 | 695 | * are not floating. | ||
713 | 696 | * | ||
714 | 697 | * Returns: the same @value | ||
715 | 698 | * | ||
716 | 699 | * Since: 2.24 | ||
717 | 700 | **/ | ||
718 | 701 | GVariant * | ||
719 | 702 | g_variant_ref_sink (GVariant *value) | ||
720 | 703 | { | ||
721 | 704 | g_return_val_if_fail (value != NULL, NULL); | ||
722 | 705 | g_return_val_if_fail (value->ref_count > 0, NULL); | ||
723 | 706 | |||
724 | 707 | g_variant_lock (value); | ||
725 | 708 | |||
726 | 709 | if (~value->state & STATE_FLOATING) | ||
727 | 710 | g_variant_ref (value); | ||
728 | 711 | else | ||
729 | 712 | value->state &= ~STATE_FLOATING; | ||
730 | 713 | |||
731 | 714 | g_variant_unlock (value); | ||
732 | 715 | |||
733 | 716 | return value; | ||
734 | 717 | } | ||
735 | 718 | |||
736 | 719 | /** | ||
737 | 720 | * g_variant_take_ref: | ||
738 | 721 | * @value: a #GVariant | ||
739 | 722 | * | ||
740 | 723 | * If @value is floating, sink it. Otherwise, do nothing. | ||
741 | 724 | * | ||
742 | 725 | * Typically you want to use g_variant_ref_sink() in order to | ||
743 | 726 | * automatically do the correct thing with respect to floating or | ||
744 | 727 | * non-floating references, but there is one specific scenario where | ||
745 | 728 | * this function is helpful. | ||
746 | 729 | * | ||
747 | 730 | * The situation where this function is helpful is when creating an API | ||
748 | 731 | * that allows the user to provide a callback function that returns a | ||
749 | 732 | * #GVariant. We certainly want to allow the user the flexibility to | ||
750 | 733 | * return a non-floating reference from this callback (for the case | ||
751 | 734 | * where the value that is being returned already exists). | ||
752 | 735 | * | ||
753 | 736 | * At the same time, the style of the #GVariant API makes it likely that | ||
754 | 737 | * for newly-created #GVariant instances, the user can be saved some | ||
755 | 738 | * typing if they are allowed to return a #GVariant with a floating | ||
756 | 739 | * reference. | ||
757 | 740 | * | ||
758 | 741 | * Using this function on the return value of the user's callback allows | ||
759 | 742 | * the user to do whichever is more convenient for them. The caller | ||
760 | 743 | * will alway receives exactly one full reference to the value: either | ||
761 | 744 | * the one that was returned in the first place, or a floating reference | ||
762 | 745 | * that has been converted to a full reference. | ||
763 | 746 | * | ||
764 | 747 | * This function has an odd interaction when combined with | ||
765 | 748 | * g_variant_ref_sink() running at the same time in another thread on | ||
766 | 749 | * the same #GVariant instance. If g_variant_ref_sink() runs first then | ||
767 | 750 | * the result will be that the floating reference is converted to a hard | ||
768 | 751 | * reference. If g_variant_take_ref() runs first then the result will | ||
769 | 752 | * be that the floating reference is converted to a hard reference and | ||
770 | 753 | * an additional reference on top of that one is added. It is best to | ||
771 | 754 | * avoid this situation. | ||
772 | 755 | * | ||
773 | 756 | * Returns: the same @value | ||
774 | 757 | **/ | ||
775 | 758 | GVariant * | ||
776 | 759 | g_variant_take_ref (GVariant *value) | ||
777 | 760 | { | ||
778 | 761 | g_return_val_if_fail (value != NULL, NULL); | ||
779 | 762 | g_return_val_if_fail (value->ref_count > 0, NULL); | ||
780 | 763 | |||
781 | 764 | g_atomic_int_and (&value->state, ~STATE_FLOATING); | ||
782 | 765 | |||
783 | 766 | return value; | ||
784 | 767 | } | ||
785 | 768 | |||
786 | 769 | /** | ||
787 | 770 | * g_variant_is_floating: | ||
788 | 771 | * @value: a #GVariant | ||
789 | 772 | * | ||
790 | 773 | * Checks whether @value has a floating reference count. | ||
791 | 774 | * | ||
792 | 775 | * This function should only ever be used to assert that a given variant | ||
793 | 776 | * is or is not floating, or for debug purposes. To acquire a reference | ||
794 | 777 | * to a variant that might be floating, always use g_variant_ref_sink() | ||
795 | 778 | * or g_variant_take_ref(). | ||
796 | 779 | * | ||
797 | 780 | * See g_variant_ref_sink() for more information about floating reference | ||
798 | 781 | * counts. | ||
799 | 782 | * | ||
800 | 783 | * Returns: whether @value is floating | ||
801 | 784 | * | ||
802 | 785 | * Since: 2.26 | ||
803 | 786 | **/ | ||
804 | 787 | gboolean | ||
805 | 788 | g_variant_is_floating (GVariant *value) | ||
806 | 789 | { | ||
807 | 790 | g_return_val_if_fail (value != NULL, FALSE); | ||
808 | 791 | |||
809 | 792 | return (value->state & STATE_FLOATING) != 0; | ||
810 | 793 | } | ||
811 | 794 | |||
812 | 795 | /** | ||
813 | 796 | * g_variant_get_size: | ||
814 | 797 | * @value: a #GVariant instance | ||
815 | 798 | * | ||
816 | 799 | * Determines the number of bytes that would be required to store @value | ||
817 | 800 | * with g_variant_store(). | ||
818 | 801 | * | ||
819 | 802 | * If @value has a fixed-sized type then this function always returned | ||
820 | 803 | * that fixed size. | ||
821 | 804 | * | ||
822 | 805 | * In the case that @value is already in serialised form or the size has | ||
823 | 806 | * already been calculated (ie: this function has been called before) | ||
824 | 807 | * then this function is O(1). Otherwise, the size is calculated, an | ||
825 | 808 | * operation which is approximately O(n) in the number of values | ||
826 | 809 | * involved. | ||
827 | 810 | * | ||
828 | 811 | * Returns: the serialised size of @value | ||
829 | 812 | * | ||
830 | 813 | * Since: 2.24 | ||
831 | 814 | **/ | ||
832 | 815 | gsize | ||
833 | 816 | g_variant_get_size (GVariant *value) | ||
834 | 817 | { | ||
835 | 818 | g_variant_lock (value); | ||
836 | 819 | g_variant_ensure_size (value); | ||
837 | 820 | g_variant_unlock (value); | ||
838 | 821 | |||
839 | 822 | return value->size; | ||
840 | 823 | } | ||
841 | 824 | |||
842 | 825 | /** | ||
843 | 826 | * g_variant_get_data: | ||
844 | 827 | * @value: a #GVariant instance | ||
845 | 828 | * | ||
846 | 829 | * Returns a pointer to the serialised form of a #GVariant instance. | ||
847 | 830 | * The returned data may not be in fully-normalised form if read from an | ||
848 | 831 | * untrusted source. The returned data must not be freed; it remains | ||
849 | 832 | * valid for as long as @value exists. | ||
850 | 833 | * | ||
851 | 834 | * If @value is a fixed-sized value that was deserialised from a | ||
852 | 835 | * corrupted serialised container then %NULL may be returned. In this | ||
853 | 836 | * case, the proper thing to do is typically to use the appropriate | ||
854 | 837 | * number of nul bytes in place of @value. If @value is not fixed-sized | ||
855 | 838 | * then %NULL is never returned. | ||
856 | 839 | * | ||
857 | 840 | * In the case that @value is already in serialised form, this function | ||
858 | 841 | * is O(1). If the value is not already in serialised form, | ||
859 | 842 | * serialisation occurs implicitly and is approximately O(n) in the size | ||
860 | 843 | * of the result. | ||
861 | 844 | * | ||
862 | 845 | * To deserialise the data returned by this function, in addition to the | ||
863 | 846 | * serialised data, you must know the type of the #GVariant, and (if the | ||
864 | 847 | * machine might be different) the endianness of the machine that stored | ||
865 | 848 | * it. As a result, file formats or network messages that incorporate | ||
866 | 849 | * serialised #GVariant<!---->s must include this information either | ||
867 | 850 | * implicitly (for instance "the file always contains a | ||
868 | 851 | * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or | ||
869 | 852 | * explicitly (by storing the type and/or endianness in addition to the | ||
870 | 853 | * serialised data). | ||
871 | 854 | * | ||
872 | 855 | * Returns: (transfer none): the serialised form of @value, or %NULL | ||
873 | 856 | * | ||
874 | 857 | * Since: 2.24 | ||
875 | 858 | **/ | ||
876 | 859 | gconstpointer | ||
877 | 860 | g_variant_get_data (GVariant *value) | ||
878 | 861 | { | ||
879 | 862 | g_variant_lock (value); | ||
880 | 863 | g_variant_ensure_serialised (value); | ||
881 | 864 | g_variant_unlock (value); | ||
882 | 865 | |||
883 | 866 | return value->contents.serialised.data; | ||
884 | 867 | } | ||
885 | 868 | |||
886 | 869 | /** | ||
887 | 870 | * g_variant_get_data_as_bytes: | ||
888 | 871 | * @value: a #GVariant | ||
889 | 872 | * | ||
890 | 873 | * Returns a pointer to the serialised form of a #GVariant instance. | ||
891 | 874 | * The semantics of this function are exactly the same as | ||
892 | 875 | * g_variant_get_data(), except that the returned #GBytes holds | ||
893 | 876 | * a reference to the variant data. | ||
894 | 877 | * | ||
895 | 878 | * Returns: (transfer full): A new #GBytes representing the variant data | ||
896 | 879 | * | ||
897 | 880 | * Since: 2.36 | ||
898 | 881 | */ | ||
899 | 882 | GBytes * | ||
900 | 883 | g_variant_get_data_as_bytes (GVariant *value) | ||
901 | 884 | { | ||
902 | 885 | g_variant_lock (value); | ||
903 | 886 | g_variant_ensure_serialised (value); | ||
904 | 887 | g_variant_unlock (value); | ||
905 | 888 | |||
906 | 889 | return g_bytes_ref (value->contents.serialised.bytes); | ||
907 | 890 | } | ||
908 | 891 | |||
909 | 892 | |||
910 | 893 | /** | ||
911 | 894 | * g_variant_n_children: | ||
912 | 895 | * @value: a container #GVariant | ||
913 | 896 | * | ||
914 | 897 | * Determines the number of children in a container #GVariant instance. | ||
915 | 898 | * This includes variants, maybes, arrays, tuples and dictionary | ||
916 | 899 | * entries. It is an error to call this function on any other type of | ||
917 | 900 | * #GVariant. | ||
918 | 901 | * | ||
919 | 902 | * For variants, the return value is always 1. For values with maybe | ||
920 | 903 | * types, it is always zero or one. For arrays, it is the length of the | ||
921 | 904 | * array. For tuples it is the number of tuple items (which depends | ||
922 | 905 | * only on the type). For dictionary entries, it is always 2 | ||
923 | 906 | * | ||
924 | 907 | * This function is O(1). | ||
925 | 908 | * | ||
926 | 909 | * Returns: the number of children in the container | ||
927 | 910 | * | ||
928 | 911 | * Since: 2.24 | ||
929 | 912 | **/ | ||
930 | 913 | gsize | ||
931 | 914 | g_variant_n_children (GVariant *value) | ||
932 | 915 | { | ||
933 | 916 | gsize n_children; | ||
934 | 917 | |||
935 | 918 | g_variant_lock (value); | ||
936 | 919 | |||
937 | 920 | if (value->state & STATE_SERIALISED) | ||
938 | 921 | { | ||
939 | 922 | GVariantSerialised serialised = { | ||
940 | 923 | value->type_info, | ||
941 | 924 | (gpointer) value->contents.serialised.data, | ||
942 | 925 | value->size | ||
943 | 926 | }; | ||
944 | 927 | |||
945 | 928 | n_children = g_variant_serialised_n_children (serialised); | ||
946 | 929 | } | ||
947 | 930 | else | ||
948 | 931 | n_children = value->contents.tree.n_children; | ||
949 | 932 | |||
950 | 933 | g_variant_unlock (value); | ||
951 | 934 | |||
952 | 935 | return n_children; | ||
953 | 936 | } | ||
954 | 937 | |||
955 | 938 | /** | ||
956 | 939 | * g_variant_get_child_value: | ||
957 | 940 | * @value: a container #GVariant | ||
958 | 941 | * @index_: the index of the child to fetch | ||
959 | 942 | * | ||
960 | 943 | * Reads a child item out of a container #GVariant instance. This | ||
961 | 944 | * includes variants, maybes, arrays, tuples and dictionary | ||
962 | 945 | * entries. It is an error to call this function on any other type of | ||
963 | 946 | * #GVariant. | ||
964 | 947 | * | ||
965 | 948 | * It is an error if @index_ is greater than the number of child items | ||
966 | 949 | * in the container. See g_variant_n_children(). | ||
967 | 950 | * | ||
968 | 951 | * The returned value is never floating. You should free it with | ||
969 | 952 | * g_variant_unref() when you're done with it. | ||
970 | 953 | * | ||
971 | 954 | * This function is O(1). | ||
972 | 955 | * | ||
973 | 956 | * Returns: (transfer full): the child at the specified index | ||
974 | 957 | * | ||
975 | 958 | * Since: 2.24 | ||
976 | 959 | **/ | ||
977 | 960 | GVariant * | ||
978 | 961 | g_variant_get_child_value (GVariant *value, | ||
979 | 962 | gsize index_) | ||
980 | 963 | { | ||
981 | 964 | g_return_val_if_fail (index_ < g_variant_n_children (value), NULL); | ||
982 | 965 | |||
983 | 966 | if (~g_atomic_int_get (&value->state) & STATE_SERIALISED) | ||
984 | 967 | { | ||
985 | 968 | g_variant_lock (value); | ||
986 | 969 | |||
987 | 970 | if (~value->state & STATE_SERIALISED) | ||
988 | 971 | { | ||
989 | 972 | GVariant *child; | ||
990 | 973 | |||
991 | 974 | child = g_variant_ref (value->contents.tree.children[index_]); | ||
992 | 975 | g_variant_unlock (value); | ||
993 | 976 | |||
994 | 977 | return child; | ||
995 | 978 | } | ||
996 | 979 | |||
997 | 980 | g_variant_unlock (value); | ||
998 | 981 | } | ||
999 | 982 | |||
1000 | 983 | { | ||
1001 | 984 | GVariantSerialised serialised = { | ||
1002 | 985 | value->type_info, | ||
1003 | 986 | (gpointer) value->contents.serialised.data, | ||
1004 | 987 | value->size | ||
1005 | 988 | }; | ||
1006 | 989 | GVariantSerialised s_child; | ||
1007 | 990 | GVariant *child; | ||
1008 | 991 | |||
1009 | 992 | /* get the serialiser to extract the serialised data for the child | ||
1010 | 993 | * from the serialised data for the container | ||
1011 | 994 | */ | ||
1012 | 995 | s_child = g_variant_serialised_get_child (serialised, index_); | ||
1013 | 996 | |||
1014 | 997 | /* create a new serialised instance out of it */ | ||
1015 | 998 | child = g_slice_new (GVariant); | ||
1016 | 999 | child->type_info = s_child.type_info; | ||
1017 | 1000 | child->state = (value->state & STATE_TRUSTED) | | ||
1018 | 1001 | STATE_SERIALISED; | ||
1019 | 1002 | child->size = s_child.size; | ||
1020 | 1003 | child->ref_count = 1; | ||
1021 | 1004 | child->contents.serialised.bytes = | ||
1022 | 1005 | g_bytes_ref (value->contents.serialised.bytes); | ||
1023 | 1006 | child->contents.serialised.data = s_child.data; | ||
1024 | 1007 | |||
1025 | 1008 | return child; | ||
1026 | 1009 | } | ||
1027 | 1010 | } | ||
1028 | 1011 | |||
1029 | 1012 | /** | ||
1030 | 1013 | * g_variant_store: | ||
1031 | 1014 | * @value: the #GVariant to store | ||
1032 | 1015 | * @data: the location to store the serialised data at | ||
1033 | 1016 | * | ||
1034 | 1017 | * Stores the serialised form of @value at @data. @data should be | ||
1035 | 1018 | * large enough. See g_variant_get_size(). | ||
1036 | 1019 | * | ||
1037 | 1020 | * The stored data is in machine native byte order but may not be in | ||
1038 | 1021 | * fully-normalised form if read from an untrusted source. See | ||
1039 | 1022 | * g_variant_get_normal_form() for a solution. | ||
1040 | 1023 | * | ||
1041 | 1024 | * As with g_variant_get_data(), to be able to deserialise the | ||
1042 | 1025 | * serialised variant successfully, its type and (if the destination | ||
1043 | 1026 | * machine might be different) its endianness must also be available. | ||
1044 | 1027 | * | ||
1045 | 1028 | * This function is approximately O(n) in the size of @data. | ||
1046 | 1029 | * | ||
1047 | 1030 | * Since: 2.24 | ||
1048 | 1031 | **/ | ||
1049 | 1032 | void | ||
1050 | 1033 | g_variant_store (GVariant *value, | ||
1051 | 1034 | gpointer data) | ||
1052 | 1035 | { | ||
1053 | 1036 | g_variant_lock (value); | ||
1054 | 1037 | |||
1055 | 1038 | if (value->state & STATE_SERIALISED) | ||
1056 | 1039 | { | ||
1057 | 1040 | if (value->contents.serialised.data != NULL) | ||
1058 | 1041 | memcpy (data, value->contents.serialised.data, value->size); | ||
1059 | 1042 | else | ||
1060 | 1043 | memset (data, 0, value->size); | ||
1061 | 1044 | } | ||
1062 | 1045 | else | ||
1063 | 1046 | g_variant_serialise (value, data); | ||
1064 | 1047 | |||
1065 | 1048 | g_variant_unlock (value); | ||
1066 | 1049 | } | ||
1067 | 1050 | |||
1068 | 1051 | /** | ||
1069 | 1052 | * g_variant_is_normal_form: | ||
1070 | 1053 | * @value: a #GVariant instance | ||
1071 | 1054 | * | ||
1072 | 1055 | * Checks if @value is in normal form. | ||
1073 | 1056 | * | ||
1074 | 1057 | * The main reason to do this is to detect if a given chunk of | ||
1075 | 1058 | * serialised data is in normal form: load the data into a #GVariant | ||
1076 | 1059 | * using g_variant_new_from_data() and then use this function to | ||
1077 | 1060 | * check. | ||
1078 | 1061 | * | ||
1079 | 1062 | * If @value is found to be in normal form then it will be marked as | ||
1080 | 1063 | * being trusted. If the value was already marked as being trusted then | ||
1081 | 1064 | * this function will immediately return %TRUE. | ||
1082 | 1065 | * | ||
1083 | 1066 | * Returns: %TRUE if @value is in normal form | ||
1084 | 1067 | * | ||
1085 | 1068 | * Since: 2.24 | ||
1086 | 1069 | **/ | ||
1087 | 1070 | gboolean | ||
1088 | 1071 | g_variant_is_normal_form (GVariant *value) | ||
1089 | 1072 | { | ||
1090 | 1073 | if (value->state & STATE_TRUSTED) | ||
1091 | 1074 | return TRUE; | ||
1092 | 1075 | |||
1093 | 1076 | g_variant_lock (value); | ||
1094 | 1077 | |||
1095 | 1078 | if (value->state & STATE_SERIALISED) | ||
1096 | 1079 | { | ||
1097 | 1080 | GVariantSerialised serialised = { | ||
1098 | 1081 | value->type_info, | ||
1099 | 1082 | (gpointer) value->contents.serialised.data, | ||
1100 | 1083 | value->size | ||
1101 | 1084 | }; | ||
1102 | 1085 | |||
1103 | 1086 | if (g_variant_serialised_is_normal (serialised)) | ||
1104 | 1087 | value->state |= STATE_TRUSTED; | ||
1105 | 1088 | } | ||
1106 | 1089 | else | ||
1107 | 1090 | { | ||
1108 | 1091 | gboolean normal = TRUE; | ||
1109 | 1092 | gsize i; | ||
1110 | 1093 | |||
1111 | 1094 | for (i = 0; i < value->contents.tree.n_children; i++) | ||
1112 | 1095 | normal &= g_variant_is_normal_form (value->contents.tree.children[i]); | ||
1113 | 1096 | |||
1114 | 1097 | if (normal) | ||
1115 | 1098 | value->state |= STATE_TRUSTED; | ||
1116 | 1099 | } | ||
1117 | 1100 | |||
1118 | 1101 | g_variant_unlock (value); | ||
1119 | 1102 | |||
1120 | 1103 | return (value->state & STATE_TRUSTED) != 0; | ||
1121 | 1104 | } | ||
1122 | 0 | 1105 | ||
1123 | === added directory '.pc/g-variant-get-data-as-bytes.patch/glib/tests' | |||
1124 | === added file '.pc/g-variant-get-data-as-bytes.patch/glib/tests/gvariant.c' | |||
1125 | --- .pc/g-variant-get-data-as-bytes.patch/glib/tests/gvariant.c 1970-01-01 00:00:00 +0000 | |||
1126 | +++ .pc/g-variant-get-data-as-bytes.patch/glib/tests/gvariant.c 2013-05-03 17:57:29 +0000 | |||
1127 | @@ -0,0 +1,4339 @@ | |||
1128 | 1 | /* | ||
1129 | 2 | * Copyright © 2010 Codethink Limited | ||
1130 | 3 | * | ||
1131 | 4 | * This library is free software; you can redistribute it and/or | ||
1132 | 5 | * modify it under the terms of the GNU Lesser General Public | ||
1133 | 6 | * License as published by the Free Software Foundation; either | ||
1134 | 7 | * version 2 of the licence, or (at your option) any later version. | ||
1135 | 8 | * | ||
1136 | 9 | * See the included COPYING file for more information. | ||
1137 | 10 | * | ||
1138 | 11 | * Author: Ryan Lortie <desrt@desrt.ca> | ||
1139 | 12 | */ | ||
1140 | 13 | |||
1141 | 14 | #include "config.h" | ||
1142 | 15 | |||
1143 | 16 | #include <glib/gvariant-internal.h> | ||
1144 | 17 | #include <string.h> | ||
1145 | 18 | #include <stdlib.h> | ||
1146 | 19 | #include <glib.h> | ||
1147 | 20 | |||
1148 | 21 | #define BASIC "bynqiuxthdsog?" | ||
1149 | 22 | #define N_BASIC (G_N_ELEMENTS (BASIC) - 1) | ||
1150 | 23 | |||
1151 | 24 | #define INVALIDS "cefjklpwz&@^$" | ||
1152 | 25 | #define N_INVALIDS (G_N_ELEMENTS (INVALIDS) - 1) | ||
1153 | 26 | |||
1154 | 27 | /* see comment in gvariant-serialiser.c about this madness. | ||
1155 | 28 | * | ||
1156 | 29 | * we use this to get testing of non-strictly-aligned GVariant instances | ||
1157 | 30 | * on machines that can tolerate it. it is necessary to support this | ||
1158 | 31 | * because some systems have malloc() that returns non-8-aligned | ||
1159 | 32 | * pointers. it is necessary to have special support in the tests | ||
1160 | 33 | * because on most machines malloc() is 8-aligned. | ||
1161 | 34 | */ | ||
1162 | 35 | #define ALIGN_BITS (sizeof (struct { char a; union { \ | ||
1163 | 36 | guint64 x; void *y; gdouble z; } b; }) - 9) | ||
1164 | 37 | |||
1165 | 38 | static gboolean | ||
1166 | 39 | randomly (gdouble prob) | ||
1167 | 40 | { | ||
1168 | 41 | return g_test_rand_double_range (0, 1) < prob; | ||
1169 | 42 | } | ||
1170 | 43 | |||
1171 | 44 | /* corecursion */ | ||
1172 | 45 | static GVariantType * | ||
1173 | 46 | append_tuple_type_string (GString *, GString *, gboolean, gint); | ||
1174 | 47 | |||
1175 | 48 | /* append a random GVariantType to a GString | ||
1176 | 49 | * append a description of the type to another GString | ||
1177 | 50 | * return what the type is | ||
1178 | 51 | */ | ||
1179 | 52 | static GVariantType * | ||
1180 | 53 | append_type_string (GString *string, | ||
1181 | 54 | GString *description, | ||
1182 | 55 | gboolean definite, | ||
1183 | 56 | gint depth) | ||
1184 | 57 | { | ||
1185 | 58 | if (!depth-- || randomly (0.3)) | ||
1186 | 59 | { | ||
1187 | 60 | gchar b = BASIC[g_test_rand_int_range (0, N_BASIC - definite)]; | ||
1188 | 61 | g_string_append_c (string, b); | ||
1189 | 62 | g_string_append_c (description, b); | ||
1190 | 63 | |||
1191 | 64 | switch (b) | ||
1192 | 65 | { | ||
1193 | 66 | case 'b': | ||
1194 | 67 | return g_variant_type_copy (G_VARIANT_TYPE_BOOLEAN); | ||
1195 | 68 | case 'y': | ||
1196 | 69 | return g_variant_type_copy (G_VARIANT_TYPE_BYTE); | ||
1197 | 70 | case 'n': | ||
1198 | 71 | return g_variant_type_copy (G_VARIANT_TYPE_INT16); | ||
1199 | 72 | case 'q': | ||
1200 | 73 | return g_variant_type_copy (G_VARIANT_TYPE_UINT16); | ||
1201 | 74 | case 'i': | ||
1202 | 75 | return g_variant_type_copy (G_VARIANT_TYPE_INT32); | ||
1203 | 76 | case 'u': | ||
1204 | 77 | return g_variant_type_copy (G_VARIANT_TYPE_UINT32); | ||
1205 | 78 | case 'x': | ||
1206 | 79 | return g_variant_type_copy (G_VARIANT_TYPE_INT64); | ||
1207 | 80 | case 't': | ||
1208 | 81 | return g_variant_type_copy (G_VARIANT_TYPE_UINT64); | ||
1209 | 82 | case 'h': | ||
1210 | 83 | return g_variant_type_copy (G_VARIANT_TYPE_HANDLE); | ||
1211 | 84 | case 'd': | ||
1212 | 85 | return g_variant_type_copy (G_VARIANT_TYPE_DOUBLE); | ||
1213 | 86 | case 's': | ||
1214 | 87 | return g_variant_type_copy (G_VARIANT_TYPE_STRING); | ||
1215 | 88 | case 'o': | ||
1216 | 89 | return g_variant_type_copy (G_VARIANT_TYPE_OBJECT_PATH); | ||
1217 | 90 | case 'g': | ||
1218 | 91 | return g_variant_type_copy (G_VARIANT_TYPE_SIGNATURE); | ||
1219 | 92 | case '?': | ||
1220 | 93 | return g_variant_type_copy (G_VARIANT_TYPE_BASIC); | ||
1221 | 94 | default: | ||
1222 | 95 | g_assert_not_reached (); | ||
1223 | 96 | } | ||
1224 | 97 | } | ||
1225 | 98 | else | ||
1226 | 99 | { | ||
1227 | 100 | GVariantType *result; | ||
1228 | 101 | |||
1229 | 102 | switch (g_test_rand_int_range (0, definite ? 5 : 7)) | ||
1230 | 103 | { | ||
1231 | 104 | case 0: | ||
1232 | 105 | { | ||
1233 | 106 | GVariantType *element; | ||
1234 | 107 | |||
1235 | 108 | g_string_append_c (string, 'a'); | ||
1236 | 109 | g_string_append (description, "a of "); | ||
1237 | 110 | element = append_type_string (string, description, | ||
1238 | 111 | definite, depth); | ||
1239 | 112 | result = g_variant_type_new_array (element); | ||
1240 | 113 | g_variant_type_free (element); | ||
1241 | 114 | } | ||
1242 | 115 | |||
1243 | 116 | g_assert (g_variant_type_is_array (result)); | ||
1244 | 117 | break; | ||
1245 | 118 | |||
1246 | 119 | case 1: | ||
1247 | 120 | { | ||
1248 | 121 | GVariantType *element; | ||
1249 | 122 | |||
1250 | 123 | g_string_append_c (string, 'm'); | ||
1251 | 124 | g_string_append (description, "m of "); | ||
1252 | 125 | element = append_type_string (string, description, | ||
1253 | 126 | definite, depth); | ||
1254 | 127 | result = g_variant_type_new_maybe (element); | ||
1255 | 128 | g_variant_type_free (element); | ||
1256 | 129 | } | ||
1257 | 130 | |||
1258 | 131 | g_assert (g_variant_type_is_maybe (result)); | ||
1259 | 132 | break; | ||
1260 | 133 | |||
1261 | 134 | case 2: | ||
1262 | 135 | result = append_tuple_type_string (string, description, | ||
1263 | 136 | definite, depth); | ||
1264 | 137 | |||
1265 | 138 | g_assert (g_variant_type_is_tuple (result)); | ||
1266 | 139 | break; | ||
1267 | 140 | |||
1268 | 141 | case 3: | ||
1269 | 142 | { | ||
1270 | 143 | GVariantType *key, *value; | ||
1271 | 144 | |||
1272 | 145 | g_string_append_c (string, '{'); | ||
1273 | 146 | g_string_append (description, "e of ["); | ||
1274 | 147 | key = append_type_string (string, description, definite, 0); | ||
1275 | 148 | g_string_append (description, ", "); | ||
1276 | 149 | value = append_type_string (string, description, definite, depth); | ||
1277 | 150 | g_string_append_c (description, ']'); | ||
1278 | 151 | g_string_append_c (string, '}'); | ||
1279 | 152 | result = g_variant_type_new_dict_entry (key, value); | ||
1280 | 153 | g_variant_type_free (key); | ||
1281 | 154 | g_variant_type_free (value); | ||
1282 | 155 | } | ||
1283 | 156 | |||
1284 | 157 | g_assert (g_variant_type_is_dict_entry (result)); | ||
1285 | 158 | break; | ||
1286 | 159 | |||
1287 | 160 | case 4: | ||
1288 | 161 | g_string_append_c (string, 'v'); | ||
1289 | 162 | g_string_append_c (description, 'V'); | ||
1290 | 163 | result = g_variant_type_copy (G_VARIANT_TYPE_VARIANT); | ||
1291 | 164 | g_assert (g_variant_type_equal (result, G_VARIANT_TYPE_VARIANT)); | ||
1292 | 165 | break; | ||
1293 | 166 | |||
1294 | 167 | case 5: | ||
1295 | 168 | g_string_append_c (string, '*'); | ||
1296 | 169 | g_string_append_c (description, 'S'); | ||
1297 | 170 | result = g_variant_type_copy (G_VARIANT_TYPE_ANY); | ||
1298 | 171 | g_assert (g_variant_type_equal (result, G_VARIANT_TYPE_ANY)); | ||
1299 | 172 | break; | ||
1300 | 173 | |||
1301 | 174 | case 6: | ||
1302 | 175 | g_string_append_c (string, 'r'); | ||
1303 | 176 | g_string_append_c (description, 'R'); | ||
1304 | 177 | result = g_variant_type_copy (G_VARIANT_TYPE_TUPLE); | ||
1305 | 178 | g_assert (g_variant_type_is_tuple (result)); | ||
1306 | 179 | break; | ||
1307 | 180 | |||
1308 | 181 | default: | ||
1309 | 182 | g_assert_not_reached (); | ||
1310 | 183 | } | ||
1311 | 184 | |||
1312 | 185 | return result; | ||
1313 | 186 | } | ||
1314 | 187 | } | ||
1315 | 188 | |||
1316 | 189 | static GVariantType * | ||
1317 | 190 | append_tuple_type_string (GString *string, | ||
1318 | 191 | GString *description, | ||
1319 | 192 | gboolean definite, | ||
1320 | 193 | gint depth) | ||
1321 | 194 | { | ||
1322 | 195 | GVariantType *result, *other_result; | ||
1323 | 196 | GVariantType **types; | ||
1324 | 197 | gint size; | ||
1325 | 198 | gint i; | ||
1326 | 199 | |||
1327 | 200 | g_string_append_c (string, '('); | ||
1328 | 201 | g_string_append (description, "t of ["); | ||
1329 | 202 | |||
1330 | 203 | size = g_test_rand_int_range (0, 20); | ||
1331 | 204 | types = g_new (GVariantType *, size + 1); | ||
1332 | 205 | |||
1333 | 206 | for (i = 0; i < size; i++) | ||
1334 | 207 | { | ||
1335 | 208 | types[i] = append_type_string (string, description, definite, depth); | ||
1336 | 209 | |||
1337 | 210 | if (i < size - 1) | ||
1338 | 211 | g_string_append (description, ", "); | ||
1339 | 212 | } | ||
1340 | 213 | |||
1341 | 214 | types[i] = NULL; | ||
1342 | 215 | |||
1343 | 216 | g_string_append_c (description, ']'); | ||
1344 | 217 | g_string_append_c (string, ')'); | ||
1345 | 218 | |||
1346 | 219 | result = g_variant_type_new_tuple ((gpointer) types, size); | ||
1347 | 220 | other_result = g_variant_type_new_tuple ((gpointer) types, -1); | ||
1348 | 221 | g_assert (g_variant_type_equal (result, other_result)); | ||
1349 | 222 | g_variant_type_free (other_result); | ||
1350 | 223 | for (i = 0; i < size; i++) | ||
1351 | 224 | g_variant_type_free (types[i]); | ||
1352 | 225 | g_free (types); | ||
1353 | 226 | |||
1354 | 227 | return result; | ||
1355 | 228 | } | ||
1356 | 229 | |||
1357 | 230 | /* given a valid type string, make it invalid */ | ||
1358 | 231 | static gchar * | ||
1359 | 232 | invalid_mutation (const gchar *type_string) | ||
1360 | 233 | { | ||
1361 | 234 | gboolean have_parens, have_braces; | ||
1362 | 235 | |||
1363 | 236 | /* it's valid, so '(' implies ')' and same for '{' and '}' */ | ||
1364 | 237 | have_parens = strchr (type_string, '(') != NULL; | ||
1365 | 238 | have_braces = strchr (type_string, '{') != NULL; | ||
1366 | 239 | |||
1367 | 240 | if (have_parens && have_braces && randomly (0.3)) | ||
1368 | 241 | { | ||
1369 | 242 | /* swap a paren and a brace */ | ||
1370 | 243 | gchar *pp, *bp; | ||
1371 | 244 | gint np, nb; | ||
1372 | 245 | gchar p, b; | ||
1373 | 246 | gchar *new; | ||
1374 | 247 | |||
1375 | 248 | new = g_strdup (type_string); | ||
1376 | 249 | |||
1377 | 250 | if (randomly (0.5)) | ||
1378 | 251 | p = '(', b = '{'; | ||
1379 | 252 | else | ||
1380 | 253 | p = ')', b = '}'; | ||
1381 | 254 | |||
1382 | 255 | np = nb = 0; | ||
1383 | 256 | pp = bp = new - 1; | ||
1384 | 257 | |||
1385 | 258 | /* count number of parens/braces */ | ||
1386 | 259 | while ((pp = strchr (pp + 1, p))) np++; | ||
1387 | 260 | while ((bp = strchr (bp + 1, b))) nb++; | ||
1388 | 261 | |||
1389 | 262 | /* randomly pick one of each */ | ||
1390 | 263 | np = g_test_rand_int_range (0, np) + 1; | ||
1391 | 264 | nb = g_test_rand_int_range (0, nb) + 1; | ||
1392 | 265 | |||
1393 | 266 | /* find it */ | ||
1394 | 267 | pp = bp = new - 1; | ||
1395 | 268 | while (np--) pp = strchr (pp + 1, p); | ||
1396 | 269 | while (nb--) bp = strchr (bp + 1, b); | ||
1397 | 270 | |||
1398 | 271 | /* swap */ | ||
1399 | 272 | g_assert (*bp == b && *pp == p); | ||
1400 | 273 | *bp = p; | ||
1401 | 274 | *pp = b; | ||
1402 | 275 | |||
1403 | 276 | return new; | ||
1404 | 277 | } | ||
1405 | 278 | |||
1406 | 279 | if ((have_parens || have_braces) && randomly (0.3)) | ||
1407 | 280 | { | ||
1408 | 281 | /* drop a paren/brace */ | ||
1409 | 282 | gchar *new; | ||
1410 | 283 | gchar *pp; | ||
1411 | 284 | gint np; | ||
1412 | 285 | gchar p; | ||
1413 | 286 | |||
1414 | 287 | if (have_parens) | ||
1415 | 288 | if (randomly (0.5)) p = '('; else p = ')'; | ||
1416 | 289 | else | ||
1417 | 290 | if (randomly (0.5)) p = '{'; else p = '}'; | ||
1418 | 291 | |||
1419 | 292 | new = g_strdup (type_string); | ||
1420 | 293 | |||
1421 | 294 | np = 0; | ||
1422 | 295 | pp = new - 1; | ||
1423 | 296 | while ((pp = strchr (pp + 1, p))) np++; | ||
1424 | 297 | np = g_test_rand_int_range (0, np) + 1; | ||
1425 | 298 | pp = new - 1; | ||
1426 | 299 | while (np--) pp = strchr (pp + 1, p); | ||
1427 | 300 | g_assert (*pp == p); | ||
1428 | 301 | |||
1429 | 302 | while (*pp) | ||
1430 | 303 | { | ||
1431 | 304 | *pp = *(pp + 1); | ||
1432 | 305 | pp++; | ||
1433 | 306 | } | ||
1434 | 307 | |||
1435 | 308 | return new; | ||
1436 | 309 | } | ||
1437 | 310 | |||
1438 | 311 | /* else, perform a random mutation at a random point */ | ||
1439 | 312 | { | ||
1440 | 313 | gint length, n; | ||
1441 | 314 | gchar *new; | ||
1442 | 315 | gchar p; | ||
1443 | 316 | |||
1444 | 317 | if (randomly (0.3)) | ||
1445 | 318 | { | ||
1446 | 319 | /* insert a paren/brace */ | ||
1447 | 320 | if (randomly (0.5)) | ||
1448 | 321 | if (randomly (0.5)) p = '('; else p = ')'; | ||
1449 | 322 | else | ||
1450 | 323 | if (randomly (0.5)) p = '{'; else p = '}'; | ||
1451 | 324 | } | ||
1452 | 325 | else if (randomly (0.5)) | ||
1453 | 326 | { | ||
1454 | 327 | /* insert junk */ | ||
1455 | 328 | p = INVALIDS[g_test_rand_int_range (0, N_INVALIDS)]; | ||
1456 | 329 | } | ||
1457 | 330 | else | ||
1458 | 331 | { | ||
1459 | 332 | /* truncate */ | ||
1460 | 333 | p = '\0'; | ||
1461 | 334 | } | ||
1462 | 335 | |||
1463 | 336 | |||
1464 | 337 | length = strlen (type_string); | ||
1465 | 338 | new = g_malloc (length + 2); | ||
1466 | 339 | n = g_test_rand_int_range (0, length); | ||
1467 | 340 | memcpy (new, type_string, n); | ||
1468 | 341 | new[n] = p; | ||
1469 | 342 | memcpy (new + n + 1, type_string + n, length - n); | ||
1470 | 343 | new[length + 1] = '\0'; | ||
1471 | 344 | |||
1472 | 345 | return new; | ||
1473 | 346 | } | ||
1474 | 347 | } | ||
1475 | 348 | |||
1476 | 349 | /* describe a type using the same language as is generated | ||
1477 | 350 | * while generating the type with append_type_string | ||
1478 | 351 | */ | ||
1479 | 352 | static gchar * | ||
1480 | 353 | describe_type (const GVariantType *type) | ||
1481 | 354 | { | ||
1482 | 355 | gchar *result; | ||
1483 | 356 | |||
1484 | 357 | if (g_variant_type_is_container (type)) | ||
1485 | 358 | { | ||
1486 | 359 | g_assert (!g_variant_type_is_basic (type)); | ||
1487 | 360 | |||
1488 | 361 | if (g_variant_type_is_array (type)) | ||
1489 | 362 | { | ||
1490 | 363 | gchar *subtype = describe_type (g_variant_type_element (type)); | ||
1491 | 364 | result = g_strdup_printf ("a of %s", subtype); | ||
1492 | 365 | g_free (subtype); | ||
1493 | 366 | } | ||
1494 | 367 | else if (g_variant_type_is_maybe (type)) | ||
1495 | 368 | { | ||
1496 | 369 | gchar *subtype = describe_type (g_variant_type_element (type)); | ||
1497 | 370 | result = g_strdup_printf ("m of %s", subtype); | ||
1498 | 371 | g_free (subtype); | ||
1499 | 372 | } | ||
1500 | 373 | else if (g_variant_type_is_tuple (type)) | ||
1501 | 374 | { | ||
1502 | 375 | if (!g_variant_type_equal (type, G_VARIANT_TYPE_TUPLE)) | ||
1503 | 376 | { | ||
1504 | 377 | const GVariantType *sub; | ||
1505 | 378 | GString *string; | ||
1506 | 379 | gint length; | ||
1507 | 380 | gint i; | ||
1508 | 381 | |||
1509 | 382 | string = g_string_new ("t of ["); | ||
1510 | 383 | |||
1511 | 384 | length = g_variant_type_n_items (type); | ||
1512 | 385 | sub = g_variant_type_first (type); | ||
1513 | 386 | for (i = 0; i < length; i++) | ||
1514 | 387 | { | ||
1515 | 388 | gchar *subtype = describe_type (sub); | ||
1516 | 389 | g_string_append (string, subtype); | ||
1517 | 390 | g_free (subtype); | ||
1518 | 391 | |||
1519 | 392 | if ((sub = g_variant_type_next (sub))) | ||
1520 | 393 | g_string_append (string, ", "); | ||
1521 | 394 | } | ||
1522 | 395 | g_assert (sub == NULL); | ||
1523 | 396 | g_string_append_c (string, ']'); | ||
1524 | 397 | |||
1525 | 398 | result = g_string_free (string, FALSE); | ||
1526 | 399 | } | ||
1527 | 400 | else | ||
1528 | 401 | result = g_strdup ("R"); | ||
1529 | 402 | } | ||
1530 | 403 | else if (g_variant_type_is_dict_entry (type)) | ||
1531 | 404 | { | ||
1532 | 405 | gchar *key, *value, *key2, *value2; | ||
1533 | 406 | |||
1534 | 407 | key = describe_type (g_variant_type_key (type)); | ||
1535 | 408 | value = describe_type (g_variant_type_value (type)); | ||
1536 | 409 | key2 = describe_type (g_variant_type_first (type)); | ||
1537 | 410 | value2 = describe_type ( | ||
1538 | 411 | g_variant_type_next (g_variant_type_first (type))); | ||
1539 | 412 | g_assert (g_variant_type_next (g_variant_type_next ( | ||
1540 | 413 | g_variant_type_first (type))) == NULL); | ||
1541 | 414 | g_assert_cmpstr (key, ==, key2); | ||
1542 | 415 | g_assert_cmpstr (value, ==, value2); | ||
1543 | 416 | result = g_strjoin ("", "e of [", key, ", ", value, "]", NULL); | ||
1544 | 417 | g_free (key2); | ||
1545 | 418 | g_free (value2); | ||
1546 | 419 | g_free (key); | ||
1547 | 420 | g_free (value); | ||
1548 | 421 | } | ||
1549 | 422 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT)) | ||
1550 | 423 | { | ||
1551 | 424 | result = g_strdup ("V"); | ||
1552 | 425 | } | ||
1553 | 426 | else | ||
1554 | 427 | g_assert_not_reached (); | ||
1555 | 428 | } | ||
1556 | 429 | else | ||
1557 | 430 | { | ||
1558 | 431 | if (g_variant_type_is_definite (type)) | ||
1559 | 432 | { | ||
1560 | 433 | g_assert (g_variant_type_is_basic (type)); | ||
1561 | 434 | |||
1562 | 435 | if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN)) | ||
1563 | 436 | result = g_strdup ("b"); | ||
1564 | 437 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE)) | ||
1565 | 438 | result = g_strdup ("y"); | ||
1566 | 439 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16)) | ||
1567 | 440 | result = g_strdup ("n"); | ||
1568 | 441 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16)) | ||
1569 | 442 | result = g_strdup ("q"); | ||
1570 | 443 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32)) | ||
1571 | 444 | result = g_strdup ("i"); | ||
1572 | 445 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32)) | ||
1573 | 446 | result = g_strdup ("u"); | ||
1574 | 447 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64)) | ||
1575 | 448 | result = g_strdup ("x"); | ||
1576 | 449 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64)) | ||
1577 | 450 | result = g_strdup ("t"); | ||
1578 | 451 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE)) | ||
1579 | 452 | result = g_strdup ("h"); | ||
1580 | 453 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE)) | ||
1581 | 454 | result = g_strdup ("d"); | ||
1582 | 455 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING)) | ||
1583 | 456 | result = g_strdup ("s"); | ||
1584 | 457 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH)) | ||
1585 | 458 | result = g_strdup ("o"); | ||
1586 | 459 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE)) | ||
1587 | 460 | result = g_strdup ("g"); | ||
1588 | 461 | else | ||
1589 | 462 | g_assert_not_reached (); | ||
1590 | 463 | } | ||
1591 | 464 | else | ||
1592 | 465 | { | ||
1593 | 466 | if (g_variant_type_equal (type, G_VARIANT_TYPE_ANY)) | ||
1594 | 467 | { | ||
1595 | 468 | result = g_strdup ("S"); | ||
1596 | 469 | } | ||
1597 | 470 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_BASIC)) | ||
1598 | 471 | { | ||
1599 | 472 | result = g_strdup ("?"); | ||
1600 | 473 | } | ||
1601 | 474 | else | ||
1602 | 475 | g_assert_not_reached (); | ||
1603 | 476 | } | ||
1604 | 477 | } | ||
1605 | 478 | |||
1606 | 479 | return result; | ||
1607 | 480 | } | ||
1608 | 481 | |||
1609 | 482 | /* given a type string, replace one of the indefinite type characters in | ||
1610 | 483 | * it with a matching type (possibly the same type). | ||
1611 | 484 | */ | ||
1612 | 485 | static gchar * | ||
1613 | 486 | generate_subtype (const gchar *type_string) | ||
1614 | 487 | { | ||
1615 | 488 | GVariantType *replacement; | ||
1616 | 489 | GString *result, *junk; | ||
1617 | 490 | gint length, n = 0, l; | ||
1618 | 491 | |||
1619 | 492 | result = g_string_new (NULL); | ||
1620 | 493 | junk = g_string_new (NULL); | ||
1621 | 494 | |||
1622 | 495 | /* count the number of indefinite type characters */ | ||
1623 | 496 | for (length = 0; type_string[length]; length++) | ||
1624 | 497 | n += type_string[length] == 'r' || | ||
1625 | 498 | type_string[length] == '?' || | ||
1626 | 499 | type_string[length] == '*'; | ||
1627 | 500 | /* length now is strlen (type_string) */ | ||
1628 | 501 | |||
1629 | 502 | /* pick one at random to replace */ | ||
1630 | 503 | n = g_test_rand_int_range (0, n) + 1; | ||
1631 | 504 | |||
1632 | 505 | /* find it */ | ||
1633 | 506 | l = -1; | ||
1634 | 507 | while (n--) l += strcspn (type_string + l + 1, "r?*") + 1; | ||
1635 | 508 | g_assert (type_string[l] == 'r' || | ||
1636 | 509 | type_string[l] == '?' || | ||
1637 | 510 | type_string[l] == '*'); | ||
1638 | 511 | |||
1639 | 512 | /* store up to that point in a GString */ | ||
1640 | 513 | g_string_append_len (result, type_string, l); | ||
1641 | 514 | |||
1642 | 515 | /* then store the replacement in the GString */ | ||
1643 | 516 | if (type_string[l] == 'r') | ||
1644 | 517 | replacement = append_tuple_type_string (result, junk, FALSE, 3); | ||
1645 | 518 | |||
1646 | 519 | else if (type_string[l] == '?') | ||
1647 | 520 | replacement = append_type_string (result, junk, FALSE, 0); | ||
1648 | 521 | |||
1649 | 522 | else if (type_string[l] == '*') | ||
1650 | 523 | replacement = append_type_string (result, junk, FALSE, 3); | ||
1651 | 524 | |||
1652 | 525 | else | ||
1653 | 526 | g_assert_not_reached (); | ||
1654 | 527 | |||
1655 | 528 | /* ensure the replacement has the proper type */ | ||
1656 | 529 | g_assert (g_variant_type_is_subtype_of (replacement, | ||
1657 | 530 | (gpointer) &type_string[l])); | ||
1658 | 531 | |||
1659 | 532 | /* store the rest from the original type string */ | ||
1660 | 533 | g_string_append (result, type_string + l + 1); | ||
1661 | 534 | |||
1662 | 535 | g_variant_type_free (replacement); | ||
1663 | 536 | g_string_free (junk, TRUE); | ||
1664 | 537 | |||
1665 | 538 | return g_string_free (result, FALSE); | ||
1666 | 539 | } | ||
1667 | 540 | |||
1668 | 541 | struct typestack | ||
1669 | 542 | { | ||
1670 | 543 | const GVariantType *type; | ||
1671 | 544 | struct typestack *parent; | ||
1672 | 545 | }; | ||
1673 | 546 | |||
1674 | 547 | /* given an indefinite type string, replace one of the indefinite | ||
1675 | 548 | * characters in it with a matching type and ensure that the result is a | ||
1676 | 549 | * subtype of the original. repeat. | ||
1677 | 550 | */ | ||
1678 | 551 | static void | ||
1679 | 552 | subtype_check (const gchar *type_string, | ||
1680 | 553 | struct typestack *parent_ts) | ||
1681 | 554 | { | ||
1682 | 555 | struct typestack ts, *node; | ||
1683 | 556 | gchar *subtype; | ||
1684 | 557 | gint depth = 0; | ||
1685 | 558 | |||
1686 | 559 | subtype = generate_subtype (type_string); | ||
1687 | 560 | |||
1688 | 561 | ts.type = G_VARIANT_TYPE (subtype); | ||
1689 | 562 | ts.parent = parent_ts; | ||
1690 | 563 | |||
1691 | 564 | for (node = &ts; node; node = node->parent) | ||
1692 | 565 | { | ||
1693 | 566 | /* this type should be a subtype of each parent type */ | ||
1694 | 567 | g_assert (g_variant_type_is_subtype_of (ts.type, node->type)); | ||
1695 | 568 | |||
1696 | 569 | /* it should only be a supertype when it is exactly equal */ | ||
1697 | 570 | g_assert (g_variant_type_is_subtype_of (node->type, ts.type) == | ||
1698 | 571 | g_variant_type_equal (ts.type, node->type)); | ||
1699 | 572 | |||
1700 | 573 | depth++; | ||
1701 | 574 | } | ||
1702 | 575 | |||
1703 | 576 | if (!g_variant_type_is_definite (ts.type) && depth < 5) | ||
1704 | 577 | { | ||
1705 | 578 | /* the type is still indefinite and we haven't repeated too many | ||
1706 | 579 | * times. go once more. | ||
1707 | 580 | */ | ||
1708 | 581 | |||
1709 | 582 | subtype_check (subtype, &ts); | ||
1710 | 583 | } | ||
1711 | 584 | |||
1712 | 585 | g_free (subtype); | ||
1713 | 586 | } | ||
1714 | 587 | |||
1715 | 588 | static void | ||
1716 | 589 | test_gvarianttype (void) | ||
1717 | 590 | { | ||
1718 | 591 | gint i; | ||
1719 | 592 | |||
1720 | 593 | for (i = 0; i < 2000; i++) | ||
1721 | 594 | { | ||
1722 | 595 | GString *type_string, *description; | ||
1723 | 596 | GVariantType *type, *other_type; | ||
1724 | 597 | const GVariantType *ctype; | ||
1725 | 598 | gchar *invalid; | ||
1726 | 599 | gchar *desc; | ||
1727 | 600 | |||
1728 | 601 | type_string = g_string_new (NULL); | ||
1729 | 602 | description = g_string_new (NULL); | ||
1730 | 603 | |||
1731 | 604 | /* generate a random type, its type string and a description | ||
1732 | 605 | * | ||
1733 | 606 | * exercises type constructor functions and g_variant_type_copy() | ||
1734 | 607 | */ | ||
1735 | 608 | type = append_type_string (type_string, description, FALSE, 6); | ||
1736 | 609 | |||
1737 | 610 | /* convert the type string to a type and ensure that it is equal | ||
1738 | 611 | * to the one produced with the type constructor routines | ||
1739 | 612 | */ | ||
1740 | 613 | ctype = G_VARIANT_TYPE (type_string->str); | ||
1741 | 614 | g_assert (g_variant_type_equal (ctype, type)); | ||
1742 | 615 | g_assert (g_variant_type_hash (ctype) == g_variant_type_hash (type)); | ||
1743 | 616 | g_assert (g_variant_type_is_subtype_of (ctype, type)); | ||
1744 | 617 | g_assert (g_variant_type_is_subtype_of (type, ctype)); | ||
1745 | 618 | |||
1746 | 619 | /* check if the type is indefinite */ | ||
1747 | 620 | if (!g_variant_type_is_definite (type)) | ||
1748 | 621 | { | ||
1749 | 622 | struct typestack ts = { type, NULL }; | ||
1750 | 623 | |||
1751 | 624 | /* if it is indefinite, then replace one of the indefinite | ||
1752 | 625 | * characters with a matching type and ensure that the result | ||
1753 | 626 | * is a subtype of the original type. repeat. | ||
1754 | 627 | */ | ||
1755 | 628 | subtype_check (type_string->str, &ts); | ||
1756 | 629 | } | ||
1757 | 630 | else | ||
1758 | 631 | /* ensure that no indefinite characters appear */ | ||
1759 | 632 | g_assert (strcspn (type_string->str, "r?*") == type_string->len); | ||
1760 | 633 | |||
1761 | 634 | |||
1762 | 635 | /* describe the type. | ||
1763 | 636 | * | ||
1764 | 637 | * exercises the type iterator interface | ||
1765 | 638 | */ | ||
1766 | 639 | desc = describe_type (type); | ||
1767 | 640 | |||
1768 | 641 | /* make sure the description matches */ | ||
1769 | 642 | g_assert_cmpstr (desc, ==, description->str); | ||
1770 | 643 | g_free (desc); | ||
1771 | 644 | |||
1772 | 645 | /* make an invalid mutation to the type and make sure the type | ||
1773 | 646 | * validation routines catch it */ | ||
1774 | 647 | invalid = invalid_mutation (type_string->str); | ||
1775 | 648 | g_assert (g_variant_type_string_is_valid (type_string->str)); | ||
1776 | 649 | g_assert (!g_variant_type_string_is_valid (invalid)); | ||
1777 | 650 | g_free (invalid); | ||
1778 | 651 | |||
1779 | 652 | /* concatenate another type to the type string and ensure that | ||
1780 | 653 | * the result is recognised as being invalid | ||
1781 | 654 | */ | ||
1782 | 655 | other_type = append_type_string (type_string, description, FALSE, 2); | ||
1783 | 656 | |||
1784 | 657 | g_string_free (description, TRUE); | ||
1785 | 658 | g_string_free (type_string, TRUE); | ||
1786 | 659 | g_variant_type_free (other_type); | ||
1787 | 660 | g_variant_type_free (type); | ||
1788 | 661 | } | ||
1789 | 662 | } | ||
1790 | 663 | |||
1791 | 664 | #define ALIGNED(x, y) (((x + (y - 1)) / y) * y) | ||
1792 | 665 | |||
1793 | 666 | /* do our own calculation of the fixed_size and alignment of a type | ||
1794 | 667 | * using a simple algorithm to make sure the "fancy" one in the | ||
1795 | 668 | * implementation is correct. | ||
1796 | 669 | */ | ||
1797 | 670 | static void | ||
1798 | 671 | calculate_type_info (const GVariantType *type, | ||
1799 | 672 | gsize *fixed_size, | ||
1800 | 673 | guint *alignment) | ||
1801 | 674 | { | ||
1802 | 675 | if (g_variant_type_is_array (type) || | ||
1803 | 676 | g_variant_type_is_maybe (type)) | ||
1804 | 677 | { | ||
1805 | 678 | calculate_type_info (g_variant_type_element (type), NULL, alignment); | ||
1806 | 679 | |||
1807 | 680 | if (fixed_size) | ||
1808 | 681 | *fixed_size = 0; | ||
1809 | 682 | } | ||
1810 | 683 | else if (g_variant_type_is_tuple (type) || | ||
1811 | 684 | g_variant_type_is_dict_entry (type)) | ||
1812 | 685 | { | ||
1813 | 686 | if (g_variant_type_n_items (type)) | ||
1814 | 687 | { | ||
1815 | 688 | const GVariantType *sub; | ||
1816 | 689 | gboolean variable; | ||
1817 | 690 | gsize size; | ||
1818 | 691 | guint al; | ||
1819 | 692 | |||
1820 | 693 | variable = FALSE; | ||
1821 | 694 | size = 0; | ||
1822 | 695 | al = 0; | ||
1823 | 696 | |||
1824 | 697 | sub = g_variant_type_first (type); | ||
1825 | 698 | do | ||
1826 | 699 | { | ||
1827 | 700 | gsize this_fs; | ||
1828 | 701 | guint this_al; | ||
1829 | 702 | |||
1830 | 703 | calculate_type_info (sub, &this_fs, &this_al); | ||
1831 | 704 | |||
1832 | 705 | al = MAX (al, this_al); | ||
1833 | 706 | |||
1834 | 707 | if (!this_fs) | ||
1835 | 708 | { | ||
1836 | 709 | variable = TRUE; | ||
1837 | 710 | size = 0; | ||
1838 | 711 | } | ||
1839 | 712 | |||
1840 | 713 | if (!variable) | ||
1841 | 714 | { | ||
1842 | 715 | size = ALIGNED (size, this_al); | ||
1843 | 716 | size += this_fs; | ||
1844 | 717 | } | ||
1845 | 718 | } | ||
1846 | 719 | while ((sub = g_variant_type_next (sub))); | ||
1847 | 720 | |||
1848 | 721 | size = ALIGNED (size, al); | ||
1849 | 722 | |||
1850 | 723 | if (alignment) | ||
1851 | 724 | *alignment = al; | ||
1852 | 725 | |||
1853 | 726 | if (fixed_size) | ||
1854 | 727 | *fixed_size = size; | ||
1855 | 728 | } | ||
1856 | 729 | else | ||
1857 | 730 | { | ||
1858 | 731 | if (fixed_size) | ||
1859 | 732 | *fixed_size = 1; | ||
1860 | 733 | |||
1861 | 734 | if (alignment) | ||
1862 | 735 | *alignment = 1; | ||
1863 | 736 | } | ||
1864 | 737 | } | ||
1865 | 738 | else | ||
1866 | 739 | { | ||
1867 | 740 | gint fs, al; | ||
1868 | 741 | |||
1869 | 742 | if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN) || | ||
1870 | 743 | g_variant_type_equal (type, G_VARIANT_TYPE_BYTE)) | ||
1871 | 744 | { | ||
1872 | 745 | al = fs = 1; | ||
1873 | 746 | } | ||
1874 | 747 | |||
1875 | 748 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16) || | ||
1876 | 749 | g_variant_type_equal (type, G_VARIANT_TYPE_UINT16)) | ||
1877 | 750 | { | ||
1878 | 751 | al = fs = 2; | ||
1879 | 752 | } | ||
1880 | 753 | |||
1881 | 754 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32) || | ||
1882 | 755 | g_variant_type_equal (type, G_VARIANT_TYPE_UINT32) || | ||
1883 | 756 | g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE)) | ||
1884 | 757 | { | ||
1885 | 758 | al = fs = 4; | ||
1886 | 759 | } | ||
1887 | 760 | |||
1888 | 761 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64) || | ||
1889 | 762 | g_variant_type_equal (type, G_VARIANT_TYPE_UINT64) || | ||
1890 | 763 | g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE)) | ||
1891 | 764 | { | ||
1892 | 765 | al = fs = 8; | ||
1893 | 766 | } | ||
1894 | 767 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING) || | ||
1895 | 768 | g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH) || | ||
1896 | 769 | g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE)) | ||
1897 | 770 | { | ||
1898 | 771 | al = 1; | ||
1899 | 772 | fs = 0; | ||
1900 | 773 | } | ||
1901 | 774 | else if (g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT)) | ||
1902 | 775 | { | ||
1903 | 776 | al = 8; | ||
1904 | 777 | fs = 0; | ||
1905 | 778 | } | ||
1906 | 779 | else | ||
1907 | 780 | g_assert_not_reached (); | ||
1908 | 781 | |||
1909 | 782 | if (fixed_size) | ||
1910 | 783 | *fixed_size = fs; | ||
1911 | 784 | |||
1912 | 785 | if (alignment) | ||
1913 | 786 | *alignment = al; | ||
1914 | 787 | } | ||
1915 | 788 | } | ||
1916 | 789 | |||
1917 | 790 | /* same as the describe_type() function above, but iterates over | ||
1918 | 791 | * typeinfo instead of types. | ||
1919 | 792 | */ | ||
1920 | 793 | static gchar * | ||
1921 | 794 | describe_info (GVariantTypeInfo *info) | ||
1922 | 795 | { | ||
1923 | 796 | gchar *result; | ||
1924 | 797 | |||
1925 | 798 | switch (g_variant_type_info_get_type_char (info)) | ||
1926 | 799 | { | ||
1927 | 800 | case G_VARIANT_TYPE_INFO_CHAR_MAYBE: | ||
1928 | 801 | { | ||
1929 | 802 | gchar *element; | ||
1930 | 803 | |||
1931 | 804 | element = describe_info (g_variant_type_info_element (info)); | ||
1932 | 805 | result = g_strdup_printf ("m of %s", element); | ||
1933 | 806 | g_free (element); | ||
1934 | 807 | } | ||
1935 | 808 | break; | ||
1936 | 809 | |||
1937 | 810 | case G_VARIANT_TYPE_INFO_CHAR_ARRAY: | ||
1938 | 811 | { | ||
1939 | 812 | gchar *element; | ||
1940 | 813 | |||
1941 | 814 | element = describe_info (g_variant_type_info_element (info)); | ||
1942 | 815 | result = g_strdup_printf ("a of %s", element); | ||
1943 | 816 | g_free (element); | ||
1944 | 817 | } | ||
1945 | 818 | break; | ||
1946 | 819 | |||
1947 | 820 | case G_VARIANT_TYPE_INFO_CHAR_TUPLE: | ||
1948 | 821 | { | ||
1949 | 822 | const gchar *sep = ""; | ||
1950 | 823 | GString *string; | ||
1951 | 824 | gint length; | ||
1952 | 825 | gint i; | ||
1953 | 826 | |||
1954 | 827 | string = g_string_new ("t of ["); | ||
1955 | 828 | length = g_variant_type_info_n_members (info); | ||
1956 | 829 | |||
1957 | 830 | for (i = 0; i < length; i++) | ||
1958 | 831 | { | ||
1959 | 832 | const GVariantMemberInfo *minfo; | ||
1960 | 833 | gchar *subtype; | ||
1961 | 834 | |||
1962 | 835 | g_string_append (string, sep); | ||
1963 | 836 | sep = ", "; | ||
1964 | 837 | |||
1965 | 838 | minfo = g_variant_type_info_member_info (info, i); | ||
1966 | 839 | subtype = describe_info (minfo->type_info); | ||
1967 | 840 | g_string_append (string, subtype); | ||
1968 | 841 | g_free (subtype); | ||
1969 | 842 | } | ||
1970 | 843 | |||
1971 | 844 | g_string_append_c (string, ']'); | ||
1972 | 845 | |||
1973 | 846 | result = g_string_free (string, FALSE); | ||
1974 | 847 | } | ||
1975 | 848 | break; | ||
1976 | 849 | |||
1977 | 850 | case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: | ||
1978 | 851 | { | ||
1979 | 852 | const GVariantMemberInfo *keyinfo, *valueinfo; | ||
1980 | 853 | gchar *key, *value; | ||
1981 | 854 | |||
1982 | 855 | g_assert_cmpint (g_variant_type_info_n_members (info), ==, 2); | ||
1983 | 856 | keyinfo = g_variant_type_info_member_info (info, 0); | ||
1984 | 857 | valueinfo = g_variant_type_info_member_info (info, 1); | ||
1985 | 858 | key = describe_info (keyinfo->type_info); | ||
1986 | 859 | value = describe_info (valueinfo->type_info); | ||
1987 | 860 | result = g_strjoin ("", "e of [", key, ", ", value, "]", NULL); | ||
1988 | 861 | g_free (key); | ||
1989 | 862 | g_free (value); | ||
1990 | 863 | } | ||
1991 | 864 | break; | ||
1992 | 865 | |||
1993 | 866 | case G_VARIANT_TYPE_INFO_CHAR_VARIANT: | ||
1994 | 867 | result = g_strdup ("V"); | ||
1995 | 868 | break; | ||
1996 | 869 | |||
1997 | 870 | default: | ||
1998 | 871 | result = g_strdup (g_variant_type_info_get_type_string (info)); | ||
1999 | 872 | g_assert_cmpint (strlen (result), ==, 1); | ||
2000 | 873 | break; | ||
2001 | 874 | } | ||
2002 | 875 | |||
2003 | 876 | return result; | ||
2004 | 877 | } | ||
2005 | 878 | |||
2006 | 879 | /* check that the O(1) method of calculating offsets meshes with the | ||
2007 | 880 | * results of simple iteration. | ||
2008 | 881 | */ | ||
2009 | 882 | static void | ||
2010 | 883 | check_offsets (GVariantTypeInfo *info, | ||
2011 | 884 | const GVariantType *type) | ||
2012 | 885 | { | ||
2013 | 886 | gint flavour; | ||
2014 | 887 | gint length; | ||
2015 | 888 | |||
2016 | 889 | length = g_variant_type_info_n_members (info); | ||
2017 | 890 | g_assert_cmpint (length, ==, g_variant_type_n_items (type)); | ||
2018 | 891 | |||
2019 | 892 | /* the 'flavour' is the low order bits of the ending point of | ||
2020 | 893 | * variable-size items in the tuple. this lets us test that the type | ||
2021 | 894 | * info is correct for various starting alignments. | ||
2022 | 895 | */ | ||
2023 | 896 | for (flavour = 0; flavour < 8; flavour++) | ||
2024 | 897 | { | ||
2025 | 898 | const GVariantType *subtype; | ||
2026 | 899 | gsize last_offset_index; | ||
2027 | 900 | gsize last_offset; | ||
2028 | 901 | gsize position; | ||
2029 | 902 | gint i; | ||
2030 | 903 | |||
2031 | 904 | subtype = g_variant_type_first (type); | ||
2032 | 905 | last_offset_index = -1; | ||
2033 | 906 | last_offset = 0; | ||
2034 | 907 | position = 0; | ||
2035 | 908 | |||
2036 | 909 | /* go through the tuple, keeping track of our position */ | ||
2037 | 910 | for (i = 0; i < length; i++) | ||
2038 | 911 | { | ||
2039 | 912 | gsize fixed_size; | ||
2040 | 913 | guint alignment; | ||
2041 | 914 | |||
2042 | 915 | calculate_type_info (subtype, &fixed_size, &alignment); | ||
2043 | 916 | |||
2044 | 917 | position = ALIGNED (position, alignment); | ||
2045 | 918 | |||
2046 | 919 | /* compare our current aligned position (ie: the start of this | ||
2047 | 920 | * item) to the start offset that would be calculated if we | ||
2048 | 921 | * used the type info | ||
2049 | 922 | */ | ||
2050 | 923 | { | ||
2051 | 924 | const GVariantMemberInfo *member; | ||
2052 | 925 | gsize start; | ||
2053 | 926 | |||
2054 | 927 | member = g_variant_type_info_member_info (info, i); | ||
2055 | 928 | g_assert_cmpint (member->i, ==, last_offset_index); | ||
2056 | 929 | |||
2057 | 930 | /* do the calculation using the typeinfo */ | ||
2058 | 931 | start = last_offset; | ||
2059 | 932 | start += member->a; | ||
2060 | 933 | start &= member->b; | ||
2061 | 934 | start |= member->c; | ||
2062 | 935 | |||
2063 | 936 | /* did we reach the same spot? */ | ||
2064 | 937 | g_assert_cmpint (start, ==, position); | ||
2065 | 938 | } | ||
2066 | 939 | |||
2067 | 940 | if (fixed_size) | ||
2068 | 941 | { | ||
2069 | 942 | /* fixed size. add that size. */ | ||
2070 | 943 | position += fixed_size; | ||
2071 | 944 | } | ||
2072 | 945 | else | ||
2073 | 946 | { | ||
2074 | 947 | /* variable size. do the flavouring. */ | ||
2075 | 948 | while ((position & 0x7) != flavour) | ||
2076 | 949 | position++; | ||
2077 | 950 | |||
2078 | 951 | /* and store the offset, just like it would be in the | ||
2079 | 952 | * serialised data. | ||
2080 | 953 | */ | ||
2081 | 954 | last_offset = position; | ||
2082 | 955 | last_offset_index++; | ||
2083 | 956 | } | ||
2084 | 957 | |||
2085 | 958 | /* next type */ | ||
2086 | 959 | subtype = g_variant_type_next (subtype); | ||
2087 | 960 | } | ||
2088 | 961 | |||
2089 | 962 | /* make sure we used up exactly all the types */ | ||
2090 | 963 | g_assert (subtype == NULL); | ||
2091 | 964 | } | ||
2092 | 965 | } | ||
2093 | 966 | |||
2094 | 967 | static void | ||
2095 | 968 | test_gvarianttypeinfo (void) | ||
2096 | 969 | { | ||
2097 | 970 | gint i; | ||
2098 | 971 | |||
2099 | 972 | for (i = 0; i < 2000; i++) | ||
2100 | 973 | { | ||
2101 | 974 | GString *type_string, *description; | ||
2102 | 975 | gsize fixed_size1, fixed_size2; | ||
2103 | 976 | guint alignment1, alignment2; | ||
2104 | 977 | GVariantTypeInfo *info; | ||
2105 | 978 | GVariantType *type; | ||
2106 | 979 | gchar *desc; | ||
2107 | 980 | |||
2108 | 981 | type_string = g_string_new (NULL); | ||
2109 | 982 | description = g_string_new (NULL); | ||
2110 | 983 | |||
2111 | 984 | /* random type */ | ||
2112 | 985 | type = append_type_string (type_string, description, TRUE, 6); | ||
2113 | 986 | |||
2114 | 987 | /* create a typeinfo for it */ | ||
2115 | 988 | info = g_variant_type_info_get (type); | ||
2116 | 989 | |||
2117 | 990 | /* make sure the typeinfo has the right type string */ | ||
2118 | 991 | g_assert_cmpstr (g_variant_type_info_get_type_string (info), ==, | ||
2119 | 992 | type_string->str); | ||
2120 | 993 | |||
2121 | 994 | /* calculate the alignment and fixed size, compare to the | ||
2122 | 995 | * typeinfo's calculations | ||
2123 | 996 | */ | ||
2124 | 997 | calculate_type_info (type, &fixed_size1, &alignment1); | ||
2125 | 998 | g_variant_type_info_query (info, &alignment2, &fixed_size2); | ||
2126 | 999 | g_assert_cmpint (fixed_size1, ==, fixed_size2); | ||
2127 | 1000 | g_assert_cmpint (alignment1, ==, alignment2 + 1); | ||
2128 | 1001 | |||
2129 | 1002 | /* test the iteration functions over typeinfo structures by | ||
2130 | 1003 | * "describing" the typeinfo and verifying equality. | ||
2131 | 1004 | */ | ||
2132 | 1005 | desc = describe_info (info); | ||
2133 | 1006 | g_assert_cmpstr (desc, ==, description->str); | ||
2134 | 1007 | |||
2135 | 1008 | /* do extra checks for containers */ | ||
2136 | 1009 | if (g_variant_type_is_array (type) || | ||
2137 | 1010 | g_variant_type_is_maybe (type)) | ||
2138 | 1011 | { | ||
2139 | 1012 | const GVariantType *element; | ||
2140 | 1013 | gsize efs1, efs2; | ||
2141 | 1014 | guint ea1, ea2; | ||
2142 | 1015 | |||
2143 | 1016 | element = g_variant_type_element (type); | ||
2144 | 1017 | calculate_type_info (element, &efs1, &ea1); | ||
2145 | 1018 | g_variant_type_info_query_element (info, &ea2, &efs2); | ||
2146 | 1019 | g_assert_cmpint (efs1, ==, efs2); | ||
2147 | 1020 | g_assert_cmpint (ea1, ==, ea2 + 1); | ||
2148 | 1021 | |||
2149 | 1022 | g_assert_cmpint (ea1, ==, alignment1); | ||
2150 | 1023 | g_assert_cmpint (0, ==, fixed_size1); | ||
2151 | 1024 | } | ||
2152 | 1025 | else if (g_variant_type_is_tuple (type) || | ||
2153 | 1026 | g_variant_type_is_dict_entry (type)) | ||
2154 | 1027 | { | ||
2155 | 1028 | /* make sure the "magic constants" are working */ | ||
2156 | 1029 | check_offsets (info, type); | ||
2157 | 1030 | } | ||
2158 | 1031 | |||
2159 | 1032 | g_string_free (type_string, TRUE); | ||
2160 | 1033 | g_string_free (description, TRUE); | ||
2161 | 1034 | g_variant_type_info_unref (info); | ||
2162 | 1035 | g_variant_type_free (type); | ||
2163 | 1036 | g_free (desc); | ||
2164 | 1037 | } | ||
2165 | 1038 | |||
2166 | 1039 | g_variant_type_info_assert_no_infos (); | ||
2167 | 1040 | } | ||
2168 | 1041 | |||
2169 | 1042 | #define MAX_FIXED_MULTIPLIER 256 | ||
2170 | 1043 | #define MAX_INSTANCE_SIZE 1024 | ||
2171 | 1044 | #define MAX_ARRAY_CHILDREN 128 | ||
2172 | 1045 | #define MAX_TUPLE_CHILDREN 128 | ||
2173 | 1046 | |||
2174 | 1047 | /* this function generates a random type such that all characteristics | ||
2175 | 1048 | * that are "interesting" to the serialiser are tested. | ||
2176 | 1049 | * | ||
2177 | 1050 | * this basically means: | ||
2178 | 1051 | * - test different alignments | ||
2179 | 1052 | * - test variable sized items and fixed sized items | ||
2180 | 1053 | * - test different fixed sizes | ||
2181 | 1054 | */ | ||
2182 | 1055 | static gchar * | ||
2183 | 1056 | random_type_string (void) | ||
2184 | 1057 | { | ||
2185 | 1058 | const guchar base_types[] = "ynix"; | ||
2186 | 1059 | guchar base_type; | ||
2187 | 1060 | |||
2188 | 1061 | base_type = base_types[g_test_rand_int_range (0, 4)]; | ||
2189 | 1062 | |||
2190 | 1063 | if (g_test_rand_bit ()) | ||
2191 | 1064 | /* construct a fixed-sized type */ | ||
2192 | 1065 | { | ||
2193 | 1066 | char type_string[MAX_FIXED_MULTIPLIER]; | ||
2194 | 1067 | guint multiplier; | ||
2195 | 1068 | guint i = 0; | ||
2196 | 1069 | |||
2197 | 1070 | multiplier = g_test_rand_int_range (1, sizeof type_string - 1); | ||
2198 | 1071 | |||
2199 | 1072 | type_string[i++] = '('; | ||
2200 | 1073 | while (multiplier--) | ||
2201 | 1074 | type_string[i++] = base_type; | ||
2202 | 1075 | type_string[i++] = ')'; | ||
2203 | 1076 | |||
2204 | 1077 | return g_strndup (type_string, i); | ||
2205 | 1078 | } | ||
2206 | 1079 | else | ||
2207 | 1080 | /* construct a variable-sized type */ | ||
2208 | 1081 | { | ||
2209 | 1082 | char type_string[2] = { 'a', base_type }; | ||
2210 | 1083 | |||
2211 | 1084 | return g_strndup (type_string, 2); | ||
2212 | 1085 | } | ||
2213 | 1086 | } | ||
2214 | 1087 | |||
2215 | 1088 | typedef struct | ||
2216 | 1089 | { | ||
2217 | 1090 | GVariantTypeInfo *type_info; | ||
2218 | 1091 | guint alignment; | ||
2219 | 1092 | gsize size; | ||
2220 | 1093 | gboolean is_fixed_sized; | ||
2221 | 1094 | |||
2222 | 1095 | guint32 seed; | ||
2223 | 1096 | |||
2224 | 1097 | #define INSTANCE_MAGIC 1287582829 | ||
2225 | 1098 | guint magic; | ||
2226 | 1099 | } RandomInstance; | ||
2227 | 1100 | |||
2228 | 1101 | static RandomInstance * | ||
2229 | 1102 | random_instance (GVariantTypeInfo *type_info) | ||
2230 | 1103 | { | ||
2231 | 1104 | RandomInstance *instance; | ||
2232 | 1105 | |||
2233 | 1106 | instance = g_slice_new (RandomInstance); | ||
2234 | 1107 | |||
2235 | 1108 | if (type_info == NULL) | ||
2236 | 1109 | { | ||
2237 | 1110 | gchar *str = random_type_string (); | ||
2238 | 1111 | instance->type_info = g_variant_type_info_get (G_VARIANT_TYPE (str)); | ||
2239 | 1112 | g_free (str); | ||
2240 | 1113 | } | ||
2241 | 1114 | else | ||
2242 | 1115 | instance->type_info = g_variant_type_info_ref (type_info); | ||
2243 | 1116 | |||
2244 | 1117 | instance->seed = g_test_rand_int (); | ||
2245 | 1118 | |||
2246 | 1119 | g_variant_type_info_query (instance->type_info, | ||
2247 | 1120 | &instance->alignment, | ||
2248 | 1121 | &instance->size); | ||
2249 | 1122 | |||
2250 | 1123 | instance->is_fixed_sized = instance->size != 0; | ||
2251 | 1124 | |||
2252 | 1125 | if (!instance->is_fixed_sized) | ||
2253 | 1126 | instance->size = g_test_rand_int_range (0, MAX_INSTANCE_SIZE); | ||
2254 | 1127 | |||
2255 | 1128 | instance->magic = INSTANCE_MAGIC; | ||
2256 | 1129 | |||
2257 | 1130 | return instance; | ||
2258 | 1131 | } | ||
2259 | 1132 | |||
2260 | 1133 | static void | ||
2261 | 1134 | random_instance_free (RandomInstance *instance) | ||
2262 | 1135 | { | ||
2263 | 1136 | g_variant_type_info_unref (instance->type_info); | ||
2264 | 1137 | g_slice_free (RandomInstance, instance); | ||
2265 | 1138 | } | ||
2266 | 1139 | |||
2267 | 1140 | static void | ||
2268 | 1141 | append_instance_size (RandomInstance *instance, | ||
2269 | 1142 | gsize *offset) | ||
2270 | 1143 | { | ||
2271 | 1144 | *offset += (-*offset) & instance->alignment; | ||
2272 | 1145 | *offset += instance->size; | ||
2273 | 1146 | } | ||
2274 | 1147 | |||
2275 | 1148 | static void | ||
2276 | 1149 | random_instance_write (RandomInstance *instance, | ||
2277 | 1150 | guchar *buffer) | ||
2278 | 1151 | { | ||
2279 | 1152 | GRand *rand; | ||
2280 | 1153 | gint i; | ||
2281 | 1154 | |||
2282 | 1155 | g_assert_cmpint ((gsize) buffer & ALIGN_BITS & instance->alignment, ==, 0); | ||
2283 | 1156 | |||
2284 | 1157 | rand = g_rand_new_with_seed (instance->seed); | ||
2285 | 1158 | for (i = 0; i < instance->size; i++) | ||
2286 | 1159 | buffer[i] = g_rand_int (rand); | ||
2287 | 1160 | g_rand_free (rand); | ||
2288 | 1161 | } | ||
2289 | 1162 | |||
2290 | 1163 | static void | ||
2291 | 1164 | append_instance_data (RandomInstance *instance, | ||
2292 | 1165 | guchar **buffer) | ||
2293 | 1166 | { | ||
2294 | 1167 | while (((gsize) *buffer) & instance->alignment) | ||
2295 | 1168 | *(*buffer)++ = '\0'; | ||
2296 | 1169 | |||
2297 | 1170 | random_instance_write (instance, *buffer); | ||
2298 | 1171 | *buffer += instance->size; | ||
2299 | 1172 | } | ||
2300 | 1173 | |||
2301 | 1174 | static gboolean | ||
2302 | 1175 | random_instance_assert (RandomInstance *instance, | ||
2303 | 1176 | guchar *buffer, | ||
2304 | 1177 | gsize size) | ||
2305 | 1178 | { | ||
2306 | 1179 | GRand *rand; | ||
2307 | 1180 | gint i; | ||
2308 | 1181 | |||
2309 | 1182 | g_assert_cmpint ((gsize) buffer & ALIGN_BITS & instance->alignment, ==, 0); | ||
2310 | 1183 | g_assert_cmpint (size, ==, instance->size); | ||
2311 | 1184 | |||
2312 | 1185 | rand = g_rand_new_with_seed (instance->seed); | ||
2313 | 1186 | for (i = 0; i < instance->size; i++) | ||
2314 | 1187 | { | ||
2315 | 1188 | guchar byte = g_rand_int (rand); | ||
2316 | 1189 | |||
2317 | 1190 | g_assert (buffer[i] == byte); | ||
2318 | 1191 | } | ||
2319 | 1192 | g_rand_free (rand); | ||
2320 | 1193 | |||
2321 | 1194 | return i == instance->size; | ||
2322 | 1195 | } | ||
2323 | 1196 | |||
2324 | 1197 | static gboolean | ||
2325 | 1198 | random_instance_check (RandomInstance *instance, | ||
2326 | 1199 | guchar *buffer, | ||
2327 | 1200 | gsize size) | ||
2328 | 1201 | { | ||
2329 | 1202 | GRand *rand; | ||
2330 | 1203 | gint i; | ||
2331 | 1204 | |||
2332 | 1205 | g_assert_cmpint ((gsize) buffer & ALIGN_BITS & instance->alignment, ==, 0); | ||
2333 | 1206 | |||
2334 | 1207 | if (size != instance->size) | ||
2335 | 1208 | return FALSE; | ||
2336 | 1209 | |||
2337 | 1210 | rand = g_rand_new_with_seed (instance->seed); | ||
2338 | 1211 | for (i = 0; i < instance->size; i++) | ||
2339 | 1212 | if (buffer[i] != (guchar) g_rand_int (rand)) | ||
2340 | 1213 | break; | ||
2341 | 1214 | g_rand_free (rand); | ||
2342 | 1215 | |||
2343 | 1216 | return i == instance->size; | ||
2344 | 1217 | } | ||
2345 | 1218 | |||
2346 | 1219 | static void | ||
2347 | 1220 | random_instance_filler (GVariantSerialised *serialised, | ||
2348 | 1221 | gpointer data) | ||
2349 | 1222 | { | ||
2350 | 1223 | RandomInstance *instance = data; | ||
2351 | 1224 | |||
2352 | 1225 | g_assert (instance->magic == INSTANCE_MAGIC); | ||
2353 | 1226 | |||
2354 | 1227 | if (serialised->type_info == NULL) | ||
2355 | 1228 | serialised->type_info = instance->type_info; | ||
2356 | 1229 | |||
2357 | 1230 | if (serialised->size == 0) | ||
2358 | 1231 | serialised->size = instance->size; | ||
2359 | 1232 | |||
2360 | 1233 | g_assert (serialised->type_info == instance->type_info); | ||
2361 | 1234 | g_assert (serialised->size == instance->size); | ||
2362 | 1235 | |||
2363 | 1236 | if (serialised->data) | ||
2364 | 1237 | random_instance_write (instance, serialised->data); | ||
2365 | 1238 | } | ||
2366 | 1239 | |||
2367 | 1240 | static gsize | ||
2368 | 1241 | calculate_offset_size (gsize body_size, | ||
2369 | 1242 | gsize n_offsets) | ||
2370 | 1243 | { | ||
2371 | 1244 | if (body_size == 0) | ||
2372 | 1245 | return 0; | ||
2373 | 1246 | |||
2374 | 1247 | if (body_size + n_offsets <= G_MAXUINT8) | ||
2375 | 1248 | return 1; | ||
2376 | 1249 | |||
2377 | 1250 | if (body_size + 2 * n_offsets <= G_MAXUINT16) | ||
2378 | 1251 | return 2; | ||
2379 | 1252 | |||
2380 | 1253 | if (body_size + 4 * n_offsets <= G_MAXUINT32) | ||
2381 | 1254 | return 4; | ||
2382 | 1255 | |||
2383 | 1256 | /* the test case won't generate anything bigger */ | ||
2384 | 1257 | g_assert_not_reached (); | ||
2385 | 1258 | } | ||
2386 | 1259 | |||
2387 | 1260 | static gpointer | ||
2388 | 1261 | flavoured_malloc (gsize size, gsize flavour) | ||
2389 | 1262 | { | ||
2390 | 1263 | g_assert (flavour < 8); | ||
2391 | 1264 | |||
2392 | 1265 | if (size == 0) | ||
2393 | 1266 | return NULL; | ||
2394 | 1267 | |||
2395 | 1268 | return ((gchar *) g_malloc (size + flavour)) + flavour; | ||
2396 | 1269 | } | ||
2397 | 1270 | |||
2398 | 1271 | static void | ||
2399 | 1272 | flavoured_free (gpointer data, | ||
2400 | 1273 | gsize flavour) | ||
2401 | 1274 | { | ||
2402 | 1275 | if (!data) | ||
2403 | 1276 | return; | ||
2404 | 1277 | g_free (((gchar *) data) - flavour); | ||
2405 | 1278 | } | ||
2406 | 1279 | |||
2407 | 1280 | static gpointer | ||
2408 | 1281 | align_malloc (gsize size) | ||
2409 | 1282 | { | ||
2410 | 1283 | gpointer mem; | ||
2411 | 1284 | |||
2412 | 1285 | #ifdef HAVE_POSIX_MEMALIGN | ||
2413 | 1286 | if (posix_memalign (&mem, 8, size)) | ||
2414 | 1287 | g_error ("posix_memalign failed"); | ||
2415 | 1288 | #else | ||
2416 | 1289 | /* NOTE: there may be platforms that lack posix_memalign() and also | ||
2417 | 1290 | * have malloc() that returns non-8-aligned. if so, we need to try | ||
2418 | 1291 | * harder here. | ||
2419 | 1292 | */ | ||
2420 | 1293 | mem = malloc (size); | ||
2421 | 1294 | #endif | ||
2422 | 1295 | |||
2423 | 1296 | return mem; | ||
2424 | 1297 | } | ||
2425 | 1298 | |||
2426 | 1299 | static void | ||
2427 | 1300 | align_free (gpointer mem) | ||
2428 | 1301 | { | ||
2429 | 1302 | free (mem); | ||
2430 | 1303 | } | ||
2431 | 1304 | |||
2432 | 1305 | static void | ||
2433 | 1306 | append_offset (guchar **offset_ptr, | ||
2434 | 1307 | gsize offset, | ||
2435 | 1308 | guint offset_size) | ||
2436 | 1309 | { | ||
2437 | 1310 | union | ||
2438 | 1311 | { | ||
2439 | 1312 | guchar bytes[sizeof (gsize)]; | ||
2440 | 1313 | gsize integer; | ||
2441 | 1314 | } tmpvalue; | ||
2442 | 1315 | |||
2443 | 1316 | tmpvalue.integer = GSIZE_TO_LE (offset); | ||
2444 | 1317 | memcpy (*offset_ptr, tmpvalue.bytes, offset_size); | ||
2445 | 1318 | *offset_ptr += offset_size; | ||
2446 | 1319 | } | ||
2447 | 1320 | |||
2448 | 1321 | static void | ||
2449 | 1322 | prepend_offset (guchar **offset_ptr, | ||
2450 | 1323 | gsize offset, | ||
2451 | 1324 | guint offset_size) | ||
2452 | 1325 | { | ||
2453 | 1326 | union | ||
2454 | 1327 | { | ||
2455 | 1328 | guchar bytes[sizeof (gsize)]; | ||
2456 | 1329 | gsize integer; | ||
2457 | 1330 | } tmpvalue; | ||
2458 | 1331 | |||
2459 | 1332 | *offset_ptr -= offset_size; | ||
2460 | 1333 | tmpvalue.integer = GSIZE_TO_LE (offset); | ||
2461 | 1334 | memcpy (*offset_ptr, tmpvalue.bytes, offset_size); | ||
2462 | 1335 | } | ||
2463 | 1336 | |||
2464 | 1337 | static void | ||
2465 | 1338 | test_maybe (void) | ||
2466 | 1339 | { | ||
2467 | 1340 | GVariantTypeInfo *type_info; | ||
2468 | 1341 | RandomInstance *instance; | ||
2469 | 1342 | gsize needed_size; | ||
2470 | 1343 | guchar *data; | ||
2471 | 1344 | |||
2472 | 1345 | instance = random_instance (NULL); | ||
2473 | 1346 | |||
2474 | 1347 | { | ||
2475 | 1348 | const gchar *element; | ||
2476 | 1349 | gchar *tmp; | ||
2477 | 1350 | |||
2478 | 1351 | element = g_variant_type_info_get_type_string (instance->type_info); | ||
2479 | 1352 | tmp = g_strdup_printf ("m%s", element); | ||
2480 | 1353 | type_info = g_variant_type_info_get (G_VARIANT_TYPE (tmp)); | ||
2481 | 1354 | g_free (tmp); | ||
2482 | 1355 | } | ||
2483 | 1356 | |||
2484 | 1357 | needed_size = g_variant_serialiser_needed_size (type_info, | ||
2485 | 1358 | random_instance_filler, | ||
2486 | 1359 | NULL, 0); | ||
2487 | 1360 | g_assert_cmpint (needed_size, ==, 0); | ||
2488 | 1361 | |||
2489 | 1362 | needed_size = g_variant_serialiser_needed_size (type_info, | ||
2490 | 1363 | random_instance_filler, | ||
2491 | 1364 | (gpointer *) &instance, 1); | ||
2492 | 1365 | |||
2493 | 1366 | if (instance->is_fixed_sized) | ||
2494 | 1367 | g_assert_cmpint (needed_size, ==, instance->size); | ||
2495 | 1368 | else | ||
2496 | 1369 | g_assert_cmpint (needed_size, ==, instance->size + 1); | ||
2497 | 1370 | |||
2498 | 1371 | { | ||
2499 | 1372 | guchar *ptr; | ||
2500 | 1373 | |||
2501 | 1374 | ptr = data = align_malloc (needed_size); | ||
2502 | 1375 | append_instance_data (instance, &ptr); | ||
2503 | 1376 | |||
2504 | 1377 | if (!instance->is_fixed_sized) | ||
2505 | 1378 | *ptr++ = '\0'; | ||
2506 | 1379 | |||
2507 | 1380 | g_assert_cmpint (ptr - data, ==, needed_size); | ||
2508 | 1381 | } | ||
2509 | 1382 | |||
2510 | 1383 | { | ||
2511 | 1384 | guint alignment; | ||
2512 | 1385 | guint flavour; | ||
2513 | 1386 | |||
2514 | 1387 | alignment = (instance->alignment & ALIGN_BITS) + 1; | ||
2515 | 1388 | |||
2516 | 1389 | for (flavour = 0; flavour < 8; flavour += alignment) | ||
2517 | 1390 | { | ||
2518 | 1391 | GVariantSerialised serialised; | ||
2519 | 1392 | GVariantSerialised child; | ||
2520 | 1393 | |||
2521 | 1394 | serialised.type_info = type_info; | ||
2522 | 1395 | serialised.data = flavoured_malloc (needed_size, flavour); | ||
2523 | 1396 | serialised.size = needed_size; | ||
2524 | 1397 | |||
2525 | 1398 | g_variant_serialiser_serialise (serialised, | ||
2526 | 1399 | random_instance_filler, | ||
2527 | 1400 | (gpointer *) &instance, 1); | ||
2528 | 1401 | child = g_variant_serialised_get_child (serialised, 0); | ||
2529 | 1402 | g_assert (child.type_info == instance->type_info); | ||
2530 | 1403 | random_instance_assert (instance, child.data, child.size); | ||
2531 | 1404 | g_variant_type_info_unref (child.type_info); | ||
2532 | 1405 | flavoured_free (serialised.data, flavour); | ||
2533 | 1406 | } | ||
2534 | 1407 | } | ||
2535 | 1408 | |||
2536 | 1409 | g_variant_type_info_unref (type_info); | ||
2537 | 1410 | random_instance_free (instance); | ||
2538 | 1411 | align_free (data); | ||
2539 | 1412 | } | ||
2540 | 1413 | |||
2541 | 1414 | static void | ||
2542 | 1415 | test_maybes (void) | ||
2543 | 1416 | { | ||
2544 | 1417 | guint i; | ||
2545 | 1418 | |||
2546 | 1419 | for (i = 0; i < 1000; i++) | ||
2547 | 1420 | test_maybe (); | ||
2548 | 1421 | |||
2549 | 1422 | g_variant_type_info_assert_no_infos (); | ||
2550 | 1423 | } | ||
2551 | 1424 | |||
2552 | 1425 | static void | ||
2553 | 1426 | test_array (void) | ||
2554 | 1427 | { | ||
2555 | 1428 | GVariantTypeInfo *element_info; | ||
2556 | 1429 | GVariantTypeInfo *array_info; | ||
2557 | 1430 | RandomInstance **instances; | ||
2558 | 1431 | gsize needed_size; | ||
2559 | 1432 | gsize offset_size; | ||
2560 | 1433 | guint n_children; | ||
2561 | 1434 | guchar *data; | ||
2562 | 1435 | |||
2563 | 1436 | { | ||
2564 | 1437 | gchar *element_type, *array_type; | ||
2565 | 1438 | |||
2566 | 1439 | element_type = random_type_string (); | ||
2567 | 1440 | array_type = g_strdup_printf ("a%s", element_type); | ||
2568 | 1441 | |||
2569 | 1442 | element_info = g_variant_type_info_get (G_VARIANT_TYPE (element_type)); | ||
2570 | 1443 | array_info = g_variant_type_info_get (G_VARIANT_TYPE (array_type)); | ||
2571 | 1444 | g_assert (g_variant_type_info_element (array_info) == element_info); | ||
2572 | 1445 | |||
2573 | 1446 | g_free (element_type); | ||
2574 | 1447 | g_free (array_type); | ||
2575 | 1448 | } | ||
2576 | 1449 | |||
2577 | 1450 | { | ||
2578 | 1451 | guint i; | ||
2579 | 1452 | |||
2580 | 1453 | n_children = g_test_rand_int_range (0, MAX_ARRAY_CHILDREN); | ||
2581 | 1454 | instances = g_new (RandomInstance *, n_children); | ||
2582 | 1455 | for (i = 0; i < n_children; i++) | ||
2583 | 1456 | instances[i] = random_instance (element_info); | ||
2584 | 1457 | } | ||
2585 | 1458 | |||
2586 | 1459 | needed_size = g_variant_serialiser_needed_size (array_info, | ||
2587 | 1460 | random_instance_filler, | ||
2588 | 1461 | (gpointer *) instances, | ||
2589 | 1462 | n_children); | ||
2590 | 1463 | |||
2591 | 1464 | { | ||
2592 | 1465 | gsize element_fixed_size; | ||
2593 | 1466 | gsize body_size = 0; | ||
2594 | 1467 | guint i; | ||
2595 | 1468 | |||
2596 | 1469 | for (i = 0; i < n_children; i++) | ||
2597 | 1470 | append_instance_size (instances[i], &body_size); | ||
2598 | 1471 | |||
2599 | 1472 | g_variant_type_info_query (element_info, NULL, &element_fixed_size); | ||
2600 | 1473 | |||
2601 | 1474 | if (!element_fixed_size) | ||
2602 | 1475 | { | ||
2603 | 1476 | offset_size = calculate_offset_size (body_size, n_children); | ||
2604 | 1477 | |||
2605 | 1478 | if (offset_size == 0) | ||
2606 | 1479 | offset_size = 1; | ||
2607 | 1480 | } | ||
2608 | 1481 | else | ||
2609 | 1482 | offset_size = 0; | ||
2610 | 1483 | |||
2611 | 1484 | g_assert_cmpint (needed_size, ==, body_size + n_children * offset_size); | ||
2612 | 1485 | } | ||
2613 | 1486 | |||
2614 | 1487 | { | ||
2615 | 1488 | guchar *offset_ptr, *body_ptr; | ||
2616 | 1489 | guint i; | ||
2617 | 1490 | |||
2618 | 1491 | body_ptr = data = align_malloc (needed_size); | ||
2619 | 1492 | offset_ptr = body_ptr + needed_size - offset_size * n_children; | ||
2620 | 1493 | |||
2621 | 1494 | for (i = 0; i < n_children; i++) | ||
2622 | 1495 | { | ||
2623 | 1496 | append_instance_data (instances[i], &body_ptr); | ||
2624 | 1497 | append_offset (&offset_ptr, body_ptr - data, offset_size); | ||
2625 | 1498 | } | ||
2626 | 1499 | |||
2627 | 1500 | g_assert (body_ptr == data + needed_size - offset_size * n_children); | ||
2628 | 1501 | g_assert (offset_ptr == data + needed_size); | ||
2629 | 1502 | } | ||
2630 | 1503 | |||
2631 | 1504 | { | ||
2632 | 1505 | guint alignment; | ||
2633 | 1506 | gsize flavour; | ||
2634 | 1507 | guint i; | ||
2635 | 1508 | |||
2636 | 1509 | g_variant_type_info_query (array_info, &alignment, NULL); | ||
2637 | 1510 | alignment = (alignment & ALIGN_BITS) + 1; | ||
2638 | 1511 | |||
2639 | 1512 | for (flavour = 0; flavour < 8; flavour += alignment) | ||
2640 | 1513 | { | ||
2641 | 1514 | GVariantSerialised serialised; | ||
2642 | 1515 | |||
2643 | 1516 | serialised.type_info = array_info; | ||
2644 | 1517 | serialised.data = flavoured_malloc (needed_size, flavour); | ||
2645 | 1518 | serialised.size = needed_size; | ||
2646 | 1519 | |||
2647 | 1520 | g_variant_serialiser_serialise (serialised, random_instance_filler, | ||
2648 | 1521 | (gpointer *) instances, n_children); | ||
2649 | 1522 | |||
2650 | 1523 | g_assert (memcmp (serialised.data, data, serialised.size) == 0); | ||
2651 | 1524 | g_assert (g_variant_serialised_n_children (serialised) == n_children); | ||
2652 | 1525 | |||
2653 | 1526 | for (i = 0; i < n_children; i++) | ||
2654 | 1527 | { | ||
2655 | 1528 | GVariantSerialised child; | ||
2656 | 1529 | |||
2657 | 1530 | child = g_variant_serialised_get_child (serialised, i); | ||
2658 | 1531 | g_assert (child.type_info == instances[i]->type_info); | ||
2659 | 1532 | random_instance_assert (instances[i], child.data, child.size); | ||
2660 | 1533 | g_variant_type_info_unref (child.type_info); | ||
2661 | 1534 | } | ||
2662 | 1535 | |||
2663 | 1536 | flavoured_free (serialised.data, flavour); | ||
2664 | 1537 | } | ||
2665 | 1538 | } | ||
2666 | 1539 | |||
2667 | 1540 | { | ||
2668 | 1541 | guint i; | ||
2669 | 1542 | |||
2670 | 1543 | for (i = 0; i < n_children; i++) | ||
2671 | 1544 | random_instance_free (instances[i]); | ||
2672 | 1545 | g_free (instances); | ||
2673 | 1546 | } | ||
2674 | 1547 | |||
2675 | 1548 | g_variant_type_info_unref (element_info); | ||
2676 | 1549 | g_variant_type_info_unref (array_info); | ||
2677 | 1550 | align_free (data); | ||
2678 | 1551 | } | ||
2679 | 1552 | |||
2680 | 1553 | static void | ||
2681 | 1554 | test_arrays (void) | ||
2682 | 1555 | { | ||
2683 | 1556 | guint i; | ||
2684 | 1557 | |||
2685 | 1558 | for (i = 0; i < 100; i++) | ||
2686 | 1559 | test_array (); | ||
2687 | 1560 | |||
2688 | 1561 | g_variant_type_info_assert_no_infos (); | ||
2689 | 1562 | } | ||
2690 | 1563 | |||
2691 | 1564 | static void | ||
2692 | 1565 | test_tuple (void) | ||
2693 | 1566 | { | ||
2694 | 1567 | GVariantTypeInfo *type_info; | ||
2695 | 1568 | RandomInstance **instances; | ||
2696 | 1569 | gboolean fixed_size; | ||
2697 | 1570 | gsize needed_size; | ||
2698 | 1571 | gsize offset_size; | ||
2699 | 1572 | guint n_children; | ||
2700 | 1573 | guint alignment; | ||
2701 | 1574 | guchar *data; | ||
2702 | 1575 | |||
2703 | 1576 | n_children = g_test_rand_int_range (0, MAX_TUPLE_CHILDREN); | ||
2704 | 1577 | instances = g_new (RandomInstance *, n_children); | ||
2705 | 1578 | |||
2706 | 1579 | { | ||
2707 | 1580 | GString *type_string; | ||
2708 | 1581 | guint i; | ||
2709 | 1582 | |||
2710 | 1583 | fixed_size = TRUE; | ||
2711 | 1584 | alignment = 0; | ||
2712 | 1585 | |||
2713 | 1586 | type_string = g_string_new ("("); | ||
2714 | 1587 | for (i = 0; i < n_children; i++) | ||
2715 | 1588 | { | ||
2716 | 1589 | const gchar *str; | ||
2717 | 1590 | |||
2718 | 1591 | instances[i] = random_instance (NULL); | ||
2719 | 1592 | |||
2720 | 1593 | alignment |= instances[i]->alignment; | ||
2721 | 1594 | if (!instances[i]->is_fixed_sized) | ||
2722 | 1595 | fixed_size = FALSE; | ||
2723 | 1596 | |||
2724 | 1597 | str = g_variant_type_info_get_type_string (instances[i]->type_info); | ||
2725 | 1598 | g_string_append (type_string, str); | ||
2726 | 1599 | } | ||
2727 | 1600 | g_string_append_c (type_string, ')'); | ||
2728 | 1601 | |||
2729 | 1602 | type_info = g_variant_type_info_get (G_VARIANT_TYPE (type_string->str)); | ||
2730 | 1603 | g_string_free (type_string, TRUE); | ||
2731 | 1604 | } | ||
2732 | 1605 | |||
2733 | 1606 | needed_size = g_variant_serialiser_needed_size (type_info, | ||
2734 | 1607 | random_instance_filler, | ||
2735 | 1608 | (gpointer *) instances, | ||
2736 | 1609 | n_children); | ||
2737 | 1610 | { | ||
2738 | 1611 | gsize body_size = 0; | ||
2739 | 1612 | gsize offsets = 0; | ||
2740 | 1613 | guint i; | ||
2741 | 1614 | |||
2742 | 1615 | for (i = 0; i < n_children; i++) | ||
2743 | 1616 | { | ||
2744 | 1617 | append_instance_size (instances[i], &body_size); | ||
2745 | 1618 | |||
2746 | 1619 | if (i != n_children - 1 && !instances[i]->is_fixed_sized) | ||
2747 | 1620 | offsets++; | ||
2748 | 1621 | } | ||
2749 | 1622 | |||
2750 | 1623 | if (fixed_size) | ||
2751 | 1624 | { | ||
2752 | 1625 | body_size += (-body_size) & alignment; | ||
2753 | 1626 | |||
2754 | 1627 | g_assert ((body_size == 0) == (n_children == 0)); | ||
2755 | 1628 | if (n_children == 0) | ||
2756 | 1629 | body_size = 1; | ||
2757 | 1630 | } | ||
2758 | 1631 | |||
2759 | 1632 | offset_size = calculate_offset_size (body_size, offsets); | ||
2760 | 1633 | g_assert_cmpint (needed_size, ==, body_size + offsets * offset_size); | ||
2761 | 1634 | } | ||
2762 | 1635 | |||
2763 | 1636 | { | ||
2764 | 1637 | guchar *body_ptr; | ||
2765 | 1638 | guchar *ofs_ptr; | ||
2766 | 1639 | guint i; | ||
2767 | 1640 | |||
2768 | 1641 | body_ptr = data = align_malloc (needed_size); | ||
2769 | 1642 | ofs_ptr = body_ptr + needed_size; | ||
2770 | 1643 | |||
2771 | 1644 | for (i = 0; i < n_children; i++) | ||
2772 | 1645 | { | ||
2773 | 1646 | append_instance_data (instances[i], &body_ptr); | ||
2774 | 1647 | |||
2775 | 1648 | if (i != n_children - 1 && !instances[i]->is_fixed_sized) | ||
2776 | 1649 | prepend_offset (&ofs_ptr, body_ptr - data, offset_size); | ||
2777 | 1650 | } | ||
2778 | 1651 | |||
2779 | 1652 | if (fixed_size) | ||
2780 | 1653 | { | ||
2781 | 1654 | while (((gsize) body_ptr) & alignment) | ||
2782 | 1655 | *body_ptr++ = '\0'; | ||
2783 | 1656 | |||
2784 | 1657 | g_assert ((body_ptr == data) == (n_children == 0)); | ||
2785 | 1658 | if (n_children == 0) | ||
2786 | 1659 | *body_ptr++ = '\0'; | ||
2787 | 1660 | |||
2788 | 1661 | } | ||
2789 | 1662 | |||
2790 | 1663 | |||
2791 | 1664 | g_assert (body_ptr == ofs_ptr); | ||
2792 | 1665 | } | ||
2793 | 1666 | |||
2794 | 1667 | { | ||
2795 | 1668 | gsize flavour; | ||
2796 | 1669 | guint i; | ||
2797 | 1670 | |||
2798 | 1671 | alignment = (alignment & ALIGN_BITS) + 1; | ||
2799 | 1672 | |||
2800 | 1673 | for (flavour = 0; flavour < 8; flavour += alignment) | ||
2801 | 1674 | { | ||
2802 | 1675 | GVariantSerialised serialised; | ||
2803 | 1676 | |||
2804 | 1677 | serialised.type_info = type_info; | ||
2805 | 1678 | serialised.data = flavoured_malloc (needed_size, flavour); | ||
2806 | 1679 | serialised.size = needed_size; | ||
2807 | 1680 | |||
2808 | 1681 | g_variant_serialiser_serialise (serialised, random_instance_filler, | ||
2809 | 1682 | (gpointer *) instances, n_children); | ||
2810 | 1683 | |||
2811 | 1684 | g_assert (memcmp (serialised.data, data, serialised.size) == 0); | ||
2812 | 1685 | g_assert (g_variant_serialised_n_children (serialised) == n_children); | ||
2813 | 1686 | |||
2814 | 1687 | for (i = 0; i < n_children; i++) | ||
2815 | 1688 | { | ||
2816 | 1689 | GVariantSerialised child; | ||
2817 | 1690 | |||
2818 | 1691 | child = g_variant_serialised_get_child (serialised, i); | ||
2819 | 1692 | g_assert (child.type_info == instances[i]->type_info); | ||
2820 | 1693 | random_instance_assert (instances[i], child.data, child.size); | ||
2821 | 1694 | g_variant_type_info_unref (child.type_info); | ||
2822 | 1695 | } | ||
2823 | 1696 | |||
2824 | 1697 | flavoured_free (serialised.data, flavour); | ||
2825 | 1698 | } | ||
2826 | 1699 | } | ||
2827 | 1700 | |||
2828 | 1701 | { | ||
2829 | 1702 | guint i; | ||
2830 | 1703 | |||
2831 | 1704 | for (i = 0; i < n_children; i++) | ||
2832 | 1705 | random_instance_free (instances[i]); | ||
2833 | 1706 | g_free (instances); | ||
2834 | 1707 | } | ||
2835 | 1708 | |||
2836 | 1709 | g_variant_type_info_unref (type_info); | ||
2837 | 1710 | align_free (data); | ||
2838 | 1711 | } | ||
2839 | 1712 | |||
2840 | 1713 | static void | ||
2841 | 1714 | test_tuples (void) | ||
2842 | 1715 | { | ||
2843 | 1716 | guint i; | ||
2844 | 1717 | |||
2845 | 1718 | for (i = 0; i < 100; i++) | ||
2846 | 1719 | test_tuple (); | ||
2847 | 1720 | |||
2848 | 1721 | g_variant_type_info_assert_no_infos (); | ||
2849 | 1722 | } | ||
2850 | 1723 | |||
2851 | 1724 | static void | ||
2852 | 1725 | test_variant (void) | ||
2853 | 1726 | { | ||
2854 | 1727 | GVariantTypeInfo *type_info; | ||
2855 | 1728 | RandomInstance *instance; | ||
2856 | 1729 | const gchar *type_string; | ||
2857 | 1730 | gsize needed_size; | ||
2858 | 1731 | guchar *data; | ||
2859 | 1732 | gsize len; | ||
2860 | 1733 | |||
2861 | 1734 | type_info = g_variant_type_info_get (G_VARIANT_TYPE_VARIANT); | ||
2862 | 1735 | instance = random_instance (NULL); | ||
2863 | 1736 | |||
2864 | 1737 | type_string = g_variant_type_info_get_type_string (instance->type_info); | ||
2865 | 1738 | len = strlen (type_string); | ||
2866 | 1739 | |||
2867 | 1740 | needed_size = g_variant_serialiser_needed_size (type_info, | ||
2868 | 1741 | random_instance_filler, | ||
2869 | 1742 | (gpointer *) &instance, 1); | ||
2870 | 1743 | |||
2871 | 1744 | g_assert_cmpint (needed_size, ==, instance->size + 1 + len); | ||
2872 | 1745 | |||
2873 | 1746 | { | ||
2874 | 1747 | guchar *ptr; | ||
2875 | 1748 | |||
2876 | 1749 | ptr = data = align_malloc (needed_size); | ||
2877 | 1750 | append_instance_data (instance, &ptr); | ||
2878 | 1751 | *ptr++ = '\0'; | ||
2879 | 1752 | memcpy (ptr, type_string, len); | ||
2880 | 1753 | ptr += len; | ||
2881 | 1754 | |||
2882 | 1755 | g_assert (data + needed_size == ptr); | ||
2883 | 1756 | } | ||
2884 | 1757 | |||
2885 | 1758 | { | ||
2886 | 1759 | gsize alignment; | ||
2887 | 1760 | gsize flavour; | ||
2888 | 1761 | |||
2889 | 1762 | /* variants are always 8-aligned */ | ||
2890 | 1763 | alignment = ALIGN_BITS + 1; | ||
2891 | 1764 | |||
2892 | 1765 | for (flavour = 0; flavour < 8; flavour += alignment) | ||
2893 | 1766 | { | ||
2894 | 1767 | GVariantSerialised serialised; | ||
2895 | 1768 | GVariantSerialised child; | ||
2896 | 1769 | |||
2897 | 1770 | serialised.type_info = type_info; | ||
2898 | 1771 | serialised.data = flavoured_malloc (needed_size, flavour); | ||
2899 | 1772 | serialised.size = needed_size; | ||
2900 | 1773 | |||
2901 | 1774 | g_variant_serialiser_serialise (serialised, random_instance_filler, | ||
2902 | 1775 | (gpointer *) &instance, 1); | ||
2903 | 1776 | |||
2904 | 1777 | g_assert (memcmp (serialised.data, data, serialised.size) == 0); | ||
2905 | 1778 | g_assert (g_variant_serialised_n_children (serialised) == 1); | ||
2906 | 1779 | |||
2907 | 1780 | child = g_variant_serialised_get_child (serialised, 0); | ||
2908 | 1781 | g_assert (child.type_info == instance->type_info); | ||
2909 | 1782 | random_instance_check (instance, child.data, child.size); | ||
2910 | 1783 | |||
2911 | 1784 | g_variant_type_info_unref (child.type_info); | ||
2912 | 1785 | flavoured_free (serialised.data, flavour); | ||
2913 | 1786 | } | ||
2914 | 1787 | } | ||
2915 | 1788 | |||
2916 | 1789 | g_variant_type_info_unref (type_info); | ||
2917 | 1790 | random_instance_free (instance); | ||
2918 | 1791 | align_free (data); | ||
2919 | 1792 | } | ||
2920 | 1793 | |||
2921 | 1794 | static void | ||
2922 | 1795 | test_variants (void) | ||
2923 | 1796 | { | ||
2924 | 1797 | guint i; | ||
2925 | 1798 | |||
2926 | 1799 | for (i = 0; i < 100; i++) | ||
2927 | 1800 | test_variant (); | ||
2928 | 1801 | |||
2929 | 1802 | g_variant_type_info_assert_no_infos (); | ||
2930 | 1803 | } | ||
2931 | 1804 | |||
2932 | 1805 | static void | ||
2933 | 1806 | test_strings (void) | ||
2934 | 1807 | { | ||
2935 | 1808 | struct { | ||
2936 | 1809 | guint flags; | ||
2937 | 1810 | guint size; | ||
2938 | 1811 | gconstpointer data; | ||
2939 | 1812 | } test_cases[] = { | ||
2940 | 1813 | #define is_nval 0 | ||
2941 | 1814 | #define is_string 1 | ||
2942 | 1815 | #define is_objpath is_string | 2 | ||
2943 | 1816 | #define is_sig is_string | 4 | ||
2944 | 1817 | { is_sig, 1, "" }, | ||
2945 | 1818 | { is_nval, 0, NULL }, | ||
2946 | 1819 | { is_nval, 13, "hello\xffworld!" }, | ||
2947 | 1820 | { is_string, 13, "hello world!" }, | ||
2948 | 1821 | { is_nval, 13, "hello world\0" }, | ||
2949 | 1822 | { is_nval, 13, "hello\0world!" }, | ||
2950 | 1823 | { is_nval, 12, "hello world!" }, | ||
2951 | 1824 | { is_nval, 13, "hello world!\xff" }, | ||
2952 | 1825 | |||
2953 | 1826 | { is_objpath, 2, "/" }, | ||
2954 | 1827 | { is_objpath, 3, "/a" }, | ||
2955 | 1828 | { is_string, 3, "//" }, | ||
2956 | 1829 | { is_objpath, 11, "/some/path" }, | ||
2957 | 1830 | { is_string, 12, "/some/path/" }, | ||
2958 | 1831 | { is_nval, 11, "/some\0path" }, | ||
2959 | 1832 | { is_string, 11, "/some\\path" }, | ||
2960 | 1833 | { is_string, 12, "/some//path" }, | ||
2961 | 1834 | { is_string, 12, "/some-/path" }, | ||
2962 | 1835 | |||
2963 | 1836 | { is_sig, 2, "i" }, | ||
2964 | 1837 | { is_sig, 2, "s" }, | ||
2965 | 1838 | { is_sig, 5, "(si)" }, | ||
2966 | 1839 | { is_string, 4, "(si" }, | ||
2967 | 1840 | { is_string, 2, "*" }, | ||
2968 | 1841 | { is_sig, 3, "ai" }, | ||
2969 | 1842 | { is_string, 3, "mi" }, | ||
2970 | 1843 | { is_string, 2, "r" }, | ||
2971 | 1844 | { is_sig, 15, "(yyy{sv}ssiai)" }, | ||
2972 | 1845 | { is_string, 16, "(yyy{yv}ssiai))" }, | ||
2973 | 1846 | { is_string, 15, "(yyy{vv}ssiai)" }, | ||
2974 | 1847 | { is_string, 15, "(yyy{sv)ssiai}" } | ||
2975 | 1848 | }; | ||
2976 | 1849 | guint i; | ||
2977 | 1850 | |||
2978 | 1851 | for (i = 0; i < G_N_ELEMENTS (test_cases); i++) | ||
2979 | 1852 | { | ||
2980 | 1853 | guint flags; | ||
2981 | 1854 | |||
2982 | 1855 | flags = g_variant_serialiser_is_string (test_cases[i].data, | ||
2983 | 1856 | test_cases[i].size) | ||
2984 | 1857 | ? 1 : 0; | ||
2985 | 1858 | |||
2986 | 1859 | flags |= g_variant_serialiser_is_object_path (test_cases[i].data, | ||
2987 | 1860 | test_cases[i].size) | ||
2988 | 1861 | ? 2 : 0; | ||
2989 | 1862 | |||
2990 | 1863 | flags |= g_variant_serialiser_is_signature (test_cases[i].data, | ||
2991 | 1864 | test_cases[i].size) | ||
2992 | 1865 | ? 4 : 0; | ||
2993 | 1866 | |||
2994 | 1867 | g_assert (flags == test_cases[i].flags); | ||
2995 | 1868 | } | ||
2996 | 1869 | } | ||
2997 | 1870 | |||
2998 | 1871 | typedef struct _TreeInstance TreeInstance; | ||
2999 | 1872 | struct _TreeInstance | ||
3000 | 1873 | { | ||
3001 | 1874 | GVariantTypeInfo *info; | ||
3002 | 1875 | |||
3003 | 1876 | TreeInstance **children; | ||
3004 | 1877 | gsize n_children; | ||
3005 | 1878 | |||
3006 | 1879 | union { | ||
3007 | 1880 | guint64 integer; | ||
3008 | 1881 | gdouble floating; | ||
3009 | 1882 | gchar string[32]; | ||
3010 | 1883 | } data; | ||
3011 | 1884 | gsize data_size; | ||
3012 | 1885 | }; | ||
3013 | 1886 | |||
3014 | 1887 | static GVariantType * | ||
3015 | 1888 | make_random_definite_type (int depth) | ||
3016 | 1889 | { | ||
3017 | 1890 | GString *description; | ||
3018 | 1891 | GString *type_string; | ||
3019 | 1892 | GVariantType *type; | ||
3020 | 1893 | |||
3021 | 1894 | description = g_string_new (NULL); | ||
3022 | 1895 | type_string = g_string_new (NULL); | ||
3023 | 1896 | type = append_type_string (type_string, description, TRUE, depth); | ||
3024 | 1897 | g_string_free (description, TRUE); | ||
3025 | 1898 | g_string_free (type_string, TRUE); | ||
3026 | 1899 | |||
3027 | 1900 | return type; | ||
3028 | 1901 | } | ||
3029 | 1902 | |||
3030 | 1903 | static void | ||
3031 | 1904 | make_random_string (gchar *string, | ||
3032 | 1905 | gsize size, | ||
3033 | 1906 | const GVariantType *type) | ||
3034 | 1907 | { | ||
3035 | 1908 | gint i; | ||
3036 | 1909 | |||
3037 | 1910 | /* create strings that are valid signature strings */ | ||
3038 | 1911 | #define good_chars "bynqiuxthdsog" | ||
3039 | 1912 | |||
3040 | 1913 | for (i = 0; i < size - 1; i++) | ||
3041 | 1914 | string[i] = good_chars[g_test_rand_int_range (0, strlen (good_chars))]; | ||
3042 | 1915 | string[i] = '\0'; | ||
3043 | 1916 | |||
3044 | 1917 | /* in case we need an object path, prefix a '/' */ | ||
3045 | 1918 | if (*g_variant_type_peek_string (type) == 'o') | ||
3046 | 1919 | string[0] = '/'; | ||
3047 | 1920 | |||
3048 | 1921 | #undef good_chars | ||
3049 | 1922 | } | ||
3050 | 1923 | |||
3051 | 1924 | static TreeInstance * | ||
3052 | 1925 | tree_instance_new (const GVariantType *type, | ||
3053 | 1926 | int depth) | ||
3054 | 1927 | { | ||
3055 | 1928 | const GVariantType *child_type = NULL; | ||
3056 | 1929 | GVariantType *mytype = NULL; | ||
3057 | 1930 | TreeInstance *instance; | ||
3058 | 1931 | gboolean is_tuple_type; | ||
3059 | 1932 | |||
3060 | 1933 | if (type == NULL) | ||
3061 | 1934 | type = mytype = make_random_definite_type (depth); | ||
3062 | 1935 | |||
3063 | 1936 | instance = g_slice_new (TreeInstance); | ||
3064 | 1937 | instance->info = g_variant_type_info_get (type); | ||
3065 | 1938 | instance->children = NULL; | ||
3066 | 1939 | instance->n_children = 0; | ||
3067 | 1940 | instance->data_size = 0; | ||
3068 | 1941 | |||
3069 | 1942 | is_tuple_type = FALSE; | ||
3070 | 1943 | |||
3071 | 1944 | switch (*g_variant_type_peek_string (type)) | ||
3072 | 1945 | { | ||
3073 | 1946 | case G_VARIANT_TYPE_INFO_CHAR_MAYBE: | ||
3074 | 1947 | instance->n_children = g_test_rand_int_range (0, 2); | ||
3075 | 1948 | child_type = g_variant_type_element (type); | ||
3076 | 1949 | break; | ||
3077 | 1950 | |||
3078 | 1951 | case G_VARIANT_TYPE_INFO_CHAR_ARRAY: | ||
3079 | 1952 | instance->n_children = g_test_rand_int_range (0, MAX_ARRAY_CHILDREN); | ||
3080 | 1953 | child_type = g_variant_type_element (type); | ||
3081 | 1954 | break; | ||
3082 | 1955 | |||
3083 | 1956 | case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: | ||
3084 | 1957 | case G_VARIANT_TYPE_INFO_CHAR_TUPLE: | ||
3085 | 1958 | instance->n_children = g_variant_type_n_items (type); | ||
3086 | 1959 | child_type = g_variant_type_first (type); | ||
3087 | 1960 | is_tuple_type = TRUE; | ||
3088 | 1961 | break; | ||
3089 | 1962 | |||
3090 | 1963 | case G_VARIANT_TYPE_INFO_CHAR_VARIANT: | ||
3091 | 1964 | instance->n_children = 1; | ||
3092 | 1965 | child_type = NULL; | ||
3093 | 1966 | break; | ||
3094 | 1967 | |||
3095 | 1968 | case 'b': | ||
3096 | 1969 | instance->data.integer = g_test_rand_int_range (0, 2); | ||
3097 | 1970 | instance->data_size = 1; | ||
3098 | 1971 | break; | ||
3099 | 1972 | |||
3100 | 1973 | case 'y': | ||
3101 | 1974 | instance->data.integer = g_test_rand_int (); | ||
3102 | 1975 | instance->data_size = 1; | ||
3103 | 1976 | break; | ||
3104 | 1977 | |||
3105 | 1978 | case 'n': case 'q': | ||
3106 | 1979 | instance->data.integer = g_test_rand_int (); | ||
3107 | 1980 | instance->data_size = 2; | ||
3108 | 1981 | break; | ||
3109 | 1982 | |||
3110 | 1983 | case 'i': case 'u': case 'h': | ||
3111 | 1984 | instance->data.integer = g_test_rand_int (); | ||
3112 | 1985 | instance->data_size = 4; | ||
3113 | 1986 | break; | ||
3114 | 1987 | |||
3115 | 1988 | case 'x': case 't': | ||
3116 | 1989 | instance->data.integer = g_test_rand_int (); | ||
3117 | 1990 | instance->data.integer <<= 32; | ||
3118 | 1991 | instance->data.integer |= (guint32) g_test_rand_int (); | ||
3119 | 1992 | instance->data_size = 8; | ||
3120 | 1993 | break; | ||
3121 | 1994 | |||
3122 | 1995 | case 'd': | ||
3123 | 1996 | instance->data.floating = g_test_rand_double (); | ||
3124 | 1997 | instance->data_size = 8; | ||
3125 | 1998 | break; | ||
3126 | 1999 | |||
3127 | 2000 | case 's': case 'o': case 'g': | ||
3128 | 2001 | instance->data_size = g_test_rand_int_range (10, 20); | ||
3129 | 2002 | make_random_string (instance->data.string, instance->data_size, type); | ||
3130 | 2003 | break; | ||
3131 | 2004 | } | ||
3132 | 2005 | |||
3133 | 2006 | if (instance->data_size == 0) | ||
3134 | 2007 | /* no data -> it is a container */ | ||
3135 | 2008 | { | ||
3136 | 2009 | guint i; | ||
3137 | 2010 | |||
3138 | 2011 | instance->children = g_new (TreeInstance *, instance->n_children); | ||
3139 | 2012 | |||
3140 | 2013 | for (i = 0; i < instance->n_children; i++) | ||
3141 | 2014 | { | ||
3142 | 2015 | instance->children[i] = tree_instance_new (child_type, depth - 1); | ||
3143 | 2016 | |||
3144 | 2017 | if (is_tuple_type) | ||
3145 | 2018 | child_type = g_variant_type_next (child_type); | ||
3146 | 2019 | } | ||
3147 | 2020 | |||
3148 | 2021 | g_assert (!is_tuple_type || child_type == NULL); | ||
3149 | 2022 | } | ||
3150 | 2023 | |||
3151 | 2024 | g_variant_type_free (mytype); | ||
3152 | 2025 | |||
3153 | 2026 | return instance; | ||
3154 | 2027 | } | ||
3155 | 2028 | |||
3156 | 2029 | static void | ||
3157 | 2030 | tree_instance_free (TreeInstance *instance) | ||
3158 | 2031 | { | ||
3159 | 2032 | gint i; | ||
3160 | 2033 | |||
3161 | 2034 | g_variant_type_info_unref (instance->info); | ||
3162 | 2035 | for (i = 0; i < instance->n_children; i++) | ||
3163 | 2036 | tree_instance_free (instance->children[i]); | ||
3164 | 2037 | g_free (instance->children); | ||
3165 | 2038 | g_slice_free (TreeInstance, instance); | ||
3166 | 2039 | } | ||
3167 | 2040 | |||
3168 | 2041 | static gboolean i_am_writing_byteswapped; | ||
3169 | 2042 | |||
3170 | 2043 | static void | ||
3171 | 2044 | tree_filler (GVariantSerialised *serialised, | ||
3172 | 2045 | gpointer data) | ||
3173 | 2046 | { | ||
3174 | 2047 | TreeInstance *instance = data; | ||
3175 | 2048 | |||
3176 | 2049 | if (serialised->type_info == NULL) | ||
3177 | 2050 | serialised->type_info = instance->info; | ||
3178 | 2051 | |||
3179 | 2052 | if (instance->data_size == 0) | ||
3180 | 2053 | /* is a container */ | ||
3181 | 2054 | { | ||
3182 | 2055 | if (serialised->size == 0) | ||
3183 | 2056 | serialised->size = | ||
3184 | 2057 | g_variant_serialiser_needed_size (instance->info, tree_filler, | ||
3185 | 2058 | (gpointer *) instance->children, | ||
3186 | 2059 | instance->n_children); | ||
3187 | 2060 | |||
3188 | 2061 | if (serialised->data) | ||
3189 | 2062 | g_variant_serialiser_serialise (*serialised, tree_filler, | ||
3190 | 2063 | (gpointer *) instance->children, | ||
3191 | 2064 | instance->n_children); | ||
3192 | 2065 | } | ||
3193 | 2066 | else | ||
3194 | 2067 | /* it is a leaf */ | ||
3195 | 2068 | { | ||
3196 | 2069 | if (serialised->size == 0) | ||
3197 | 2070 | serialised->size = instance->data_size; | ||
3198 | 2071 | |||
3199 | 2072 | if (serialised->data) | ||
3200 | 2073 | { | ||
3201 | 2074 | switch (instance->data_size) | ||
3202 | 2075 | { | ||
3203 | 2076 | case 1: | ||
3204 | 2077 | *serialised->data = instance->data.integer; | ||
3205 | 2078 | break; | ||
3206 | 2079 | |||
3207 | 2080 | case 2: | ||
3208 | 2081 | { | ||
3209 | 2082 | guint16 value = instance->data.integer; | ||
3210 | 2083 | |||
3211 | 2084 | if (i_am_writing_byteswapped) | ||
3212 | 2085 | value = GUINT16_SWAP_LE_BE (value); | ||
3213 | 2086 | |||
3214 | 2087 | *(guint16 *) serialised->data = value; | ||
3215 | 2088 | } | ||
3216 | 2089 | break; | ||
3217 | 2090 | |||
3218 | 2091 | case 4: | ||
3219 | 2092 | { | ||
3220 | 2093 | guint32 value = instance->data.integer; | ||
3221 | 2094 | |||
3222 | 2095 | if (i_am_writing_byteswapped) | ||
3223 | 2096 | value = GUINT32_SWAP_LE_BE (value); | ||
3224 | 2097 | |||
3225 | 2098 | *(guint32 *) serialised->data = value; | ||
3226 | 2099 | } | ||
3227 | 2100 | break; | ||
3228 | 2101 | |||
3229 | 2102 | case 8: | ||
3230 | 2103 | { | ||
3231 | 2104 | guint64 value = instance->data.integer; | ||
3232 | 2105 | |||
3233 | 2106 | if (i_am_writing_byteswapped) | ||
3234 | 2107 | value = GUINT64_SWAP_LE_BE (value); | ||
3235 | 2108 | |||
3236 | 2109 | *(guint64 *) serialised->data = value; | ||
3237 | 2110 | } | ||
3238 | 2111 | break; | ||
3239 | 2112 | |||
3240 | 2113 | default: | ||
3241 | 2114 | memcpy (serialised->data, | ||
3242 | 2115 | instance->data.string, | ||
3243 | 2116 | instance->data_size); | ||
3244 | 2117 | break; | ||
3245 | 2118 | } | ||
3246 | 2119 | } | ||
3247 | 2120 | } | ||
3248 | 2121 | } | ||
3249 | 2122 | |||
3250 | 2123 | static gboolean | ||
3251 | 2124 | check_tree (TreeInstance *instance, | ||
3252 | 2125 | GVariantSerialised serialised) | ||
3253 | 2126 | { | ||
3254 | 2127 | if (instance->info != serialised.type_info) | ||
3255 | 2128 | return FALSE; | ||
3256 | 2129 | |||
3257 | 2130 | if (instance->data_size == 0) | ||
3258 | 2131 | /* is a container */ | ||
3259 | 2132 | { | ||
3260 | 2133 | gint i; | ||
3261 | 2134 | |||
3262 | 2135 | if (g_variant_serialised_n_children (serialised) != | ||
3263 | 2136 | instance->n_children) | ||
3264 | 2137 | return FALSE; | ||
3265 | 2138 | |||
3266 | 2139 | for (i = 0; i < instance->n_children; i++) | ||
3267 | 2140 | { | ||
3268 | 2141 | GVariantSerialised child; | ||
3269 | 2142 | gpointer data = NULL; | ||
3270 | 2143 | gboolean ok; | ||
3271 | 2144 | |||
3272 | 2145 | child = g_variant_serialised_get_child (serialised, i); | ||
3273 | 2146 | if (child.size && child.data == NULL) | ||
3274 | 2147 | child.data = data = g_malloc0 (child.size); | ||
3275 | 2148 | ok = check_tree (instance->children[i], child); | ||
3276 | 2149 | g_variant_type_info_unref (child.type_info); | ||
3277 | 2150 | g_free (data); | ||
3278 | 2151 | |||
3279 | 2152 | if (!ok) | ||
3280 | 2153 | return FALSE; | ||
3281 | 2154 | } | ||
3282 | 2155 | |||
3283 | 2156 | return TRUE; | ||
3284 | 2157 | } | ||
3285 | 2158 | else | ||
3286 | 2159 | /* it is a leaf */ | ||
3287 | 2160 | { | ||
3288 | 2161 | switch (instance->data_size) | ||
3289 | 2162 | { | ||
3290 | 2163 | case 1: | ||
3291 | 2164 | g_assert (serialised.size == 1); | ||
3292 | 2165 | return *(guint8 *) serialised.data == | ||
3293 | 2166 | (guint8) instance->data.integer; | ||
3294 | 2167 | |||
3295 | 2168 | case 2: | ||
3296 | 2169 | g_assert (serialised.size == 2); | ||
3297 | 2170 | return *(guint16 *) serialised.data == | ||
3298 | 2171 | (guint16) instance->data.integer; | ||
3299 | 2172 | |||
3300 | 2173 | case 4: | ||
3301 | 2174 | g_assert (serialised.size == 4); | ||
3302 | 2175 | return *(guint32 *) serialised.data == | ||
3303 | 2176 | (guint32) instance->data.integer; | ||
3304 | 2177 | |||
3305 | 2178 | case 8: | ||
3306 | 2179 | g_assert (serialised.size == 8); | ||
3307 | 2180 | return *(guint64 *) serialised.data == | ||
3308 | 2181 | (guint64) instance->data.integer; | ||
3309 | 2182 | |||
3310 | 2183 | default: | ||
3311 | 2184 | if (serialised.size != instance->data_size) | ||
3312 | 2185 | return FALSE; | ||
3313 | 2186 | |||
3314 | 2187 | return memcmp (serialised.data, | ||
3315 | 2188 | instance->data.string, | ||
3316 | 2189 | instance->data_size) == 0; | ||
3317 | 2190 | } | ||
3318 | 2191 | } | ||
3319 | 2192 | } | ||
3320 | 2193 | |||
3321 | 2194 | static void | ||
3322 | 2195 | serialise_tree (TreeInstance *tree, | ||
3323 | 2196 | GVariantSerialised *serialised) | ||
3324 | 2197 | { | ||
3325 | 2198 | GVariantSerialised empty = { }; | ||
3326 | 2199 | |||
3327 | 2200 | *serialised = empty; | ||
3328 | 2201 | tree_filler (serialised, tree); | ||
3329 | 2202 | serialised->data = g_malloc (serialised->size); | ||
3330 | 2203 | tree_filler (serialised, tree); | ||
3331 | 2204 | } | ||
3332 | 2205 | |||
3333 | 2206 | static void | ||
3334 | 2207 | test_byteswap (void) | ||
3335 | 2208 | { | ||
3336 | 2209 | GVariantSerialised one, two; | ||
3337 | 2210 | TreeInstance *tree; | ||
3338 | 2211 | |||
3339 | 2212 | tree = tree_instance_new (NULL, 3); | ||
3340 | 2213 | serialise_tree (tree, &one); | ||
3341 | 2214 | |||
3342 | 2215 | i_am_writing_byteswapped = TRUE; | ||
3343 | 2216 | serialise_tree (tree, &two); | ||
3344 | 2217 | i_am_writing_byteswapped = FALSE; | ||
3345 | 2218 | |||
3346 | 2219 | g_variant_serialised_byteswap (two); | ||
3347 | 2220 | |||
3348 | 2221 | g_assert_cmpint (one.size, ==, two.size); | ||
3349 | 2222 | g_assert (memcmp (one.data, two.data, one.size) == 0); | ||
3350 | 2223 | |||
3351 | 2224 | tree_instance_free (tree); | ||
3352 | 2225 | g_free (one.data); | ||
3353 | 2226 | g_free (two.data); | ||
3354 | 2227 | } | ||
3355 | 2228 | |||
3356 | 2229 | static void | ||
3357 | 2230 | test_byteswaps (void) | ||
3358 | 2231 | { | ||
3359 | 2232 | int i; | ||
3360 | 2233 | |||
3361 | 2234 | for (i = 0; i < 200; i++) | ||
3362 | 2235 | test_byteswap (); | ||
3363 | 2236 | |||
3364 | 2237 | g_variant_type_info_assert_no_infos (); | ||
3365 | 2238 | } | ||
3366 | 2239 | |||
3367 | 2240 | static void | ||
3368 | 2241 | test_fuzz (gdouble *fuzziness) | ||
3369 | 2242 | { | ||
3370 | 2243 | GVariantSerialised serialised; | ||
3371 | 2244 | TreeInstance *tree; | ||
3372 | 2245 | |||
3373 | 2246 | /* make an instance */ | ||
3374 | 2247 | tree = tree_instance_new (NULL, 3); | ||
3375 | 2248 | |||
3376 | 2249 | /* serialise it */ | ||
3377 | 2250 | serialise_tree (tree, &serialised); | ||
3378 | 2251 | |||
3379 | 2252 | g_assert (g_variant_serialised_is_normal (serialised)); | ||
3380 | 2253 | g_assert (check_tree (tree, serialised)); | ||
3381 | 2254 | |||
3382 | 2255 | if (serialised.size) | ||
3383 | 2256 | { | ||
3384 | 2257 | gboolean fuzzed = FALSE; | ||
3385 | 2258 | gboolean a, b; | ||
3386 | 2259 | |||
3387 | 2260 | while (!fuzzed) | ||
3388 | 2261 | { | ||
3389 | 2262 | gint i; | ||
3390 | 2263 | |||
3391 | 2264 | for (i = 0; i < serialised.size; i++) | ||
3392 | 2265 | if (randomly (*fuzziness)) | ||
3393 | 2266 | { | ||
3394 | 2267 | serialised.data[i] += g_test_rand_int_range (1, 256); | ||
3395 | 2268 | fuzzed = TRUE; | ||
3396 | 2269 | } | ||
3397 | 2270 | } | ||
3398 | 2271 | |||
3399 | 2272 | /* at least one byte in the serialised data has changed. | ||
3400 | 2273 | * | ||
3401 | 2274 | * this means that at least one of the following is true: | ||
3402 | 2275 | * | ||
3403 | 2276 | * - the serialised data now represents a different value: | ||
3404 | 2277 | * check_tree() will return FALSE | ||
3405 | 2278 | * | ||
3406 | 2279 | * - the serialised data is in non-normal form: | ||
3407 | 2280 | * g_variant_serialiser_is_normal() will return FALSE | ||
3408 | 2281 | * | ||
3409 | 2282 | * we always do both checks to increase exposure of the serialiser | ||
3410 | 2283 | * to corrupt data. | ||
3411 | 2284 | */ | ||
3412 | 2285 | a = g_variant_serialised_is_normal (serialised); | ||
3413 | 2286 | b = check_tree (tree, serialised); | ||
3414 | 2287 | |||
3415 | 2288 | g_assert (!a || !b); | ||
3416 | 2289 | } | ||
3417 | 2290 | |||
3418 | 2291 | tree_instance_free (tree); | ||
3419 | 2292 | g_free (serialised.data); | ||
3420 | 2293 | } | ||
3421 | 2294 | |||
3422 | 2295 | |||
3423 | 2296 | static void | ||
3424 | 2297 | test_fuzzes (gpointer data) | ||
3425 | 2298 | { | ||
3426 | 2299 | gdouble fuzziness; | ||
3427 | 2300 | int i; | ||
3428 | 2301 | |||
3429 | 2302 | fuzziness = GPOINTER_TO_INT (data) / 100.; | ||
3430 | 2303 | |||
3431 | 2304 | for (i = 0; i < 200; i++) | ||
3432 | 2305 | test_fuzz (&fuzziness); | ||
3433 | 2306 | |||
3434 | 2307 | g_variant_type_info_assert_no_infos (); | ||
3435 | 2308 | } | ||
3436 | 2309 | |||
3437 | 2310 | static GVariant * | ||
3438 | 2311 | tree_instance_get_gvariant (TreeInstance *tree) | ||
3439 | 2312 | { | ||
3440 | 2313 | const GVariantType *type; | ||
3441 | 2314 | GVariant *result; | ||
3442 | 2315 | |||
3443 | 2316 | type = (GVariantType *) g_variant_type_info_get_type_string (tree->info); | ||
3444 | 2317 | |||
3445 | 2318 | switch (g_variant_type_info_get_type_char (tree->info)) | ||
3446 | 2319 | { | ||
3447 | 2320 | case G_VARIANT_TYPE_INFO_CHAR_MAYBE: | ||
3448 | 2321 | { | ||
3449 | 2322 | const GVariantType *child_type; | ||
3450 | 2323 | GVariant *child; | ||
3451 | 2324 | |||
3452 | 2325 | if (tree->n_children) | ||
3453 | 2326 | child = tree_instance_get_gvariant (tree->children[0]); | ||
3454 | 2327 | else | ||
3455 | 2328 | child = NULL; | ||
3456 | 2329 | |||
3457 | 2330 | child_type = g_variant_type_element (type); | ||
3458 | 2331 | |||
3459 | 2332 | if (child != NULL && randomly (0.5)) | ||
3460 | 2333 | child_type = NULL; | ||
3461 | 2334 | |||
3462 | 2335 | result = g_variant_new_maybe (child_type, child); | ||
3463 | 2336 | } | ||
3464 | 2337 | break; | ||
3465 | 2338 | |||
3466 | 2339 | case G_VARIANT_TYPE_INFO_CHAR_ARRAY: | ||
3467 | 2340 | { | ||
3468 | 2341 | const GVariantType *child_type; | ||
3469 | 2342 | GVariant **children; | ||
3470 | 2343 | gint i; | ||
3471 | 2344 | |||
3472 | 2345 | children = g_new (GVariant *, tree->n_children); | ||
3473 | 2346 | for (i = 0; i < tree->n_children; i++) | ||
3474 | 2347 | children[i] = tree_instance_get_gvariant (tree->children[i]); | ||
3475 | 2348 | |||
3476 | 2349 | child_type = g_variant_type_element (type); | ||
3477 | 2350 | |||
3478 | 2351 | if (i > 0 && randomly (0.5)) | ||
3479 | 2352 | child_type = NULL; | ||
3480 | 2353 | |||
3481 | 2354 | result = g_variant_new_array (child_type, children, tree->n_children); | ||
3482 | 2355 | g_free (children); | ||
3483 | 2356 | } | ||
3484 | 2357 | break; | ||
3485 | 2358 | |||
3486 | 2359 | case G_VARIANT_TYPE_INFO_CHAR_TUPLE: | ||
3487 | 2360 | { | ||
3488 | 2361 | GVariant **children; | ||
3489 | 2362 | gint i; | ||
3490 | 2363 | |||
3491 | 2364 | children = g_new (GVariant *, tree->n_children); | ||
3492 | 2365 | for (i = 0; i < tree->n_children; i++) | ||
3493 | 2366 | children[i] = tree_instance_get_gvariant (tree->children[i]); | ||
3494 | 2367 | |||
3495 | 2368 | result = g_variant_new_tuple (children, tree->n_children); | ||
3496 | 2369 | g_free (children); | ||
3497 | 2370 | } | ||
3498 | 2371 | break; | ||
3499 | 2372 | |||
3500 | 2373 | case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: | ||
3501 | 2374 | { | ||
3502 | 2375 | GVariant *key, *val; | ||
3503 | 2376 | |||
3504 | 2377 | g_assert (tree->n_children == 2); | ||
3505 | 2378 | |||
3506 | 2379 | key = tree_instance_get_gvariant (tree->children[0]); | ||
3507 | 2380 | val = tree_instance_get_gvariant (tree->children[1]); | ||
3508 | 2381 | |||
3509 | 2382 | result = g_variant_new_dict_entry (key, val); | ||
3510 | 2383 | } | ||
3511 | 2384 | break; | ||
3512 | 2385 | |||
3513 | 2386 | case G_VARIANT_TYPE_INFO_CHAR_VARIANT: | ||
3514 | 2387 | { | ||
3515 | 2388 | GVariant *value; | ||
3516 | 2389 | |||
3517 | 2390 | g_assert (tree->n_children == 1); | ||
3518 | 2391 | |||
3519 | 2392 | value = tree_instance_get_gvariant (tree->children[0]); | ||
3520 | 2393 | result = g_variant_new_variant (value); | ||
3521 | 2394 | } | ||
3522 | 2395 | break; | ||
3523 | 2396 | |||
3524 | 2397 | case 'b': | ||
3525 | 2398 | result = g_variant_new_boolean (tree->data.integer > 0); | ||
3526 | 2399 | break; | ||
3527 | 2400 | |||
3528 | 2401 | case 'y': | ||
3529 | 2402 | result = g_variant_new_byte (tree->data.integer); | ||
3530 | 2403 | break; | ||
3531 | 2404 | |||
3532 | 2405 | case 'n': | ||
3533 | 2406 | result = g_variant_new_int16 (tree->data.integer); | ||
3534 | 2407 | break; | ||
3535 | 2408 | |||
3536 | 2409 | case 'q': | ||
3537 | 2410 | result = g_variant_new_uint16 (tree->data.integer); | ||
3538 | 2411 | break; | ||
3539 | 2412 | |||
3540 | 2413 | case 'i': | ||
3541 | 2414 | result = g_variant_new_int32 (tree->data.integer); | ||
3542 | 2415 | break; | ||
3543 | 2416 | |||
3544 | 2417 | case 'u': | ||
3545 | 2418 | result = g_variant_new_uint32 (tree->data.integer); | ||
3546 | 2419 | break; | ||
3547 | 2420 | |||
3548 | 2421 | case 'x': | ||
3549 | 2422 | result = g_variant_new_int64 (tree->data.integer); | ||
3550 | 2423 | break; | ||
3551 | 2424 | |||
3552 | 2425 | case 't': | ||
3553 | 2426 | result = g_variant_new_uint64 (tree->data.integer); | ||
3554 | 2427 | break; | ||
3555 | 2428 | |||
3556 | 2429 | case 'h': | ||
3557 | 2430 | result = g_variant_new_handle (tree->data.integer); | ||
3558 | 2431 | break; | ||
3559 | 2432 | |||
3560 | 2433 | case 'd': | ||
3561 | 2434 | result = g_variant_new_double (tree->data.floating); | ||
3562 | 2435 | break; | ||
3563 | 2436 | |||
3564 | 2437 | case 's': | ||
3565 | 2438 | result = g_variant_new_string (tree->data.string); | ||
3566 | 2439 | break; | ||
3567 | 2440 | |||
3568 | 2441 | case 'o': | ||
3569 | 2442 | result = g_variant_new_object_path (tree->data.string); | ||
3570 | 2443 | break; | ||
3571 | 2444 | |||
3572 | 2445 | case 'g': | ||
3573 | 2446 | result = g_variant_new_signature (tree->data.string); | ||
3574 | 2447 | break; | ||
3575 | 2448 | |||
3576 | 2449 | default: | ||
3577 | 2450 | g_assert_not_reached (); | ||
3578 | 2451 | } | ||
3579 | 2452 | |||
3580 | 2453 | return result; | ||
3581 | 2454 | } | ||
3582 | 2455 | |||
3583 | 2456 | static gboolean | ||
3584 | 2457 | tree_instance_check_gvariant (TreeInstance *tree, | ||
3585 | 2458 | GVariant *value) | ||
3586 | 2459 | { | ||
3587 | 2460 | const GVariantType *type; | ||
3588 | 2461 | |||
3589 | 2462 | type = (GVariantType *) g_variant_type_info_get_type_string (tree->info); | ||
3590 | 2463 | g_assert (g_variant_is_of_type (value, type)); | ||
3591 | 2464 | |||
3592 | 2465 | switch (g_variant_type_info_get_type_char (tree->info)) | ||
3593 | 2466 | { | ||
3594 | 2467 | case G_VARIANT_TYPE_INFO_CHAR_MAYBE: | ||
3595 | 2468 | { | ||
3596 | 2469 | GVariant *child; | ||
3597 | 2470 | gboolean equal; | ||
3598 | 2471 | |||
3599 | 2472 | child = g_variant_get_maybe (value); | ||
3600 | 2473 | |||
3601 | 2474 | if (child != NULL && tree->n_children == 1) | ||
3602 | 2475 | equal = tree_instance_check_gvariant (tree->children[0], child); | ||
3603 | 2476 | else if (child == NULL && tree->n_children == 0) | ||
3604 | 2477 | equal = TRUE; | ||
3605 | 2478 | else | ||
3606 | 2479 | equal = FALSE; | ||
3607 | 2480 | |||
3608 | 2481 | if (child != NULL) | ||
3609 | 2482 | g_variant_unref (child); | ||
3610 | 2483 | |||
3611 | 2484 | return equal; | ||
3612 | 2485 | } | ||
3613 | 2486 | break; | ||
3614 | 2487 | |||
3615 | 2488 | case G_VARIANT_TYPE_INFO_CHAR_ARRAY: | ||
3616 | 2489 | case G_VARIANT_TYPE_INFO_CHAR_TUPLE: | ||
3617 | 2490 | case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: | ||
3618 | 2491 | { | ||
3619 | 2492 | gsize i; | ||
3620 | 2493 | |||
3621 | 2494 | if (g_variant_n_children (value) != tree->n_children) | ||
3622 | 2495 | return FALSE; | ||
3623 | 2496 | |||
3624 | 2497 | for (i = 0; i < tree->n_children; i++) | ||
3625 | 2498 | { | ||
3626 | 2499 | GVariant *child; | ||
3627 | 2500 | gboolean equal; | ||
3628 | 2501 | |||
3629 | 2502 | child = g_variant_get_child_value (value, i); | ||
3630 | 2503 | equal = tree_instance_check_gvariant (tree->children[i], child); | ||
3631 | 2504 | g_variant_unref (child); | ||
3632 | 2505 | |||
3633 | 2506 | if (!equal) | ||
3634 | 2507 | return FALSE; | ||
3635 | 2508 | } | ||
3636 | 2509 | |||
3637 | 2510 | return TRUE; | ||
3638 | 2511 | } | ||
3639 | 2512 | break; | ||
3640 | 2513 | |||
3641 | 2514 | case G_VARIANT_TYPE_INFO_CHAR_VARIANT: | ||
3642 | 2515 | { | ||
3643 | 2516 | const gchar *str1, *str2; | ||
3644 | 2517 | GVariant *child; | ||
3645 | 2518 | gboolean equal; | ||
3646 | 2519 | |||
3647 | 2520 | child = g_variant_get_variant (value); | ||
3648 | 2521 | str1 = g_variant_get_type_string (child); | ||
3649 | 2522 | str2 = g_variant_type_info_get_type_string (tree->children[0]->info); | ||
3650 | 2523 | /* GVariant only keeps one copy of type strings around */ | ||
3651 | 2524 | equal = str1 == str2 && | ||
3652 | 2525 | tree_instance_check_gvariant (tree->children[0], child); | ||
3653 | 2526 | |||
3654 | 2527 | g_variant_unref (child); | ||
3655 | 2528 | |||
3656 | 2529 | return equal; | ||
3657 | 2530 | } | ||
3658 | 2531 | break; | ||
3659 | 2532 | |||
3660 | 2533 | case 'b': | ||
3661 | 2534 | return g_variant_get_boolean (value) == tree->data.integer; | ||
3662 | 2535 | |||
3663 | 2536 | case 'y': | ||
3664 | 2537 | return g_variant_get_byte (value) == (guchar) tree->data.integer; | ||
3665 | 2538 | |||
3666 | 2539 | case 'n': | ||
3667 | 2540 | return g_variant_get_int16 (value) == (gint16) tree->data.integer; | ||
3668 | 2541 | |||
3669 | 2542 | case 'q': | ||
3670 | 2543 | return g_variant_get_uint16 (value) == (guint16) tree->data.integer; | ||
3671 | 2544 | |||
3672 | 2545 | case 'i': | ||
3673 | 2546 | return g_variant_get_int32 (value) == (gint32) tree->data.integer; | ||
3674 | 2547 | |||
3675 | 2548 | case 'u': | ||
3676 | 2549 | return g_variant_get_uint32 (value) == (guint32) tree->data.integer; | ||
3677 | 2550 | |||
3678 | 2551 | case 'x': | ||
3679 | 2552 | return g_variant_get_int64 (value) == (gint64) tree->data.integer; | ||
3680 | 2553 | |||
3681 | 2554 | case 't': | ||
3682 | 2555 | return g_variant_get_uint64 (value) == (guint64) tree->data.integer; | ||
3683 | 2556 | |||
3684 | 2557 | case 'h': | ||
3685 | 2558 | return g_variant_get_handle (value) == (gint32) tree->data.integer; | ||
3686 | 2559 | |||
3687 | 2560 | case 'd': | ||
3688 | 2561 | { | ||
3689 | 2562 | gdouble floating = g_variant_get_double (value); | ||
3690 | 2563 | |||
3691 | 2564 | return memcmp (&floating, &tree->data.floating, sizeof floating) == 0; | ||
3692 | 2565 | } | ||
3693 | 2566 | |||
3694 | 2567 | case 's': | ||
3695 | 2568 | case 'o': | ||
3696 | 2569 | case 'g': | ||
3697 | 2570 | return strcmp (g_variant_get_string (value, NULL), | ||
3698 | 2571 | tree->data.string) == 0; | ||
3699 | 2572 | |||
3700 | 2573 | default: | ||
3701 | 2574 | g_assert_not_reached (); | ||
3702 | 2575 | } | ||
3703 | 2576 | } | ||
3704 | 2577 | |||
3705 | 2578 | static void | ||
3706 | 2579 | tree_instance_build_gvariant (TreeInstance *tree, | ||
3707 | 2580 | GVariantBuilder *builder, | ||
3708 | 2581 | gboolean guess_ok) | ||
3709 | 2582 | { | ||
3710 | 2583 | const GVariantType *type; | ||
3711 | 2584 | |||
3712 | 2585 | type = (GVariantType *) g_variant_type_info_get_type_string (tree->info); | ||
3713 | 2586 | |||
3714 | 2587 | if (g_variant_type_is_container (type)) | ||
3715 | 2588 | { | ||
3716 | 2589 | gsize i; | ||
3717 | 2590 | |||
3718 | 2591 | /* force GVariantBuilder to guess the type half the time */ | ||
3719 | 2592 | if (guess_ok && randomly (0.5)) | ||
3720 | 2593 | { | ||
3721 | 2594 | if (g_variant_type_is_array (type) && tree->n_children) | ||
3722 | 2595 | type = G_VARIANT_TYPE_ARRAY; | ||
3723 | 2596 | |||
3724 | 2597 | if (g_variant_type_is_maybe (type) && tree->n_children) | ||
3725 | 2598 | type = G_VARIANT_TYPE_MAYBE; | ||
3726 | 2599 | |||
3727 | 2600 | if (g_variant_type_is_tuple (type)) | ||
3728 | 2601 | type = G_VARIANT_TYPE_TUPLE; | ||
3729 | 2602 | |||
3730 | 2603 | if (g_variant_type_is_dict_entry (type)) | ||
3731 | 2604 | type = G_VARIANT_TYPE_DICT_ENTRY; | ||
3732 | 2605 | } | ||
3733 | 2606 | else | ||
3734 | 2607 | guess_ok = FALSE; | ||
3735 | 2608 | |||
3736 | 2609 | g_variant_builder_open (builder, type); | ||
3737 | 2610 | |||
3738 | 2611 | for (i = 0; i < tree->n_children; i++) | ||
3739 | 2612 | tree_instance_build_gvariant (tree->children[i], builder, guess_ok); | ||
3740 | 2613 | |||
3741 | 2614 | g_variant_builder_close (builder); | ||
3742 | 2615 | } | ||
3743 | 2616 | else | ||
3744 | 2617 | g_variant_builder_add_value (builder, tree_instance_get_gvariant (tree)); | ||
3745 | 2618 | } | ||
3746 | 2619 | |||
3747 | 2620 | |||
3748 | 2621 | static gboolean | ||
3749 | 2622 | tree_instance_check_iter (TreeInstance *tree, | ||
3750 | 2623 | GVariantIter *iter) | ||
3751 | 2624 | { | ||
3752 | 2625 | GVariant *value; | ||
3753 | 2626 | |||
3754 | 2627 | value = g_variant_iter_next_value (iter); | ||
3755 | 2628 | |||
3756 | 2629 | if (g_variant_is_container (value)) | ||
3757 | 2630 | { | ||
3758 | 2631 | gsize i; | ||
3759 | 2632 | |||
3760 | 2633 | iter = g_variant_iter_new (value); | ||
3761 | 2634 | g_variant_unref (value); | ||
3762 | 2635 | |||
3763 | 2636 | if (g_variant_iter_n_children (iter) != tree->n_children) | ||
3764 | 2637 | { | ||
3765 | 2638 | g_variant_iter_free (iter); | ||
3766 | 2639 | return FALSE; | ||
3767 | 2640 | } | ||
3768 | 2641 | |||
3769 | 2642 | for (i = 0; i < tree->n_children; i++) | ||
3770 | 2643 | if (!tree_instance_check_iter (tree->children[i], iter)) | ||
3771 | 2644 | { | ||
3772 | 2645 | g_variant_iter_free (iter); | ||
3773 | 2646 | return FALSE; | ||
3774 | 2647 | } | ||
3775 | 2648 | |||
3776 | 2649 | g_assert (g_variant_iter_next_value (iter) == NULL); | ||
3777 | 2650 | g_variant_iter_free (iter); | ||
3778 | 2651 | |||
3779 | 2652 | return TRUE; | ||
3780 | 2653 | } | ||
3781 | 2654 | |||
3782 | 2655 | else | ||
3783 | 2656 | { | ||
3784 | 2657 | gboolean equal; | ||
3785 | 2658 | |||
3786 | 2659 | equal = tree_instance_check_gvariant (tree, value); | ||
3787 | 2660 | g_variant_unref (value); | ||
3788 | 2661 | |||
3789 | 2662 | return equal; | ||
3790 | 2663 | } | ||
3791 | 2664 | } | ||
3792 | 2665 | |||
3793 | 2666 | static void | ||
3794 | 2667 | test_container (void) | ||
3795 | 2668 | { | ||
3796 | 2669 | TreeInstance *tree; | ||
3797 | 2670 | GVariant *value; | ||
3798 | 2671 | gchar *s1, *s2; | ||
3799 | 2672 | |||
3800 | 2673 | tree = tree_instance_new (NULL, 3); | ||
3801 | 2674 | value = g_variant_ref_sink (tree_instance_get_gvariant (tree)); | ||
3802 | 2675 | |||
3803 | 2676 | s1 = g_variant_print (value, TRUE); | ||
3804 | 2677 | g_assert (tree_instance_check_gvariant (tree, value)); | ||
3805 | 2678 | |||
3806 | 2679 | g_variant_get_data (value); | ||
3807 | 2680 | |||
3808 | 2681 | s2 = g_variant_print (value, TRUE); | ||
3809 | 2682 | g_assert (tree_instance_check_gvariant (tree, value)); | ||
3810 | 2683 | |||
3811 | 2684 | g_assert_cmpstr (s1, ==, s2); | ||
3812 | 2685 | |||
3813 | 2686 | if (g_variant_is_container (value)) | ||
3814 | 2687 | { | ||
3815 | 2688 | GVariantBuilder builder; | ||
3816 | 2689 | GVariantIter iter; | ||
3817 | 2690 | GVariant *built; | ||
3818 | 2691 | GVariant *val; | ||
3819 | 2692 | gchar *s3; | ||
3820 | 2693 | |||
3821 | 2694 | g_variant_builder_init (&builder, G_VARIANT_TYPE_VARIANT); | ||
3822 | 2695 | tree_instance_build_gvariant (tree, &builder, TRUE); | ||
3823 | 2696 | built = g_variant_builder_end (&builder); | ||
3824 | 2697 | g_variant_ref_sink (built); | ||
3825 | 2698 | g_variant_get_data (built); | ||
3826 | 2699 | val = g_variant_get_variant (built); | ||
3827 | 2700 | |||
3828 | 2701 | s3 = g_variant_print (val, TRUE); | ||
3829 | 2702 | g_assert_cmpstr (s1, ==, s3); | ||
3830 | 2703 | |||
3831 | 2704 | g_variant_iter_init (&iter, built); | ||
3832 | 2705 | g_assert (tree_instance_check_iter (tree, &iter)); | ||
3833 | 2706 | g_assert (g_variant_iter_next_value (&iter) == NULL); | ||
3834 | 2707 | |||
3835 | 2708 | g_variant_unref (built); | ||
3836 | 2709 | g_variant_unref (val); | ||
3837 | 2710 | g_free (s3); | ||
3838 | 2711 | } | ||
3839 | 2712 | |||
3840 | 2713 | tree_instance_free (tree); | ||
3841 | 2714 | g_variant_unref (value); | ||
3842 | 2715 | g_free (s2); | ||
3843 | 2716 | g_free (s1); | ||
3844 | 2717 | } | ||
3845 | 2718 | |||
3846 | 2719 | static void | ||
3847 | 2720 | test_utf8 (void) | ||
3848 | 2721 | { | ||
3849 | 2722 | const gchar invalid[] = "hello\xffworld"; | ||
3850 | 2723 | GVariant *value; | ||
3851 | 2724 | |||
3852 | 2725 | /* ensure that the test data is not valid utf8... */ | ||
3853 | 2726 | g_assert (!g_utf8_validate (invalid, -1, NULL)); | ||
3854 | 2727 | |||
3855 | 2728 | /* load the data untrusted */ | ||
3856 | 2729 | value = g_variant_new_from_data (G_VARIANT_TYPE_STRING, | ||
3857 | 2730 | invalid, sizeof invalid, | ||
3858 | 2731 | FALSE, NULL, NULL); | ||
3859 | 2732 | |||
3860 | 2733 | /* ensure that the problem is caught and we get valid UTF-8 */ | ||
3861 | 2734 | g_assert (g_utf8_validate (g_variant_get_string (value, NULL), -1, NULL)); | ||
3862 | 2735 | g_variant_unref (value); | ||
3863 | 2736 | |||
3864 | 2737 | |||
3865 | 2738 | /* now load it trusted */ | ||
3866 | 2739 | value = g_variant_new_from_data (G_VARIANT_TYPE_STRING, | ||
3867 | 2740 | invalid, sizeof invalid, | ||
3868 | 2741 | TRUE, NULL, NULL); | ||
3869 | 2742 | |||
3870 | 2743 | /* ensure we get the invalid data (ie: make sure that time wasn't | ||
3871 | 2744 | * wasted on validating data that was marked as trusted) | ||
3872 | 2745 | */ | ||
3873 | 2746 | g_assert (g_variant_get_string (value, NULL) == invalid); | ||
3874 | 2747 | g_variant_unref (value); | ||
3875 | 2748 | } | ||
3876 | 2749 | |||
3877 | 2750 | static void | ||
3878 | 2751 | test_containers (void) | ||
3879 | 2752 | { | ||
3880 | 2753 | gint i; | ||
3881 | 2754 | |||
3882 | 2755 | for (i = 0; i < 100; i++) | ||
3883 | 2756 | { | ||
3884 | 2757 | test_container (); | ||
3885 | 2758 | } | ||
3886 | 2759 | |||
3887 | 2760 | g_variant_type_info_assert_no_infos (); | ||
3888 | 2761 | } | ||
3889 | 2762 | |||
3890 | 2763 | static void | ||
3891 | 2764 | test_format_strings (void) | ||
3892 | 2765 | { | ||
3893 | 2766 | GVariantType *type; | ||
3894 | 2767 | const gchar *end; | ||
3895 | 2768 | |||
3896 | 2769 | g_assert (g_variant_format_string_scan ("i", NULL, &end) && *end == '\0'); | ||
3897 | 2770 | g_assert (g_variant_format_string_scan ("@i", NULL, &end) && *end == '\0'); | ||
3898 | 2771 | g_assert (g_variant_format_string_scan ("@ii", NULL, &end) && *end == 'i'); | ||
3899 | 2772 | g_assert (g_variant_format_string_scan ("^a&s", NULL, &end) && *end == '\0'); | ||
3900 | 2773 | g_assert (g_variant_format_string_scan ("(^as)", NULL, &end) && | ||
3901 | 2774 | *end == '\0'); | ||
3902 | 2775 | g_assert (!g_variant_format_string_scan ("(^s)", NULL, &end)); | ||
3903 | 2776 | g_assert (!g_variant_format_string_scan ("(^a)", NULL, &end)); | ||
3904 | 2777 | g_assert (!g_variant_format_string_scan ("(z)", NULL, &end)); | ||
3905 | 2778 | g_assert (!g_variant_format_string_scan ("az", NULL, &end)); | ||
3906 | 2779 | g_assert (!g_variant_format_string_scan ("{**}", NULL, &end)); | ||
3907 | 2780 | g_assert (!g_variant_format_string_scan ("{@**}", NULL, &end)); | ||
3908 | 2781 | g_assert (g_variant_format_string_scan ("{@y*}", NULL, &end) && | ||
3909 | 2782 | *end == '\0'); | ||
3910 | 2783 | g_assert (g_variant_format_string_scan ("{yv}", NULL, &end) && | ||
3911 | 2784 | *end == '\0'); | ||
3912 | 2785 | g_assert (!g_variant_format_string_scan ("{&?v}", NULL, &end)); | ||
3913 | 2786 | g_assert (g_variant_format_string_scan ("{@?v}", NULL, &end) && | ||
3914 | 2787 | *end == '\0'); | ||
3915 | 2788 | g_assert (!g_variant_format_string_scan ("{&@sv}", NULL, &end)); | ||
3916 | 2789 | g_assert (!g_variant_format_string_scan ("{@&sv}", NULL, &end)); | ||
3917 | 2790 | g_assert (g_variant_format_string_scan ("{&sv}", NULL, &end) && | ||
3918 | 2791 | *end == '\0'); | ||
3919 | 2792 | g_assert (!g_variant_format_string_scan ("{vv}", NULL, &end)); | ||
3920 | 2793 | g_assert (!g_variant_format_string_scan ("{y}", NULL, &end)); | ||
3921 | 2794 | g_assert (!g_variant_format_string_scan ("{yyy}", NULL, &end)); | ||
3922 | 2795 | g_assert (!g_variant_format_string_scan ("{ya}", NULL, &end)); | ||
3923 | 2796 | g_assert (g_variant_format_string_scan ("&s", NULL, &end) && *end == '\0'); | ||
3924 | 2797 | g_assert (!g_variant_format_string_scan ("&as", NULL, &end)); | ||
3925 | 2798 | g_assert (!g_variant_format_string_scan ("@z", NULL, &end)); | ||
3926 | 2799 | g_assert (!g_variant_format_string_scan ("az", NULL, &end)); | ||
3927 | 2800 | g_assert (!g_variant_format_string_scan ("a&s", NULL, &end)); | ||
3928 | 2801 | |||
3929 | 2802 | type = g_variant_format_string_scan_type ("mm(@xy^a&s*?@?)", NULL, &end); | ||
3930 | 2803 | g_assert (type && *end == '\0'); | ||
3931 | 2804 | g_assert (g_variant_type_equal (type, G_VARIANT_TYPE ("mm(xyas*?\?)"))); | ||
3932 | 2805 | g_variant_type_free (type); | ||
3933 | 2806 | |||
3934 | 2807 | type = g_variant_format_string_scan_type ("mm(@xy^a&*?@?)", NULL, NULL); | ||
3935 | 2808 | g_assert (type == NULL); | ||
3936 | 2809 | } | ||
3937 | 2810 | |||
3938 | 2811 | static void | ||
3939 | 2812 | exit_on_abort (int signal) | ||
3940 | 2813 | { | ||
3941 | 2814 | exit (signal); | ||
3942 | 2815 | } | ||
3943 | 2816 | |||
3944 | 2817 | static gboolean | ||
3945 | 2818 | do_failed_test (const gchar *pattern) | ||
3946 | 2819 | { | ||
3947 | 2820 | if (g_test_trap_fork (1000000, G_TEST_TRAP_SILENCE_STDERR)) | ||
3948 | 2821 | { | ||
3949 | 2822 | signal (SIGABRT, exit_on_abort); | ||
3950 | 2823 | return TRUE; | ||
3951 | 2824 | } | ||
3952 | 2825 | |||
3953 | 2826 | g_test_trap_assert_failed (); | ||
3954 | 2827 | g_test_trap_assert_stderr (pattern); | ||
3955 | 2828 | |||
3956 | 2829 | return FALSE; | ||
3957 | 2830 | } | ||
3958 | 2831 | |||
3959 | 2832 | static void | ||
3960 | 2833 | test_invalid_varargs (void) | ||
3961 | 2834 | { | ||
3962 | 2835 | GVariant *value; | ||
3963 | 2836 | const gchar *end; | ||
3964 | 2837 | |||
3965 | 2838 | if (!g_test_undefined ()) | ||
3966 | 2839 | return; | ||
3967 | 2840 | |||
3968 | 2841 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
3969 | 2842 | "*GVariant format string*"); | ||
3970 | 2843 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
3971 | 2844 | "*valid_format_string*"); | ||
3972 | 2845 | value = g_variant_new ("z"); | ||
3973 | 2846 | g_test_assert_expected_messages (); | ||
3974 | 2847 | g_assert (value == NULL); | ||
3975 | 2848 | |||
3976 | 2849 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
3977 | 2850 | "*valid GVariant format string as a prefix*"); | ||
3978 | 2851 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
3979 | 2852 | "*valid_format_string*"); | ||
3980 | 2853 | value = g_variant_new_va ("z", &end, NULL); | ||
3981 | 2854 | g_test_assert_expected_messages (); | ||
3982 | 2855 | g_assert (value == NULL); | ||
3983 | 2856 | |||
3984 | 2857 | value = g_variant_new ("y", 'a'); | ||
3985 | 2858 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
3986 | 2859 | "*type of `q' but * has a type of `y'*"); | ||
3987 | 2860 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
3988 | 2861 | "*valid_format_string*"); | ||
3989 | 2862 | g_variant_get (value, "q"); | ||
3990 | 2863 | g_test_assert_expected_messages (); | ||
3991 | 2864 | g_variant_unref (value); | ||
3992 | 2865 | } | ||
3993 | 2866 | |||
3994 | 2867 | static void | ||
3995 | 2868 | check_and_free (GVariant *value, | ||
3996 | 2869 | const gchar *str) | ||
3997 | 2870 | { | ||
3998 | 2871 | gchar *valstr = g_variant_print (value, FALSE); | ||
3999 | 2872 | g_assert_cmpstr (str, ==, valstr); | ||
4000 | 2873 | g_variant_unref (value); | ||
4001 | 2874 | g_free (valstr); | ||
4002 | 2875 | } | ||
4003 | 2876 | |||
4004 | 2877 | static void | ||
4005 | 2878 | test_varargs (void) | ||
4006 | 2879 | { | ||
4007 | 2880 | { | ||
4008 | 2881 | GVariantBuilder array; | ||
4009 | 2882 | |||
4010 | 2883 | g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY); | ||
4011 | 2884 | g_variant_builder_add_parsed (&array, "{'size', <(%i, %i)> }", 800, 600); | ||
4012 | 2885 | g_variant_builder_add (&array, "{sv}", "title", | ||
4013 | 2886 | g_variant_new_string ("Test case")); | ||
4014 | 2887 | g_variant_builder_add_value (&array, | ||
4015 | 2888 | g_variant_new_dict_entry (g_variant_new_string ("temperature"), | ||
4016 | 2889 | g_variant_new_variant ( | ||
4017 | 2890 | g_variant_new_double (37.5)))); | ||
4018 | 2891 | check_and_free (g_variant_new ("(ma{sv}m(a{sv})ma{sv}ii)", | ||
4019 | 2892 | NULL, FALSE, NULL, &array, 7777, 8888), | ||
4020 | 2893 | "(nothing, nothing, {'size': <(800, 600)>, " | ||
4021 | 2894 | "'title': <'Test case'>, " | ||
4022 | 2895 | "'temperature': <37.5>}, " | ||
4023 | 2896 | "7777, 8888)"); | ||
4024 | 2897 | |||
4025 | 2898 | check_and_free (g_variant_new ("(imimimmimmimmi)", | ||
4026 | 2899 | 123, | ||
4027 | 2900 | FALSE, 321, | ||
4028 | 2901 | TRUE, 123, | ||
4029 | 2902 | FALSE, TRUE, 321, | ||
4030 | 2903 | TRUE, FALSE, 321, | ||
4031 | 2904 | TRUE, TRUE, 123), | ||
4032 | 2905 | "(123, nothing, 123, nothing, just nothing, 123)"); | ||
4033 | 2906 | |||
4034 | 2907 | check_and_free (g_variant_new ("(ybnixd)", | ||
4035 | 2908 | 'a', 1, 22, 33, (guint64) 44, 5.5), | ||
4036 | 2909 | "(0x61, true, 22, 33, 44, 5.5)"); | ||
4037 | 2910 | |||
4038 | 2911 | check_and_free (g_variant_new ("(@y?*rv)", | ||
4039 | 2912 | g_variant_new ("y", 'a'), | ||
4040 | 2913 | g_variant_new ("y", 'b'), | ||
4041 | 2914 | g_variant_new ("y", 'c'), | ||
4042 | 2915 | g_variant_new ("(y)", 'd'), | ||
4043 | 2916 | g_variant_new ("y", 'e')), | ||
4044 | 2917 | "(0x61, 0x62, 0x63, (0x64,), <byte 0x65>)"); | ||
4045 | 2918 | } | ||
4046 | 2919 | |||
4047 | 2920 | { | ||
4048 | 2921 | GVariantBuilder array; | ||
4049 | 2922 | GVariantIter iter; | ||
4050 | 2923 | GVariant *value; | ||
4051 | 2924 | gchar *number; | ||
4052 | 2925 | gboolean just; | ||
4053 | 2926 | gint i, val; | ||
4054 | 2927 | |||
4055 | 2928 | g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY); | ||
4056 | 2929 | for (i = 0; i < 100; i++) | ||
4057 | 2930 | { | ||
4058 | 2931 | number = g_strdup_printf ("%d", i); | ||
4059 | 2932 | g_variant_builder_add (&array, "s", number); | ||
4060 | 2933 | g_free (number); | ||
4061 | 2934 | } | ||
4062 | 2935 | |||
4063 | 2936 | value = g_variant_builder_end (&array); | ||
4064 | 2937 | g_variant_iter_init (&iter, value); | ||
4065 | 2938 | |||
4066 | 2939 | i = 0; | ||
4067 | 2940 | while (g_variant_iter_loop (&iter, "s", &number)) | ||
4068 | 2941 | { | ||
4069 | 2942 | gchar *check = g_strdup_printf ("%d", i++); | ||
4070 | 2943 | g_assert_cmpstr (number, ==, check); | ||
4071 | 2944 | g_free (check); | ||
4072 | 2945 | } | ||
4073 | 2946 | g_assert (number == NULL); | ||
4074 | 2947 | g_assert (i == 100); | ||
4075 | 2948 | |||
4076 | 2949 | g_variant_unref (value); | ||
4077 | 2950 | |||
4078 | 2951 | g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY); | ||
4079 | 2952 | for (i = 0; i < 100; i++) | ||
4080 | 2953 | g_variant_builder_add (&array, "mi", i % 2 == 0, i); | ||
4081 | 2954 | value = g_variant_builder_end (&array); | ||
4082 | 2955 | |||
4083 | 2956 | i = 0; | ||
4084 | 2957 | g_variant_iter_init (&iter, value); | ||
4085 | 2958 | while (g_variant_iter_loop (&iter, "mi", NULL, &val)) | ||
4086 | 2959 | g_assert (val == i++ || val == 0); | ||
4087 | 2960 | g_assert (i == 100); | ||
4088 | 2961 | |||
4089 | 2962 | i = 0; | ||
4090 | 2963 | g_variant_iter_init (&iter, value); | ||
4091 | 2964 | while (g_variant_iter_loop (&iter, "mi", &just, &val)) | ||
4092 | 2965 | { | ||
4093 | 2966 | gint this = i++; | ||
4094 | 2967 | |||
4095 | 2968 | if (this % 2 == 0) | ||
4096 | 2969 | { | ||
4097 | 2970 | g_assert (just); | ||
4098 | 2971 | g_assert (val == this); | ||
4099 | 2972 | } | ||
4100 | 2973 | else | ||
4101 | 2974 | { | ||
4102 | 2975 | g_assert (!just); | ||
4103 | 2976 | g_assert (val == 0); | ||
4104 | 2977 | } | ||
4105 | 2978 | } | ||
4106 | 2979 | g_assert (i == 100); | ||
4107 | 2980 | |||
4108 | 2981 | g_variant_unref (value); | ||
4109 | 2982 | } | ||
4110 | 2983 | |||
4111 | 2984 | { | ||
4112 | 2985 | const gchar *strvector[] = {"/hello", "/world", NULL}; | ||
4113 | 2986 | const gchar *test_strs[] = {"/foo", "/bar", "/baz" }; | ||
4114 | 2987 | GVariantBuilder builder; | ||
4115 | 2988 | GVariantIter *array; | ||
4116 | 2989 | GVariantIter tuple; | ||
4117 | 2990 | const gchar **strv; | ||
4118 | 2991 | gchar **my_strv; | ||
4119 | 2992 | GVariant *value; | ||
4120 | 2993 | gchar *str; | ||
4121 | 2994 | gint i; | ||
4122 | 2995 | |||
4123 | 2996 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); | ||
4124 | 2997 | g_variant_builder_add (&builder, "s", "/foo"); | ||
4125 | 2998 | g_variant_builder_add (&builder, "s", "/bar"); | ||
4126 | 2999 | g_variant_builder_add (&builder, "s", "/baz"); | ||
4127 | 3000 | value = g_variant_new("(as^as^a&s)", &builder, strvector, strvector); | ||
4128 | 3001 | g_variant_iter_init (&tuple, value); | ||
4129 | 3002 | g_variant_iter_next (&tuple, "as", &array); | ||
4130 | 3003 | |||
4131 | 3004 | i = 0; | ||
4132 | 3005 | while (g_variant_iter_loop (array, "s", &str)) | ||
4133 | 3006 | g_assert_cmpstr (str, ==, test_strs[i++]); | ||
4134 | 3007 | g_assert (i == 3); | ||
4135 | 3008 | |||
4136 | 3009 | g_variant_iter_free (array); | ||
4137 | 3010 | |||
4138 | 3011 | /* start over */ | ||
4139 | 3012 | g_variant_iter_init (&tuple, value); | ||
4140 | 3013 | g_variant_iter_next (&tuple, "as", &array); | ||
4141 | 3014 | |||
4142 | 3015 | i = 0; | ||
4143 | 3016 | while (g_variant_iter_loop (array, "&s", &str)) | ||
4144 | 3017 | g_assert_cmpstr (str, ==, test_strs[i++]); | ||
4145 | 3018 | g_assert (i == 3); | ||
4146 | 3019 | |||
4147 | 3020 | g_variant_iter_free (array); | ||
4148 | 3021 | |||
4149 | 3022 | g_variant_iter_next (&tuple, "^a&s", &strv); | ||
4150 | 3023 | g_variant_iter_next (&tuple, "^as", &my_strv); | ||
4151 | 3024 | |||
4152 | 3025 | g_assert_cmpstr (strv[0], ==, "/hello"); | ||
4153 | 3026 | g_assert_cmpstr (strv[1], ==, "/world"); | ||
4154 | 3027 | g_assert (strv[2] == NULL); | ||
4155 | 3028 | g_assert_cmpstr (my_strv[0], ==, "/hello"); | ||
4156 | 3029 | g_assert_cmpstr (my_strv[1], ==, "/world"); | ||
4157 | 3030 | g_assert (my_strv[2] == NULL); | ||
4158 | 3031 | |||
4159 | 3032 | g_variant_unref (value); | ||
4160 | 3033 | g_strfreev (my_strv); | ||
4161 | 3034 | g_free (strv); | ||
4162 | 3035 | } | ||
4163 | 3036 | |||
4164 | 3037 | { | ||
4165 | 3038 | const gchar *strvector[] = {"/hello", "/world", NULL}; | ||
4166 | 3039 | const gchar *test_strs[] = {"/foo", "/bar", "/baz" }; | ||
4167 | 3040 | GVariantBuilder builder; | ||
4168 | 3041 | GVariantIter *array; | ||
4169 | 3042 | GVariantIter tuple; | ||
4170 | 3043 | const gchar **strv; | ||
4171 | 3044 | gchar **my_strv; | ||
4172 | 3045 | GVariant *value; | ||
4173 | 3046 | gchar *str; | ||
4174 | 3047 | gint i; | ||
4175 | 3048 | |||
4176 | 3049 | g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY); | ||
4177 | 3050 | g_variant_builder_add (&builder, "o", "/foo"); | ||
4178 | 3051 | g_variant_builder_add (&builder, "o", "/bar"); | ||
4179 | 3052 | g_variant_builder_add (&builder, "o", "/baz"); | ||
4180 | 3053 | value = g_variant_new("(ao^ao^a&o)", &builder, strvector, strvector); | ||
4181 | 3054 | g_variant_iter_init (&tuple, value); | ||
4182 | 3055 | g_variant_iter_next (&tuple, "ao", &array); | ||
4183 | 3056 | |||
4184 | 3057 | i = 0; | ||
4185 | 3058 | while (g_variant_iter_loop (array, "o", &str)) | ||
4186 | 3059 | g_assert_cmpstr (str, ==, test_strs[i++]); | ||
4187 | 3060 | g_assert (i == 3); | ||
4188 | 3061 | |||
4189 | 3062 | g_variant_iter_free (array); | ||
4190 | 3063 | |||
4191 | 3064 | /* start over */ | ||
4192 | 3065 | g_variant_iter_init (&tuple, value); | ||
4193 | 3066 | g_variant_iter_next (&tuple, "ao", &array); | ||
4194 | 3067 | |||
4195 | 3068 | i = 0; | ||
4196 | 3069 | while (g_variant_iter_loop (array, "&o", &str)) | ||
4197 | 3070 | g_assert_cmpstr (str, ==, test_strs[i++]); | ||
4198 | 3071 | g_assert (i == 3); | ||
4199 | 3072 | |||
4200 | 3073 | g_variant_iter_free (array); | ||
4201 | 3074 | |||
4202 | 3075 | g_variant_iter_next (&tuple, "^a&o", &strv); | ||
4203 | 3076 | g_variant_iter_next (&tuple, "^ao", &my_strv); | ||
4204 | 3077 | |||
4205 | 3078 | g_assert_cmpstr (strv[0], ==, "/hello"); | ||
4206 | 3079 | g_assert_cmpstr (strv[1], ==, "/world"); | ||
4207 | 3080 | g_assert (strv[2] == NULL); | ||
4208 | 3081 | g_assert_cmpstr (my_strv[0], ==, "/hello"); | ||
4209 | 3082 | g_assert_cmpstr (my_strv[1], ==, "/world"); | ||
4210 | 3083 | g_assert (my_strv[2] == NULL); | ||
4211 | 3084 | |||
4212 | 3085 | g_variant_unref (value); | ||
4213 | 3086 | g_strfreev (my_strv); | ||
4214 | 3087 | g_free (strv); | ||
4215 | 3088 | } | ||
4216 | 3089 | |||
4217 | 3090 | { | ||
4218 | 3091 | const gchar *strvector[] = { "i", "ii", "iii", "iv", "v", "vi", NULL }; | ||
4219 | 3092 | GVariantBuilder builder; | ||
4220 | 3093 | GVariantIter iter; | ||
4221 | 3094 | GVariantIter *i2; | ||
4222 | 3095 | GVariantIter *i3; | ||
4223 | 3096 | GVariant *value; | ||
4224 | 3097 | GVariant *sub; | ||
4225 | 3098 | gchar **strv; | ||
4226 | 3099 | gint i; | ||
4227 | 3100 | |||
4228 | 3101 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("aas")); | ||
4229 | 3102 | g_variant_builder_open (&builder, G_VARIANT_TYPE ("as")); | ||
4230 | 3103 | for (i = 0; i < 6; i++) | ||
4231 | 3104 | if (i & 1) | ||
4232 | 3105 | g_variant_builder_add (&builder, "s", strvector[i]); | ||
4233 | 3106 | else | ||
4234 | 3107 | g_variant_builder_add (&builder, "&s", strvector[i]); | ||
4235 | 3108 | g_variant_builder_close (&builder); | ||
4236 | 3109 | g_variant_builder_add (&builder, "^as", strvector); | ||
4237 | 3110 | g_variant_builder_add (&builder, "^as", strvector); | ||
4238 | 3111 | value = g_variant_new ("aas", &builder); | ||
4239 | 3112 | |||
4240 | 3113 | g_variant_iter_init (&iter, value); | ||
4241 | 3114 | while (g_variant_iter_loop (&iter, "^as", &strv)) | ||
4242 | 3115 | for (i = 0; i < 6; i++) | ||
4243 | 3116 | g_assert_cmpstr (strv[i], ==, strvector[i]); | ||
4244 | 3117 | |||
4245 | 3118 | g_variant_iter_init (&iter, value); | ||
4246 | 3119 | while (g_variant_iter_loop (&iter, "^a&s", &strv)) | ||
4247 | 3120 | for (i = 0; i < 6; i++) | ||
4248 | 3121 | g_assert_cmpstr (strv[i], ==, strvector[i]); | ||
4249 | 3122 | |||
4250 | 3123 | g_variant_iter_init (&iter, value); | ||
4251 | 3124 | while (g_variant_iter_loop (&iter, "as", &i2)) | ||
4252 | 3125 | { | ||
4253 | 3126 | gchar *str; | ||
4254 | 3127 | |||
4255 | 3128 | i = 0; | ||
4256 | 3129 | while (g_variant_iter_loop (i2, "s", &str)) | ||
4257 | 3130 | g_assert_cmpstr (str, ==, strvector[i++]); | ||
4258 | 3131 | g_assert (i == 6); | ||
4259 | 3132 | } | ||
4260 | 3133 | |||
4261 | 3134 | g_variant_iter_init (&iter, value); | ||
4262 | 3135 | i3 = g_variant_iter_copy (&iter); | ||
4263 | 3136 | while (g_variant_iter_loop (&iter, "@as", &sub)) | ||
4264 | 3137 | { | ||
4265 | 3138 | gchar *str = g_variant_print (sub, TRUE); | ||
4266 | 3139 | g_assert_cmpstr (str, ==, | ||
4267 | 3140 | "['i', 'ii', 'iii', 'iv', 'v', 'vi']"); | ||
4268 | 3141 | g_free (str); | ||
4269 | 3142 | } | ||
4270 | 3143 | |||
4271 | 3144 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, | ||
4272 | 3145 | "*NULL has already been returned*"); | ||
4273 | 3146 | g_variant_iter_next_value (&iter); | ||
4274 | 3147 | g_test_assert_expected_messages (); | ||
4275 | 3148 | |||
4276 | 3149 | while (g_variant_iter_loop (i3, "*", &sub)) | ||
4277 | 3150 | { | ||
4278 | 3151 | gchar *str = g_variant_print (sub, TRUE); | ||
4279 | 3152 | g_assert_cmpstr (str, ==, | ||
4280 | 3153 | "['i', 'ii', 'iii', 'iv', 'v', 'vi']"); | ||
4281 | 3154 | g_free (str); | ||
4282 | 3155 | } | ||
4283 | 3156 | |||
4284 | 3157 | g_variant_iter_free (i3); | ||
4285 | 3158 | |||
4286 | 3159 | for (i = 0; i < g_variant_n_children (value); i++) | ||
4287 | 3160 | { | ||
4288 | 3161 | gint j; | ||
4289 | 3162 | |||
4290 | 3163 | g_variant_get_child (value, i, "*", &sub); | ||
4291 | 3164 | |||
4292 | 3165 | for (j = 0; j < g_variant_n_children (sub); j++) | ||
4293 | 3166 | { | ||
4294 | 3167 | const gchar *str = NULL; | ||
4295 | 3168 | GVariant *cval; | ||
4296 | 3169 | |||
4297 | 3170 | g_variant_get_child (sub, j, "&s", &str); | ||
4298 | 3171 | g_assert_cmpstr (str, ==, strvector[j]); | ||
4299 | 3172 | |||
4300 | 3173 | cval = g_variant_get_child_value (sub, j); | ||
4301 | 3174 | g_variant_get (cval, "&s", &str); | ||
4302 | 3175 | g_assert_cmpstr (str, ==, strvector[j]); | ||
4303 | 3176 | g_variant_unref (cval); | ||
4304 | 3177 | } | ||
4305 | 3178 | |||
4306 | 3179 | g_variant_unref (sub); | ||
4307 | 3180 | } | ||
4308 | 3181 | |||
4309 | 3182 | g_variant_unref (value); | ||
4310 | 3183 | } | ||
4311 | 3184 | |||
4312 | 3185 | { | ||
4313 | 3186 | gboolean justs[10]; | ||
4314 | 3187 | GVariant *value; | ||
4315 | 3188 | |||
4316 | 3189 | GVariant *vval; | ||
4317 | 3190 | guchar byteval; | ||
4318 | 3191 | gboolean bval; | ||
4319 | 3192 | gint16 i16val; | ||
4320 | 3193 | guint16 u16val; | ||
4321 | 3194 | gint32 i32val; | ||
4322 | 3195 | guint32 u32val; | ||
4323 | 3196 | gint64 i64val; | ||
4324 | 3197 | guint64 u64val; | ||
4325 | 3198 | gdouble dval; | ||
4326 | 3199 | gint32 hval; | ||
4327 | 3200 | |||
4328 | 3201 | /* test all 'nothing' */ | ||
4329 | 3202 | value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)", | ||
4330 | 3203 | FALSE, 'a', | ||
4331 | 3204 | FALSE, TRUE, | ||
4332 | 3205 | FALSE, (gint16) 123, | ||
4333 | 3206 | FALSE, (guint16) 123, | ||
4334 | 3207 | FALSE, (gint32) 123, | ||
4335 | 3208 | FALSE, (guint32) 123, | ||
4336 | 3209 | FALSE, (gint64) 123, | ||
4337 | 3210 | FALSE, (guint64) 123, | ||
4338 | 3211 | FALSE, (gint32) -1, | ||
4339 | 3212 | FALSE, (gdouble) 37.5, | ||
4340 | 3213 | NULL); | ||
4341 | 3214 | |||
4342 | 3215 | /* both NULL */ | ||
4343 | 3216 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4344 | 3217 | NULL, NULL, | ||
4345 | 3218 | NULL, NULL, | ||
4346 | 3219 | NULL, NULL, | ||
4347 | 3220 | NULL, NULL, | ||
4348 | 3221 | NULL, NULL, | ||
4349 | 3222 | NULL, NULL, | ||
4350 | 3223 | NULL, NULL, | ||
4351 | 3224 | NULL, NULL, | ||
4352 | 3225 | NULL, NULL, | ||
4353 | 3226 | NULL, NULL, | ||
4354 | 3227 | NULL); | ||
4355 | 3228 | |||
4356 | 3229 | /* NULL values */ | ||
4357 | 3230 | memset (justs, 1, sizeof justs); | ||
4358 | 3231 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4359 | 3232 | &justs[0], NULL, | ||
4360 | 3233 | &justs[1], NULL, | ||
4361 | 3234 | &justs[2], NULL, | ||
4362 | 3235 | &justs[3], NULL, | ||
4363 | 3236 | &justs[4], NULL, | ||
4364 | 3237 | &justs[5], NULL, | ||
4365 | 3238 | &justs[6], NULL, | ||
4366 | 3239 | &justs[7], NULL, | ||
4367 | 3240 | &justs[8], NULL, | ||
4368 | 3241 | &justs[9], NULL, | ||
4369 | 3242 | NULL); | ||
4370 | 3243 | g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] || | ||
4371 | 3244 | justs[5] || justs[6] || justs[7] || justs[8] || justs[9])); | ||
4372 | 3245 | |||
4373 | 3246 | /* both non-NULL */ | ||
4374 | 3247 | memset (justs, 1, sizeof justs); | ||
4375 | 3248 | byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88; | ||
4376 | 3249 | vval = (void *) 1; | ||
4377 | 3250 | bval = TRUE; | ||
4378 | 3251 | dval = 88.88; | ||
4379 | 3252 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4380 | 3253 | &justs[0], &byteval, | ||
4381 | 3254 | &justs[1], &bval, | ||
4382 | 3255 | &justs[2], &i16val, | ||
4383 | 3256 | &justs[3], &u16val, | ||
4384 | 3257 | &justs[4], &i32val, | ||
4385 | 3258 | &justs[5], &u32val, | ||
4386 | 3259 | &justs[6], &i64val, | ||
4387 | 3260 | &justs[7], &u64val, | ||
4388 | 3261 | &justs[8], &hval, | ||
4389 | 3262 | &justs[9], &dval, | ||
4390 | 3263 | &vval); | ||
4391 | 3264 | g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] || | ||
4392 | 3265 | justs[5] || justs[6] || justs[7] || justs[8] || justs[9])); | ||
4393 | 3266 | g_assert (byteval == '\0' && bval == FALSE); | ||
4394 | 3267 | g_assert (i16val == 0 && u16val == 0 && i32val == 0 && | ||
4395 | 3268 | u32val == 0 && i64val == 0 && u64val == 0 && | ||
4396 | 3269 | hval == 0 && dval == 0.0); | ||
4397 | 3270 | g_assert (vval == NULL); | ||
4398 | 3271 | |||
4399 | 3272 | /* NULL justs */ | ||
4400 | 3273 | byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88; | ||
4401 | 3274 | vval = (void *) 1; | ||
4402 | 3275 | bval = TRUE; | ||
4403 | 3276 | dval = 88.88; | ||
4404 | 3277 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4405 | 3278 | NULL, &byteval, | ||
4406 | 3279 | NULL, &bval, | ||
4407 | 3280 | NULL, &i16val, | ||
4408 | 3281 | NULL, &u16val, | ||
4409 | 3282 | NULL, &i32val, | ||
4410 | 3283 | NULL, &u32val, | ||
4411 | 3284 | NULL, &i64val, | ||
4412 | 3285 | NULL, &u64val, | ||
4413 | 3286 | NULL, &hval, | ||
4414 | 3287 | NULL, &dval, | ||
4415 | 3288 | &vval); | ||
4416 | 3289 | g_assert (byteval == '\0' && bval == FALSE); | ||
4417 | 3290 | g_assert (i16val == 0 && u16val == 0 && i32val == 0 && | ||
4418 | 3291 | u32val == 0 && i64val == 0 && u64val == 0 && | ||
4419 | 3292 | hval == 0 && dval == 0.0); | ||
4420 | 3293 | g_assert (vval == NULL); | ||
4421 | 3294 | |||
4422 | 3295 | g_variant_unref (value); | ||
4423 | 3296 | |||
4424 | 3297 | |||
4425 | 3298 | /* test all 'just' */ | ||
4426 | 3299 | value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)", | ||
4427 | 3300 | TRUE, 'a', | ||
4428 | 3301 | TRUE, TRUE, | ||
4429 | 3302 | TRUE, (gint16) 123, | ||
4430 | 3303 | TRUE, (guint16) 123, | ||
4431 | 3304 | TRUE, (gint32) 123, | ||
4432 | 3305 | TRUE, (guint32) 123, | ||
4433 | 3306 | TRUE, (gint64) 123, | ||
4434 | 3307 | TRUE, (guint64) 123, | ||
4435 | 3308 | TRUE, (gint32) -1, | ||
4436 | 3309 | TRUE, (gdouble) 37.5, | ||
4437 | 3310 | g_variant_new ("()")); | ||
4438 | 3311 | |||
4439 | 3312 | /* both NULL */ | ||
4440 | 3313 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4441 | 3314 | NULL, NULL, | ||
4442 | 3315 | NULL, NULL, | ||
4443 | 3316 | NULL, NULL, | ||
4444 | 3317 | NULL, NULL, | ||
4445 | 3318 | NULL, NULL, | ||
4446 | 3319 | NULL, NULL, | ||
4447 | 3320 | NULL, NULL, | ||
4448 | 3321 | NULL, NULL, | ||
4449 | 3322 | NULL, NULL, | ||
4450 | 3323 | NULL, NULL, | ||
4451 | 3324 | NULL); | ||
4452 | 3325 | |||
4453 | 3326 | /* NULL values */ | ||
4454 | 3327 | memset (justs, 0, sizeof justs); | ||
4455 | 3328 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4456 | 3329 | &justs[0], NULL, | ||
4457 | 3330 | &justs[1], NULL, | ||
4458 | 3331 | &justs[2], NULL, | ||
4459 | 3332 | &justs[3], NULL, | ||
4460 | 3333 | &justs[4], NULL, | ||
4461 | 3334 | &justs[5], NULL, | ||
4462 | 3335 | &justs[6], NULL, | ||
4463 | 3336 | &justs[7], NULL, | ||
4464 | 3337 | &justs[8], NULL, | ||
4465 | 3338 | &justs[9], NULL, | ||
4466 | 3339 | NULL); | ||
4467 | 3340 | g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] && | ||
4468 | 3341 | justs[5] && justs[6] && justs[7] && justs[8] && justs[9]); | ||
4469 | 3342 | |||
4470 | 3343 | /* both non-NULL */ | ||
4471 | 3344 | memset (justs, 0, sizeof justs); | ||
4472 | 3345 | byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88; | ||
4473 | 3346 | vval = (void *) 1; | ||
4474 | 3347 | bval = FALSE; | ||
4475 | 3348 | dval = 88.88; | ||
4476 | 3349 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4477 | 3350 | &justs[0], &byteval, | ||
4478 | 3351 | &justs[1], &bval, | ||
4479 | 3352 | &justs[2], &i16val, | ||
4480 | 3353 | &justs[3], &u16val, | ||
4481 | 3354 | &justs[4], &i32val, | ||
4482 | 3355 | &justs[5], &u32val, | ||
4483 | 3356 | &justs[6], &i64val, | ||
4484 | 3357 | &justs[7], &u64val, | ||
4485 | 3358 | &justs[8], &hval, | ||
4486 | 3359 | &justs[9], &dval, | ||
4487 | 3360 | &vval); | ||
4488 | 3361 | g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] && | ||
4489 | 3362 | justs[5] && justs[6] && justs[7] && justs[8] && justs[9]); | ||
4490 | 3363 | g_assert (byteval == 'a' && bval == TRUE); | ||
4491 | 3364 | g_assert (i16val == 123 && u16val == 123 && i32val == 123 && | ||
4492 | 3365 | u32val == 123 && i64val == 123 && u64val == 123 && | ||
4493 | 3366 | hval == -1 && dval == 37.5); | ||
4494 | 3367 | g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT)); | ||
4495 | 3368 | g_variant_unref (vval); | ||
4496 | 3369 | |||
4497 | 3370 | /* NULL justs */ | ||
4498 | 3371 | byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88; | ||
4499 | 3372 | vval = (void *) 1; | ||
4500 | 3373 | bval = TRUE; | ||
4501 | 3374 | dval = 88.88; | ||
4502 | 3375 | g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)", | ||
4503 | 3376 | NULL, &byteval, | ||
4504 | 3377 | NULL, &bval, | ||
4505 | 3378 | NULL, &i16val, | ||
4506 | 3379 | NULL, &u16val, | ||
4507 | 3380 | NULL, &i32val, | ||
4508 | 3381 | NULL, &u32val, | ||
4509 | 3382 | NULL, &i64val, | ||
4510 | 3383 | NULL, &u64val, | ||
4511 | 3384 | NULL, &hval, | ||
4512 | 3385 | NULL, &dval, | ||
4513 | 3386 | &vval); | ||
4514 | 3387 | g_assert (byteval == 'a' && bval == TRUE); | ||
4515 | 3388 | g_assert (i16val == 123 && u16val == 123 && i32val == 123 && | ||
4516 | 3389 | u32val == 123 && i64val == 123 && u64val == 123 && | ||
4517 | 3390 | hval == -1 && dval == 37.5); | ||
4518 | 3391 | g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT)); | ||
4519 | 3392 | g_variant_unref (vval); | ||
4520 | 3393 | |||
4521 | 3394 | g_variant_unref (value); | ||
4522 | 3395 | } | ||
4523 | 3396 | |||
4524 | 3397 | { | ||
4525 | 3398 | GVariant *value; | ||
4526 | 3399 | gchar *str; | ||
4527 | 3400 | |||
4528 | 3401 | value = g_variant_new ("(masas)", NULL, NULL); | ||
4529 | 3402 | g_variant_ref_sink (value); | ||
4530 | 3403 | |||
4531 | 3404 | str = g_variant_print (value, TRUE); | ||
4532 | 3405 | g_assert_cmpstr (str, ==, "(@mas nothing, @as [])"); | ||
4533 | 3406 | g_variant_unref (value); | ||
4534 | 3407 | g_free (str); | ||
4535 | 3408 | |||
4536 | 3409 | if (do_failed_test ("*which type of empty array*")) | ||
4537 | 3410 | g_variant_new ("(a{s*})", NULL); | ||
4538 | 3411 | } | ||
4539 | 3412 | |||
4540 | 3413 | g_variant_type_info_assert_no_infos (); | ||
4541 | 3414 | } | ||
4542 | 3415 | |||
4543 | 3416 | static void | ||
4544 | 3417 | hash_get (GVariant *value, | ||
4545 | 3418 | const gchar *format, | ||
4546 | 3419 | ...) | ||
4547 | 3420 | { | ||
4548 | 3421 | const gchar *endptr = NULL; | ||
4549 | 3422 | gboolean hash; | ||
4550 | 3423 | va_list ap; | ||
4551 | 3424 | |||
4552 | 3425 | hash = g_str_has_suffix (format, "#"); | ||
4553 | 3426 | |||
4554 | 3427 | va_start (ap, format); | ||
4555 | 3428 | g_variant_get_va (value, format, hash ? &endptr : NULL, &ap); | ||
4556 | 3429 | va_end (ap); | ||
4557 | 3430 | |||
4558 | 3431 | if (hash) | ||
4559 | 3432 | g_assert (*endptr == '#'); | ||
4560 | 3433 | } | ||
4561 | 3434 | |||
4562 | 3435 | static GVariant * | ||
4563 | 3436 | hash_new (const gchar *format, | ||
4564 | 3437 | ...) | ||
4565 | 3438 | { | ||
4566 | 3439 | const gchar *endptr = NULL; | ||
4567 | 3440 | GVariant *value; | ||
4568 | 3441 | gboolean hash; | ||
4569 | 3442 | va_list ap; | ||
4570 | 3443 | |||
4571 | 3444 | hash = g_str_has_suffix (format, "#"); | ||
4572 | 3445 | |||
4573 | 3446 | va_start (ap, format); | ||
4574 | 3447 | value = g_variant_new_va (format, hash ? &endptr : NULL, &ap); | ||
4575 | 3448 | va_end (ap); | ||
4576 | 3449 | |||
4577 | 3450 | if (hash) | ||
4578 | 3451 | g_assert (*endptr == '#'); | ||
4579 | 3452 | |||
4580 | 3453 | return value; | ||
4581 | 3454 | } | ||
4582 | 3455 | |||
4583 | 3456 | static void | ||
4584 | 3457 | test_valist (void) | ||
4585 | 3458 | { | ||
4586 | 3459 | GVariant *value; | ||
4587 | 3460 | gint32 x; | ||
4588 | 3461 | |||
4589 | 3462 | x = 0; | ||
4590 | 3463 | value = hash_new ("i", 234); | ||
4591 | 3464 | hash_get (value, "i", &x); | ||
4592 | 3465 | g_assert (x == 234); | ||
4593 | 3466 | g_variant_unref (value); | ||
4594 | 3467 | |||
4595 | 3468 | x = 0; | ||
4596 | 3469 | value = hash_new ("i#", 234); | ||
4597 | 3470 | hash_get (value, "i#", &x); | ||
4598 | 3471 | g_assert (x == 234); | ||
4599 | 3472 | g_variant_unref (value); | ||
4600 | 3473 | |||
4601 | 3474 | g_variant_type_info_assert_no_infos (); | ||
4602 | 3475 | } | ||
4603 | 3476 | |||
4604 | 3477 | static void | ||
4605 | 3478 | test_builder_memory (void) | ||
4606 | 3479 | { | ||
4607 | 3480 | GVariantBuilder *hb; | ||
4608 | 3481 | GVariantBuilder sb; | ||
4609 | 3482 | |||
4610 | 3483 | hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY); | ||
4611 | 3484 | g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY); | ||
4612 | 3485 | g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY); | ||
4613 | 3486 | g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY); | ||
4614 | 3487 | g_variant_builder_add (hb, "s", "some value"); | ||
4615 | 3488 | g_variant_builder_ref (hb); | ||
4616 | 3489 | g_variant_builder_unref (hb); | ||
4617 | 3490 | g_variant_builder_unref (hb); | ||
4618 | 3491 | |||
4619 | 3492 | hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY); | ||
4620 | 3493 | g_variant_builder_unref (hb); | ||
4621 | 3494 | |||
4622 | 3495 | hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY); | ||
4623 | 3496 | g_variant_builder_clear (hb); | ||
4624 | 3497 | g_variant_builder_unref (hb); | ||
4625 | 3498 | |||
4626 | 3499 | g_variant_builder_init (&sb, G_VARIANT_TYPE_ARRAY); | ||
4627 | 3500 | g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY); | ||
4628 | 3501 | g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY); | ||
4629 | 3502 | g_variant_builder_add (&sb, "s", "some value"); | ||
4630 | 3503 | g_variant_builder_clear (&sb); | ||
4631 | 3504 | |||
4632 | 3505 | g_variant_type_info_assert_no_infos (); | ||
4633 | 3506 | } | ||
4634 | 3507 | |||
4635 | 3508 | static void | ||
4636 | 3509 | test_hashing (void) | ||
4637 | 3510 | { | ||
4638 | 3511 | GVariant *items[4096]; | ||
4639 | 3512 | GHashTable *table; | ||
4640 | 3513 | gint i; | ||
4641 | 3514 | |||
4642 | 3515 | table = g_hash_table_new_full (g_variant_hash, g_variant_equal, | ||
4643 | 3516 | (GDestroyNotify ) g_variant_unref, | ||
4644 | 3517 | NULL); | ||
4645 | 3518 | |||
4646 | 3519 | for (i = 0; i < G_N_ELEMENTS (items); i++) | ||
4647 | 3520 | { | ||
4648 | 3521 | TreeInstance *tree; | ||
4649 | 3522 | gint j; | ||
4650 | 3523 | |||
4651 | 3524 | again: | ||
4652 | 3525 | tree = tree_instance_new (NULL, 0); | ||
4653 | 3526 | items[i] = tree_instance_get_gvariant (tree); | ||
4654 | 3527 | tree_instance_free (tree); | ||
4655 | 3528 | |||
4656 | 3529 | for (j = 0; j < i; j++) | ||
4657 | 3530 | if (g_variant_equal (items[i], items[j])) | ||
4658 | 3531 | { | ||
4659 | 3532 | g_variant_unref (items[i]); | ||
4660 | 3533 | goto again; | ||
4661 | 3534 | } | ||
4662 | 3535 | |||
4663 | 3536 | g_hash_table_insert (table, | ||
4664 | 3537 | g_variant_ref_sink (items[i]), | ||
4665 | 3538 | GINT_TO_POINTER (i)); | ||
4666 | 3539 | } | ||
4667 | 3540 | |||
4668 | 3541 | for (i = 0; i < G_N_ELEMENTS (items); i++) | ||
4669 | 3542 | { | ||
4670 | 3543 | gpointer result; | ||
4671 | 3544 | |||
4672 | 3545 | result = g_hash_table_lookup (table, items[i]); | ||
4673 | 3546 | g_assert_cmpint (GPOINTER_TO_INT (result), ==, i); | ||
4674 | 3547 | } | ||
4675 | 3548 | |||
4676 | 3549 | g_hash_table_unref (table); | ||
4677 | 3550 | |||
4678 | 3551 | g_variant_type_info_assert_no_infos (); | ||
4679 | 3552 | } | ||
4680 | 3553 | |||
4681 | 3554 | static void | ||
4682 | 3555 | test_gv_byteswap (void) | ||
4683 | 3556 | { | ||
4684 | 3557 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
4685 | 3558 | # define native16(x) x, 0 | ||
4686 | 3559 | # define swapped16(x) 0, x | ||
4687 | 3560 | #else | ||
4688 | 3561 | # define native16(x) 0, x | ||
4689 | 3562 | # define swapped16(x) x, 0 | ||
4690 | 3563 | #endif | ||
4691 | 3564 | /* all kinds of of crazy randomised testing already performed on the | ||
4692 | 3565 | * byteswapper in the /gvariant/serialiser/byteswap test and all kinds | ||
4693 | 3566 | * of crazy randomised testing performed against the serialiser | ||
4694 | 3567 | * normalisation functions in the /gvariant/serialiser/fuzz/ tests. | ||
4695 | 3568 | * | ||
4696 | 3569 | * just test a few simple cases here to make sure they each work | ||
4697 | 3570 | */ | ||
4698 | 3571 | guchar validbytes[] = { 'a', '\0', swapped16(66), 2, | ||
4699 | 3572 | 0, | ||
4700 | 3573 | 'b', '\0', swapped16(77), 2, | ||
4701 | 3574 | 5, 11 }; | ||
4702 | 3575 | guchar corruptbytes[] = { 'a', '\0', swapped16(66), 2, | ||
4703 | 3576 | 0, | ||
4704 | 3577 | 'b', '\0', swapped16(77), 2, | ||
4705 | 3578 | 6, 11 }; | ||
4706 | 3579 | guint valid_data[4], corrupt_data[4]; | ||
4707 | 3580 | GVariant *value, *swapped; | ||
4708 | 3581 | gchar *string, *string2; | ||
4709 | 3582 | |||
4710 | 3583 | memcpy (valid_data, validbytes, sizeof validbytes); | ||
4711 | 3584 | memcpy (corrupt_data, corruptbytes, sizeof corruptbytes); | ||
4712 | 3585 | |||
4713 | 3586 | /* trusted */ | ||
4714 | 3587 | value = g_variant_new_from_data (G_VARIANT_TYPE ("a(sn)"), | ||
4715 | 3588 | valid_data, sizeof validbytes, TRUE, | ||
4716 | 3589 | NULL, NULL); | ||
4717 | 3590 | swapped = g_variant_byteswap (value); | ||
4718 | 3591 | g_variant_unref (value); | ||
4719 | 3592 | g_assert (g_variant_get_size (swapped) == 13); | ||
4720 | 3593 | string = g_variant_print (swapped, FALSE); | ||
4721 | 3594 | g_variant_unref (swapped); | ||
4722 | 3595 | g_assert_cmpstr (string, ==, "[('a', 66), ('b', 77)]"); | ||
4723 | 3596 | g_free (string); | ||
4724 | 3597 | |||
4725 | 3598 | /* untrusted but valid */ | ||
4726 | 3599 | value = g_variant_new_from_data (G_VARIANT_TYPE ("a(sn)"), | ||
4727 | 3600 | valid_data, sizeof validbytes, FALSE, | ||
4728 | 3601 | NULL, NULL); | ||
4729 | 3602 | swapped = g_variant_byteswap (value); | ||
4730 | 3603 | g_variant_unref (value); | ||
4731 | 3604 | g_assert (g_variant_get_size (swapped) == 13); | ||
4732 | 3605 | string = g_variant_print (swapped, FALSE); | ||
4733 | 3606 | g_variant_unref (swapped); | ||
4734 | 3607 | g_assert_cmpstr (string, ==, "[('a', 66), ('b', 77)]"); | ||
4735 | 3608 | g_free (string); | ||
4736 | 3609 | |||
4737 | 3610 | /* untrusted, invalid */ | ||
4738 | 3611 | value = g_variant_new_from_data (G_VARIANT_TYPE ("a(sn)"), | ||
4739 | 3612 | corrupt_data, sizeof corruptbytes, FALSE, | ||
4740 | 3613 | NULL, NULL); | ||
4741 | 3614 | string = g_variant_print (value, FALSE); | ||
4742 | 3615 | swapped = g_variant_byteswap (value); | ||
4743 | 3616 | g_variant_unref (value); | ||
4744 | 3617 | g_assert (g_variant_get_size (swapped) == 13); | ||
4745 | 3618 | value = g_variant_byteswap (swapped); | ||
4746 | 3619 | g_variant_unref (swapped); | ||
4747 | 3620 | string2 = g_variant_print (value, FALSE); | ||
4748 | 3621 | g_assert (g_variant_get_size (value) == 13); | ||
4749 | 3622 | g_variant_unref (value); | ||
4750 | 3623 | g_assert_cmpstr (string, ==, string2); | ||
4751 | 3624 | g_free (string2); | ||
4752 | 3625 | g_free (string); | ||
4753 | 3626 | } | ||
4754 | 3627 | |||
4755 | 3628 | static void | ||
4756 | 3629 | test_parser (void) | ||
4757 | 3630 | { | ||
4758 | 3631 | TreeInstance *tree; | ||
4759 | 3632 | GVariant *parsed; | ||
4760 | 3633 | GVariant *value; | ||
4761 | 3634 | gchar *pt, *p; | ||
4762 | 3635 | gchar *res; | ||
4763 | 3636 | |||
4764 | 3637 | tree = tree_instance_new (NULL, 3); | ||
4765 | 3638 | value = tree_instance_get_gvariant (tree); | ||
4766 | 3639 | tree_instance_free (tree); | ||
4767 | 3640 | |||
4768 | 3641 | pt = g_variant_print (value, TRUE); | ||
4769 | 3642 | p = g_variant_print (value, FALSE); | ||
4770 | 3643 | |||
4771 | 3644 | parsed = g_variant_parse (NULL, pt, NULL, NULL, NULL); | ||
4772 | 3645 | res = g_variant_print (parsed, FALSE); | ||
4773 | 3646 | g_assert_cmpstr (p, ==, res); | ||
4774 | 3647 | g_variant_unref (parsed); | ||
4775 | 3648 | g_free (res); | ||
4776 | 3649 | |||
4777 | 3650 | parsed = g_variant_parse (g_variant_get_type (value), p, | ||
4778 | 3651 | NULL, NULL, NULL); | ||
4779 | 3652 | res = g_variant_print (parsed, TRUE); | ||
4780 | 3653 | g_assert_cmpstr (pt, ==, res); | ||
4781 | 3654 | g_variant_unref (parsed); | ||
4782 | 3655 | g_free (res); | ||
4783 | 3656 | |||
4784 | 3657 | g_variant_unref (value); | ||
4785 | 3658 | g_free (pt); | ||
4786 | 3659 | g_free (p); | ||
4787 | 3660 | } | ||
4788 | 3661 | |||
4789 | 3662 | static void | ||
4790 | 3663 | test_parses (void) | ||
4791 | 3664 | { | ||
4792 | 3665 | gint i; | ||
4793 | 3666 | |||
4794 | 3667 | for (i = 0; i < 100; i++) | ||
4795 | 3668 | { | ||
4796 | 3669 | test_parser (); | ||
4797 | 3670 | } | ||
4798 | 3671 | |||
4799 | 3672 | /* mini test */ | ||
4800 | 3673 | { | ||
4801 | 3674 | GError *error = NULL; | ||
4802 | 3675 | gchar str[128]; | ||
4803 | 3676 | GVariant *val; | ||
4804 | 3677 | gchar *p, *p2; | ||
4805 | 3678 | |||
4806 | 3679 | for (i = 0; i < 127; i++) | ||
4807 | 3680 | str[i] = i + 1; | ||
4808 | 3681 | str[i] = 0; | ||
4809 | 3682 | |||
4810 | 3683 | val = g_variant_new_string (str); | ||
4811 | 3684 | p = g_variant_print (val, FALSE); | ||
4812 | 3685 | g_variant_unref (val); | ||
4813 | 3686 | |||
4814 | 3687 | val = g_variant_parse (NULL, p, NULL, NULL, &error); | ||
4815 | 3688 | p2 = g_variant_print (val, FALSE); | ||
4816 | 3689 | |||
4817 | 3690 | g_assert_cmpstr (str, ==, g_variant_get_string (val, NULL)); | ||
4818 | 3691 | g_assert_cmpstr (p, ==, p2); | ||
4819 | 3692 | |||
4820 | 3693 | g_variant_unref (val); | ||
4821 | 3694 | g_free (p2); | ||
4822 | 3695 | g_free (p); | ||
4823 | 3696 | } | ||
4824 | 3697 | |||
4825 | 3698 | /* another mini test */ | ||
4826 | 3699 | { | ||
4827 | 3700 | const gchar *end; | ||
4828 | 3701 | GVariant *value; | ||
4829 | 3702 | |||
4830 | 3703 | value = g_variant_parse (G_VARIANT_TYPE_INT32, "1 2 3", NULL, &end, NULL); | ||
4831 | 3704 | g_assert_cmpint (g_variant_get_int32 (value), ==, 1); | ||
4832 | 3705 | /* make sure endptr returning works */ | ||
4833 | 3706 | g_assert_cmpstr (end, ==, " 2 3"); | ||
4834 | 3707 | g_variant_unref (value); | ||
4835 | 3708 | } | ||
4836 | 3709 | |||
4837 | 3710 | /* unicode mini test */ | ||
4838 | 3711 | { | ||
4839 | 3712 | /* ał𝄞 */ | ||
4840 | 3713 | const gchar orig[] = "a\xc5\x82\xf0\x9d\x84\x9e \t\n"; | ||
4841 | 3714 | GVariant *value; | ||
4842 | 3715 | gchar *printed; | ||
4843 | 3716 | |||
4844 | 3717 | value = g_variant_new_string (orig); | ||
4845 | 3718 | printed = g_variant_print (value, FALSE); | ||
4846 | 3719 | g_variant_unref (value); | ||
4847 | 3720 | |||
4848 | 3721 | g_assert_cmpstr (printed, ==, "'a\xc5\x82\xf0\x9d\x84\x9e \\t\\n'"); | ||
4849 | 3722 | value = g_variant_parse (NULL, printed, NULL, NULL, NULL); | ||
4850 | 3723 | g_assert_cmpstr (g_variant_get_string (value, NULL), ==, orig); | ||
4851 | 3724 | g_variant_unref (value); | ||
4852 | 3725 | g_free (printed); | ||
4853 | 3726 | } | ||
4854 | 3727 | |||
4855 | 3728 | /* inf/nan mini test */ | ||
4856 | 3729 | { | ||
4857 | 3730 | const gchar *tests[] = { "inf", "-inf", "nan" }; | ||
4858 | 3731 | GVariant *value; | ||
4859 | 3732 | gchar *printed; | ||
4860 | 3733 | gint i; | ||
4861 | 3734 | |||
4862 | 3735 | for (i = 0; i < G_N_ELEMENTS (tests); i++) | ||
4863 | 3736 | { | ||
4864 | 3737 | GError *error = NULL; | ||
4865 | 3738 | value = g_variant_parse (NULL, tests[i], NULL, NULL, &error); | ||
4866 | 3739 | printed = g_variant_print (value, FALSE); | ||
4867 | 3740 | g_assert (g_str_has_prefix (printed, tests[i])); | ||
4868 | 3741 | g_free (printed); | ||
4869 | 3742 | g_variant_unref (value); | ||
4870 | 3743 | } | ||
4871 | 3744 | } | ||
4872 | 3745 | |||
4873 | 3746 | g_variant_type_info_assert_no_infos (); | ||
4874 | 3747 | } | ||
4875 | 3748 | |||
4876 | 3749 | static void | ||
4877 | 3750 | test_parse_failures (void) | ||
4878 | 3751 | { | ||
4879 | 3752 | const gchar *test[] = { | ||
4880 | 3753 | "[1, 2,", "6:", "expected value", | ||
4881 | 3754 | "", "0:", "expected value", | ||
4882 | 3755 | "(1, 2,", "6:", "expected value", | ||
4883 | 3756 | "<1", "2:", "expected `>'", | ||
4884 | 3757 | "[]", "0-2:", "unable to infer", | ||
4885 | 3758 | "(,", "1:", "expected value", | ||
4886 | 3759 | "[4,'']", "1-2,3-5:", "common type", | ||
4887 | 3760 | "[4, '', 5]", "1-2,4-6:", "common type", | ||
4888 | 3761 | "['', 4, 5]", "1-3,5-6:", "common type", | ||
4889 | 3762 | "[4, 5, '']", "1-2,7-9:", "common type", | ||
4890 | 3763 | "[[4], [], ['']]", "1-4,10-14:", "common type", | ||
4891 | 3764 | "[[], [4], ['']]", "5-8,10-14:", "common type", | ||
4892 | 3765 | "just", "4:", "expected value", | ||
4893 | 3766 | "nothing", "0-7:", "unable to infer", | ||
4894 | 3767 | "just [4, '']", "6-7,9-11:", "common type", | ||
4895 | 3768 | "[[4,'']]", "2-3,4-6:", "common type", | ||
4896 | 3769 | "([4,''],)", "2-3,4-6:", "common type", | ||
4897 | 3770 | "(4)", "2:", "`,'", | ||
4898 | 3771 | "{}", "0-2:", "unable to infer", | ||
4899 | 3772 | "{[1,2],[3,4]}", "0-13:", "basic types", | ||
4900 | 3773 | "{[1,2]:[3,4]}", "0-13:", "basic types", | ||
4901 | 3774 | "justt", "0-5:", "unknown keyword", | ||
4902 | 3775 | "nothng", "0-6:", "unknown keyword", | ||
4903 | 3776 | "uint33", "0-6:", "unknown keyword", | ||
4904 | 3777 | "@mi just ''", "9-11:", "can not parse as", | ||
4905 | 3778 | "@ai ['']", "5-7:", "can not parse as", | ||
4906 | 3779 | "@(i) ('',)", "6-8:", "can not parse as", | ||
4907 | 3780 | "[[], 5]", "1-3,5-6:", "common type", | ||
4908 | 3781 | "[[5], 5]", "1-4,6-7:", "common type", | ||
4909 | 3782 | "5 5", "2:", "expected end of input", | ||
4910 | 3783 | "[5, [5, '']]", "5-6,8-10:", "common type", | ||
4911 | 3784 | "@i just 5", "3-9:", "can not parse as", | ||
4912 | 3785 | "@i nothing", "3-10:", "can not parse as", | ||
4913 | 3786 | "@i []", "3-5:", "can not parse as", | ||
4914 | 3787 | "@i ()", "3-5:", "can not parse as", | ||
4915 | 3788 | "@ai (4,)", "4-8:", "can not parse as", | ||
4916 | 3789 | "@(i) []", "5-7:", "can not parse as", | ||
4917 | 3790 | "(5 5)", "3:", "expected `,'", | ||
4918 | 3791 | "[5 5]", "3:", "expected `,' or `]'", | ||
4919 | 3792 | "(5, 5 5)", "6:", "expected `,' or `)'", | ||
4920 | 3793 | "[5, 5 5]", "6:", "expected `,' or `]'", | ||
4921 | 3794 | "<@i []>", "4-6:", "can not parse as", | ||
4922 | 3795 | "<[5 5]>", "4:", "expected `,' or `]'", | ||
4923 | 3796 | "{[4,''],5}", "2-3,4-6:", "common type", | ||
4924 | 3797 | "{5,[4,'']}", "4-5,6-8:", "common type", | ||
4925 | 3798 | "@i {1,2}", "3-8:", "can not parse as", | ||
4926 | 3799 | "{@i '', 5}", "4-6:", "can not parse as", | ||
4927 | 3800 | "{5, @i ''}", "7-9:", "can not parse as", | ||
4928 | 3801 | "@ai {}", "4-6:", "can not parse as", | ||
4929 | 3802 | "{@i '': 5}", "4-6:", "can not parse as", | ||
4930 | 3803 | "{5: @i ''}", "7-9:", "can not parse as", | ||
4931 | 3804 | "{<4,5}", "3:", "expected `>'", | ||
4932 | 3805 | "{4,<5}", "5:", "expected `>'", | ||
4933 | 3806 | "{4,5,6}", "4:", "expected `}'", | ||
4934 | 3807 | "{5 5}", "3:", "expected `:' or `,'", | ||
4935 | 3808 | "{4: 5: 6}", "5:", "expected `,' or `}'", | ||
4936 | 3809 | "{4:5,<6:7}", "7:", "expected `>'", | ||
4937 | 3810 | "{4:5,6:<7}", "9:", "expected `>'", | ||
4938 | 3811 | "{4:5,6 7}", "7:", "expected `:'", | ||
4939 | 3812 | "@o 'foo'", "3-8:", "object path", | ||
4940 | 3813 | "@g 'zzz'", "3-8:", "signature", | ||
4941 | 3814 | "@i true", "3-7:", "can not parse as", | ||
4942 | 3815 | "@z 4", "0-2:", "invalid type", | ||
4943 | 3816 | "@a* []", "0-3:", "definite", | ||
4944 | 3817 | "@ai [3 3]", "7:", "expected `,' or `]'", | ||
4945 | 3818 | "18446744073709551616", "0-20:", "too big for any type", | ||
4946 | 3819 | "-18446744073709551616", "0-21:", "too big for any type", | ||
4947 | 3820 | "byte 256", "5-8:", "out of range for type", | ||
4948 | 3821 | "byte -1", "5-7:", "out of range for type", | ||
4949 | 3822 | "int16 32768", "6-11:", "out of range for type", | ||
4950 | 3823 | "int16 -32769", "6-12:", "out of range for type", | ||
4951 | 3824 | "uint16 -1", "7-9:", "out of range for type", | ||
4952 | 3825 | "uint16 65536", "7-12:", "out of range for type", | ||
4953 | 3826 | "2147483648", "0-10:", "out of range for type", | ||
4954 | 3827 | "-2147483649", "0-11:", "out of range for type", | ||
4955 | 3828 | "uint32 -1", "7-9:", "out of range for type", | ||
4956 | 3829 | "uint32 4294967296", "7-17:", "out of range for type", | ||
4957 | 3830 | "@x 9223372036854775808", "3-22:", "out of range for type", | ||
4958 | 3831 | "@x -9223372036854775809", "3-23:", "out of range for type", | ||
4959 | 3832 | "@t -1", "3-5:", "out of range for type", | ||
4960 | 3833 | "@t 18446744073709551616", "3-23:", "too big for any type", | ||
4961 | 3834 | "handle 2147483648", "7-17:", "out of range for type", | ||
4962 | 3835 | "handle -2147483649", "7-18:", "out of range for type", | ||
4963 | 3836 | "1.798e308", "0-9:", "too big for any type", | ||
4964 | 3837 | "37.5a488", "4-5:", "invalid character", | ||
4965 | 3838 | "0x7ffgf", "5-6:", "invalid character", | ||
4966 | 3839 | "07758", "4-5:", "invalid character", | ||
4967 | 3840 | "123a5", "3-4:", "invalid character", | ||
4968 | 3841 | "@ai 123", "4-7:", "can not parse as", | ||
4969 | 3842 | "'\"\\'", "0-4:", "unterminated string", | ||
4970 | 3843 | "'\"\\'\\", "0-5:", "unterminated string", | ||
4971 | 3844 | "boolean 4", "8-9:", "can not parse as", | ||
4972 | 3845 | "int32 true", "6-10:", "can not parse as", | ||
4973 | 3846 | "[double 5, int32 5]", "1-9,11-18:", "common type", | ||
4974 | 3847 | "string 4", "7-8:", "can not parse as" | ||
4975 | 3848 | }; | ||
4976 | 3849 | gint i; | ||
4977 | 3850 | |||
4978 | 3851 | for (i = 0; i < G_N_ELEMENTS (test); i += 3) | ||
4979 | 3852 | { | ||
4980 | 3853 | GError *error = NULL; | ||
4981 | 3854 | GVariant *value; | ||
4982 | 3855 | |||
4983 | 3856 | value = g_variant_parse (NULL, test[i], NULL, NULL, &error); | ||
4984 | 3857 | g_assert (value == NULL); | ||
4985 | 3858 | |||
4986 | 3859 | if (!strstr (error->message, test[i+2])) | ||
4987 | 3860 | g_error ("test %d: Can't find `%s' in `%s'", i / 3, | ||
4988 | 3861 | test[i+2], error->message); | ||
4989 | 3862 | |||
4990 | 3863 | if (!g_str_has_prefix (error->message, test[i+1])) | ||
4991 | 3864 | g_error ("test %d: Expected location `%s' in `%s'", i / 3, | ||
4992 | 3865 | test[i+1], error->message); | ||
4993 | 3866 | |||
4994 | 3867 | g_error_free (error); | ||
4995 | 3868 | } | ||
4996 | 3869 | } | ||
4997 | 3870 | |||
4998 | 3871 | static void | ||
4999 | 3872 | test_parse_positional (void) | ||
5000 | 3873 | { |
The diff has been truncated for viewing.
thanks, setting as "merged" since we got the new glib