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> 50 const unsigned char *ap;
51 const unsigned char *bp;
52 const unsigned char *a_end;
53 unsigned long c_str_len;
58 c_str_len = strlen (c_str);
59 if (((
unsigned long)real_a->
len) < c_str_len)
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;
105 if (real->str[i] == byte)
119 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 120 #include "dbus-test.h" 124 test_hex_roundtrip (
const char *data,
136 _dbus_test_fatal (
"could not init string");
139 _dbus_test_fatal (
"could not init string");
142 _dbus_test_fatal (
"could not init string");
145 _dbus_test_fatal (
"couldn't append orig data");
148 _dbus_test_fatal (
"could not encode");
151 _dbus_test_fatal (
"could not decode");
153 _dbus_assert (_dbus_string_get_length (&encoded) == end);
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");
174 typedef void (* TestRoundtripFunc) (
const char *data,
177 test_roundtrips (TestRoundtripFunc func)
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);
189 (* func) (
"1234", 4);
190 (* func) (
"12345", 5);
195 (* func) (
"1234", 5);
196 (* func) (
"12345", 6);
198 unsigned char buf[512];
210 (* func) ((
const char *) buf, i);
229 _dbus_string_test (
void)
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 };
240 _dbus_assert (strcmp ((
const char *) real_test_static_string->
str,
"hello") == 0);
260 _dbus_test_fatal (
"failed to init string");
263 _dbus_test_fatal (
"failed to set string length");
272 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
287 _dbus_test_fatal (
"oom");
290 _dbus_test_fatal (
"oom");
330 _dbus_test_fatal (
"failed to init string");
336 _dbus_test_fatal (
"failed to append string to string");
338 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
341 _dbus_test_fatal (
"failed to append byte to string");
343 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
353 _dbus_test_fatal (
"failed to init string");
356 _dbus_test_fatal (
"could not append to string");
358 i = _dbus_string_get_length (&str);
361 _dbus_test_fatal (
"failed to steal data");
371 _dbus_test_fatal (
"could not append to string");
373 i = _dbus_string_get_length (&str);
376 _dbus_test_fatal (
"could not init string");
379 _dbus_test_fatal (
"could not move");
385 _dbus_test_fatal (
"could not append to string");
388 _dbus_test_fatal (
"could not move");
391 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
394 _dbus_test_fatal (
"could not append to string");
397 _dbus_test_fatal (
"could not move");
400 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
407 _dbus_test_fatal (
"could not append to string");
409 i = _dbus_string_get_length (&str);
412 _dbus_test_fatal (
"could not init string");
415 _dbus_test_fatal (
"could not copy");
421 _dbus_test_fatal (
"could not copy");
424 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
426 "Hello WorldHello World"));
429 _dbus_test_fatal (
"could not copy");
432 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
434 "Hello WorldHello WorldHello World"));
442 _dbus_test_fatal (
"failed to init string");
445 _dbus_test_fatal (
"could not append to string");
447 i = _dbus_string_get_length (&str);
450 _dbus_test_fatal (
"could not init string");
453 &other, 0, _dbus_string_get_length (&other)))
454 _dbus_test_fatal (
"could not replace");
462 _dbus_test_fatal (
"could not replace center space");
465 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
467 "HelloHello WorldWorld"));
472 _dbus_string_get_length (&other) - 1,
474 _dbus_test_fatal (
"could not replace end character");
477 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
479 "HelloHello WorldWorle"));
490 _dbus_test_fatal (
"failed to init string");
493 _dbus_test_fatal (
"could not append to string");
495 i = _dbus_string_get_length (&str);
498 _dbus_test_fatal (
"could not init string");
501 _dbus_test_fatal (
"could not append to string");
503 a = _dbus_string_get_length (&other);
507 _dbus_test_fatal (
"could not replace 0 length");
510 _dbus_assert (_dbus_string_get_length (&other) == a + 6);
512 "Foo Hello String"));
516 _dbus_string_get_length (&other),
518 _dbus_test_fatal (
"could not replace at the end");
521 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
523 "Foo Hello String World"));
527 _dbus_string_get_length (&other) - 5,
529 _dbus_test_fatal (
"could not replace same length");
532 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
534 "Foo Hello String Hello"));
538 _dbus_test_fatal (
"could not replace with shorter string");
541 _dbus_assert (_dbus_string_get_length (&other) == a + 5);
547 _dbus_test_fatal (
"could not replace at the beginning");
550 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
556 _dbus_string_get_length (&other) - 5,
558 _dbus_test_fatal (
"could not replace same length");
561 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
571 _dbus_test_fatal (
"failed to init string");
574 _dbus_test_fatal (
"failed to append Hello");
582 _dbus_string_set_byte (&str, 1,
'q');
586 _dbus_test_fatal (
"can't insert byte");
589 _dbus_test_fatal (
"can't insert byte");
592 _dbus_test_fatal (
"can't insert byte");
611 _dbus_test_fatal (
"failed to init string");
614 _dbus_test_fatal (
"failed to append int");
616 i = _dbus_string_get_length (&str);
619 _dbus_test_fatal (
"failed to parse int");
628 _dbus_test_fatal (
"failed to init string");
631 _dbus_test_fatal (
"couldn't append to string");
634 _dbus_test_fatal (
"didn't find 'He'");
638 _dbus_test_fatal (
"didn't find 'Hello'");
642 _dbus_test_fatal (
"didn't find 'ello'");
646 _dbus_test_fatal (
"didn't find 'lo'");
650 _dbus_test_fatal (
"didn't find 'lo'");
654 _dbus_test_fatal (
"did find 'lo'");
657 _dbus_test_fatal (
"didn't find 'l'");
661 _dbus_test_fatal (
"didn't find 'H'");
665 _dbus_test_fatal (
"didn't find ''");
669 _dbus_test_fatal (
"Did find 'Hello!'");
672 _dbus_test_fatal (
"Did find 'Oh, Hello'");
675 _dbus_test_fatal (
"Did find 'ill'");
678 _dbus_test_fatal (
"Did find 'q'");
681 _dbus_test_fatal (
"Didn't find 'He'");
684 _dbus_test_fatal (
"Did find 'Hello'");
687 _dbus_test_fatal (
"Did not find 'H'");
691 _dbus_test_fatal (
"Did not find 'o'");
695 _dbus_test_fatal (
"Did find 'o'");
699 _dbus_test_fatal (
"Did find 'e'");
703 _dbus_test_fatal (
"Didn't find 'e'");
711 _dbus_test_fatal (
"could not init string");
714 _dbus_test_fatal (
"deccoded bogus hex string with no error");
720 test_roundtrips (test_hex_roundtrip);
725 int found, found_len;
730 _dbus_test_fatal (
"Did not find '\\r\\n'");
731 if (found != 3 || found_len != 2)
732 _dbus_test_fatal (
"invalid return values");
735 _dbus_test_fatal (
"Did not find '\\n'");
736 if (found != 8 || found_len != 1)
737 _dbus_test_fatal (
"invalid return values");
740 _dbus_test_fatal (
"Found not expected '\\n'");
741 else if (found != 11 || found_len != 0)
742 _dbus_test_fatal (
"invalid return values '\\n'");
748 _dbus_test_fatal (
"found an eol in an empty string");
756 _dbus_test_fatal (
"found eol in string that lacks one");
764 _dbus_test_fatal (
"did not find eol in string that has one at end");
772 #define FIRST_LINE "this is a line" 773 #define SECOND_LINE "this is a second line" 775 #define THIRD_LINE "" 776 #define FOURTH_LINE "this is a fourth line" 779 _dbus_test_fatal (
"no memory");
781 if (!
_dbus_string_append (&str, FIRST_LINE
"\n" SECOND_LINE
"\r\n" THIRD_LINE
"\n" FOURTH_LINE))
782 _dbus_test_fatal (
"no memory");
785 _dbus_test_fatal (
"no memory");
788 _dbus_test_fatal (
"failed to pop first line");
793 _dbus_test_fatal (
"failed to pop second line");
798 _dbus_test_fatal (
"failed to pop third line");
803 _dbus_test_fatal (
"failed to pop fourth line");
813 _dbus_test_fatal (
"no memory");
815 for (i = 0; i < 10000; i++)
817 _dbus_test_fatal (
"no memory");
820 _dbus_test_fatal (
"failed to set length");
824 _dbus_test_fatal (
"failed to compact after set_length");
828 _dbus_test_fatal (
"compacting string didn't do anything");
831 _dbus_test_fatal (
"unexpected content after compact");
835 _dbus_test_fatal (
"failed to compact 2nd time");
838 _dbus_test_fatal (
"unexpected content after 2nd compact");
842 _dbus_test_fatal (
"failed to append after compact");
845 _dbus_test_fatal (
"unexpected content after append");
849 _dbus_test_fatal (
"failed to compact after grow");
853 _dbus_test_fatal (
"failed to compact after grow");
856 _dbus_test_fatal (
"unexpected content");
859 _dbus_test_fatal (
"failed to append after compact");
862 _dbus_test_fatal (
"unexpected content");
868 const char two_strings[] =
"one\ttwo";
871 _dbus_test_fatal (
"no memory");
874 _dbus_test_fatal (
"no memory");
877 _dbus_test_fatal (
"no memory");
880 _dbus_test_fatal (
"no memory or delimiter not found");
882 if (strcmp (_dbus_string_get_data (&str),
"one") != 0)
883 _dbus_test_fatal (
"left side after split on tab is wrong");
885 if (strcmp (_dbus_string_get_data (&other),
"two") != 0)
886 _dbus_test_fatal (
"right side after split on tab is wrong");
893 const char upper_string[] =
"TOUPPERSTRING";
894 const char lower_string[] =
"toupperstring";
895 const char lower2_string[] =
"toupperSTRING";
898 _dbus_test_fatal (
"no memory");
901 _dbus_test_fatal (
"no memory");
906 _dbus_test_fatal (
"_dbus_string_tolower_ascii failed");
911 _dbus_test_fatal (
"no memory");
914 _dbus_test_fatal (
"no memory");
919 _dbus_test_fatal (
"_dbus_string_tolower_ascii failed in partial conversion");
925 const char lower_string[] =
"toupperstring";
926 const char upper_string[] =
"TOUPPERSTRING";
927 const char upper2_string[] =
"TOUPPERstring";
930 _dbus_test_fatal (
"no memory");
933 _dbus_test_fatal (
"no memory");
938 _dbus_test_fatal (
"_dbus_string_toupper_ascii failed");
943 _dbus_test_fatal (
"no memory");
946 _dbus_test_fatal (
"no memory");
951 _dbus_test_fatal (
"_dbus_string_toupper_ascii failed in partial conversion");
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.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
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.
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
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 ...
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().
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.
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
#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.
#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.
void _dbus_string_shorten(DBusString *str, int length_to_remove)
Makes a string shorter by the given number of bytes.
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's copied to the d...
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...
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
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.
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
#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.
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...
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.
#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'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.
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...
#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.
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.
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 ...
_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.
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.
void _dbus_string_toupper_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to upper case.
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...