Beispiel #1
0
static void test_sc_string_append_char_pair__normal()
{
	char buf[16];
	size_t len;
	sc_string_init(buf, sizeof buf);

	len = sc_string_append_char_pair(buf, sizeof buf, 'h', 'e');
	g_assert_cmpstr(buf, ==, "he");
	g_assert_cmpint(len, ==, 2);
	len = sc_string_append_char_pair(buf, sizeof buf, 'l', 'l');
	g_assert_cmpstr(buf, ==, "hell");
	g_assert_cmpint(len, ==, 4);
	len = sc_string_append_char_pair(buf, sizeof buf, 'o', '!');
	g_assert_cmpstr(buf, ==, "hello!");
	g_assert_cmpint(len, ==, 6);
}
Beispiel #2
0
static void test_sc_string_append_char_pair__NULL_buf()
{
	if (g_test_subprocess()) {
		sc_string_append_char_pair(NULL, 3, 'a', 'b');

		g_test_message
		    ("expected sc_string_append_char_pair not to return");
		g_test_fail();
		return;
	}
	g_test_trap_subprocess(NULL, 0, 0);
	g_test_trap_assert_failed();
	g_test_trap_assert_stderr
	    ("cannot append character pair: buffer is NULL\n");
}
Beispiel #3
0
static void test_sc_string_append_char_pair__invalid_zero_c2()
{
	if (g_test_subprocess()) {
		char buf[3] = { 0 };
		sc_string_append_char_pair(buf, sizeof buf, 'a', '\0');

		g_test_message
		    ("expected sc_string_append_char_pair not to return");
		g_test_fail();
		return;
	}
	g_test_trap_subprocess(NULL, 0, 0);
	g_test_trap_assert_failed();
	g_test_trap_assert_stderr
	    ("cannot append character pair: cannot append string terminator\n");
}
Beispiel #4
0
static void test_sc_string_append_char_pair__overflow()
{
	if (g_test_subprocess()) {
		char buf[2] = { 0 };
		sc_string_append_char_pair(buf, sizeof buf, 'a', 'b');

		g_test_message
		    ("expected sc_string_append_char_pair not to return");
		g_test_fail();
		return;
	}
	g_test_trap_subprocess(NULL, 0, 0);
	g_test_trap_assert_failed();
	g_test_trap_assert_stderr
	    ("cannot append character pair: not enough space\n");
}
Beispiel #5
0
static void test_sc_string_append_char_pair__uninitialized_buf()
{
	if (g_test_subprocess()) {
		char buf[3] = { 0xFF, 0xFF, 0xFF };
		sc_string_append_char_pair(buf, sizeof buf, 'a', 'b');

		g_test_message
		    ("expected sc_string_append_char_pair not to return");
		g_test_fail();
		return;
	}
	g_test_trap_subprocess(NULL, 0, 0);
	g_test_trap_assert_failed();
	g_test_trap_assert_stderr
	    ("cannot append character pair: dst is unterminated\n");
}
void sc_string_quote(char *buf, size_t buf_size, const char *str)
{
	if (str == NULL) {
		die("cannot quote string: string is NULL");
	}
	const char *hex = "0123456789abcdef";
	// NOTE: this also checks buf/buf_size sanity so that we don't have to.
	sc_string_init(buf, buf_size);
	sc_string_append_char(buf, buf_size, '"');
	for (unsigned char c; (c = *str) != 0; ++str) {
		switch (c) {
			// Pass ASCII letters and digits unmodified.
		case '0' ... '9':
		case 'A' ... 'Z':
		case 'a' ... 'z':
			// Pass most of the punctuation unmodified.
		case ' ':
		case '!':
		case '#':
		case '$':
		case '%':
		case '&':
		case '(':
		case ')':
		case '*':
		case '+':
		case ',':
		case '-':
		case '.':
		case '/':
		case ':':
		case ';':
		case '<':
		case '=':
		case '>':
		case '?':
		case '@':
		case '[':
		case '\'':
		case ']':
		case '^':
		case '_':
		case '`':
		case '{':
		case '|':
		case '}':
		case '~':
			sc_string_append_char(buf, buf_size, c);
			break;
			// Escape special whitespace characters.
		case '\n':
			sc_string_append_char_pair(buf, buf_size, '\\', 'n');
			break;
		case '\r':
			sc_string_append_char_pair(buf, buf_size, '\\', 'r');
			break;
		case '\t':
			sc_string_append_char_pair(buf, buf_size, '\\', 't');
			break;
		case '\v':
			sc_string_append_char_pair(buf, buf_size, '\\', 'v');
			break;
			// Escape the escape character.
		case '\\':
			sc_string_append_char_pair(buf, buf_size, '\\', '\\');
			break;
			// Escape double quote character.
		case '"':
			sc_string_append_char_pair(buf, buf_size, '\\', '"');
			break;
			// Escape everything else as a generic hexadecimal escape string.
		default:
			sc_string_append_char_pair(buf, buf_size, '\\', 'x');
			sc_string_append_char_pair(buf, buf_size, hex[c >> 4],
						   hex[c & 15]);
			break;
		}
	}
	sc_string_append_char(buf, buf_size, '"');
}