Esempio n. 1
0
	def renameFile(self, currentFilename, newFilename):
		"""
		Rename a file
		
		@param currentFilename: current filename
		@type currentFilename: string
		
		@param newFilename: new filename
		@type newFilename: string
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.rename(fromFilename=currentFilename, toFilename=newFilename), cmd=RENAME_FILE )
		tpl.addRaw( currentFilename )
		self.logSentEvent( shortEvt = "rename file", tplEvt = tpl )
		
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  RENAME_FILE, 'current-filename': currentFilename, 'new-filename': newFilename }
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				self.ssh().channel().rename(currentFilename, newFilename)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=RENAME_FILE  )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(), cmd=RENAME_FILE  )
				self.logRecvEvent( shortEvt = "file renamed", tplEvt = tpl )
Esempio n. 2
0
    def hasReceivedRow(self,
                       timeout=1.0,
                       row=None,
                       rowIndex=None,
                       rowMax=None):
        """
		Waits to receive "row" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float			

		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
        if not (isinstance(timeout, int)
                or isinstance(timeout, float)) or isinstance(timeout, bool):
            raise TestAdapterLib.ValueException(
                TestAdapterLib.caller(),
                "timeout argument is not a float or integer (%s)" %
                type(timeout))

        expected = templates.db(host=self.cfg['host'],
                                port=self.cfg['port'],
                                more=templates.response(row=row,
                                                        rowIndex=None,
                                                        rowMax=None))
        evt = self.received(expected=self.encapsule(db_event=expected),
                            timeout=timeout)
        return evt
Esempio n. 3
0
	def renameFolder(self, currentPath, newPath):
		"""
		Rename a folder
		
		@param currentPath: current path folder
		@type currentPath: string
		
		@param newPath: new path folder
		@type newPath: string
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.rename(fromPath=currentPath, toPath=newPath), cmd=RENAME_FOLDER )
		tpl.addRaw( currentPath )
		self.logSentEvent( shortEvt = "rename folder", tplEvt = tpl )
		
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  RENAME_FOLDER, 'current-path': currentPath, 'new-path': newPath  }
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				self.ssh().channel().rename(currentPath, newPath)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=RENAME_FOLDER )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response() , cmd=RENAME_FOLDER )
				self.logRecvEvent( shortEvt = "folder renamed", tplEvt = tpl )
Esempio n. 4
0
	def deleteFolder(self, path):
		"""
		Delete a folder
		
		@param path: path folder
		@type path: string
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.delete(path=path), cmd=DELETE_FOLDER )
		tpl.addRaw( path )
		self.logSentEvent( shortEvt = "delete folder", tplEvt = tpl )
		
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  DELETE_FOLDER, 'path': path }
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				self.ssh().channel().rmdir(path)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=DELETE_FOLDER )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(), cmd=DELETE_FOLDER )
				self.logRecvEvent( shortEvt = "folder deleted", tplEvt = tpl )
Esempio n. 5
0
    def hasReceivedRow(self,
                       timeout=1.0,
                       row=None,
                       rowIndex=None,
                       rowMax=None):
        """
		Waits to receive "response" event until the end of the timeout
		
		@param timeout: time max to wait to receive event 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)

        expected = templates.db(host=self.cfg['host'],
                                port=self.cfg['port'],
                                more=templates.response(row=row,
                                                        rowIndex=None,
                                                        rowMax=None))
        evt = self.received(expected=self.encapsule(db_event=expected),
                            timeout=timeout)
        return evt
Esempio n. 6
0
    def getTemplateResponse(self,
                            code="200",
                            phrase="OK",
                            version="HTTP/1.1",
                            headers={},
                            body=None,
                            overwriteCl=False):
        """
		"""
        __hdrs = {}

        # add additional headers
        __hdrs.update(self.getDefaultHeaders())
        __hdrs.update(headers)

        if body is not None:
            lenBod = len(body)
            if lenBod > 0: self.addContentLength(__hdrs, lenBod, overwriteCl)

        # prepare response template and send it
        tpl = templates.response(version=version,
                                 code=code,
                                 phrase=phrase,
                                 headers=__hdrs,
                                 body=body)
        return tpl
Esempio n. 7
0
	def addFolder(self, path, mode=511):
		"""
		Add a folder
		
		@param path: path folder
		@type path: string
		
		@param mode: folder mode in octal (default=511 (0777))
		@type mode: integer
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.add(path=path, mode="%s" % mode), cmd=ADD_FOLDER )
		tpl.addRaw( path )
		self.logSentEvent( shortEvt = "add", tplEvt = tpl )

		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  ADD_FOLDER, 'path': path, 'mode':  mode }
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				self.ssh().channel().mkdir(path, mode)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=ADD_FOLDER )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(), cmd=ADD_FOLDER )
				self.logRecvEvent( shortEvt = "folder added", tplEvt = tpl )
