From 62fcbae63de111cd2191cce3af4158af819914ee Mon Sep 17 00:00:00 2001 From: Martin Ebourne Date: Mon, 12 Dec 2005 23:56:44 +0000 Subject: Merged 210:218 from chris/win32/merge/07-win32-fixes to trunk --- infrastructure/buildenv-testmain-template.cpp | 17 +++- infrastructure/makebuildenv.pl | 4 +- lib/common/DebugAssertFailed.cpp | 7 +- lib/common/DebugPrintf.cpp | 7 +- lib/common/FileStream.cpp | 4 +- lib/common/Test.h | 112 +++++++++++++++++++++++ modules.txt | 8 +- runtest.pl | 2 +- test/common/testcommon.cpp | 123 ++++++++++++++++++-------- test/crypto/testcrypto.cpp | 2 +- 10 files changed, 237 insertions(+), 49 deletions(-) diff --git a/infrastructure/buildenv-testmain-template.cpp b/infrastructure/buildenv-testmain-template.cpp index 36cc4da0..f95f01e6 100644 --- a/infrastructure/buildenv-testmain-template.cpp +++ b/infrastructure/buildenv-testmain-template.cpp @@ -20,11 +20,16 @@ #include #include #include -#include #include #include #include +#ifdef WIN32 + #include "emu.h" +#else + #include +#endif + #include "MemLeakFindOn.h" int test(int argc, const char *argv[]); @@ -39,6 +44,14 @@ int failures = 0; int filedes_open_at_beginning = -1; +#ifdef WIN32 + +// any way to check for open file descriptors on Win32? +inline int count_filedes() { return 0; } +inline bool checkfilesleftopen() { return false; } + +#else // !WIN32 + int count_filedes() { int c = 0; @@ -71,6 +84,8 @@ bool checkfilesleftopen() return filedes_open_at_beginning != count_filedes(); } +#endif + int main(int argc, const char *argv[]) { // Start memory leak testing diff --git a/infrastructure/makebuildenv.pl b/infrastructure/makebuildenv.pl index 64c73eb0..ee0e6506 100755 --- a/infrastructure/makebuildenv.pl +++ b/infrastructure/makebuildenv.pl @@ -408,9 +408,9 @@ __E } writetestfile("$mod/_t", - './test${platform_exe_ext} $1 $2 $3 $4 $5', $mod); + './test' . $platform_exe_ext . '$1 $2 $3 $4 $5', $mod); writetestfile("$mod/_t-gdb", - 'gdb ./test${platform_exe_ext}', $mod); + 'gdb ./test ' . $platform_exe_ext, $mod); } diff --git a/lib/common/DebugAssertFailed.cpp b/lib/common/DebugAssertFailed.cpp index 2acf4602..0c5dc548 100644 --- a/lib/common/DebugAssertFailed.cpp +++ b/lib/common/DebugAssertFailed.cpp @@ -12,7 +12,12 @@ #include "Box.h" #include -#include + +#ifdef WIN32 + #include "emu.h" +#else + #include +#endif #include "MemLeakFindOn.h" diff --git a/lib/common/DebugPrintf.cpp b/lib/common/DebugPrintf.cpp index d07604b7..8d75f458 100644 --- a/lib/common/DebugPrintf.cpp +++ b/lib/common/DebugPrintf.cpp @@ -13,7 +13,12 @@ #include #include -#include + +#ifdef WIN32 + #include "emu.h" +#else + #include +#endif #include "MemLeakFindOn.h" diff --git a/lib/common/FileStream.cpp b/lib/common/FileStream.cpp index 9f5460bb..917dc9c9 100644 --- a/lib/common/FileStream.cpp +++ b/lib/common/FileStream.cpp @@ -197,11 +197,9 @@ void FileStream::Write(const void *pBuffer, int NBytes) if ( (res == 0) || (numBytesWritten != NBytes)) { - DWORD err = GetLastError(); + // DWORD err = GetLastError(); THROW_EXCEPTION(CommonException, OSFileWriteError) } - - #else if(::write(mOSFileHandle, pBuffer, NBytes) != NBytes) { diff --git a/lib/common/Test.h b/lib/common/Test.h index bd69cd5a..472f6342 100644 --- a/lib/common/Test.h +++ b/lib/common/Test.h @@ -108,6 +108,116 @@ inline int LaunchServer(const char *CommandLine, const char *pidFile) return pid; } +#ifdef WIN32 + +#include "WinNamedPipeStream.h" +#include "IOStreamGetLine.h" +#include "BoxPortsAndFiles.h" + +bool SendCommands(const std::string& rCmd) +{ + WinNamedPipeStream connection; + + try + { + connection.Connect(BOX_NAMED_PIPE_NAME); + } + catch(...) + { + printf("Failed to connect to daemon control socket.\n"); + return false; + } + + // For receiving data + IOStreamGetLine getLine(connection); + + // Wait for the configuration summary + std::string configSummary; + if(!getLine.GetLine(configSummary)) + { + printf("Failed to receive configuration summary from daemon\n"); + return false; + } + + // Was the connection rejected by the server? + if(getLine.IsEOF()) + { + printf("Server rejected the connection.\n"); + return false; + } + + // Decode it + int autoBackup, updateStoreInterval, minimumFileAge, maxUploadWait; + if(::sscanf(configSummary.c_str(), "bbackupd: %d %d %d %d", + &autoBackup, &updateStoreInterval, + &minimumFileAge, &maxUploadWait) != 4) + { + printf("Config summary didn't decode\n"); + return false; + } + + std::string cmds; + bool expectResponse; + + if (rCmd != "") + { + cmds = rCmd; + cmds += "\nquit\n"; + expectResponse = true; + } + else + { + cmds = "quit\n"; + expectResponse = false; + } + + connection.Write(cmds.c_str(), cmds.size()); + + // Read the response + std::string line; + bool statusOk = !expectResponse; + + while (expectResponse && !getLine.IsEOF() && getLine.GetLine(line)) + { + // Is this an OK or error line? + if (line == "ok") + { + statusOk = true; + } + else if (line == "error") + { + printf("ERROR (%s)\n", rCmd.c_str()); + break; + } + else + { + printf("WARNING: Unexpected response to command '%s': " + "%s", rCmd.c_str(), line.c_str()); + } + } + + return statusOk; +} + +inline bool ServerIsAlive() +{ + return SendCommands(""); +} + +inline bool HUPServer(int pid) +{ + return SendCommands("reload"); +} + +inline bool KillServer(int pid) +{ + TEST_THAT(SendCommands("terminate")); + ::sleep(1); + return !ServerIsAlive(); +} + +#else // !WIN32 + inline bool ServerIsAlive(int pid) { if(pid == 0) return false; @@ -129,6 +239,8 @@ inline bool KillServer(int pid) return !ServerIsAlive(pid); } +#endif // WIN32 + inline void TestRemoteProcessMemLeaks(const char *filename) { #ifdef BOX_MEMORY_LEAK_TESTING diff --git a/modules.txt b/modules.txt index 7b9f8ce7..de00f08b 100644 --- a/modules.txt +++ b/modules.txt @@ -18,12 +18,12 @@ lib/crypto lib/server lib/win32 lib/server lib/compress -test/common -test/crypto lib/crypto -test/compress lib/compress -test/basicserver lib/server +test/common lib/win32 +test/crypto lib/crypto lib/win32 +test/compress lib/compress lib/win32 OMIT:mingw32 +test/basicserver lib/server lib/win32 OMIT:CYGWIN test/raidfile lib/raidfile END-OMIT diff --git a/runtest.pl b/runtest.pl index 4cda8554..4430128f 100755 --- a/runtest.pl +++ b/runtest.pl @@ -38,7 +38,7 @@ else next if m/\AEND-OMIT/; if(m/\AOMIT:(.+)/) { - if($1 eq $build_os) + if($1 eq $build_os or $1 eq $target_os) { while() { diff --git a/test/common/testcommon.cpp b/test/common/testcommon.cpp index 546228bc..910b6814 100644 --- a/test/common/testcommon.cpp +++ b/test/common/testcommon.cpp @@ -181,7 +181,8 @@ int test(int argc, const char *argv[]) // First, test the FdGetLine class -- rather important this works! { - FileHandleGuard file("testfiles/fdgetlinetest.txt"); + FileHandleGuard file("testfiles" + DIRECTORY_SEPARATOR "fdgetlinetest.txt"); FdGetLine getline(file); int l = 0; @@ -198,8 +199,10 @@ int test(int argc, const char *argv[]) } // and again without pre-processing { - FileHandleGuard file("testfiles/fdgetlinetest.txt"); - FILE *file2 = fopen("testfiles/fdgetlinetest.txt", "r"); + FileHandleGuard file("testfiles" + DIRECTORY_SEPARATOR "fdgetlinetest.txt"); + FILE *file2 = fopen("testfiles" DIRECTORY_SEPARATOR + "fdgetlinetest.txt", "r"); TEST_THAT_ABORTONFAIL(file2 != 0); FdGetLine getline(file); char ll[512]; @@ -227,7 +230,8 @@ int test(int argc, const char *argv[]) // Then the IOStream version of get line, seeing as we're here... { - FileStream file("testfiles/fdgetlinetest.txt", O_RDONLY); + FileStream file("testfiles" DIRECTORY_SEPARATOR + "fdgetlinetest.txt", O_RDONLY); IOStreamGetLine getline(file); int l = 0; @@ -247,10 +251,12 @@ int test(int argc, const char *argv[]) } // and again without pre-processing { - FileStream file("testfiles/fdgetlinetest.txt", O_RDONLY); + FileStream file("testfiles" DIRECTORY_SEPARATOR + "fdgetlinetest.txt", O_RDONLY); IOStreamGetLine getline(file); - FILE *file2 = fopen("testfiles/fdgetlinetest.txt", "r"); + FILE *file2 = fopen("testfiles" DIRECTORY_SEPARATOR + "fdgetlinetest.txt", "r"); TEST_THAT_ABORTONFAIL(file2 != 0); char ll[512]; @@ -281,13 +287,20 @@ int test(int argc, const char *argv[]) // Doesn't exist { std::string errMsg; - TEST_CHECK_THROWS(std::auto_ptr pconfig(Configuration::LoadAndVerify("testfiles/DOESNTEXIST", &verify, errMsg)), CommonException, OSFileOpenError); + TEST_CHECK_THROWS(std::auto_ptr pconfig( + Configuration::LoadAndVerify( + "testfiles" DIRECTORY_SEPARATOR "DOESNTEXIST", + &verify, errMsg)), + CommonException, OSFileOpenError); } // Basic configuration test { std::string errMsg; - std::auto_ptr pconfig(Configuration::LoadAndVerify("testfiles/config1.txt", &verify, errMsg)); + std::auto_ptr pconfig( + Configuration::LoadAndVerify( + "testfiles" DIRECTORY_SEPARATOR "config1.txt", + &verify, errMsg)); if(!errMsg.empty()) { printf("UNEXPECTED error msg is:\n------\n%s------\n", errMsg.c_str()); @@ -332,22 +345,38 @@ int test(int argc, const char *argv[]) static const char *file[] = { - "testfiles/config2.txt", // Value missing from root - "testfiles/config3.txt", // Unexpected { - "testfiles/config4.txt", // Missing } - "testfiles/config5.txt", // { expected, but wasn't there - "testfiles/config6.txt", // Duplicate key - "testfiles/config7.txt", // Invalid key (no name) - "testfiles/config8.txt", // Not all sub blocks terminated - "testfiles/config9.txt", // Not valid integer - "testfiles/config9b.txt", // Not valid integer - "testfiles/config9c.txt", // Not valid integer - "testfiles/config9d.txt", // Not valid integer - "testfiles/config10.txt", // Missing key (in subblock) - "testfiles/config11.txt", // Unknown key - "testfiles/config12.txt", // Missing block - "testfiles/config13.txt", // Subconfig (wildcarded) should exist, but missing (ie nothing present) - "testfiles/config16.txt", // bad boolean value + "testfiles" DIRECTORY_SEPARATOR "config2.txt", + // Value missing from root + "testfiles" DIRECTORY_SEPARATOR "config3.txt", + // Unexpected { + "testfiles" DIRECTORY_SEPARATOR "config4.txt", + // Missing } + "testfiles" DIRECTORY_SEPARATOR "config5.txt", + // { expected, but wasn't there + "testfiles" DIRECTORY_SEPARATOR "config6.txt", + // Duplicate key + "testfiles" DIRECTORY_SEPARATOR "config7.txt", + // Invalid key (no name) + "testfiles" DIRECTORY_SEPARATOR "config8.txt", + // Not all sub blocks terminated + "testfiles" DIRECTORY_SEPARATOR "config9.txt", + // Not valid integer + "testfiles" DIRECTORY_SEPARATOR "config9b.txt", + // Not valid integer + "testfiles" DIRECTORY_SEPARATOR "config9c.txt", + // Not valid integer + "testfiles" DIRECTORY_SEPARATOR "config9d.txt", + // Not valid integer + "testfiles" DIRECTORY_SEPARATOR "config10.txt", + // Missing key (in subblock) + "testfiles" DIRECTORY_SEPARATOR "config11.txt", + // Unknown key + "testfiles" DIRECTORY_SEPARATOR "config12.txt", + // Missing block + "testfiles" DIRECTORY_SEPARATOR "config13.txt", + // Subconfig (wildcarded) should exist, but missing (ie nothing present) + "testfiles" DIRECTORY_SEPARATOR "config16.txt", + // bad boolean value 0 }; @@ -364,7 +393,10 @@ int test(int argc, const char *argv[]) // (single value in a multivalue already checked) { std::string errMsg; - std::auto_ptr pconfig(Configuration::LoadAndVerify("testfiles/config14.txt", &verify, errMsg)); + std::auto_ptr pconfig( + Configuration::LoadAndVerify( + "testfiles" DIRECTORY_SEPARATOR "config14.txt", + &verify, errMsg)); TEST_THAT(pconfig.get() != 0); TEST_THAT(errMsg.empty()); TEST_THAT(pconfig->KeyExists("MultiValue")); @@ -378,7 +410,10 @@ int test(int argc, const char *argv[]) // Check boolean values { std::string errMsg; - std::auto_ptr pconfig(Configuration::LoadAndVerify("testfiles/config15.txt", &verify, errMsg)); + std::auto_ptr pconfig( + Configuration::LoadAndVerify( + "testfiles" DIRECTORY_SEPARATOR "config15.txt", + &verify, errMsg)); TEST_THAT(pconfig.get() != 0); TEST_THAT(errMsg.empty()); TEST_THAT(pconfig->GetKeyValueBool("BoolTrue1") == true); @@ -391,32 +426,50 @@ int test(int argc, const char *argv[]) { NamedLock lock1; // Try and get a lock on a name in a directory which doesn't exist - TEST_CHECK_THROWS(lock1.TryAndGetLock("testfiles/non-exist/lock"), CommonException, OSFileError); + TEST_CHECK_THROWS(lock1.TryAndGetLock( + "testfiles" + DIRECTORY_SEPARATOR "non-exist" + DIRECTORY_SEPARATOR "lock"), + CommonException, OSFileError); + // And a more resonable request - TEST_THAT(lock1.TryAndGetLock("testfiles/lock1") == true); + TEST_THAT(lock1.TryAndGetLock( + "testfiles" DIRECTORY_SEPARATOR "lock1") == true); + // Try to lock something using the same lock - TEST_CHECK_THROWS(lock1.TryAndGetLock("testfiles/non-exist/lock2"), CommonException, NamedLockAlreadyLockingSomething); + TEST_CHECK_THROWS( + lock1.TryAndGetLock( + "testfiles" + DIRECTORY_SEPARATOR "non-exist" + DIRECTORY_SEPARATOR "lock2"), + CommonException, NamedLockAlreadyLockingSomething); #if defined(HAVE_FLOCK) || HAVE_DECL_O_EXLOCK // And again on that name NamedLock lock2; - TEST_THAT(lock2.TryAndGetLock("testfiles/lock1") == false); + TEST_THAT(lock2.TryAndGetLock( + "testfiles" DIRECTORY_SEPARATOR "lock1") == false); #endif } { // Check that it unlocked when it went out of scope NamedLock lock3; - TEST_THAT(lock3.TryAndGetLock("testfiles/lock1") == true); + TEST_THAT(lock3.TryAndGetLock( + "testfiles" DIRECTORY_SEPARATOR "lock1") == true); } { // And unlocking works NamedLock lock4; - TEST_CHECK_THROWS(lock4.ReleaseLock(), CommonException, NamedLockNotHeld); - TEST_THAT(lock4.TryAndGetLock("testfiles/lock4") == true); + TEST_CHECK_THROWS(lock4.ReleaseLock(), CommonException, + NamedLockNotHeld); + TEST_THAT(lock4.TryAndGetLock( + "testfiles" DIRECTORY_SEPARATOR "lock4") == true); lock4.ReleaseLock(); NamedLock lock5; - TEST_THAT(lock5.TryAndGetLock("testfiles/lock4") == true); + TEST_THAT(lock5.TryAndGetLock( + "testfiles" DIRECTORY_SEPARATOR "lock4") == true); // And can reuse it - TEST_THAT(lock4.TryAndGetLock("testfiles/lock5") == true); + TEST_THAT(lock4.TryAndGetLock( + "testfiles" DIRECTORY_SEPARATOR "lock5") == true); } // Test the ReadGatherStream diff --git a/test/crypto/testcrypto.cpp b/test/crypto/testcrypto.cpp index c312a4a0..6d90e5e7 100644 --- a/test/crypto/testcrypto.cpp +++ b/test/crypto/testcrypto.cpp @@ -43,7 +43,7 @@ void check_random_int(uint32_t max) } } -#define ZERO_BUFFER(x) ::bzero(x, sizeof(x)); +#define ZERO_BUFFER(x) ::memset(x, 0, sizeof(x)); template void test_cipher() -- cgit v1.2.3