示例#1
0
void checkToBinary(const Sid::String& test, const char* expected, size_t expectedSize)
{
    const char* origData = test.data();
    char* res = new char[test.size()+1];
    size_t size = test.toBinary(res);
    CPPUNIT_ASSERT_PRINTF(size == expectedSize,
                            "toBinary() yielded a result that was too long, "
                            "expected %u, got %u\n",
                            expectedSize,
                            size);
    CPPUNIT_ASSERT_PRINTF(!memcmp(expected, res, size),
                            "%s",
                            "toBinary() yielded unexpected result\n");
    CPPUNIT_ASSERT_MESSAGE("toBinary() causes object to relinquish its reference!",
                            origData == test.data());
}
示例#2
0
void StringTestCase::escapingAndUnescapingTest()
{
    checkEscaping(Sid::String(), Sid::String());
    checkEscaping(Sid::String(4), "");
    checkEscaping("", "");
    checkEscaping(",", "\\,");
    checkEscaping("\"", "\\\"");
    checkEscaping("\\", "\\\\");
    checkEscaping("foobar", "foobar");
    checkEscaping("foo,bar", "foo\\,bar");
    checkEscaping("foo\"bar", "foo\\\"bar");
    checkEscaping("foo\\bar", "foo\\\\bar");
    checkEscaping(",,", "\\,\\,");
    checkEscaping("\"\"", "\\\"\\\"");
    checkEscaping("\\\\", "\\\\\\\\");

    checkUnescaping("\\", "");
    checkUnescaping("foo\\bar", "foobar");
    checkUnescaping("\\foobar", "foobar");
    checkUnescaping("foobar\\", "foobar");

    // .bwc. This code came from an #ifdef SE_STRING_TEST_PROGRAM block in
    // skype_string.cpp.
    int i;
    Sid::String strings[] = {
    	"Hello World!",
    	"Hel,lo\\ World!",
    	"Hel\"lo Wo\\9rld!",
    	"Hel,lo Wo\\0rld!",
    	Sid::String()
    };
    for (i = 0; !strings[i].isNull(); i++) {
    	Sid::String escaped = strings[i].escape();
    	Sid::String unescaped = escaped.unescape();
    	CPPUNIT_ASSERT_PRINTF( !strcmp((const char*)strings[i], (const char*)unescaped),
    				"String Failed:\n%s\n%s\n%s\n\n", 
    				(const char*)strings[i], 
    				(const char*)escaped, 
    				(const char*)unescaped);
    }

    // 0x5c = '\\', 0x30 = '0', 0x00 = 0, 0x22 = '"'
    const char bins[] = {
    	'1', '2', '3', '4', '5', '6', '7', '8' ,
    	'1', '2', 0x5c, 0x30, '5', '6', '7', '8' ,
    	'1', '2', 0x5c, 0x30, 0x5c, 0x5c, 0x5c, '8' ,
    	'1', '2', 0x5c, 0x30, '5', '6', '7', '8' ,
    	'1', '2', 0x00, '4', '5', '6', '7', '8' ,
    	'1', 0x5c, 0x00, 0x5c, '5', '6', 0x5c, 0x5c ,
    	'1', 0x5c, 0x00, 0x00, '5', 0x00, 0x5c, '8' ,
    	0x00, '2', 0x5c, 0x30, '5', 0x22, 0x22, '8' ,
    	0x5c, '2', 0x5c, 0x30, '5', 0x22, 0x22, 0x00 ,
    	0x00, 0x00, 0x5c, 0x30, '5', 0x22, 0x22, 0x5c ,
    	'E'
    };
    size_t bins_len = 8;
    for (i = 0; bins[i] != 'E'; i += bins_len) {
    	Sid::String escaped = Sid::String::from((char*)(bins + i), bins_len);
    	char *unescaped = (char *)malloc(escaped.length());
    	size_t unescaped_len = escaped.toBinary(unescaped);
    	//printf("%d, escaped.length()=%d unescaped_len=%d\n", i/bins_len, escaped.length(), unescaped_len);
    	CPPUNIT_ASSERT_PRINTF ( !memcmp((char*)(bins + i), unescaped, bins_len) && (unescaped_len == bins_len),
    				"Binary Failed: index %d\n", 
    				i/bins_len);
    	//printf("%s\n", (const char*)escaped.getHexRepresentation());
    	free(unescaped);
    }

    {
    	Sid::String test("foo");
    	test.markAsBinary();
    	CPPUNIT_ASSERT_PRINTF(test.isBinary(), 
    				"%s", "markAsBinary() seems "
    						"to have no effect.\n");
    }
}