def fTestServer( cHTTPServer, cHTTPClient, oCertificateStore, oServerURL, nEndWaitTimeoutInSeconds, f0LogEvents, ): # Can be use to test cHTTPServer with a http:// or https:// URL. if oServerURL.bSecure: oConsole.fPrint( "\xFE\xFE\xFE\xFE Creating a cSSLContext instance for %s... " % oServerURL.sHostname, sPadding="\xFE") oSSLContext = oCertificateStore.foGetServersideSSLContextForHostname( oServerURL.sHostname) oConsole.fOutput( 0x0F0F, repr(oSSLContext._cSSLContext__oPythonSSLContext.get_ca_certs())) oConsole.fPrint("* oSSLContext for ", oServerURL.sHostname, ": ", str(oSSLContext)) else: oSSLContext = None oConsole.fPrint( "\xFE\xFE\xFE\xFE Creating a cHTTPServer instance at %s... " % oServerURL, sPadding="\xFE") oHTTPServer = cHTTPServer(ftxRequestHandler, oServerURL.sHostname, oServerURL.uPort, oSSLContext) if f0LogEvents: f0LogEvents(oHTTPServer, "oHTTPServer") oConsole.fPrint("\xFE\xFE\xFE\xFE Creating a new cHTTPClient instance... ", sPadding="\xFE") oHTTPClient = cHTTPClient(oCertificateStore) if f0LogEvents: f0LogEvents(oHTTPClient, "oHTTPClient") oConsole.fPrint("\xFE\xFE\xFE\xFE Making a first test request to %s... " % oServerURL, sPadding="\xFE") o0Response = oHTTPClient.fo0GetResponseForURL(oServerURL) assert o0Response, \ "No response!?" oConsole.fPrint(repr(o0Response.fsSerialize())) oConsole.fPrint("\xFE\xFE\xFE\xFE Making a second test request to %s... " % oServerURL, sPadding="\xFE") o0Response = oHTTPClient.fo0GetResponseForURL(oServerURL) assert o0Response, \ "No response!?" oConsole.fPrint(repr(o0Response.fsSerialize())) oConsole.fPrint( "\xFE\xFE\xFE\xFE Stopping the cHTTPServer instance at %s... " % oServerURL, sPadding="\xFE") oHTTPServer.fStop() assert oHTTPServer.fbWait(nEndWaitTimeoutInSeconds), \ "cHTTPServer instance did not stop in time" oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping the cHTTPClient instance... ", sPadding="\xFE") oHTTPClient.fStop() assert oHTTPClient.fbWait(nEndWaitTimeoutInSeconds), \ "oHTTPClient instance did not stop in time"
def fDisconnectAndWait(oConnection): fStartTransactionIfPossible(oConnection) oConsole.fOutput("%s: Disconnecting..." % oConnection) oConnection.fDisconnect() fEndTransactionIfPossible(oConnection) oConsole.fOutput("%s: Waiting for connection to terminate..." % oConnection) assert oConnection.fbWait(nTimeoutInSeconds = 1), \ "%s did not disconnect in a reasonable time!?"
def fbInstallAsJITDebugger(asArguments): sBugIdCommandLine = fsCreateBugIdCommandLine([ "--pid=%ld", "--handle-jit-event=%ld", ] + asArguments) oRegistryHiveKey = mRegistry.cRegistryHiveKey( sHiveName=mJITDebuggerRegistry.sRegistryHiveName, sKeyName=mJITDebuggerRegistry.sComandLineKeyPath, ) oConsole.fStatus("* Installing as JIT debugger...") for (sName, sValue) in { "Auto": "1", "Debugger": sBugIdCommandLine }.items(): try: oRegistryHiveKey.foSetNamedValue(sValueName=sName, sTypeName="SZ", xValue=sValue) except WindowsError, oException: if oException.winerror == 5: oConsole.fOutput( ERROR, "- BugId cannot be installed as the default JIT debugger.") oConsole.fOutput( ERROR, " Access to the relevant registry keys is denied.") oConsole.fOutput( ERROR, " Please try again with elevated priviledges.") return False raise
def fWaitForAndReadData(oConnection): oConsole.fOutput("%s: Waiting until bytes are available for reading..." % oConnection) oConnection.fWaitUntilBytesAreAvailableForReading() oConsole.fOutput("%s: Reading data..." % oConnection) sReceivedData = oConnection.fsReadAvailableBytes() oConsole.fOutput("%s: Read %d bytes data: %s." % (oConnection, len(sReceivedData), repr(sReceivedData)))
def __fHandleServerSideConnection(oAcceptor, oConnection): oConsole.fOutput("Server side: New connection %s." % oConnection) fStartTransactionIfPossible(oConnection) fWaitForAndReadData(oConnection) fWriteData(oConnection) fEndTransactionIfPossible(oConnection) fDisconnectAndWait(oConnection) oConsole.fOutput("Server side: Ended connection %s." % oConnection) oConsole.fOutput("Stopping acceptor %s..." % oAcceptor) oAcceptor.fStop()
sBugIdCommandLine = fsCreateBugIdCommandLine([ "--pid=%ld", "--handle-jit-event=%ld", ] + asArguments) oRegistryHiveKey = mRegistry.cRegistryHiveKey( sHiveName=mJITDebuggerRegistry.sRegistryHiveName, sKeyName=mJITDebuggerRegistry.sComandLineKeyPath, ) oConsole.fStatus("* Installing as JIT debugger...") for (sName, sValue) in { "Auto": "1", "Debugger": sBugIdCommandLine }.items(): try: oRegistryHiveKey.foSetNamedValue(sValueName=sName, sTypeName="SZ", xValue=sValue) except WindowsError, oException: if oException.winerror == 5: oConsole.fOutput( ERROR, "- BugId cannot be installed as the default JIT debugger.") oConsole.fOutput( ERROR, " Access to the relevant registry keys is denied.") oConsole.fOutput( ERROR, " Please try again with elevated priviledges.") return False raise oConsole.fOutput("+ BugId is now installed as the default JIT debugger.") oConsole.fOutput(" Command line: ", INFO, sBugIdCommandLine) return True
def fPrintUsageInformation(): oConsole.fLock() try: oConsole.fOutput(HILITE, "Usage:") oConsole.fOutput(" ", INFO, "FileSystemBrowser", NORMAL, " [", INFO, "\"path to browse\"", NORMAL, "] [", INFO, "OPTIONS", NORMAL, "]") oConsole.fOutput() oConsole.fOutput(HILITE, "Arguments:") oConsole.fOutput(" ", INFO, "\"path to browser\"") oConsole.fOutput( " Path to use as the root node (default = current working directory)" ) oConsole.fOutput() oConsole.fOutput(HILITE, "Options:") oConsole.fOutput(" ", INFO, "--help") oConsole.fOutput(" This cruft.") oConsole.fOutput(" ", INFO, "--arguments=\"path\"") oConsole.fOutput( " Read and process additional arguments from a file.") oConsole.fOutput(" ", INFO, "--offline=\"path\"") oConsole.fOutput( " Do not start a webserver to allow browsing but instead create files for" ) oConsole.fOutput(" offline browsing or uploading to server.") oConsole.fOutput(" ", INFO, "--apply-sharepoint-hacks") oConsole.fOutput( " Modify offline files for use on SharePoint server where .json files are not" ) oConsole.fOutput(" allowed.") oConsole.fOutput(" ", INFO, "--http-direct") oConsole.fOutput( " Make direct requests to look up favicons for website links.") oConsole.fOutput(" ", INFO, "--http-proxy=hostname:port") oConsole.fOutput( " Use the specified hostname and port as a HTTP proxy to make requests to" ) oConsole.fOutput(" look up favicons for website links.") oConsole.fOutput(" ", INFO, "--http-timeout=<seconds>") oConsole.fOutput( " Wait for a HTTP response from a server or proxy for at most the given number" ) oConsole.fOutput(" of seconds before failing the request.") oConsole.fOutput(" ", INFO, "--debug") oConsole.fOutput(" Show debug output") oConsole.fOutput("") oConsole.fOutput(HILITE, "Notes:") oConsole.fOutput(" You can provide the ", INFO, "--http-direct", NORMAL, " and/or ", INFO, "--http-proxy", NORMAL, " arguments to allow") oConsole.fOutput( " FileSystemBrowser to request any webpages that are linked to in order to" ) oConsole.fOutput( " determine their 'favicon' icon and use that for the link. Note that this can" ) oConsole.fOutput(" slow down the creation of the tree significantly.") oConsole.fOutput(" You can combine the ", INFO, "--http-direct", NORMAL, " and ", INFO, "--http-proxy", NORMAL, " arguments to try both") oConsole.fOutput( " direct requests and requests through a proxy when on an intranet. You can" ) oConsole.fOutput(" also specify multiple ", INFO, "--http-proxy", NORMAL, " arguments to have this") oConsole.fOutput( " script to try multiple proxies for each webpage untill the webpage can" ) oConsole.fOutput(" successfully be downloaded.") oConsole.fOutput(" If you provide the ", INFO, "--http-timeout", NORMAL, " argument, it will affect only") oConsole.fOutput(" connections made by the client based on ", INFO, "--http-direct", NORMAL, " and/or ", INFO, "--http-proxy") oConsole.fOutput( " arguments that come after it. This allows you to set different timeouts for" ) oConsole.fOutput(" these arguments.") oConsole.fOutput(" The order in which you provide the ", INFO, "--http-direct", NORMAL, " and ", INFO, "--http-proxy", NORMAL, " arguments") oConsole.fOutput( " determines the order in which request for webpages are attempted directly or" ) oConsole.fOutput(" through proxies.") finally: oConsole.fUnlock()
def fTestClient( oHTTPClient, oCertificateStore, nEndWaitTimeoutInSeconds, ): oServersShouldBeRunningLock = threading.Lock(); oServersShouldBeRunningLock.acquire(); # Released once servers should stop runnning. oConsole.fPrint("\xFE\xFE\xFE\xFE Making a first test request to %s " % oTestURL, sPadding = "\xFE"); (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oTestURL); assert o0Response, \ "No response!?"; oResponse = o0Response; oConsole.fPrint(" oRequest = %s" % oRequest.fsSerialize()); oConsole.fPrint(" oResponse = %s" % oResponse.fsSerialize()); oConsole.fPrint("\xFE\xFE\xFE\xFE Making a second test request to %s " % oTestURL, sPadding = "\xFE"); (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oTestURL); assert o0Response, \ "No response!?"; oResponse = o0Response; oConsole.fPrint(" oRequest = %s" % oRequest); oConsole.fPrint(" oResponse = %s" % oResponse); if oHTTPClient.__class__.__name__ == "cHTTPClient": # cHTTPClient specific checks asConnectionPoolsProtocolHostPort = set(oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.keys()); assert asConnectionPoolsProtocolHostPort == set((oTestURL.sBase,)), \ "Expected a oHTTPClient instance to have one cConnectionsToServerPool instance for %s, but found %s" % \ (oTestURL.sBase, repr(asConnectionPoolsProtocolHostPort)); oConnectionsToServerPool = oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.get(oTestURL.sBase); assert oConnectionsToServerPool, \ "Expected a cConnectionsToServerPool instance for %s, but found none" % oTestURL; aoConnections = oConnectionsToServerPool._cHTTPConnectionsToServerPool__aoConnections; assert len(aoConnections) == 1, \ "Expected a cConnectionsToServerPool instance with one connection for %s, but found %d connections" % \ (oTestURL, len(aoConnections)); if oHTTPClient.__class__.__name__ == "cHTTPClientUsingProxyServer": # cHTTPClientUsingProxyServer specific checks aoConnectionsToProxyNotConnectedToAServer = oHTTPClient._cHTTPClientUsingProxyServer__aoConnectionsToProxyNotConnectedToAServer; assert len(aoConnectionsToProxyNotConnectedToAServer) == 1, \ "Expected one connection to the proxy, but found %d connections" % len(aoConnectionsToProxyNotConnectedToAServer); doSecureConnectionToServerThroughProxy_by_sProtocolHostPort = oHTTPClient._cHTTPClientUsingProxyServer__doSecureConnectionToServerThroughProxy_by_sProtocolHostPort; asSecureConnectionTargets = doSecureConnectionToServerThroughProxy_by_sProtocolHostPort.keys(); assert len(asSecureConnectionTargets) == 0, \ "Expected no secure connections, but found %s" % repr(asSecureConnectionTargets); # Wrapping SSL secured sockets in SSL is not currently supported, so the # client cannot secure a connection to a server over a secure connection to a # proxy. oProxyServerURLForSecureTestURL = oHTTPClient.fo0GetProxyServerURLForURL(oSecureTestURL); # If we are not using a proxy, or the URL for the proxy server is not secure, # we can test a secure connection to the server. if not oProxyServerURLForSecureTestURL or not oProxyServerURLForSecureTestURL.bSecure: oConsole.fPrint("\xFE\xFE\xFE\xFE Making a first test request to %s " % oSecureTestURL, sPadding = "\xFE"); (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oSecureTestURL); assert o0Response, \ "No response!?"; oResponse = o0Response; oConsole.fPrint(" oRequest = %s" % oRequest); oConsole.fPrint(" oResponse = %s" % oResponse); oConsole.fPrint("\xFE\xFE\xFE\xFE Making a second test request to %s " % oSecureTestURL, sPadding = "\xFE"); (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oSecureTestURL); assert o0Response, \ "No response!?"; oResponse = o0Response; oConsole.fPrint(" oRequest = %s" % oRequest); oConsole.fPrint(" oResponse = %s" % oResponse); if oHTTPClient.__class__.__name__ == "cHTTPClient": # cHTTPClient specific checks asConnectionPoolsProtocolHostPort = set(oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.keys()); assert asConnectionPoolsProtocolHostPort == set((oTestURL.sBase, oSecureTestURL.sBase)), \ "Expected a oHTTPClient instance to have a cConnectionsToServerPool instance for %s and %s, but found %s" % \ (oTestURL.sBase, oSecureTestURL.sBase, repr(asConnectionPoolsProtocolHostPort)); oConnectionsToServerPool = oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.get(oSecureTestURL.sBase); assert oConnectionsToServerPool, \ "Expected a cConnectionsToServerPool instance for %s, but found none" % oSecureTestURL; aoConnections = oConnectionsToServerPool._cHTTPConnectionsToServerPool__aoConnections; assert len(aoConnections) == 1, \ "Expected a cConnectionsToServerPool instance with one connection for %s, but found %d connections" % \ (oSecureTestURL, len(aoConnections)); if oHTTPClient.__class__.__name__ == "cHTTPClientUsingProxyServer": # cHTTPClientUsingProxyServer specific checks aoConnectionsToProxyNotConnectedToAServer = oHTTPClient._cHTTPClientUsingProxyServer__aoConnectionsToProxyNotConnectedToAServer; doSecureConnectionToServerThroughProxy_by_sProtocolHostPort = oHTTPClient._cHTTPClientUsingProxyServer__doSecureConnectionToServerThroughProxy_by_sProtocolHostPort; asSecureConnectionTargets = doSecureConnectionToServerThroughProxy_by_sProtocolHostPort.keys(); bFoundUnexpectedNonSecureConnections = len(aoConnectionsToProxyNotConnectedToAServer) != 0; bFoundUnexpectedSecureConnections = set(asSecureConnectionTargets) != set((oSecureTestURL.sBase,)); if bFoundUnexpectedNonSecureConnections or bFoundUnexpectedSecureConnections: if bFoundUnexpectedNonSecureConnections: print "The HTTP client has unexpected non-secure connections!"; if bFoundUnexpectedSecureConnections: print "The HTTP client has unexpected secure connections!"; print "Non-secure connections:"; for oNonSecureConnection in aoConnectionsToProxyNotConnectedToAServer: print "* %s" % repr(oNonSecureConnection); print "Secure connections:"; for (sProtocolHostPort, oSecureConnection) in doSecureConnectionToServerThroughProxy_by_sProtocolHostPort.items(): print "* %S => %s" % (sProtocolHostPort, repr(oSecureConnection)); raise AssertionError(); # Create a server on a socket but do not listen so connections are refused. oConnectionRefusedServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0); oConnectionRefusedServerSocket.bind((oConnectionRefusedURL.sHostname, oConnectionRefusedURL.uPort)); # Create a server on a socket that sends out-of-band data. oOutOfBandDataServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0); oOutOfBandDataServerSocket.bind((oOutOfBandDataURL.sHostname, oOutOfBandDataURL.uPort)); oOutOfBandDataServerSocket.listen(1); oResponse = cHTTPConnection.foCreateResponse(s0Data = "Hello, world!"); sResponseWithOutOfBandData = oResponse.fsSerialize() + oResponse.fsSerialize(); def fOutOfBandDataServerThread(): (oClientSocket, (sClientIP, uClientPort)) = oOutOfBandDataServerSocket.accept(); oConsole.fPrint("Out-of-band data server receiving request..."); oClientSocket.recv(0x1000); oConsole.fPrint("Out-of-band data server sending valid response with out-of-band data..."); oClientSocket.send(oResponse.fsSerialize() + "X"); oConsole.fPrint("Out-of-band data server thread terminated."); oClientSocket.close(); oOutOfBandDataServerThread = cThread(fOutOfBandDataServerThread); oOutOfBandDataServerThread.fStart(bVital = False); # Create a server on a socket that immediately closes the connection. oConnectionDisconnectedServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0); oConnectionDisconnectedServerSocket.bind((oConnectionDisconnectedURL.sHostname, oConnectionDisconnectedURL.uPort)); oConnectionDisconnectedServerSocket.listen(1); def fConnectionDisconnectedServerThread(): (oClientSocket, (sClientIP, uClientPort)) = oConnectionDisconnectedServerSocket.accept(); oConsole.fPrint("Disconnect server is disconnecting the connection..."); oClientSocket.close(); oConsole.fPrint("Disconnect server thread terminated."); oConnectionDisconnectedServerThread = cThread(fConnectionDisconnectedServerThread); oConnectionDisconnectedServerThread.fStart(bVital = False); # Create a server on a socket that immediately shuts down the connection. oConnectionShutdownServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0); oConnectionShutdownServerSocket.bind((oConnectionShutdownURL.sHostname, oConnectionShutdownURL.uPort)); oConnectionShutdownServerSocket.listen(1); def fConnectionShutdownServerThread(): (oClientSocket, (sClientIP, uClientPort)) = oConnectionShutdownServerSocket.accept(); oConsole.fPrint("Shutdown server is shutting down the connection for writing..."); oClientSocket.shutdown(socket.SHUT_WR); oConsole.fPrint("Shutdown server is sleeping to keep the connection open...."); oServersShouldBeRunningLock.acquire(); oServersShouldBeRunningLock.release(); oConsole.fPrint("Shutdown server thread terminated."); oConnectionShutdownServerThread = cThread(fConnectionShutdownServerThread); oConnectionShutdownServerThread.fStart(bVital = False); # Create a server on a socket that does not send a response. oResponseTimeoutServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0); oResponseTimeoutServerSocket.bind((oResponseTimeoutURL.sHostname, oResponseTimeoutURL.uPort)); oResponseTimeoutServerSocket.listen(1); def fResponseTimeoutServerThread(): (oClientSocket, (sClientIP, uClientPort)) = oResponseTimeoutServerSocket.accept(); oConsole.fPrint("Response timeout server receiving request..."); oClientSocket.recv(0x1000); oConsole.fPrint("Response timeout server is sleeping to avoid sending a response..."); oServersShouldBeRunningLock.acquire(); oServersShouldBeRunningLock.release(); oConsole.fPrint("Response timeout thread terminated."); oResponseTimeoutServerThread = cThread(fResponseTimeoutServerThread); oResponseTimeoutServerThread.fStart(bVital = False); # Create a server on a socket that sends an invalid response. oInvalidHTTPMessageServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0); oInvalidHTTPMessageServerSocket.bind((oInvalidHTTPMessageURL.sHostname, oInvalidHTTPMessageURL.uPort)); oInvalidHTTPMessageServerSocket.listen(1); sInvalidResponse = "Hello, world!\r\n"; def fInvalidHTTPMessageServerThread(): (oClientSocket, (sClientIP, uClientPort)) = oInvalidHTTPMessageServerSocket.accept(); oConsole.fPrint("Invalid HTTP Message server received request; sending invalid response..."); oClientSocket.recv(0x1000); # This should cover the request, which we discard. oClientSocket.send(sInvalidResponse); oConsole.fPrint("Invalid HTTP Message server thread terminated."); oInvalidHTTPMessageServerThread = cThread(fInvalidHTTPMessageServerThread); oInvalidHTTPMessageServerThread.fStart(bVital = False); for (uNumberOfRequests, oURL, cExpectedExceptionClass, acAcceptableExceptionClasses, auAcceptableStatusCodes) in ( (1, oUnknownHostnameURL, cDNSUnknownHostnameException, [], [400]), (1, oInvalidAddressURL, cTCPIPInvalidAddressException, [], [400]), (1, oConnectionRefusedURL, cTCPIPConnectionRefusedException, [cTCPIPConnectTimeoutException], [502]), (1, oConnectTimeoutURL, cTCPIPConnectTimeoutException, [], [502, 504]), (1, oConnectionDisconnectedURL, cTCPIPConnectionDisconnectedException, [], [502]), (1, oConnectionShutdownURL, cTCPIPConnectionShutdownException, [], [502]), (1, oResponseTimeoutURL, cTCPIPDataTimeoutException, [], [504]), (2, oOutOfBandDataURL, cHTTPOutOfBandDataException, [], [502]), (1, oInvalidHTTPMessageURL, cHTTPInvalidMessageException, [], [502]), ): oConsole.fPrint("\xFE\xFE\xFE\xFE Making a test request to %s " % oURL, sPadding = "\xFE"); if oHTTPClient.__class__.__name__ == "cHTTPClient": oConsole.fStatus(" * Expecting %s exception..." % cExpectedExceptionClass.__name__); auAcceptableStatusCodes = None; if oHTTPClient.__class__.__name__ == "cHTTPClientUsingProxyServer": if auAcceptableStatusCodes: oConsole.fStatus(" * Expecting a HTTP %s reponse..." % "/".join(["%03d" % uStatusCode for uStatusCode in auAcceptableStatusCodes])); cExpectedExceptionClass = None; for uConnectionNumber in xrange(1, uNumberOfRequests + 1): if uConnectionNumber < uNumberOfRequests: # We do not yet expect an exception, so we won't handle one. o0Response = oHTTPClient.fo0GetResponseForURL(oURL); assert o0Response, \ "No response!?"; oResponse = o0Response; oConsole.fPrint(" oResponse = %s" % oResponse); else: try: # Use a short connect timeout to speed things up: all connections should be created in about 1 second except the # one that purposefully times out and this way we do not have to wait for that to happen very long. o0Response = oHTTPClient.fo0GetResponseForURL(oURL); assert o0Response, \ "No response!?"; oResponse = o0Response; if auAcceptableStatusCodes: assert oResponse.uStatusCode in auAcceptableStatusCodes, \ "Expected a HTTP %s response, got %s" % \ ("/".join(["%03d" % uStatusCode for uStatusCode in auAcceptableStatusCodes]), oResponse.fsGetStatusLine()); oConsole.fOutput(" oResponse = %s" % oResponse); except Exception as oException: if oException.__class__ is cExpectedExceptionClass: oConsole.fPrint(OK, " + Threw %s." % repr(oException)); elif oException.__class__ in acAcceptableExceptionClasses: oConsole.fPrint(WARNING, " ~ Threw %s." % repr(oException)); oConsole.fPrint(" Expected %s." % cExpectedExceptionClass.__name__); else: oConsole.fPrint(ERROR, " - Threw %s." % repr(oException)); if cExpectedExceptionClass: oConsole.fPrint(" Expected %s." % cExpectedExceptionClass.__name__); else: oConsole.fPrint(" No exception expected."); raise; else: if cExpectedExceptionClass: oConsole.fPrint(ERROR, " - Expected %s." % cExpectedExceptionClass.__name__); raise AssertionError("No exception"); # Allow server threads to stop. oServersShouldBeRunningLock.release(); oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping HTTP Client ", sPadding = "\xFE"); oHTTPClient.fStop(); assert oHTTPClient.fbWait(nEndWaitTimeoutInSeconds), \ "HTTP Client did not stop in time"; oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping connection refused server ", sPadding = "\xFE"); oConnectionRefusedServerSocket.close(); # Has no thread. oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping out-of-band data server ", sPadding = "\xFE"); oOutOfBandDataServerSocket.close(); assert oOutOfBandDataServerThread.fbWait(nEndWaitTimeoutInSeconds), \ "Out-of-band data server thread (%d/0x%X) did not stop in time." % \ (oOutOfBandDataServerThread.uId, oOutOfBandDataServerThread.uId); oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping connection closed server ", sPadding = "\xFE"); oConnectionDisconnectedServerSocket.close(); assert oConnectionDisconnectedServerThread.fbWait(nEndWaitTimeoutInSeconds), \ "Connection closed server thread (%d/0x%X) did not stop in time." % \ (oConnectionDisconnectedServerThread.uId, oConnectionDisconnectedServerThread.uId); oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping connection shutdown server ", sPadding = "\xFE"); oConnectionShutdownServerSocket.close(); assert oConnectionShutdownServerThread.fbWait(nEndWaitTimeoutInSeconds), \ "Connection shutdown server thread (%d/0x%X) did not stop in time." % \ (oConnectionShutdownServerThread.uId, oConnectionShutdownServerThread.uId); oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping response timeout server ", sPadding = "\xFE"); oResponseTimeoutServerSocket.close(); assert oResponseTimeoutServerThread.fbWait(nEndWaitTimeoutInSeconds), \ "Connection shutdown server thread (%d/0x%X) did not stop in time." % \ (oResponseTimeoutServerThread.uId, oResponseTimeoutServerThread.uId); oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping invalid http message server ", sPadding = "\xFE"); oInvalidHTTPMessageServerSocket.close(); assert oInvalidHTTPMessageServerThread.fbWait(nEndWaitTimeoutInSeconds), \ "Invalid http message server thread (%d/0x%X) did not stop in time." % \ (oInvalidHTTPMessageServerThread.uId, oInvalidHTTPMessageServerThread.uId);
bTestProxy = True elif sArgument == "--auto-proxy": bTestAutoProxy = True else: raise AssertionError("Unknown argument %s" % sArgument) nEndWaitTimeoutInSeconds = 10 sCertificatesPath = os.path.join(os.path.dirname(__file__), "Certificates") oLocalNonSecureURL = mHTTP.cURL.foFromString( "http://localhost:28876/local-non-secure") oLocalSecureURL = mHTTP.cURL.foFromString( "https://localhost:28876/local-secure") oProxyServerURL = mHTTP.cURL.foFromString("https://localhost:28876") oConsole.fOutput( "\xFE\xFE\xFE\xFE Creating a cCertificateStore instance ".ljust( 160, "\xFE")) oCertificateStore = mSSL.cCertificateStore() oCertificateStore.fAddCertificateAuthority(mSSL.oCertificateAuthority) oConsole.fOutput(" oCertificateStore = %s" % oCertificateStore) # Reset the certificate authority and generate an SSL certificate and key # for "localhost". oConsole.fOutput( "\xFE\xFE\xFE\xFE Resetting oCertificateAuthority...".ljust( 160, "\xFE")) oConsole.fOutput(" oCertificateAuthority = %s" % mSSL.oCertificateAuthority) mSSL.oCertificateAuthority.fReset() oConsole.fOutput(("\xFE\xFE\xFE\xFE Getting a certificate for %s " % oLocalSecureURL.sHostname).ljust(160, "\xFE")) mSSL.oCertificateAuthority.foGenerateServersideSSLContextForHostname(
def fWriteData(oConnection): oConsole.fOutput("%s: writing %d bytes data: %s." % (oConnection, len(gsData), repr(gsData))) oConnection.fWriteBytes(gsData)
def fbInstallAsJITDebugger(asAdditionalArguments): # When BugId gets started as a JIT debugger, we can use the string "%ld" in the arguments twice, which will get # replaced by the process id and the JIT event number, in order. We will add arguments to that effect at the start # of the arument list provided by the user (later). asDefaultArguments = [ # '%' gets escaped to avoid environment variable expansion. However, here we do want a '%' in the command line. # We can use '%%' to do so '%': "--pid=%%ld", "--handle-jit-event=%%ld", ] # To prevent the user from accidentally providing these arguments themselves, we scan the arguments provided by the # user for for these and report an error and return false if we find them. We will also check if the user has provided # a report folder path. s0BugIdReportsFolder = dxConfig["sReportFolderPath"] for sArgument in asAdditionalArguments: if sArgument.startswith("--") and "=" in sArgument: (sName, sValue) = sArgument[2:].split("=") if sName in ["pid", "pids", "handle-jit-event"]: oConsole.fOutput(ERROR, "- You cannot use ", ERROR_INFO, sArgument, ERROR, " in combination with ", ERROR_INFO, "-I", ERROR, " in the arguments.") return False if sName in [ "report", "reports", "report-folder", "reports-folder", "report-folder-path", "reports-folder-path", "sReportFolderPath" ]: s0BugIdReportsFolder = sValue # By default the python process hosting BugId will be run in the Windows System32 folder. We cannot save bug # reports there. To make sure we will save bug reports somewhere we can write and where the user will likely find # them, we will add an argument if s0BugIdReportsFolder is None: sBugIdReportsFolder = "%s\\BugId reports" % os.getenv("USERPROFILE") asDefaultArguments.append("--reports=%s" % sBugIdReportsFolder) else: sBugIdReportsFolder = s0BugIdReportsFolder sBugIdCommandLine = fsCreateBugIdCommandLine(asDefaultArguments + asAdditionalArguments) oRegistryHiveKey = mRegistry.cRegistryHiveKey( sHiveName=mJITDebuggerRegistry.sComandLineHiveName, sKeyName=mJITDebuggerRegistry.sComandLineKeyPath, ) oConsole.fStatus("* Installing as JIT debugger...") for (sName, sValue) in { "Auto": "1", "Debugger": sBugIdCommandLine }.items(): try: oRegistryHiveKey.foSetNamedValue(sValueName=sName, sTypeName="SZ", xValue=sValue) except WindowsError, oException: if oException.winerror == 5: oConsole.fOutput( ERROR, "- BugId cannot be installed as the default JIT debugger.") oConsole.fOutput( ERROR, " Access to the relevant registry keys is denied.") oConsole.fOutput( ERROR, " Please try again with elevated priviledges.") return False raise
def fTestConnectionAndAcceptor(cConnection, cConnectionAcceptor): def __fHandleServerSideConnection(oAcceptor, oConnection): oConsole.fOutput("Server side: New connection %s." % oConnection) fStartTransactionIfPossible(oConnection) fWaitForAndReadData(oConnection) fWriteData(oConnection) fEndTransactionIfPossible(oConnection) fDisconnectAndWait(oConnection) oConsole.fOutput("Server side: Ended connection %s." % oConnection) oConsole.fOutput("Stopping acceptor %s..." % oAcceptor) oAcceptor.fStop() # Function code starts here oConsole.fOutput("Creating Acceptor...") oAcceptor = cConnectionAcceptor(__fHandleServerSideConnection, gsHostname, guPort) oConsole.fOutput(" Acceptor(%s): Listening on %s:%d." % (oAcceptor, gsHostname, guPort)) oConsole.fOutput("Creating Connection...") oConnection = cConnection.foConnectTo(gsHostname, guPort) oConsole.fOutput("Client side: New connection %s." % oConnection) fStartTransactionIfPossible(oConnection) fWriteData(oConnection) fWaitForAndReadData(oConnection) fEndTransactionIfPossible(oConnection) fDisconnectAndWait(oConnection) oConsole.fOutput("Client side: Ended connection %s." % oConnection) oConsole.fOutput("Waiting for acceptor %s to stop..." % oAcceptor) assert oAcceptor.fbWait(nTimeoutInSeconds = 1), \ "Acceptor did not terminate in a reasonable time!?" oConsole.fOutput("Done.")
def fEndTransactionIfPossible(oConnection): if hasattr(oConnection, "fEndTransaction"): oConsole.fOutput("%s: Ending transaction..." % oConnection) oConnection.fEndTransaction()
def fStartTransactionIfPossible(oConnection): if hasattr(oConnection, "fbStartTransaction"): oConsole.fOutput("%s: Starting transaction..." % oConnection) assert oConnection.fbStartTransaction(), \ "Could not start a transaction on %s!?" % oConnection
def fStatus(*txArguments, **dxArguments): pass; import json, os, sys; import mSSL; for sArgument in sys.argv[1:]: if sArgument == "--quick": pass; # Always quick :) elif sArgument == "--debug": fEnableDebugOutputForModule(mSSL); else: raise AssertionError("Unknown argument %s" % sArgument); oConsole.fOutput("\xFE\xFE\xFE\xFE Resetting oCertificateAuthority... ", sPadding = "\xFE"); oConsole.fOutput(" oCertificateAuthority = ", str(mSSL.oCertificateAuthority)); mSSL.oCertificateAuthority.fReset(); oConsole.fOutput("\xFE\xFE\xFE\xFE Ask oCertificateAuthority to generate cSSLContext for hostname 'test-hostname'...", sPadding = "\xFE"); oSSLContext = mSSL.oCertificateAuthority.foGenerateServersideSSLContextForHostname("test-hostname"); oConsole.fOutput(" oSSLContext = ", str(oSSLContext)); oConsole.fOutput("\xFE\xFE\xFE\xFE Generate cCertificateStore instance...", sPadding = "\xFE"); oCertificateStore = mSSL.cCertificateStore(); oConsole.fOutput("\xFE\xFE\xFE\xFE Add oCertificateAuthority to oCertificateStore...", sPadding = "\xFE"); oCertificateStore.fAddCertificateAuthority(mSSL.oCertificateAuthority); oConsole.fOutput("\xFE\xFE\xFE\xFE Ask oCertificateStore for cSSLContext for hostname 'test-hostname'...", sPadding = "\xFE"); oSSLContext = oCertificateStore.foGetServersideSSLContextForHostname("test-hostname");
from fTestUser32GDIDLL import fTestUser32GDIDLL from fTestCharacterTypes import fTestCharacterTypes from fTestIntegerTypes import fTestIntegerTypes from mWindowsSDK.fsDumpInteger import fsDumpInteger for (uValue, sValue) in { 1000: "1,000 / 0x3E8", 1000000: "1,000,000 / 0xF4240", }.items(): assert fsDumpInteger(uValue) == sValue, \ "Expected fsDumpInteger(%d) to be %s, but got %s" % \ (uValue, repr(sValue), repr(fsDumpInteger(uValue))) assert NT_SUCCESS(STATUS_SUCCESS), \ "NT_SUCCESS(STATUS_SUCCESS) => %s instead of True" % repr(NT_SUCCESS(STATUS_SUCCESS)) assert not NT_ERROR(STATUS_SUCCESS), \ "NT_ERROR(STATUS_SUCCESS) => %s instead of False" % repr(NT_ERROR(STATUS_SUCCESS)) fTestCharacterTypes(oConsole) fTestIntegerTypes(oConsole) fTestGdi32(oConsole) fTestKernel32DLL(oConsole) fTestUser32GDIDLL(oConsole) oConsole.fOutput("+ Done.") except Exception as oException: if mDebugOutput: mDebugOutput.fTerminateWithException(oException, bShowStacksForAllThread=True) raise
for sArgument in sys.argv[1:]: if sArgument == "--quick": pass # Always quick :) elif sArgument == "--debug": fEnableDebugOutputForModule(mTCPIPConnections) else: raise AssertionError("Unknown argument %s" % sArgument) from fRunTestsOnTCPIPConnectionClasses import fRunTestsOnTCPIPConnectionClasses fRunTestsOnTCPIPConnectionClasses(oConsole) from fTestConnectionAndAcceptor import fTestConnectionAndAcceptor oConsole.fOutput("=== Testing TCP/IP Connections ".ljust(80, "=")) fTestConnectionAndAcceptor(mTCPIPConnections.cTCPIPConnection, mTCPIPConnections.cTCPIPConnectionAcceptor) oConsole.fOutput("=== Testing Buffered TCP/IP Connections ".ljust(80, "=")) fTestConnectionAndAcceptor( mTCPIPConnections.cBufferedTCPIPConnection, mTCPIPConnections.cBufferedTCPIPConnectionAcceptor) oConsole.fOutput( "=== Testing Transactional Buffered TCP/IP Connections ".ljust( 80, "=")) fTestConnectionAndAcceptor( mTCPIPConnections.cTransactionalBufferedTCPIPConnection, mTCPIPConnections.cTransactionalBufferedTCPIPConnectionAcceptor) for sArgument in sys.argv[1:]: if sArgument == "--debug":