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 --- lib/common/Test.h | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'lib/common/Test.h') 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 -- cgit v1.2.3