def hasReceivedPing(self, timeout=1.0): """ Waits to receive "ping" event until the end of the timeout. @param timeout: time to wait response in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl_rsp = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl_rsp.addLayer(layer_agent) tpl_rsp.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl_rsp.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.cfg['ssl-support']: tpl_rsp.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) tpl_rsp.addLayer( templates.ws(opcode=codec.WEBSOCKET_OPCODE_PING, more=templates.received())) return self.received(expected=tpl_rsp, timeout=timeout)
def hasReceivedHttpResponse(self, httpCode="200", httpPhrase="OK", headers=None, timeout=1.0): """ Wait to receive "http response" until the end of the timeout. @param httpCode: http code (default=200) @type httpCode: string @param httpPhrase: http phrase (default=OK) @type httpPhrase: string @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response @rtype: template """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent= TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name'] ) layer_agent.addKey(name='type', data=self.cfg['agent']['type'] ) tpl.addLayer( layer_agent ) tpl.addLayer( AdapterIP.ip( more=AdapterIP.received() ) ) tpl.addLayer( AdapterTCP.tcp(more=AdapterTCP.received()) ) if self.ADP_HTTP.tcp().cfg['ssl-support']: tpl.addLayer( AdapterSSL.ssl(more=AdapterSSL.received()) ) tpl.addLayer( AdapterHTTP.response( version="HTTP/1.1", code=httpCode, phrase=httpPhrase, headers=headers ) ) return self.hasReceivedResponse(expected=tpl, timeout=timeout)
def isHandshakeSuccessful(self, timeout=1.0): """ Wait to receive "handshake successful" event until the end of the timeout. @param timeout: time to wait response in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl_rsp = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl_rsp.addLayer(layer_agent) tpl_rsp.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl_rsp.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.cfg['ssl-support']: tpl_rsp.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) tpl_rsp.addLayer(AdapterHTTP.response()) tpl_rsp.addLayer(templates.ws(more=templates.handshake_ok())) return self.received(expected=tpl_rsp, timeout=timeout)
def hasReceivedResponse(self, expected, timeout=1.0): """ Wait response until the end of the timeout. @param expected: response template @type expected: templatemessage @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: response @rtype: template """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) tpl.addLayer(AdapterTelnet.dataIncoming()) tpl.addLayer(expected) evt = self.received(expected=tpl, timeout=timeout) return evt
def getTemplateRequest(self, httpMethod="GET", httpUri="/", httpVersion='HTTP/1.1', httpHeaders={}, httpBody=None): """ """ tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_TCP.cfg['ssl-support']: tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) headers = {} headers.update(httpHeaders) tpl.addLayer( templates.request(method=httpMethod, uri=httpUri, version=httpVersion, headers=headers, body=httpBody)) return tpl
def hasReceivedHttpRequest(self, httpMethod="GET", httpUri="/", httpVersion='HTTP/1.1', timeout=1.0, httpHeaders={}, httpBody=None): """ Wait to receive "http request" until the end of the timeout. @param httpMethod: http method (default=GET) @type httpMethod: string @param httpUri: http phrase (default=OK) @type httpUri: string @param httpVersion: http version (default=HTTP/1.1) @type httpVersion: string @param httpHeaders: expected http headers @type httpHeaders: dict @param httpBody: expected body (default=None) @type httpBody: string/none @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response @rtype: template """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_TCP.cfg['ssl-support']: tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) headers = {} headers.update(httpHeaders) tpl.addLayer( templates.request(method=httpMethod, uri=httpUri, version=httpVersion, headers=headers, body=httpBody)) return self.hasReceivedRequest(expected=tpl, timeout=timeout)
def getExpectedTemplate(self, event, versionIp=None, sourceIp=None, destinationIp=None, sourcePort=None, destinationPort=None): """ Return an expected template with ip and tcp layers """ # prepare layers defaultVer = self.cfg['sock-family'] if versionIp is not None: defaultVer = versionIp layer_ip = AdapterIP.ip( source=sourceIp, destination=destinationIp, version=defaultVer ) # tcp layer_tcp = AdapterTCP.tcp(source=sourcePort, destination=destinationPort) # ssh layer_ssh = templates.ssh(more=event) # prepare template if self.cfg['agent-support']: layer_agent= TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name'] ) layer_agent.addKey(name='type', data=self.cfg['agent']['type'] ) tpl = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: tpl.addLayer(layer=layer_agent) tpl.addLayer(layer=layer_ip) tpl.addLayer(layer=layer_tcp) tpl.addLayer(layer=layer_ssh) return tpl
def encapsule(self, ip_event, ssh_event): """ encapsule template """ # prepare layers # ip src = self.sourceIp dst = self.cfg['dst-ip'] srcP = self.sourcePort dstP = self.cfg['dst-port'] if ip_event == AdapterIP.received(): src = self.cfg['dst-ip'] dst = self.sourceIp srcP = self.cfg['dst-port'] dstP = self.sourcePort layer_ip = AdapterIP.ip( source=src, destination=dst, version=self.cfg['sock-family'], more=ip_event ) # tcp if ip_event == AdapterIP.received(): moreTcp = AdapterTCP.received() else: moreTcp = AdapterTCP.sent() layer_tcp = AdapterTCP.tcp(source=srcP, destination=dstP, more=moreTcp) # ssh layer_ssh = templates.ssh() layer_ssh.addMore(more=ssh_event) # prepare template if self.cfg['agent-support']: layer_agent= TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name'] ) layer_agent.addKey(name='type', data=self.cfg['agent']['type'] ) tpl = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: tpl.addLayer(layer=layer_agent) tpl.addLayer(layer=layer_ip) tpl.addLayer(layer=layer_tcp) tpl.addLayer(layer=layer_ssh) return tpl
def hasReceivedRequest(self, expected, timeout=1.0): """ Wait to receive "response" until the end of the timeout. @param expected: response template @type expected: templatemessage/templatelayer @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response or none otherwise @rtype: templatemessage/templatelayer/none """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) if expected is None: raise Exception( 'has received response: expected template cannot be empty') tpl_expected = expected if isinstance(expected, TestTemplates.TemplateLayer): tpl_expected = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl_expected.addLayer(layer_agent) tpl_expected.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl_expected.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_TCP.cfg['ssl-support']: tpl_expected.addLayer( AdapterSSL.ssl(more=AdapterSSL.received())) tpl_expected.addLayer(expected) evt = self.received(expected=tpl_expected, timeout=timeout) if evt is None: return None return evt
def hasReceivedData(self, data="", timeout=1.0): """ Wait response until the end of the timeout. @param data: data @type data: string @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: response @rtype: template """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl_expected = templates_catalyst.catalyst_data(data=data) tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) tpl.addLayer(AdapterTelnet.dataIncoming()) tpl.addLayer(tpl_expected) evt = self.received(expected=tpl, timeout=timeout) ret = None if evt is not None: ret = evt.get('TELNET', 'data') return ret
def hasReceivedData(self, timeout=1.0, dataExpected=None): """ Waits to receive "data" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @param dataExpected: data expected (default=None) @type dataExpected: string/operators/none @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) layer_ip = AdapterIP.ip() layer_tcp = AdapterTCP.tcp() tpl = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(layer=layer_ip) tpl.addLayer(layer=layer_tcp) if self.tcp.cfg['proxy-enabled']: layer_sock = AdapterSOCKS.socks(more=AdapterSOCKS.received()) tpl.addLayer(layer=layer_sock) layer_telnet = templates.telnet() if dataExpected is not None: layer_telnet.addKey(name='data', data=dataExpected) tpl.addLayer(layer=layer_telnet) evt = self.received(expected=tpl, timeout=timeout) return evt
def __init__(self, parent, bindIp='', bindPort=0, destIp='127.0.0.1', destPort=23, destHost='', socketTimeout=10.0, socketFamily=AdapterIP.IPv4, doEcho=True, name=None, proxyType=AdapterTCP.PROXY_SOCKS4, proxyUserID='xtc', proxyIp='', proxyPort=3128, proxyHost='', proxyEnabled=False, debug=False, logEventSent=True, logEventReceived=True, parentName=None, agentSupport=False, agent=None, shared=False, saveContent=False): """ This class enable to use TELNET (rfc854) as client only, lower network layer (IP, Ethernet) are not controlable. @param parent: parent testcase @type parent: testcase @param name: adapter name used with from origin/to destination (default=None) @type name: string/none @param bindIp: bind on ip (source ip) @type bindIp: string @param bindPort: bind on port (source port) @type bindPort: integer @param destIp: destination ip @type destIp: string @param destPort: destination port @type destPort: integer @param destHost: destination host (automatic dns resolution) @type destHost: string @param proxyType: SutAdapters.TCP.PROXY_SOCKS4 | SutAdapters.TCP.PROXY_SOCKS5 (default=PROXY_SOCKS4) @type proxyType: strconstant @param proxyUserID: user id with socks (default=xtc) @type proxyUserID: string @param proxyIp: proxy ip @type proxyIp: string @param proxyPort: proxy port @type proxyPort: integer @param proxyHost: proxy host (automatic dns resolution) @type proxyHost: string @param proxyEnabled: True to support proxy (default=False) @type proxyEnabled: boolean @param socketFamily: SutAdapters.IP.IPv4 (default) | SutAdapters.IP.IPv6 @type socketFamily: intconstant @param socketTimeout: timeout to connect in second (default=1s) @type socketTimeout: float @param doEcho: send do echo command on True @type doEcho: boolean @param debug: True to activate debug mode (default=False) @type debug: boolean @param agentSupport: agent support to use a remote socket (default=False) @type agentSupport: boolean @param agent: agent to use when this mode is activated @type agent: string/None @param shared: shared adapter (default=False) @type shared: boolean @param saveContent: save content in the private storage (default=False) @type saveContent: boolean """ if not isinstance(bindPort, int): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "bindPort argument is not a integer (%s)" % type(bindPort)) if not isinstance(destPort, int): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "destPort argument is not a integer (%s)" % type(destPort)) if not isinstance(proxyPort, int): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "proxyPort argument is not a integer (%s)" % type(proxyPort)) # init adapter TestAdapterLib.Adapter.__init__(self, name=__NAME__, parent=parent, realname=name, shared=shared, debug=debug, agentSupport=agentSupport, agent=agent, caller=TestAdapterLib.caller(), agentType=AGENT_TYPE_EXPECTED) if parentName is not None: TestAdapterLib.Adapter.setName(self, name="%s>%s" % (parentName, __NAME__)) self.logEventSent = logEventSent self.logEventReceived = logEventReceived # prepare tcp adapter self.tcp = AdapterTCP.Client(parent=parent, bindIp=bindIp, bindPort=bindPort, destinationIp=destIp, destinationPort=destPort, destinationHost=destHost, socketTimeout=socketTimeout, socketFamily=socketFamily, inactivityTimeout=0, proxyType=proxyType, proxyUserID=proxyUserID, proxyIp=proxyIp, proxyPort=proxyPort, proxyHost=proxyHost, proxyEnabled=proxyEnabled, separatorDisabled=True, sslSupport=False, name=name, debug=debug, logEventSent=False, logEventReceived=False, shared=shared, parentName=__NAME__, agentSupport=agentSupport, agent=agent) # callback tcp self.tcp.handleIncomingData = self.onIncomingData self.tcp.handleNoMoreData = self.onNoMoreData # telnet options self.cfg = {} # proxy self.cfg['proxy-enabled'] = proxyEnabled # option to sent self.cfg['do-echo'] = doEcho # ip layer self.cfg['telnet-source-ip'] = bindIp self.cfg['telnet-source-port'] = int(bindPort) self.cfg['telnet-destination-ip'] = destIp self.cfg['telnet-destination-port'] = int(destPort) # options self.cfg['telnet-remote-opts'] = [] self.cfg['telnet-save-content'] = saveContent # agent self.cfg['agent-support'] = agentSupport if self.cfg['agent-support']: self.cfg['agent'] = agent self.cfg['agent-name'] = agent['name'] self.doEchoSent = False self.buf = '' # init the telnet encoder/decoder self.telnetCodec = codec.Codec(parent=self) if self.cfg['telnet-save-content']: self.warning("TELNET AdapterID=%s" % self.getAdapterId())
def __init__(self, parent, name=None, debug=False, shared=False, agentSupport=False, agent=None, bindIp='', bindPort=0, sslSupport=False, strictMode=False, octetStreamSupport=True, manStreamSupport=True, websocketMode=False, truncateBody=False, logEventSent=True, logEventReceived=True, checkCert=AdapterSSL.CHECK_CERT_NO, certfile="/tmp/cert.file", keyfile="/tmp/key.file"): """ HTTP server, based on TCP server adapter. @param parent: parent testcase @type parent: testcase @param name: adapter name used with from origin/to destination (default=None) @type name: string/none @param debug: active debug mode (default=False) @type debug: boolean @param shared: shared adapter (default=False) @type shared: boolean @param agentSupport: agent support (default=False) @type agentSupport: boolean @param agent: agent to use (default=None) @type agent: string/none @param bindIp: bind on ip (source ip) @type bindIp: string @param bindPort: bind on port (source port) @type bindPort: integer @param sslSupport: activate SSL channel (default=False) @type sslSupport: boolean @param checkCert: SutAdapters.SSL.CHECK_CERT_NO | SutAdapters.SSL.CHECK_CERT_OPTIONAL | SutAdapters.SSL.CHECK_CERT_REQUIRED @type checkCert: strconstant @param certFile: certificate file (default=/tmp/cert.file) @type certFile: string @param keyFile: key file (default=/tmp/key.file) @type keyFile: string """ TestAdapterLib.Adapter.__init__(self, name=__NAME__, parent=parent, debug=debug, realname=name, agentSupport=agentSupport, agent=agent, shared=shared, caller=TestAdapterLib.caller(), agentType=AGENT_TYPE_EXPECTED) self.parent = parent self.cfg = {} if agent is not None: self.cfg['agent'] = agent self.cfg['agent-name'] = agent['name'] self.cfg['agent-support'] = agentSupport self.logEventSent = logEventSent self.logEventReceived = logEventReceived self.cfg['http_truncate_body'] = truncateBody self.cfg['http_strict_mode'] = strictMode self.cfg['http_websocket_mode'] = websocketMode self.cfg['http_octet_stream_support'] = octetStreamSupport self.cfg['http_man_stream_support'] = manStreamSupport self.clients = {} self.eventsExpected = [] self.eventsQueue = Queue.Queue(0) self.ADP_TCP = AdapterTCP.Server(parent=parent, bindIp=bindIp, bindPort=bindPort, separatorDisabled=True, logEventSent=False, logEventReceived=False, agent=agent, shared=shared, agentSupport=agentSupport, sslSupport=sslSupport, checkCert=checkCert, certfile=certfile, keyfile=keyfile) self.ADP_TCP.onClientIncomingData = self.onClientIncomingData self.ADP_TCP.onClientNoMoreData = self.onClientNoMoreData self.__checkConfig()
def hasReceivedRestResponse(self, httpCode="200", httpPhrase="OK", httpVersion='HTTP/1.1', timeout=1.0, httpHeaders={}, httpBody=None): """ Wait to receive "rest response" until the end of the timeout. @param httpCode: http code (default=200) @type httpCode: string @param httpPhrase: http phrase (default=OK) @type httpPhrase: string @param httpVersion: http version (default=HTTP/1.1) @type httpVersion: string @param httpHeaders: expected http headers @type httpHeaders: dict @param httpBody: expected body (default=None) @type httpBody: string/none @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response @rtype: template """ TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout) tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_HTTP.tcp().cfg['ssl-support']: tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) headers = { 'content-type': TestOperators.Contains(needle='application/json', AND=True, OR=False) } headers.update(httpHeaders) tpl.addLayer( AdapterHTTP.response(version=httpVersion, code=httpCode, phrase=httpPhrase, headers=headers, body=httpBody)) tpl.addLayer(templates.response()) return self.hasReceivedResponse(expected=tpl, timeout=timeout)