2005-02-11 07:39:03 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/filetype/filetype.cpp
|
2005-02-12 16:53:51 -05:00
|
|
|
// Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
|
2005-02-11 07:39:03 -05:00
|
|
|
// Author: Mike Wetherell
|
|
|
|
// Copyright: (c) 2005 Mike Wetherell
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2005-02-11 07:39:03 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2009-06-06 19:20:12 -04:00
|
|
|
#if wxUSE_STREAMS
|
|
|
|
|
2005-02-11 07:39:03 -05:00
|
|
|
#ifdef __UNIX__
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/file.h"
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
#include "wx/wfstream.h"
|
|
|
|
#include "wx/filename.h"
|
|
|
|
#include "wx/socket.h"
|
|
|
|
#include "wx/sckstrm.h"
|
|
|
|
#include "wx/mstream.h"
|
|
|
|
|
2009-06-06 19:20:12 -04:00
|
|
|
#ifdef __VISUALC__
|
|
|
|
#define isatty _isatty
|
|
|
|
#define fdopen _fdopen
|
|
|
|
#define fileno _fileno
|
|
|
|
#endif
|
2005-02-11 07:39:03 -05:00
|
|
|
|
2017-11-05 11:28:24 -05:00
|
|
|
#include "testfile.h"
|
|
|
|
|
2005-02-11 07:39:03 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The test case
|
|
|
|
|
2005-02-12 16:53:51 -05:00
|
|
|
class FileKindTestCase : public CppUnit::TestCase
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_TEST_SUITE(FileKindTestCase);
|
2005-02-11 07:39:03 -05:00
|
|
|
CPPUNIT_TEST(File);
|
|
|
|
#if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
|
|
|
|
CPPUNIT_TEST(Pipe);
|
|
|
|
#endif
|
|
|
|
#if defined __UNIX__
|
|
|
|
CPPUNIT_TEST(Socket);
|
|
|
|
#endif
|
|
|
|
CPPUNIT_TEST(Stdin);
|
|
|
|
CPPUNIT_TEST(MemoryStream);
|
2005-04-25 06:32:55 -04:00
|
|
|
#if wxUSE_SOCKETS
|
2005-02-11 07:39:03 -05:00
|
|
|
CPPUNIT_TEST(SocketStream);
|
2005-04-25 06:32:55 -04:00
|
|
|
#endif
|
2005-02-11 07:39:03 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void File();
|
|
|
|
void Pipe();
|
|
|
|
void Socket();
|
|
|
|
void Stdin();
|
|
|
|
void MemoryStream();
|
2005-04-25 06:32:55 -04:00
|
|
|
#if wxUSE_SOCKETS
|
2005-02-11 07:39:03 -05:00
|
|
|
void SocketStream();
|
2005-04-25 06:32:55 -04:00
|
|
|
#endif
|
2005-02-11 07:39:03 -05:00
|
|
|
|
|
|
|
void TestFILE(wxFFile& file, bool expected);
|
|
|
|
void TestFd(wxFile& file, bool expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
// test a wxFFile and wxFFileInput/OutputStreams of a known type
|
2018-07-29 05:09:17 -04:00
|
|
|
//
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT(file.IsOpened());
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
|
|
|
|
CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
|
2005-02-11 07:39:03 -05:00
|
|
|
|
|
|
|
wxFFileInputStream inStream(file);
|
|
|
|
CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
|
|
|
|
|
|
|
|
wxFFileOutputStream outStream(file);
|
|
|
|
CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test a wxFile and wxFileInput/OutputStreams of a known type
|
|
|
|
//
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::TestFd(wxFile& file, bool expected)
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT(file.IsOpened());
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
|
|
|
|
CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
|
2005-02-11 07:39:03 -05:00
|
|
|
|
|
|
|
wxFileInputStream inStream(file);
|
|
|
|
CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
|
|
|
|
|
|
|
|
wxFileOutputStream outStream(file);
|
|
|
|
CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with an ordinary file
|
|
|
|
//
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::File()
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
TempFile tmp; // put first
|
|
|
|
wxFile file;
|
2017-11-05 11:28:24 -05:00
|
|
|
tmp.Assign(wxFileName::CreateTempFileName(wxT("wxft"), &file));
|
2005-02-11 07:39:03 -05:00
|
|
|
TestFd(file, true);
|
|
|
|
file.Close();
|
|
|
|
|
2017-11-05 11:28:24 -05:00
|
|
|
wxFFile ffile(tmp.GetName());
|
2005-02-11 07:39:03 -05:00
|
|
|
TestFILE(ffile, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with a pipe
|
|
|
|
//
|
|
|
|
#if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::Pipe()
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
int afd[2];
|
2012-10-20 10:49:13 -04:00
|
|
|
int rc;
|
2005-02-11 07:39:03 -05:00
|
|
|
#ifdef __UNIX__
|
2012-10-20 10:49:13 -04:00
|
|
|
rc = pipe(afd);
|
2005-02-11 07:39:03 -05:00
|
|
|
#else
|
2012-10-20 10:49:13 -04:00
|
|
|
rc = _pipe(afd, 256, O_BINARY);
|
2005-02-11 07:39:03 -05:00
|
|
|
#endif
|
2012-10-20 10:49:13 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to create pipe", 0, rc);
|
2005-02-11 07:39:03 -05:00
|
|
|
|
|
|
|
wxFile file0(afd[0]);
|
|
|
|
wxFile file1(afd[1]);
|
|
|
|
TestFd(file0, false);
|
|
|
|
file0.Detach();
|
|
|
|
|
|
|
|
wxFFile ffile(fdopen(afd[0], "r"));
|
|
|
|
TestFILE(ffile, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// test with a socket
|
|
|
|
//
|
|
|
|
#if defined __UNIX__
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::Socket()
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
int s = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
wxFile file(s);
|
|
|
|
TestFd(file, false);
|
|
|
|
file.Detach();
|
|
|
|
|
|
|
|
wxFFile ffile(fdopen(s, "r"));
|
|
|
|
TestFILE(ffile, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Socket streams should be non-seekable
|
|
|
|
//
|
|
|
|
#if wxUSE_SOCKETS
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::SocketStream()
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
wxSocketClient client;
|
|
|
|
wxSocketInputStream inStream(client);
|
|
|
|
CPPUNIT_ASSERT(!inStream.IsSeekable());
|
|
|
|
wxSocketOutputStream outStream(client);
|
|
|
|
CPPUNIT_ASSERT(!outStream.IsSeekable());
|
|
|
|
|
|
|
|
wxBufferedInputStream nonSeekableBufferedInput(inStream);
|
|
|
|
CPPUNIT_ASSERT(!nonSeekableBufferedInput.IsSeekable());
|
|
|
|
wxBufferedOutputStream nonSeekableBufferedOutput(outStream);
|
|
|
|
CPPUNIT_ASSERT(!nonSeekableBufferedOutput.IsSeekable());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Memory streams should be seekable
|
|
|
|
//
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::MemoryStream()
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
wxMemoryInputStream inStream(buf, sizeof(buf));
|
|
|
|
CPPUNIT_ASSERT(inStream.IsSeekable());
|
|
|
|
wxMemoryOutputStream outStream(buf, sizeof(buf));
|
|
|
|
CPPUNIT_ASSERT(outStream.IsSeekable());
|
|
|
|
|
|
|
|
wxBufferedInputStream seekableBufferedInput(inStream);
|
|
|
|
CPPUNIT_ASSERT(seekableBufferedInput.IsSeekable());
|
|
|
|
wxBufferedOutputStream seekableBufferedOutput(outStream);
|
|
|
|
CPPUNIT_ASSERT(seekableBufferedOutput.IsSeekable());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stdin will usually be a terminal, if so then test it
|
2018-07-29 05:09:17 -04:00
|
|
|
//
|
2005-02-12 16:53:51 -05:00
|
|
|
void FileKindTestCase::Stdin()
|
2005-02-11 07:39:03 -05:00
|
|
|
{
|
|
|
|
if (isatty(0))
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
|
2005-02-11 07:39:03 -05:00
|
|
|
if (isatty(fileno(stdin)))
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
|
2005-02-11 07:39:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
|
2005-02-11 07:39:03 -05:00
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2005-02-12 16:53:51 -05:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
|
2005-02-11 07:39:03 -05:00
|
|
|
|
|
|
|
#endif // wxUSE_STREAMS
|