Esempio n. 8
0
	def deleteFile(self, filename):
		"""
		Delete a file
		
		@param filename:  the name of the file to delete
		@type filename: string
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
			
		tpl = self.encapsule( sftp_event=templates.delete(filename=filename), cmd=DELETE_FILE )
		tpl.addRaw( filename )
		self.logSentEvent( shortEvt = "delete file", tplEvt = tpl )
		
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  DELETE_FILE, 'filename': filename }
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				self.ssh().channel().remove(filename)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=DELETE_FILE )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(), cmd=DELETE_FILE )
				self.logRecvEvent( shortEvt = "file deleted", tplEvt = tpl )
Esempio n. 9
0
	def putFile(self, toFilename, fromFilename=None, rawContent=None):
		"""
		Put file  in binary transfer mode.
		
		@param toFilename: path of the destination file
		@type toFilename: string			
		
		@param fromFilename: the file to upload
		@type fromFilename: string/None		
		
		@param rawContent: raw content to upload
		@type rawContent: string/None			
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		if fromFilename is None and rawContent is None:
			raise Exception("please to choose a type of upload")
		
		if fromFilename is not None:
			tpl = self.encapsule( sftp_event=templates.put(fromPath=fromFilename, toPath=toFilename), cmd=PUT_FILE )
			tpl.addRaw( fromFilename )
		if rawContent is not None:
			tpl = self.encapsule( sftp_event=templates.put(content=rawContent, toPath=toFilename) , cmd=PUT_FILE )
			tpl.addRaw( rawContent )
		self.logSentEvent( shortEvt = "put file", tplEvt = tpl )
	
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  PUT_FILE, 'to-filename': toFilename, }
			if fromFilename is not None:
				agent_cmd[ 'from-filename'] = fromFilename
			else:
				agent_cmd[ 'from-filename'] = ''
			if rawContent is not None:
				agent_cmd[ 'raw-content'] = rawContent
			else:
				agent_cmd[ 'raw-content'] = ''
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				if rawContent is not None:
					myfile = io.BytesIO( rawContent )
					
				if fromFilename is not None:
					myfile = open(fromFilename, 'rb')
				
				rsp = self.ssh().channel().putfo(myfile, toFilename)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=PUT_FILE )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(rsp=str(rsp)), cmd=PUT_FILE )
				tpl.addRaw( str(rsp) )
				self.logRecvEvent( shortEvt = "file uploaded", tplEvt = tpl )
