1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/textfile/textfile.cpp
3 // Purpose:     wxTextFile unit test
4 // Author:      Vadim Zeitlin
5 // Created:     2006-03-31
6 // Copyright:   (c) 2006 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12 
13 #include "testprec.h"
14 
15 
16 #if wxUSE_TEXTFILE
17 
18 #ifndef WX_PRECOMP
19 #endif // WX_PRECOMP
20 
21 #include "wx/ffile.h"
22 #include "wx/textfile.h"
23 
24 #ifdef __VISUALC__
25     #define unlink _unlink
26 #endif
27 
28 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31 
32 class TextFileTestCase : public CppUnit::TestCase
33 {
34 public:
TextFileTestCase()35     TextFileTestCase()
36     {
37         srand((unsigned)time(NULL));
38     }
39 
tearDown()40     virtual void tearDown() wxOVERRIDE { unlink(GetTestFileName()); }
41 
42 private:
43     CPPUNIT_TEST_SUITE( TextFileTestCase );
44         CPPUNIT_TEST( ReadEmpty );
45         CPPUNIT_TEST( ReadDOS );
46         CPPUNIT_TEST( ReadDOSLast );
47         CPPUNIT_TEST( ReadUnix );
48         CPPUNIT_TEST( ReadUnixLast );
49         CPPUNIT_TEST( ReadMac );
50         CPPUNIT_TEST( ReadMacLast );
51         CPPUNIT_TEST( ReadMixed );
52         CPPUNIT_TEST( ReadMixedWithFuzzing );
53         CPPUNIT_TEST( ReadCRCRLF );
54 #if wxUSE_UNICODE
55         CPPUNIT_TEST( ReadUTF8 );
56         CPPUNIT_TEST( ReadUTF16 );
57 #endif // wxUSE_UNICODE
58         CPPUNIT_TEST( ReadBig );
59     CPPUNIT_TEST_SUITE_END();
60 
61     void ReadEmpty();
62     void ReadDOS();
63     void ReadDOSLast();
64     void ReadUnix();
65     void ReadUnixLast();
66     void ReadMac();
67     void ReadMacLast();
68     void ReadMixed();
69     void ReadMixedWithFuzzing();
70     void ReadCRCRLF();
71 #if wxUSE_UNICODE
72     void ReadUTF8();
73     void ReadUTF16();
74 #endif // wxUSE_UNICODE
75     void ReadBig();
76 
77     // return the name of the test file we use
GetTestFileName()78     static const char *GetTestFileName() { return "textfiletest.txt"; }
79 
80     // create the test file with the given contents
CreateTestFile(const char * contents)81     static void CreateTestFile(const char *contents)
82     {
83         CreateTestFile(strlen(contents), contents);
84     }
85 
86     // create the test file with the given contents (version must be used if
87     // contents contains NULs)
88     static void CreateTestFile(size_t len, const char *contents);
89 
90 
91     wxDECLARE_NO_COPY_CLASS(TextFileTestCase);
92 };
93 
94 // register in the unnamed registry so that these tests are run by default
95 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
96 
97 // also include in its own registry so that these tests can be run alone
98 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
99 
CreateTestFile(size_t len,const char * contents)100 void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
101 {
102     FILE *f = fopen(GetTestFileName(), "wb");
103     CPPUNIT_ASSERT( f );
104 
105     CPPUNIT_ASSERT_EQUAL( len, fwrite(contents, 1, len, f) );
106     CPPUNIT_ASSERT_EQUAL( 0, fclose(f) );
107 }
108 
ReadEmpty()109 void TextFileTestCase::ReadEmpty()
110 {
111     CreateTestFile("");
112 
113     wxTextFile f;
114     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
115 
116     CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
117     CPPUNIT_ASSERT( f.Eof() );
118     CPPUNIT_ASSERT_EQUAL( "", f.GetFirstLine() );
119     CPPUNIT_ASSERT_EQUAL( "", f.GetLastLine() );
120 }
121 
ReadDOS()122 void TextFileTestCase::ReadDOS()
123 {
124     CreateTestFile("foo\r\nbar\r\nbaz");
125 
126     wxTextFile f;
127     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
128 
129     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
130     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
131     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
132     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
133     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
134 }
135 
ReadDOSLast()136 void TextFileTestCase::ReadDOSLast()
137 {
138     CreateTestFile("foo\r\n");
139 
140     wxTextFile f;
141     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
142 
143     CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
144     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
145     CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
146 }
147 
ReadUnix()148 void TextFileTestCase::ReadUnix()
149 {
150     CreateTestFile("foo\nbar\nbaz");
151 
152     wxTextFile f;
153     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
154 
155     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
156     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
157     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
158     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
159     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
160 }
161 
ReadUnixLast()162 void TextFileTestCase::ReadUnixLast()
163 {
164     CreateTestFile("foo\n");
165 
166     wxTextFile f;
167     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
168 
169     CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
170     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
171     CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
172 }
173 
ReadMac()174 void TextFileTestCase::ReadMac()
175 {
176     CreateTestFile("foo\rbar\r\rbaz");
177 
178     wxTextFile f;
179     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
180 
181     CPPUNIT_ASSERT_EQUAL( (size_t)4, f.GetLineCount() );
182     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
183     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(1) );
184     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(2) );
185     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(3) );
186     CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f.GetLine(0) );
187     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
188     CPPUNIT_ASSERT_EQUAL( wxString(wxT("")), f.GetLine(2) );
189     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
190 }
191 
ReadMacLast()192 void TextFileTestCase::ReadMacLast()
193 {
194     CreateTestFile("foo\r");
195 
196     wxTextFile f;
197     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
198 
199     CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
200     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
201     CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
202 }
203 
ReadMixed()204 void TextFileTestCase::ReadMixed()
205 {
206     CreateTestFile("foo\rbar\r\nbaz\n");
207 
208     wxTextFile f;
209     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
210 
211     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
212     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
213     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
214     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
215     CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f.GetFirstLine() );
216     CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
217     CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
218 }
219 
ReadMixedWithFuzzing()220 void TextFileTestCase::ReadMixedWithFuzzing()
221 {
222     for ( int iteration = 0; iteration < 100; iteration++)
223     {
224         // Create a random buffer with lots of newlines. This is intended to catch
225         // bad parsing in unexpected situations such as the one from ReadCRCRLF()
226         // (which is so common it deserves a test of its own).
227         static const char CHOICES[] = {'\r', '\n', 'X'};
228 
229         const size_t BUF_LEN = 100;
230         char data[BUF_LEN + 1];
231         data[0] = 'X';
232         data[BUF_LEN] = '\0';
233         unsigned linesCnt = 0;
234         for ( size_t i = 1; i < BUF_LEN; i++ )
235         {
236             char ch = CHOICES[rand() % WXSIZEOF(CHOICES)];
237             data[i] = ch;
238             if ( ch == '\r' || (ch == '\n' && data[i-1] != '\r') )
239                 linesCnt++;
240         }
241         if (data[BUF_LEN-1] != '\r' && data[BUF_LEN-1] != '\n')
242             linesCnt++; // last line was unterminated
243 
244         CreateTestFile(data);
245 
246         wxTextFile f;
247         CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
248         CPPUNIT_ASSERT_EQUAL( (size_t)linesCnt, f.GetLineCount() );
249     }
250 }
251 
ReadCRCRLF()252 void TextFileTestCase::ReadCRCRLF()
253 {
254     // Notepad may create files with CRCRLF line endings (see
255     // https://stackoverflow.com/questions/6998506/text-file-with-0d-0d-0a-line-breaks).
256     // Older versions of wx would loose all data when reading such files.
257     // Test that the data are read, but don't worry about empty lines in between or
258     // line endings. Also include a longer streak of CRs, because they can
259     // happen as well.
260     CreateTestFile("foo\r\r\nbar\r\r\r\nbaz\r\r\n");
261 
262     wxTextFile f;
263     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
264 
265     wxString all;
266     for ( wxString str = f.GetFirstLine(); !f.Eof(); str = f.GetNextLine() )
267         all += str;
268 
269     CPPUNIT_ASSERT_EQUAL( "foobarbaz", all );
270 }
271 
272 #if wxUSE_UNICODE
273 
ReadUTF8()274 void TextFileTestCase::ReadUTF8()
275 {
276     CreateTestFile("\xd0\x9f\n"
277                    "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
278 
279     wxTextFile f;
280     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8) );
281 
282     CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
283     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
284     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
285 #ifdef wxHAVE_U_ESCAPE
286     CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
287     CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
288                           f.GetLastLine() );
289 #endif // wxHAVE_U_ESCAPE
290 }
291 
ReadUTF16()292 void TextFileTestCase::ReadUTF16()
293 {
294     CreateTestFile(16,
295                    "\x1f\x04\x0d\x00\x0a\x00"
296                    "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
297 
298     wxTextFile f;
299     wxMBConvUTF16LE conv;
300     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
301 
302     CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
303     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
304     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
305 
306 #ifdef wxHAVE_U_ESCAPE
307     CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
308     CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
309                           f.GetLastLine() );
310 #endif // wxHAVE_U_ESCAPE
311 }
312 
313 #endif // wxUSE_UNICODE
314 
ReadBig()315 void TextFileTestCase::ReadBig()
316 {
317     static const size_t NUM_LINES = 10000;
318 
319     {
320         wxFFile f(GetTestFileName(), "w");
321         for ( size_t n = 0; n < NUM_LINES; n++ )
322         {
323             fprintf(f.fp(), "Line %lu\n", (unsigned long)n + 1);
324         }
325     }
326 
327     wxTextFile f;
328     CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
329 
330     CPPUNIT_ASSERT_EQUAL( NUM_LINES, f.GetLineCount() );
331     CPPUNIT_ASSERT_EQUAL( wxString("Line 1"), f[0] );
332     CPPUNIT_ASSERT_EQUAL( wxString("Line 999"), f[998] );
333     CPPUNIT_ASSERT_EQUAL( wxString("Line 1000"), f[999] );
334     CPPUNIT_ASSERT_EQUAL( wxString::Format("Line %lu", (unsigned long)NUM_LINES),
335                           f[NUM_LINES - 1] );
336 }
337 
338 #ifdef __LINUX__
339 
340 // Check if using wxTextFile with special files, whose reported size doesn't
341 // correspond to the real amount of data in them, works.
342 TEST_CASE("wxTextFile::Special", "[textfile][linux][special-file]")
343 {
344     // LXC containers don't (always) populate /proc and /sys, so skip these
345     // tests there.
346     if ( IsRunningInLXC() )
347         return;
348 
349     SECTION("/proc")
350     {
351         wxTextFile f;
352         CHECK( f.Open("/proc/cpuinfo") );
353         CHECK( f.GetLineCount() > 1 );
354     }
355 
356     SECTION("/sys")
357     {
358         wxTextFile f;
359         CHECK( f.Open("/sys/power/state") );
360         REQUIRE( f.GetLineCount() == 1 );
361         INFO( "/sys/power/state contains \"" << f[0] << "\"" );
362         CHECK( (f[0].find("mem") != wxString::npos || f[0].find("disk") != wxString::npos) );
363     }
364 }
365 
366 #endif // __LINUX__
367 
368 #endif // wxUSE_TEXTFILE
369