D-Bus  1.13.7
dbus-string-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-string-util.c Would be in dbus-string.c, but not used in libdbus
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-string.h"
28 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
29 #include "dbus-string-private.h"
30 #include <dbus/dbus-test-tap.h>
31 
48  const char *c_str)
49 {
50  const unsigned char *ap;
51  const unsigned char *bp;
52  const unsigned char *a_end;
53  unsigned long c_str_len;
54  const DBusRealString *real_a = (const DBusRealString*) a;
56  _dbus_assert (c_str != NULL);
57 
58  c_str_len = strlen (c_str);
59  if (((unsigned long)real_a->len) < c_str_len)
60  return FALSE;
61 
62  ap = real_a->str + (real_a->len - c_str_len);
63  bp = (const unsigned char*) c_str;
64  a_end = real_a->str + real_a->len;
65  while (ap != a_end)
66  {
67  if (*ap != *bp)
68  return FALSE;
69 
70  ++ap;
71  ++bp;
72  }
73 
74  _dbus_assert (*ap == '\0');
75  _dbus_assert (*bp == '\0');
76 
77  return TRUE;
78 }
79 
92  int start,
93  unsigned char byte,
94  int *found)
95 {
96  int i;
98  _dbus_assert (start <= real->len);
99  _dbus_assert (start >= 0);
100  _dbus_assert (found != NULL);
101 
102  i = start - 1;
103  while (i >= 0)
104  {
105  if (real->str[i] == byte)
106  break;
107 
108  --i;
109  }
110 
111  if (found)
112  *found = i;
113 
114  return i >= 0;
115 }
116 
119 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
120 #include "dbus-test.h"
121 #include <stdio.h>
122 
123 static void
124 test_hex_roundtrip (const char *data,
125  int len)
126 {
127  DBusString orig;
128  DBusString encoded;
129  DBusString decoded;
130  int end;
131 
132  if (len < 0)
133  len = strlen (data);
134 
135  if (!_dbus_string_init (&orig))
136  _dbus_test_fatal ("could not init string");
137 
138  if (!_dbus_string_init (&encoded))
139  _dbus_test_fatal ("could not init string");
140 
141  if (!_dbus_string_init (&decoded))
142  _dbus_test_fatal ("could not init string");
143 
144  if (!_dbus_string_append_len (&orig, data, len))
145  _dbus_test_fatal ("couldn't append orig data");
146 
147  if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
148  _dbus_test_fatal ("could not encode");
149 
150  if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
151  _dbus_test_fatal ("could not decode");
152 
153  _dbus_assert (_dbus_string_get_length (&encoded) == end);
154 
155  if (!_dbus_string_equal (&orig, &decoded))
156  {
157  const char *s;
158 
159  _dbus_test_diag ("Original string %d bytes encoded %d bytes decoded %d bytes",
160  _dbus_string_get_length (&orig),
161  _dbus_string_get_length (&encoded),
162  _dbus_string_get_length (&decoded));
163  _dbus_test_diag ("Original: %s", data);
164  s = _dbus_string_get_const_data (&decoded);
165  _dbus_test_diag ("Decoded: %s", s);
166  _dbus_test_fatal ("original string not the same as string decoded from hex");
167  }
168 
169  _dbus_string_free (&orig);
170  _dbus_string_free (&encoded);
171  _dbus_string_free (&decoded);
172 }
173 
174 typedef void (* TestRoundtripFunc) (const char *data,
175  int len);
176 static void
177 test_roundtrips (TestRoundtripFunc func)
178 {
179  (* func) ("Hello this is a string\n", -1);
180  (* func) ("Hello this is a string\n1", -1);
181  (* func) ("Hello this is a string\n12", -1);
182  (* func) ("Hello this is a string\n123", -1);
183  (* func) ("Hello this is a string\n1234", -1);
184  (* func) ("Hello this is a string\n12345", -1);
185  (* func) ("", 0);
186  (* func) ("1", 1);
187  (* func) ("12", 2);
188  (* func) ("123", 3);
189  (* func) ("1234", 4);
190  (* func) ("12345", 5);
191  (* func) ("", 1);
192  (* func) ("1", 2);
193  (* func) ("12", 3);
194  (* func) ("123", 4);
195  (* func) ("1234", 5);
196  (* func) ("12345", 6);
197  {
198  unsigned char buf[512];
199  int i;
200 
201  i = 0;
202  while (i < _DBUS_N_ELEMENTS (buf))
203  {
204  buf[i] = (i & 0xff);
205  ++i;
206  }
207  i = 0;
208  while (i < _DBUS_N_ELEMENTS (buf))
209  {
210  (* func) ((const char *) buf, i);
211  ++i;
212  }
213  }
214 }
215 
216 _DBUS_STRING_DEFINE_STATIC (test_static_string, "hello");
217 
229 _dbus_string_test (void)
230 {
231  DBusString str = _DBUS_STRING_INIT_INVALID;
232  DBusString other;
233  int i, a, end;
234  long v;
235  int lens[] = { 0, 1, 2, 3, 4, 5, 10, 16, 17, 18, 25, 31, 32, 33, 34, 35, 63, 64, 65, 66, 67, 68, 69, 70, 71, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136 };
236  char *s;
237  DBusRealString *real_test_static_string = (DBusRealString *) &test_static_string;
238 
239  _dbus_assert (real_test_static_string->str != NULL);
240  _dbus_assert (strcmp ((const char *) real_test_static_string->str, "hello") == 0);
241  _dbus_assert (real_test_static_string->len == 5);
242  _dbus_assert (real_test_static_string->allocated > 5);
243  _dbus_assert (real_test_static_string->constant);
244  _dbus_assert (real_test_static_string->locked);
245  _dbus_assert (real_test_static_string->valid);
246  _dbus_assert (real_test_static_string->align_offset == 0);
247 
248  /* Test that _DBUS_STRING_INIT_INVALID has the desired effect */
249  _dbus_string_free (&str);
250  _dbus_string_free (&str);
251  _dbus_string_free (&str);
252 
253  /* Test shortening and setting length */
254  i = 0;
255  while (i < _DBUS_N_ELEMENTS (lens))
256  {
257  int j;
258 
259  if (!_dbus_string_init (&str))
260  _dbus_test_fatal ("failed to init string");
261 
262  if (!_dbus_string_set_length (&str, lens[i]))
263  _dbus_test_fatal ("failed to set string length");
264 
265  j = lens[i];
266  while (j > 0)
267  {
268  _dbus_assert (_dbus_string_get_length (&str) == j);
269  if (j > 0)
270  {
271  _dbus_string_shorten (&str, 1);
272  _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
273  }
274  --j;
275  }
276 
277  _dbus_string_free (&str);
278  /* Test that a cleared string is effectively _DBUS_STRING_INIT_INVALID */
279  _dbus_string_free (&str);
280  _dbus_string_free (&str);
281 
282  ++i;
283  }
284 
285  /* Test equality */
286  if (!_dbus_string_init (&str))
287  _dbus_test_fatal ("oom");
288 
289  if (!_dbus_string_append (&str, "Hello World"))
290  _dbus_test_fatal ("oom");
291 
292  _dbus_string_init_const (&other, "H");
293  _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
294  _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
295  _dbus_string_init_const (&other, "Hello");
296  _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
297  _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
298  _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
299  _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
300  _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
301  _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
302 
303  _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
304  _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
305  _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
306  _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
307  _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
308  _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
309 
310 
311  _dbus_string_init_const (&other, "World");
312  _dbus_assert (_dbus_string_equal_substring (&str, 6, 5, &other, 0));
313  _dbus_assert (_dbus_string_equal_substring (&str, 7, 4, &other, 1));
314  _dbus_assert (_dbus_string_equal_substring (&str, 8, 3, &other, 2));
315  _dbus_assert (_dbus_string_equal_substring (&str, 9, 2, &other, 3));
316  _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
317  _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
318 
319  _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
320  _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
321  _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
322  _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
323  _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
324  _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
325 
326  _dbus_string_free (&str);
327 
328  /* Test appending data */
329  if (!_dbus_string_init (&str))
330  _dbus_test_fatal ("failed to init string");
331 
332  i = 0;
333  while (i < 10)
334  {
335  if (!_dbus_string_append (&str, "a"))
336  _dbus_test_fatal ("failed to append string to string");
337 
338  _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
339 
340  if (!_dbus_string_append_byte (&str, 'b'))
341  _dbus_test_fatal ("failed to append byte to string");
342 
343  _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
344 
345  ++i;
346  }
347 
348  _dbus_string_free (&str);
349 
350  /* Check steal_data */
351 
352  if (!_dbus_string_init (&str))
353  _dbus_test_fatal ("failed to init string");
354 
355  if (!_dbus_string_append (&str, "Hello World"))
356  _dbus_test_fatal ("could not append to string");
357 
358  i = _dbus_string_get_length (&str);
359 
360  if (!_dbus_string_steal_data (&str, &s))
361  _dbus_test_fatal ("failed to steal data");
362 
363  _dbus_assert (_dbus_string_get_length (&str) == 0);
364  _dbus_assert (((int)strlen (s)) == i);
365 
366  dbus_free (s);
367 
368  /* Check move */
369 
370  if (!_dbus_string_append (&str, "Hello World"))
371  _dbus_test_fatal ("could not append to string");
372 
373  i = _dbus_string_get_length (&str);
374 
375  if (!_dbus_string_init (&other))
376  _dbus_test_fatal ("could not init string");
377 
378  if (!_dbus_string_move (&str, 0, &other, 0))
379  _dbus_test_fatal ("could not move");
380 
381  _dbus_assert (_dbus_string_get_length (&str) == 0);
382  _dbus_assert (_dbus_string_get_length (&other) == i);
383 
384  if (!_dbus_string_append (&str, "Hello World"))
385  _dbus_test_fatal ("could not append to string");
386 
387  if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
388  _dbus_test_fatal ("could not move");
389 
390  _dbus_assert (_dbus_string_get_length (&str) == 0);
391  _dbus_assert (_dbus_string_get_length (&other) == i * 2);
392 
393  if (!_dbus_string_append (&str, "Hello World"))
394  _dbus_test_fatal ("could not append to string");
395 
396  if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
397  _dbus_test_fatal ("could not move");
398 
399  _dbus_assert (_dbus_string_get_length (&str) == 0);
400  _dbus_assert (_dbus_string_get_length (&other) == i * 3);
401 
402  _dbus_string_free (&other);
403 
404  /* Check copy */
405 
406  if (!_dbus_string_append (&str, "Hello World"))
407  _dbus_test_fatal ("could not append to string");
408 
409  i = _dbus_string_get_length (&str);
410 
411  if (!_dbus_string_init (&other))
412  _dbus_test_fatal ("could not init string");
413 
414  if (!_dbus_string_copy (&str, 0, &other, 0))
415  _dbus_test_fatal ("could not copy");
416 
417  _dbus_assert (_dbus_string_get_length (&str) == i);
418  _dbus_assert (_dbus_string_get_length (&other) == i);
419 
420  if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
421  _dbus_test_fatal ("could not copy");
422 
423  _dbus_assert (_dbus_string_get_length (&str) == i);
424  _dbus_assert (_dbus_string_get_length (&other) == i * 2);
426  "Hello WorldHello World"));
427 
428  if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
429  _dbus_test_fatal ("could not copy");
430 
431  _dbus_assert (_dbus_string_get_length (&str) == i);
432  _dbus_assert (_dbus_string_get_length (&other) == i * 3);
434  "Hello WorldHello WorldHello World"));
435 
436  _dbus_string_free (&str);
437  _dbus_string_free (&other);
438 
439  /* Check replace */
440 
441  if (!_dbus_string_init (&str))
442  _dbus_test_fatal ("failed to init string");
443 
444  if (!_dbus_string_append (&str, "Hello World"))
445  _dbus_test_fatal ("could not append to string");
446 
447  i = _dbus_string_get_length (&str);
448 
449  if (!_dbus_string_init (&other))
450  _dbus_test_fatal ("could not init string");
451 
452  if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
453  &other, 0, _dbus_string_get_length (&other)))
454  _dbus_test_fatal ("could not replace");
455 
456  _dbus_assert (_dbus_string_get_length (&str) == i);
457  _dbus_assert (_dbus_string_get_length (&other) == i);
458  _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
459 
460  if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
461  &other, 5, 1))
462  _dbus_test_fatal ("could not replace center space");
463 
464  _dbus_assert (_dbus_string_get_length (&str) == i);
465  _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
467  "HelloHello WorldWorld"));
468 
469 
470  if (!_dbus_string_replace_len (&str, 1, 1,
471  &other,
472  _dbus_string_get_length (&other) - 1,
473  1))
474  _dbus_test_fatal ("could not replace end character");
475 
476  _dbus_assert (_dbus_string_get_length (&str) == i);
477  _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
479  "HelloHello WorldWorle"));
480 
481  _dbus_string_free (&str);
482  _dbus_string_free (&other);
483 
484  /* Different tests are provided because different behaviours are
485  * implemented in _dbus_string_replace_len() in function of replacing and
486  * replaced lengths
487  */
488 
489  if (!_dbus_string_init (&str))
490  _dbus_test_fatal ("failed to init string");
491 
492  if (!_dbus_string_append (&str, "Hello World"))
493  _dbus_test_fatal ("could not append to string");
494 
495  i = _dbus_string_get_length (&str);
496 
497  if (!_dbus_string_init (&other))
498  _dbus_test_fatal ("could not init string");
499 
500  if (!_dbus_string_append (&other, "Foo String"))
501  _dbus_test_fatal ("could not append to string");
502 
503  a = _dbus_string_get_length (&other);
504 
505  if (!_dbus_string_replace_len (&str, 0, 6,
506  &other, 4, 0))
507  _dbus_test_fatal ("could not replace 0 length");
508 
509  _dbus_assert (_dbus_string_get_length (&str) == i);
510  _dbus_assert (_dbus_string_get_length (&other) == a + 6);
512  "Foo Hello String"));
513 
514  if (!_dbus_string_replace_len (&str, 5, 6,
515  &other,
516  _dbus_string_get_length (&other),
517  0))
518  _dbus_test_fatal ("could not replace at the end");
519 
520  _dbus_assert (_dbus_string_get_length (&str) == i);
521  _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
523  "Foo Hello String World"));
524 
525  if (!_dbus_string_replace_len (&str, 0, 5,
526  &other,
527  _dbus_string_get_length (&other) - 5,
528  5))
529  _dbus_test_fatal ("could not replace same length");
530 
531  _dbus_assert (_dbus_string_get_length (&str) == i);
532  _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
534  "Foo Hello String Hello"));
535 
536  if (!_dbus_string_replace_len (&str, 6, 5,
537  &other, 4, 12))
538  _dbus_test_fatal ("could not replace with shorter string");
539 
540  _dbus_assert (_dbus_string_get_length (&str) == i);
541  _dbus_assert (_dbus_string_get_length (&other) == a + 5);
543  "Foo World Hello"));
544 
545  if (!_dbus_string_replace_len (&str, 0, 1,
546  &other, 0, 3))
547  _dbus_test_fatal ("could not replace at the beginning");
548 
549  _dbus_assert (_dbus_string_get_length (&str) == i);
550  _dbus_assert (_dbus_string_get_length (&other) == a + 3);
552  "H World Hello"));
553 
554  if (!_dbus_string_replace_len (&str, 6, 5,
555  &other,
556  _dbus_string_get_length (&other) - 5,
557  5))
558  _dbus_test_fatal ("could not replace same length");
559 
560  _dbus_assert (_dbus_string_get_length (&str) == i);
561  _dbus_assert (_dbus_string_get_length (&other) == a + 3);
563  "H World World"));
564 
565  _dbus_string_free (&str);
566  _dbus_string_free (&other);
567 
568  /* Check insert/set/get byte */
569 
570  if (!_dbus_string_init (&str))
571  _dbus_test_fatal ("failed to init string");
572 
573  if (!_dbus_string_append (&str, "Hello"))
574  _dbus_test_fatal ("failed to append Hello");
575 
576  _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
577  _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
578  _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
579  _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
580  _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
581 
582  _dbus_string_set_byte (&str, 1, 'q');
583  _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
584 
585  if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
586  _dbus_test_fatal ("can't insert byte");
587 
588  if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
589  _dbus_test_fatal ("can't insert byte");
590 
591  if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
592  _dbus_test_fatal ("can't insert byte");
593 
594  _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
595  _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
596  _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
597  _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
598  _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
599  _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
600  _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
601  _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
602  _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
603  _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
604  _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
605 
606  _dbus_string_free (&str);
607 
608  /* Check append/parse int/double */
609 
610  if (!_dbus_string_init (&str))
611  _dbus_test_fatal ("failed to init string");
612 
613  if (!_dbus_string_append_int (&str, 27))
614  _dbus_test_fatal ("failed to append int");
615 
616  i = _dbus_string_get_length (&str);
617 
618  if (!_dbus_string_parse_int (&str, 0, &v, &end))
619  _dbus_test_fatal ("failed to parse int");
620 
621  _dbus_assert (v == 27);
622  _dbus_assert (end == i);
623 
624  _dbus_string_free (&str);
625 
626  /* Test find */
627  if (!_dbus_string_init (&str))
628  _dbus_test_fatal ("failed to init string");
629 
630  if (!_dbus_string_append (&str, "Hello"))
631  _dbus_test_fatal ("couldn't append to string");
632 
633  if (!_dbus_string_find (&str, 0, "He", &i))
634  _dbus_test_fatal ("didn't find 'He'");
635  _dbus_assert (i == 0);
636 
637  if (!_dbus_string_find (&str, 0, "Hello", &i))
638  _dbus_test_fatal ("didn't find 'Hello'");
639  _dbus_assert (i == 0);
640 
641  if (!_dbus_string_find (&str, 0, "ello", &i))
642  _dbus_test_fatal ("didn't find 'ello'");
643  _dbus_assert (i == 1);
644 
645  if (!_dbus_string_find (&str, 0, "lo", &i))
646  _dbus_test_fatal ("didn't find 'lo'");
647  _dbus_assert (i == 3);
648 
649  if (!_dbus_string_find (&str, 2, "lo", &i))
650  _dbus_test_fatal ("didn't find 'lo'");
651  _dbus_assert (i == 3);
652 
653  if (_dbus_string_find (&str, 4, "lo", &i))
654  _dbus_test_fatal ("did find 'lo'");
655 
656  if (!_dbus_string_find (&str, 0, "l", &i))
657  _dbus_test_fatal ("didn't find 'l'");
658  _dbus_assert (i == 2);
659 
660  if (!_dbus_string_find (&str, 0, "H", &i))
661  _dbus_test_fatal ("didn't find 'H'");
662  _dbus_assert (i == 0);
663 
664  if (!_dbus_string_find (&str, 0, "", &i))
665  _dbus_test_fatal ("didn't find ''");
666  _dbus_assert (i == 0);
667 
668  if (_dbus_string_find (&str, 0, "Hello!", NULL))
669  _dbus_test_fatal ("Did find 'Hello!'");
670 
671  if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
672  _dbus_test_fatal ("Did find 'Oh, Hello'");
673 
674  if (_dbus_string_find (&str, 0, "ill", NULL))
675  _dbus_test_fatal ("Did find 'ill'");
676 
677  if (_dbus_string_find (&str, 0, "q", NULL))
678  _dbus_test_fatal ("Did find 'q'");
679 
680  if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
681  _dbus_test_fatal ("Didn't find 'He'");
682 
683  if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
684  _dbus_test_fatal ("Did find 'Hello'");
685 
686  if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
687  _dbus_test_fatal ("Did not find 'H'");
688  _dbus_assert (i == 0);
689 
690  if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
691  _dbus_test_fatal ("Did not find 'o'");
692  _dbus_assert (i == _dbus_string_get_length (&str) - 1);
693 
694  if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
695  _dbus_test_fatal ("Did find 'o'");
696  _dbus_assert (i == -1);
697 
698  if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
699  _dbus_test_fatal ("Did find 'e'");
700  _dbus_assert (i == -1);
701 
702  if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
703  _dbus_test_fatal ("Didn't find 'e'");
704  _dbus_assert (i == 1);
705 
706  _dbus_string_free (&str);
707 
708  /* Hex encoding */
709  _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
710  if (!_dbus_string_init (&other))
711  _dbus_test_fatal ("could not init string");
712 
713  if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
714  _dbus_test_fatal ("deccoded bogus hex string with no error");
715 
716  _dbus_assert (end == 8);
717 
718  _dbus_string_free (&other);
719 
720  test_roundtrips (test_hex_roundtrip);
721 
722  _dbus_string_free (&str);
723 
724  {
725  int found, found_len;
726 
727  _dbus_string_init_const (&str, "012\r\n567\n90");
728 
729  if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
730  _dbus_test_fatal ("Did not find '\\r\\n'");
731  if (found != 3 || found_len != 2)
732  _dbus_test_fatal ("invalid return values");
733 
734  if (!_dbus_string_find_eol (&str, 5, &found, &found_len))
735  _dbus_test_fatal ("Did not find '\\n'");
736  if (found != 8 || found_len != 1)
737  _dbus_test_fatal ("invalid return values");
738 
739  if (_dbus_string_find_eol (&str, 9, &found, &found_len))
740  _dbus_test_fatal ("Found not expected '\\n'");
741  else if (found != 11 || found_len != 0)
742  _dbus_test_fatal ("invalid return values '\\n'");
743 
744  found = -1;
745  found_len = -1;
746  _dbus_string_init_const (&str, "");
747  if (_dbus_string_find_eol (&str, 0, &found, &found_len))
748  _dbus_test_fatal ("found an eol in an empty string");
749  _dbus_assert (found == 0);
750  _dbus_assert (found_len == 0);
751 
752  found = -1;
753  found_len = -1;
754  _dbus_string_init_const (&str, "foobar");
755  if (_dbus_string_find_eol (&str, 0, &found, &found_len))
756  _dbus_test_fatal ("found eol in string that lacks one");
757  _dbus_assert (found == 6);
758  _dbus_assert (found_len == 0);
759 
760  found = -1;
761  found_len = -1;
762  _dbus_string_init_const (&str, "foobar\n");
763  if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
764  _dbus_test_fatal ("did not find eol in string that has one at end");
765  _dbus_assert (found == 6);
766  _dbus_assert (found_len == 1);
767  }
768 
769  {
770  DBusString line;
771 
772 #define FIRST_LINE "this is a line"
773 #define SECOND_LINE "this is a second line"
774  /* third line is empty */
775 #define THIRD_LINE ""
776 #define FOURTH_LINE "this is a fourth line"
777 
778  if (!_dbus_string_init (&str))
779  _dbus_test_fatal ("no memory");
780 
781  if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
782  _dbus_test_fatal ("no memory");
783 
784  if (!_dbus_string_init (&line))
785  _dbus_test_fatal ("no memory");
786 
787  if (!_dbus_string_pop_line (&str, &line))
788  _dbus_test_fatal ("failed to pop first line");
789 
790  _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
791 
792  if (!_dbus_string_pop_line (&str, &line))
793  _dbus_test_fatal ("failed to pop second line");
794 
795  _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
796 
797  if (!_dbus_string_pop_line (&str, &line))
798  _dbus_test_fatal ("failed to pop third line");
799 
800  _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
801 
802  if (!_dbus_string_pop_line (&str, &line))
803  _dbus_test_fatal ("failed to pop fourth line");
804 
805  _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
806 
807  _dbus_string_free (&str);
808  _dbus_string_free (&line);
809  }
810 
811  {
812  if (!_dbus_string_init (&str))
813  _dbus_test_fatal ("no memory");
814 
815  for (i = 0; i < 10000; i++)
816  if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
817  _dbus_test_fatal ("no memory");
818 
819  if (!_dbus_string_set_length (&str, 10))
820  _dbus_test_fatal ("failed to set length");
821 
822  /* actually compact */
823  if (!_dbus_string_compact (&str, 2048))
824  _dbus_test_fatal ("failed to compact after set_length");
825 
826  /* peek inside to make sure it worked */
827  if (((DBusRealString *)&str)->allocated > 30)
828  _dbus_test_fatal ("compacting string didn't do anything");
829 
830  if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
831  _dbus_test_fatal ("unexpected content after compact");
832 
833  /* compact nothing */
834  if (!_dbus_string_compact (&str, 2048))
835  _dbus_test_fatal ("failed to compact 2nd time");
836 
837  if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
838  _dbus_test_fatal ("unexpected content after 2nd compact");
839 
840  /* and make sure it still works...*/
841  if (!_dbus_string_append (&str, "123456"))
842  _dbus_test_fatal ("failed to append after compact");
843 
844  if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
845  _dbus_test_fatal ("unexpected content after append");
846 
847  /* after growing automatically, this should do nothing */
848  if (!_dbus_string_compact (&str, 20000))
849  _dbus_test_fatal ("failed to compact after grow");
850 
851  /* but this one will do something */
852  if (!_dbus_string_compact (&str, 0))
853  _dbus_test_fatal ("failed to compact after grow");
854 
855  if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
856  _dbus_test_fatal ("unexpected content");
857 
858  if (!_dbus_string_append (&str, "!@#$%"))
859  _dbus_test_fatal ("failed to append after compact");
860 
861  if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
862  _dbus_test_fatal ("unexpected content");
863 
864  _dbus_string_free (&str);
865  }
866 
867  {
868  const char two_strings[] = "one\ttwo";
869 
870  if (!_dbus_string_init (&str))
871  _dbus_test_fatal ("no memory");
872 
873  if (!_dbus_string_init (&other))
874  _dbus_test_fatal ("no memory");
875 
876  if (!_dbus_string_append (&str, two_strings))
877  _dbus_test_fatal ("no memory");
878 
879  if (!_dbus_string_split_on_byte (&str, '\t', &other))
880  _dbus_test_fatal ("no memory or delimiter not found");
881 
882  if (strcmp (_dbus_string_get_data (&str), "one") != 0)
883  _dbus_test_fatal ("left side after split on tab is wrong");
884 
885  if (strcmp (_dbus_string_get_data (&other), "two") != 0)
886  _dbus_test_fatal ("right side after split on tab is wrong");
887 
888  _dbus_string_free (&str);
889  _dbus_string_free (&other);
890  }
891 
892  {
893  const char upper_string[] = "TOUPPERSTRING";
894  const char lower_string[] = "toupperstring";
895  const char lower2_string[] = "toupperSTRING";
896 
897  if (!_dbus_string_init (&str))
898  _dbus_test_fatal ("no memory");
899 
900  if (!_dbus_string_append (&str, upper_string))
901  _dbus_test_fatal ("no memory");
902 
903  _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
904 
905  if (!_dbus_string_equal_c_str (&str, lower_string))
906  _dbus_test_fatal ("_dbus_string_tolower_ascii failed");
907 
908  _dbus_string_free (&str);
909 
910  if (!_dbus_string_init (&str))
911  _dbus_test_fatal ("no memory");
912 
913  if (!_dbus_string_append (&str, upper_string))
914  _dbus_test_fatal ("no memory");
915 
916  _dbus_string_tolower_ascii (&str, 0, 7);
917 
918  if (!_dbus_string_equal_c_str (&str, lower2_string))
919  _dbus_test_fatal ("_dbus_string_tolower_ascii failed in partial conversion");
920 
921  _dbus_string_free (&str);
922  }
923 
924  {
925  const char lower_string[] = "toupperstring";
926  const char upper_string[] = "TOUPPERSTRING";
927  const char upper2_string[] = "TOUPPERstring";
928 
929  if (!_dbus_string_init (&str))
930  _dbus_test_fatal ("no memory");
931 
932  if (!_dbus_string_append (&str, lower_string))
933  _dbus_test_fatal ("no memory");
934 
935  _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
936 
937  if (!_dbus_string_equal_c_str (&str, upper_string))
938  _dbus_test_fatal ("_dbus_string_toupper_ascii failed");
939 
940  _dbus_string_free (&str);
941 
942  if (!_dbus_string_init (&str))
943  _dbus_test_fatal ("no memory");
944 
945  if (!_dbus_string_append (&str, lower_string))
946  _dbus_test_fatal ("no memory");
947 
948  _dbus_string_toupper_ascii (&str, 0, 7);
949 
950  if (!_dbus_string_equal_c_str (&str, upper2_string))
951  _dbus_test_fatal ("_dbus_string_toupper_ascii failed in partial conversion");
952 
953  _dbus_string_free (&str);
954  }
955 
956  return TRUE;
957 }
958 
959 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
dbus_bool_t _dbus_string_insert_bytes(DBusString *str, int i, int n_bytes, unsigned char byte)
Inserts a number of bytes of a given value at the given position.
Definition: dbus-string.c:601
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:952
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString.
Definition: dbus-sysdeps.c:444
#define NULL
A null pointer, defined appropriately for C or C++.
Internals of DBusString.
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2030
dbus_bool_t _dbus_string_find_eol(const DBusString *str, int start, int *found, int *found_len)
Finds end of line ("\r\n" or "\n") in the string, returning TRUE and filling in the byte index where ...
Definition: dbus-string.c:1644
unsigned int valid
DBusString is valid (initialized and not freed)
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:703
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition: dbus-string.c:2276
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:363
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void _dbus_string_tolower_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to lower case.
Definition: dbus-string.c:2502
#define DBUS_CONST_STRING_PREAMBLE(str)
Checks assertions about a string that may be const or locked.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
void _dbus_string_shorten(DBusString *str, int length_to_remove)
Makes a string shorter by the given number of bytes.
Definition: dbus-string.c:797
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1300
dbus_bool_t _dbus_string_ends_with_c_str(const DBusString *a, const char *c_str)
Returns whether a string ends with the given suffix.
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition: dbus-string.c:1621
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
Definition: dbus-string.c:1276
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
Definition: dbus-string.c:2169
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
Definition: dbus-string.c:406
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
#define DBUS_GENERIC_STRING_PREAMBLE(real)
Checks a bunch of assertions about a string object.
dbus_bool_t _dbus_string_replace_len(const DBusString *source, int start, int len, DBusString *dest, int replace_at, int replace_len)
Replaces a segment of dest string with a segment of source string.
Definition: dbus-string.c:1421
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
Definition: dbus-string.c:1926
int len
Length without nul.
dbus_bool_t _dbus_string_equal_substring(const DBusString *a, int a_start, int a_len, const DBusString *b, int b_start)
Tests two sub-parts of two DBusString for equality.
Definition: dbus-string.c:2121
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
unsigned int locked
DBusString has been locked and can&#39;t be changed.
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1174
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:264
#define TRUE
Expands to "1".
int allocated
Allocated size of data.
dbus_bool_t _dbus_string_find_byte_backward(const DBusString *str, int start, unsigned char byte, int *found)
Find the given byte scanning backward from the given start.
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition: dbus-string.c:2326
unsigned int align_offset
str - align_offset is the actual malloc block
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
Definition: dbus-string.c:1154
dbus_bool_t _dbus_string_find_to(const DBusString *str, int start, int end, const char *substr, int *found)
Finds the given substring in the string, up to a certain position, returning TRUE and filling in the ...
Definition: dbus-string.c:1714
_DBUS_STRING_DEFINE_STATIC(_dbus_header_signature_str, DBUS_HEADER_SIGNATURE)
Static DBusString containing the signature of a message header.
unsigned char * str
String data, plus nul termination.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:819
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:658
void _dbus_string_toupper_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to upper case.
Definition: dbus-string.c:2533
unsigned int constant
String data is not owned by DBusString.
dbus_bool_t _dbus_string_split_on_byte(DBusString *source, unsigned char byte, DBusString *tail)
Looks for the first occurance of a byte, deletes that byte, and moves everything after the byte to th...
Definition: dbus-string.c:1484