Esempio n. 10
0
	def receivedNotifyFromAgent(self, data):
		"""
		Function to reimplement
		"""
		self.debug( data )
		if 'cmd' in data:
			if data['cmd'] == AGENT_INITIALIZED:
					tpl = TestTemplatesLib.TemplateMessage()
					layer = TestTemplatesLib.TemplateLayer('AGENT')
					layer.addKey("ready", True)
					layer.addKey(name='name', data=self.cfg['agent']['name'] )
					layer.addKey(name='type', data=self.cfg['agent']['type'] )
					tpl.addLayer(layer= layer)
					self.logRecvEvent( shortEvt = "Agent Is Ready" , tplEvt = tpl )	
			
			elif data['cmd'] == 'Connect':
				self.connPtr  = FakePtr()
				tpl = templates.db( host=data['host'], port=data['port'], user=data['user'], password=data['password'], 
																more=templates.connected() )
				self.logRecvEvent( shortEvt = "connected", tplEvt = self.encapsule(db_event=tpl) )
			
			elif data['cmd'] == 'Disconnect':
				tpl = templates.db(host=data['host'], port=data['port'], user=data['user'], 
															password=data['password'], more=templates.disconnected() )
				self.logRecvEvent( shortEvt = "disconnected", tplEvt = self.encapsule(db_event=tpl) )
				self.connPtr = None
				
			elif data['cmd'] == 'Query':
				tpl = templates.db( host=data['host'], port=data['port'], user=data['user'],  password=data['password'], 
																		more=templates.response(row=data['row'], rowIndex=data['row-index'], rowMax=data['row-max']) )
				if self.logEventReceived:
					self.logRecvEvent( shortEvt = "row", tplEvt = self.encapsule(db_event=tpl) )
	
				self.handleIncomingRow(lower=self.encapsule(db_event=tpl) )

			elif data['cmd'] == 'Executed':
				tpl = templates.db( host=data['host'], port=data['port'], user=data['user'],  password=data['password'], 
																	 more=templates.executed(nbChanged=data['nb-changed'])  )
				self.logRecvEvent( shortEvt = "executed", tplEvt = self.encapsule(db_event=tpl) )
				
			elif data['cmd'] == 'Terminated':
					tpl = templates.db( host=data['host'], port=data['port'], user=data['user'],  password=data['password'],
																		 more=templates.terminated(nbRow=['nb-rows'])  )
					self.logRecvEvent( shortEvt = "terminated", tplEvt = self.encapsule(db_event=tpl) )
				
			else:
				self.error("unknown command received: %s" % data["cmd"])
		else:
			self.error("no cmd detected %s" % data)
Esempio n. 11
0
    def sendHttp(self,
                 clientId,
                 code="200",
                 phrase="OK",
                 version="HTTP/1.1",
                 headers={},
                 body=None,
                 overwriteCl=False):
        """
		Send a response.
		
		@param clientId: client id
		@type clientId: integer
		
		@param code: code  (default=200)
		@type code: string
		
		@param phrase: phrase  (default=OK)
		@type phrase: string
		
		@param version: http version (default=HTTP/1.1)
		@type version: string

		@param headers: additional headers to add {key:value, ...}
		@type headers: dict

		@param body: http content of the response
		@type body: string/none
		"""
        __hdrs = {}

        # add additional headers
        __hdrs.update(self.getDefaultHeaders())
        __hdrs.update(headers)

        if body is not None:
            lenBod = len(body)
            if lenBod > 0: self.addContentLength(__hdrs, lenBod, overwriteCl)

        # prepare response template and send it
        tpl = templates.response(version=version,
                                 code=code,
                                 phrase=phrase,
                                 headers=__hdrs,
                                 body=body)

        self.sendResponse(clientId=clientId, tpl=tpl)
Esempio n. 12
0
	def hasReceivedResponse(self, timeout=1.0):
		"""
		Wait to receive "response" event
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float	

		@return: an event matching with the template or None otherwise
		@rtype: templatemessage		
		"""
		TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout)
		
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl_expected = self.encapsule( sftp_event=templates.response() )
		evt = self.received( expected = tpl_expected, timeout = timeout )
		return evt
Esempio n. 13
0
	def hasReceivedResponse(self, timeout=1.0):
		"""
		Wait to receive "response" event
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float	

		@return: an event matching with the template or None otherwise
		@rtype: templatemessage		
		"""
		if not ( isinstance(timeout, int) or isinstance(timeout, float) ) or isinstance(timeout,bool): 
			raise TestAdapter.ValueException(TestAdapter.caller(), "timeout argument is not a float or integer (%s)" % type(timeout) )
		
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl_expected = self.encapsule( sftp_event=templates.response() )
		evt = self.received( expected = tpl_expected, timeout = timeout )
		return evt
