filekind.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/filetype/filetype.cpp
  3. // Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
  4. // Author: Mike Wetherell
  5. // Copyright: (c) 2005 Mike Wetherell
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. #pragma hdrstop
  11. #endif
  12. // for all others, include the necessary headers
  13. #ifndef WX_PRECOMP
  14. #include "wx/wx.h"
  15. #endif
  16. #if wxUSE_STREAMS
  17. #ifdef __UNIX__
  18. #include <sys/socket.h>
  19. #endif
  20. #include "wx/file.h"
  21. #include "wx/ffile.h"
  22. #include "wx/wfstream.h"
  23. #include "wx/filename.h"
  24. #include "wx/socket.h"
  25. #include "wx/sckstrm.h"
  26. #include "wx/mstream.h"
  27. #ifdef __VISUALC__
  28. #define isatty _isatty
  29. #define fdopen _fdopen
  30. #define fileno _fileno
  31. #endif
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // The test case
  34. class FileKindTestCase : public CppUnit::TestCase
  35. {
  36. CPPUNIT_TEST_SUITE(FileKindTestCase);
  37. CPPUNIT_TEST(File);
  38. #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
  39. CPPUNIT_TEST(Pipe);
  40. #endif
  41. #if defined __UNIX__
  42. CPPUNIT_TEST(Socket);
  43. #endif
  44. CPPUNIT_TEST(Stdin);
  45. CPPUNIT_TEST(MemoryStream);
  46. #if wxUSE_SOCKETS
  47. CPPUNIT_TEST(SocketStream);
  48. #endif
  49. CPPUNIT_TEST_SUITE_END();
  50. void File();
  51. void Pipe();
  52. void Socket();
  53. void Stdin();
  54. void MemoryStream();
  55. #if wxUSE_SOCKETS
  56. void SocketStream();
  57. #endif
  58. void TestFILE(wxFFile& file, bool expected);
  59. void TestFd(wxFile& file, bool expected);
  60. };
  61. // test a wxFFile and wxFFileInput/OutputStreams of a known type
  62. //
  63. void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
  64. {
  65. CPPUNIT_ASSERT(file.IsOpened());
  66. CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
  67. CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
  68. wxFFileInputStream inStream(file);
  69. CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
  70. wxFFileOutputStream outStream(file);
  71. CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
  72. }
  73. // test a wxFile and wxFileInput/OutputStreams of a known type
  74. //
  75. void FileKindTestCase::TestFd(wxFile& file, bool expected)
  76. {
  77. CPPUNIT_ASSERT(file.IsOpened());
  78. CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
  79. CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
  80. wxFileInputStream inStream(file);
  81. CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
  82. wxFileOutputStream outStream(file);
  83. CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
  84. }
  85. struct TempFile
  86. {
  87. ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
  88. wxString m_name;
  89. };
  90. // test with an ordinary file
  91. //
  92. void FileKindTestCase::File()
  93. {
  94. TempFile tmp; // put first
  95. wxFile file;
  96. tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file);
  97. TestFd(file, true);
  98. file.Close();
  99. wxFFile ffile(tmp.m_name);
  100. TestFILE(ffile, true);
  101. }
  102. // test with a pipe
  103. //
  104. #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
  105. void FileKindTestCase::Pipe()
  106. {
  107. int afd[2];
  108. int rc;
  109. #ifdef __UNIX__
  110. rc = pipe(afd);
  111. #else
  112. rc = _pipe(afd, 256, O_BINARY);
  113. #endif
  114. CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to create pipe", 0, rc);
  115. wxFile file0(afd[0]);
  116. wxFile file1(afd[1]);
  117. TestFd(file0, false);
  118. file0.Detach();
  119. wxFFile ffile(fdopen(afd[0], "r"));
  120. TestFILE(ffile, false);
  121. }
  122. #endif
  123. // test with a socket
  124. //
  125. #if defined __UNIX__
  126. void FileKindTestCase::Socket()
  127. {
  128. int s = socket(PF_INET, SOCK_STREAM, 0);
  129. wxFile file(s);
  130. TestFd(file, false);
  131. file.Detach();
  132. wxFFile ffile(fdopen(s, "r"));
  133. TestFILE(ffile, false);
  134. }
  135. #endif
  136. // Socket streams should be non-seekable
  137. //
  138. #if wxUSE_SOCKETS
  139. void FileKindTestCase::SocketStream()
  140. {
  141. wxSocketClient client;
  142. wxSocketInputStream inStream(client);
  143. CPPUNIT_ASSERT(!inStream.IsSeekable());
  144. wxSocketOutputStream outStream(client);
  145. CPPUNIT_ASSERT(!outStream.IsSeekable());
  146. wxBufferedInputStream nonSeekableBufferedInput(inStream);
  147. CPPUNIT_ASSERT(!nonSeekableBufferedInput.IsSeekable());
  148. wxBufferedOutputStream nonSeekableBufferedOutput(outStream);
  149. CPPUNIT_ASSERT(!nonSeekableBufferedOutput.IsSeekable());
  150. }
  151. #endif
  152. // Memory streams should be seekable
  153. //
  154. void FileKindTestCase::MemoryStream()
  155. {
  156. char buf[20];
  157. wxMemoryInputStream inStream(buf, sizeof(buf));
  158. CPPUNIT_ASSERT(inStream.IsSeekable());
  159. wxMemoryOutputStream outStream(buf, sizeof(buf));
  160. CPPUNIT_ASSERT(outStream.IsSeekable());
  161. wxBufferedInputStream seekableBufferedInput(inStream);
  162. CPPUNIT_ASSERT(seekableBufferedInput.IsSeekable());
  163. wxBufferedOutputStream seekableBufferedOutput(outStream);
  164. CPPUNIT_ASSERT(seekableBufferedOutput.IsSeekable());
  165. }
  166. // Stdin will usually be a terminal, if so then test it
  167. //
  168. void FileKindTestCase::Stdin()
  169. {
  170. if (isatty(0))
  171. CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
  172. if (isatty(fileno(stdin)))
  173. CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
  174. }
  175. // register in the unnamed registry so that these tests are run by default
  176. CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
  177. // also include in its own registry so that these tests can be run alone
  178. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
  179. #endif // wxUSE_STREAMS