vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / support / bstring / StringAccessTest.cpp
blobbde3a0638ddd67c51fbe22cf635f0f4892ee2e37
1 #include "StringAccessTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <stdio.h>
4 #include <String.h>
5 #include <UTF8.h>
7 StringAccessTest::StringAccessTest(std::string name) :
8 BTestCase(name)
14 StringAccessTest::~StringAccessTest()
19 void
20 StringAccessTest::PerformTest(void)
22 //CountChars(), Length(), String()
23 NextSubTest();
24 BString string("Something" B_UTF8_ELLIPSIS);
25 CPPUNIT_ASSERT(string.CountChars() == 10);
26 CPPUNIT_ASSERT((unsigned)string.Length() == strlen(string.String()));
28 NextSubTest();
29 BString string2("ABCD");
30 CPPUNIT_ASSERT(string2.CountChars() == 4);
31 CPPUNIT_ASSERT((unsigned)string2.Length() == strlen(string2.String()));
33 NextSubTest();
34 static char s[64];
35 strcpy(s, B_UTF8_ELLIPSIS);
36 strcat(s, B_UTF8_SMILING_FACE);
37 BString string3(s);
38 CPPUNIT_ASSERT(string3.CountChars() == 2);
39 CPPUNIT_ASSERT((unsigned)string3.Length() == strlen(string3.String()));
41 //An empty string
42 NextSubTest();
43 BString empty;
44 CPPUNIT_ASSERT(strcmp(empty.String(), "") == 0);
45 CPPUNIT_ASSERT(empty.Length() == 0);
46 CPPUNIT_ASSERT(empty.CountChars() == 0);
48 //Truncate the string at end so we are left with an invalid
49 //UTF8 character
50 NextSubTest();
51 BString invalid("some text with utf8 characters" B_UTF8_ELLIPSIS);
52 invalid.Truncate(invalid.Length() -1);
53 CPPUNIT_ASSERT(invalid.CountChars() == 31);
55 //LockBuffer(int32) and UnlockBuffer(int32)
56 NextSubTest();
57 BString locked("a string");
58 char *ptrstr = locked.LockBuffer(20);
59 CPPUNIT_ASSERT(strcmp(ptrstr, "a string") == 0);
60 strcat(ptrstr, " to be locked");
61 locked.UnlockBuffer();
62 CPPUNIT_ASSERT(strcmp(ptrstr, "a string to be locked") == 0);
64 NextSubTest();
65 BString locked2("some text");
66 char *ptr = locked2.LockBuffer(3);
67 CPPUNIT_ASSERT(strcmp(ptr, "some text") == 0);
68 locked2.UnlockBuffer(4);
69 CPPUNIT_ASSERT(strcmp(locked2.String(), "some") == 0);
70 CPPUNIT_ASSERT(locked2.Length() == 4);
72 NextSubTest();
73 BString emptylocked;
74 ptr = emptylocked.LockBuffer(10);
75 CPPUNIT_ASSERT(strcmp(ptr, "") == 0);
76 strcat(ptr, "pippo");
77 emptylocked.UnlockBuffer();
78 CPPUNIT_ASSERT(strcmp(emptylocked.String(), "pippo") == 0);
80 // LockBuffer(0) and UnlockBuffer(-1) on a zero lenght string
81 #ifndef TEST_R5
82 NextSubTest();
83 BString crashesR5;
84 ptr = crashesR5.LockBuffer(0);
85 crashesR5.UnlockBuffer(-1);
86 CPPUNIT_ASSERT(strcmp(crashesR5.String(), "") == 0);
87 #endif
91 CppUnit::Test *StringAccessTest::suite(void)
93 typedef CppUnit::TestCaller<StringAccessTest>
94 StringAccessTestCaller;
96 return(new StringAccessTestCaller("BString::Access Test", &StringAccessTest::PerformTest));