Esempio n. 14
0
	def hasUploadedFolder(self, timeout=1.0, nbFiles=None):
		"""
		Waiting uploaded event for folder
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float	
		
		@param nbFiles: number of file succesfully uploaded
		@type nbFiles: none/int	
		
		@return: an event matching with the template or None otherwise
		@rtype: templatemessage		
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl_expected = self.encapsule( sftp_event=templates.response(rsp=nbFiles), cmd=PUT_FOLDER )
		evt = self.received( expected = tpl_expected, timeout = timeout )
		return evt
Esempio n. 15
0
	def putFolder(self, fromPath, toPath, overwrite=False):
		"""
		Put folder content with recursive mode
		
		@param fromPath: local path to upload
		@type fromPath: string
		
		@param toPath: remote destination
		@type toPath: string
		
		@param overwrite: overwrite file/folder if already exists in remote side
		@type overwrite: boolean
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.put_folder(fromPath=fromPath, toPath=toPath, overwrite=overwrite), cmd=PUT_FOLDER )
		tpl.addRaw( fromPath )
		self.logSentEvent( shortEvt = "put folder", tplEvt = tpl )
		
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  PUT_FOLDER, 'from-path': fromPath, 'overwrite':  overwrite}
			if toPath is not None:
				agent_cmd[ 'to-path'] = toPath
			else:
				agent_cmd[ 'to-path'] = ''
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				rsp = 0 # nb files uploaded
				rsp = self.__putFolder(fromPath=fromPath, toPath=toPath, overwrite=overwrite)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=PUT_FOLDER )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(rsp=str(rsp)), cmd=PUT_FOLDER )
				tpl.addRaw( str(rsp) )
				self.logRecvEvent( shortEvt = "folder uploaded", tplEvt = tpl )
Esempio n. 16
0
	def getFile(self,  filename, toPrivate=False):
		"""
		Get file content 
	
		@param filename: file name 
		@type filename: string				
		
		@param toPrivate: save the file in the private area on True (default=False)
		@type toPrivate: boolean			
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.get(filename=filename), cmd=GET_FILE )
		tpl.addRaw( filename )
		self.logSentEvent( shortEvt = "get file", tplEvt = tpl )
		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  GET_FILE, 'filename': filename}
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				myfile = io.BytesIO()
				self.ssh().channel().getfo(filename, myfile)
				read_data = myfile.getvalue() 
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=GET_FILE )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				if toPrivate:
					head, tail = os.path.split(filename)
					self.privateSaveFile(destname=tail, data=read_data)
					
				# log event
				tpl = self.encapsule( sftp_event=templates.response(content=read_data), cmd=GET_FILE )
				tpl.addRaw( read_data )
				self.logRecvEvent( shortEvt = "file downloaded", tplEvt = tpl )
Esempio n. 17
0
	def listingFolder(self, path, extended=False):
		"""
		Listing the folder.
		
		@param path: current path folder
		@type path: string

		@param extended: extended mode (default=False)
		@type extended: boolean
		"""
		if not self.__logged:
			self.warning( "not logged" )
			return
		
		tpl = self.encapsule( sftp_event=templates.listing(path=path), cmd=LISTING_FOLDER )
		if path is not None: tpl.addRaw( path )
		self.logSentEvent( shortEvt = "listing", tplEvt = tpl )

		if self.cfg['agent-support']:
			agent_cmd = {'cmd':  LISTING_FOLDER, 'path': path, 'extended': extended}
			self.ssh().notifyAgent(cfg=agent_cmd)
		else:
			try:
				if extended:
					ret = []
					ret_tmp = self.ssh().channel().listdir_attr(path=path)
					for attr in ret_tmp:
						ret.append( str(attr) )
				else:
					ret = self.ssh().channel().listdir(path=path)
			except Exception as e:
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=str(e)), cmd=LISTING_FOLDER )
				tpl.addRaw( str(e) )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
			else:
				tpl = self.encapsule( sftp_event=templates.response(rsp='\n'.join(ret) ), cmd=LISTING_FOLDER )
				tpl.addRaw( '\n'.join(ret) )
				self.logRecvEvent( shortEvt = "folder listing", tplEvt = tpl )
Esempio n. 18
0
    def query(self, query, queryName=None):
        """
		Query the database
		
		@param query: sql query
		@type query: string			
		
		@param queryName: query identifier
		@type queryName: string/none	
		"""
        if self.connPtr is None:
            self.warning('connect first to the database')
        else:
            self.debug('query db')
            # log query event
            tpl = templates.db(host=self.cfg['host'],
                               port=self.cfg['port'],
                               user=self.cfg['user'],
                               password=self.cfg['password'],
                               more=templates.query(query=query))
            self.logSentEvent(shortEvt="query",
                              tplEvt=self.encapsule(db_event=tpl))

            if self.cfg['agent-support']:
                remote_cfg = {
                    'cmd': 'Query',
                    'query': query,
                    'user': self.cfg['user'],
                    'password': self.cfg['password'],
                    'host': self.cfg['host'],
                    'port': self.cfg['port']
                }
                self.sendNotifyToAgent(data=remote_cfg)
            else:
                try:
                    cursor = self.connPtr.cursor()
                    cursor.execute(query)

                    i = 0
                    # log response event
                    tpl = templates.db(host=self.cfg['host'],
                                       port=self.cfg['port'],
                                       user=self.cfg['user'],
                                       password=self.cfg['password'],
                                       more=templates.executed(
                                           status=cursor.statusmessage,
                                           nbChanged=str(cursor.rowcount)))
                    self.logRecvEvent(shortEvt="executed",
                                      tplEvt=self.encapsule(db_event=tpl))

                    try:
                        row = cursor.fetchone()
                        while row:
                            i += 1
                            self.debug(row)
                            # as dict
                            fields = map(lambda x: x[0], cursor.description)
                            ret = dict(zip(fields, row))

                            # each value as str
                            ret_str = {}
                            if queryName is not None:
                                ret_str['query-name'] = queryName
                            for k, v in ret.items():
                                ret_str[k] = str(v)

                            # log response event
                            tpl = templates.db(host=self.cfg['host'],
                                               port=self.cfg['port'],
                                               user=self.cfg['user'],
                                               password=self.cfg['password'],
                                               more=templates.response(
                                                   row=ret_str,
                                                   rowIndex=i,
                                                   rowMax=cursor.rowcount))
                            if self.logEventReceived:
                                self.logRecvEvent(
                                    shortEvt="row",
                                    tplEvt=self.encapsule(db_event=tpl))

                            self.handleIncomingRow(lower=self.encapsule(
                                db_event=tpl))

                            row = cursor.fetchone()
                    except psycopg2.ProgrammingError as e:
                        pass  # no more to read

                    # log response event
                    tpl = templates.db(host=self.cfg['host'],
                                       port=self.cfg['port'],
                                       user=self.cfg['user'],
                                       password=self.cfg['password'],
                                       more=templates.terminated(nbRow=i))
                    self.logRecvEvent(shortEvt="terminated",
                                      tplEvt=self.encapsule(db_event=tpl))

                    # close the cursor and commit
                    cursor.close()
                    self.connPtr.commit()
                except psycopg2.Error as e:
                    self.onError(err=(0, str(e)))
                except Exception as e:
                    self.error(str(e))
                else:
                    pass
Esempio n. 19
0
	def decode(self, rsp, nomoredata=False, request=False):
		"""
		Try to decode some data to a valid payload.
		"""
		if len(rsp) == 0 and nomoredata:
			return (DECODING_NOTHING_TODO, None, None, None)

		# strict mode for the separator between headers and body ?
		if '\r\n\r\n' not in rsp:
			if not self.strictMode:
				if '\n\r\n' in rsp:
					rsp = rsp.replace('\n\r\n', '\r\n\r\n' )
				
		bod = ''
		# split all headers
		hdrs = rsp.split(self.sep)

		# parse response code and phrase
		rspDecoded = hdrs[0].split(" ", 2)
		if len(rspDecoded) != 3:
			if request:
				self.debug('malformed http code request: %s' % rsp)
			else:
				self.debug('malformed http code response: %s' % rsp)
			return (DECODING_NEED_MORE_DATA, None, None, None)
		if request:
			httpMethod = rspDecoded[0]
			httpUri = rspDecoded[1]
			httpVersion = rspDecoded[2]
			summary = "%s %s %s" % (httpMethod, httpUri, httpVersion)
		else:
			httpVersion = rspDecoded[0]
			httpCode = rspDecoded[1]
			httpPhrase = (rspDecoded[2].decode("ISO8859-1")).encode("utf-8")
			summary = "%s %s %s" % (httpVersion, httpCode, httpPhrase)
	
		# parse hdrs to list
		http_body_detected = False
		content_type_hdr = None
		content_length_hdr = None
		transfer_encoding_hdr = None

		hdrs_dict = {}
		i = 1
		for hdr in hdrs[1:]: 
			i += 1
			h = hdr.strip() # remove spaces before and after
			if not hdr:
				http_body_detected = True
				break # reached body and its empty line
				
			fv = hdr.split(':', 1)
			if len(fv) != 2:
				raise Exception('invalid http headers: %s' % fv )
			# rfc 2616: Field names are case-insensitive
			f = fv[0].lower().strip() # extract field and remove spaces
			v = fv[1].strip() # extract value and remove spaces
			
			if f == "content-type": content_type_hdr = v
			if f == "content-length": content_length_hdr = v
			if f == "transfer-encoding": transfer_encoding_hdr = v
				
			if hdrs_dict.has_key(f):
				if isinstance( hdrs_dict[f], TestTemplatesLib.TemplateLayer):
					tpl.addKey(name="%s" % tpl.getLenItems(), data=v )
				else:
					tpl = TestTemplatesLib.TemplateLayer('')
					tpl.addKey(name="%s" % tpl.getLenItems(), data=hdrs_dict[f])
					tpl.addKey(name="%s" % tpl.getLenItems(), data=v )
					hdrs_dict[f] = tpl
			else:
				hdrs_dict[f] = v

		if not http_body_detected:
			self.debug("body not detected")
			return (DECODING_NEED_MORE_DATA, None, None, None)
		

		# transfer-encoding header is present ?
		if transfer_encoding_hdr is not None:
			self.debug( "transfer encoding header detected (%s)" % str(transfer_encoding_hdr) )
			if transfer_encoding_hdr == 'chunked':
				try:
					if not hdrs[i].strip():
						self.debug("wait for the chunk size in a next payload" )
						return (DECODING_NEED_MORE_DATA, None, None, None) # Let's wait for the chunk size in a next payload

					chunkSize = int(hdrs[i].strip(), 16)
					self.debug("chunk size detected: %s" % chunkSize )
					remainingPayload = '\r\n'.join(hdrs[i+1:])

					# consuming chunk with thr exactly chunkSize characters then followed by an empty line or possibly another chunksize
					while chunkSize != 0:
						chunk = remainingPayload[:chunkSize]
						if len(chunk) < chunkSize:
							self.debug("current chunk (%s) lower than chunk size expected (%s), need more data" % ( len(chunk), chunkSize) )
							return (DECODING_NEED_MORE_DATA, None, None, None)
						bod += chunk

						remainingPayload = remainingPayload[chunkSize:]

						lines = remainingPayload.split('\r\n')  # ['', '0', '', '']
						if lines[0]:
							# should be an empty line... 
							raise Exception("No chunk boundary at the end of the chunk. Invalid data.")
						if not lines[1]:
							self.debug("No next chunk size yet" )
							return (DECODING_NEED_MORE_DATA, None, None, None) # No next chunk size yet
						else:
							chunkSize = int(lines[1].strip(), 16)
							self.debug("next chunk size detected: %s" % chunkSize )
						
						# fix issue, final chunk size received but crlf is missing
						if len(lines) <= 2:
							self.debug("next chunk size received but crlf is missing, waiting more data" )
							return (DECODING_NEED_MORE_DATA, None, None, None) # final chunk size received but crlf is missing
						# end of fix
						
						remainingPayload = '\r\n'.join(lines[2:])
				except IndexError:
					return (DECODING_NEED_MORE_DATA, None, None, None)
			else:
				raise Exception("not yet supported")
		
		# content-length header is present ?
		else:
			bod  ="\r\n".join(hdrs[i:])
			if content_length_hdr is not None:
				self.debug( "content-length header detected (%s)" % str(content_length_hdr) )
				cl = int(content_length_hdr)
				bl = len(bod)
				
				truncate_body = False
				if self.truncateBody:
					bod = '%s ...stream truncated....' % bod[:100]
					truncate_body = True

				if not ( truncate_body ):	
					if bl < cl:
						return (DECODING_NEED_MORE_DATA, None, None, None)
					elif bl > cl:
						# Truncate the body
						bod = bod[:cl]
			else:
				self.debug( "no transfer encoding header or content-length header" )
				
				if request:
					if http_body_detected and len(bod) == 0: 
						nomoredata = True

				# if the status code is 100 or 200 OK from a proxy, discard the first response and read the next one instead. 
				if len(hdrs) == 3 and len(bod) == 0:
					if len(hdrs[0]) and not len(hdrs[1]) and not len(hdrs[2]):
						self.debug('message with one line only detected, discard and read the next one')
						bod = ''
						nomoredata = True

				if self.websocketMode:
					self.debug( "websocket mode activated on codec" )
					if http_body_detected and len(bod) == 0: 
						nomoredata = True
				
				if not request:
					if http_body_detected and len(bod) == 0 and httpCode == "204": 
						self.debug('204 no content detected')
						bod = ''
						nomoredata = True
						
				# No chunk, no content-length, no body separator detected
				# wait until the end of the connection
				if not nomoredata:
					return (DECODING_NEED_MORE_DATA, None, None, None)

		# content-type header is present ?
		if content_type_hdr is not None:
			self.debug( "content type header detected (%s)" % str(content_type_hdr) )
			content_type = content_type_hdr.split(";", 1)[0] # split the type of data
			# update summary with content type
			summary = "%s (%s)" % (summary, content_type)
			
			# decode the body with the charset defined in the header content-type and re-encode to utf8
			if content_type == "text/html" or content_type == "text/plain" :
				if len(bod)> 0: # fix error in adapter 2.1.0, decode body only if the length > 0
					content_type_charset = content_type_hdr.split("charset=")
					if len(content_type_charset)==1:
						self.debug("Codec: charset is missing, decode body with iso-8859-1")
						content_type_charset = "iso-8859-1" 
					else:
						content_type_charset = content_type_hdr.split("charset=")[1].split(";", 1)[0]
					try:
						bod_decoded = bod.decode(content_type_charset)
						bod = bod_decoded.encode("utf-8")
					except Exception as e:
						self.warning('Codec: unable to decode body with charset: %s' % content_type_charset)

		# create template
		if request:
			ret = templates.request(method=httpMethod, uri=httpUri, version=httpVersion, headers=hdrs_dict, body=bod) 
		else:
			if not len(hdrs_dict):
				hdrs_dict = None
			if not len(bod):
				bod=None
			ret = templates.response(version=httpVersion, code=httpCode, phrase=httpPhrase, headers=hdrs_dict, body=bod) 
		if not request:
			if httpCode == "100":
				left_data = "\r\n".join(hdrs[i:])
				ret.addRaw(hdrs[0])
				return (DECODING_OK_CONTINUE, ret, summary, left_data ) 
		return (DECODING_OK, ret, summary, None)
Esempio n. 20
0
	def onSftpEvent(self, event):
		"""
		"""
		if 'sftp-event' in event:
			
			if event['sftp-event'] == 'response-error' :
				tpl = self.encapsule( sftp_event=templates.response_error(rsp=event['err']), cmd=event['cmd'] )
				tpl.addRaw( event['err'] )
				self.logRecvEvent( shortEvt = "response error", tplEvt = tpl )
				
			if event['sftp-event'] == 'folder-listing':
				tpl = self.encapsule( sftp_event=templates.response(rsp=event['rsp'] ), cmd=LISTING_FOLDER )
				tpl.addRaw( event['rsp'] )
				self.logRecvEvent( shortEvt = "folder listing", tplEvt = tpl )
				
			if event['sftp-event'] == 'folder-added':
				tpl = self.encapsule( sftp_event=templates.response(), cmd=ADD_FOLDER )
				self.logRecvEvent( shortEvt = "folder added", tplEvt = tpl )

			if event['sftp-event'] == 'file-renamed':
				tpl = self.encapsule( sftp_event=templates.response(), cmd=RENAME_FILE  )
				self.logRecvEvent( shortEvt = "file renamed", tplEvt = tpl )
				
			if event['sftp-event'] == 'file-deleted':
				tpl = self.encapsule( sftp_event=templates.response(), cmd=DELETE_FILE  )
				self.logRecvEvent( shortEvt = "file deleted", tplEvt = tpl )
				
			if event['sftp-event'] == 'folder-renamed':
				tpl = self.encapsule( sftp_event=templates.response() , cmd=RENAME_FOLDER )
				self.logRecvEvent( shortEvt = "folder renamed", tplEvt = tpl )
				
			if event['sftp-event'] == 'folder-deleted':
				tpl = self.encapsule( sftp_event=templates.response(), cmd=DELETE_FOLDER )
				self.logRecvEvent( shortEvt = "folder deleted", tplEvt = tpl )

			if event['sftp-event'] == 'file-downloaded':
				tpl = self.encapsule( sftp_event=templates.response(content=event['content']), cmd=GET_FILE )
				tpl.addRaw( event['content'] )
				self.logRecvEvent( shortEvt = "file downloaded", tplEvt = tpl )
				
			if event['sftp-event'] == 'file-uploaded':
				tpl = self.encapsule( sftp_event=templates.response(rsp=event['rsp']), cmd=PUT_FILE )
				tpl.addRaw( event['rsp'] )
				self.logRecvEvent( shortEvt = "file uploaded", tplEvt = tpl )
				
			if event['sftp-event'] == 'folder-downloaded':
				tpl = self.encapsule( sftp_event=templates.response(content=event['content']), cmd=GET_FOLDER )
				tpl.addRaw( event['content'] )
				self.logRecvEvent( shortEvt = "folder downloaded", tplEvt = tpl )
				
			if event['sftp-event'] == 'folder-uploaded':
				tpl = self.encapsule( sftp_event=templates.response(rsp=event['rsp']), cmd=PUT_FOLDER )
				tpl.addRaw( event['rsp'] )
				self.logRecvEvent( shortEvt = "folder uploaded", tplEvt = tpl )
				
			if event['sftp-event'] == 'wait-file':
				tpl = self.encapsule( sftp_event=templates.wait_file(path=event['path'], filename=event['filename'], result=event['result']), cmd=WAIT_FILE )
				self.logRecvEvent( shortEvt = "wait file", tplEvt = tpl )
				
			if event['sftp-event'] == 'wait-folder':
				tpl = self.encapsule( sftp_event=templates.wait_folder(path=event['path'], folder=event['folder'], result=event['result']), cmd=WAIT_FOLDER )
				self.logRecvEvent( shortEvt = "wait folder", tplEvt = tpl )
Esempio n. 21
0
    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	
		"""
        if not (isinstance(timeout, int) or isinstance(timeout, float)):
            raise TestAdapter.ValueException(
                TestAdapter.caller(),
                "timeout argument is not a float or integer (%s)" %
                type(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)