def testSize(self): b = MonitoredBuffer() self.assertEqual(b.size(), 0) b.append('foo') self.assertEqual(b.size(), 3) b.append('bar') self.assertEqual(b.size(), 6)
def testAddMonitor(self): b = MonitoredBuffer() # Set the monitor callback up. def monitor_cb(thedata, *args, **kwargs): thedata['args'] = args thedata['kwargs'] = kwargs data = {} b.add_monitor('abc', partial(monitor_cb, data)) # Test some non-matching data. b.append('aaa') self.assertEqual(data, {}) b.append('aaa') self.assertEqual(data, {}) # Test some matching data. b.append('abc') self.assertEqual(len(data.get('args')), 2) self.assertEqual(data.get('args')[0], 0) self.assertEqual(data.get('args')[1].group(0), 'abc') self.assertEqual(data.get('kwargs'), {}) # Make sure that the same monitor is not called again. data.pop('args') data.pop('kwargs') b.append('bbb') self.assertEqual(data, {}) # Test some matching data. b.append('abc') self.assertEqual(len(data.get('args')), 2) self.assertEqual(data.get('args')[0], 0) self.assertEqual(data.get('args')[1].group(0), 'abc') self.assertEqual(data.get('kwargs'), {})
def testClear(self): b = MonitoredBuffer() self.assertEqual(str(b), '') b.append('foo') self.assertEqual(str(b), 'foo') b.clear() self.assertEqual(str(b), '') b.clear() self.assertEqual(str(b), '')
def testAppend(self): b = MonitoredBuffer() self.assertEqual(str(b), '') b.append('foo') self.assertEqual(str(b), 'foo') b.append('bar') self.assertEqual(str(b), 'foobar') b.append('doh') self.assertEqual(str(b), 'foobardoh')
def testTail(self): b = MonitoredBuffer() self.assertEqual(str(b), '') self.assertEqual(b.tail(0), '') self.assertEqual(b.tail(10), '') b.append('foobar') self.assertEqual(str(b), 'foobar') self.assertEqual(b.tail(0), '') self.assertEqual(b.tail(1), 'r') self.assertEqual(b.tail(6), 'foobar') self.assertEqual(b.tail(10), 'foobar')
def testHead(self): b = MonitoredBuffer() self.assertEqual(str(b), '') self.assertEqual(b.head(0), '') self.assertEqual(b.head(10), '') b.append('foobar') self.assertEqual(str(b), 'foobar') self.assertEqual(b.head(0), '') self.assertEqual(b.head(1), 'f') self.assertEqual(b.head(6), 'foobar') self.assertEqual(b.head(10), 'foobar')
def testPop(self): b = MonitoredBuffer() self.assertEqual(str(b), '') self.assertEqual(b.pop(0), '') self.assertEqual(str(b), '') self.assertEqual(b.pop(10), '') self.assertEqual(str(b), '') b.append('foobar') self.assertEqual(str(b), 'foobar') self.assertEqual(b.pop(0), '') self.assertEqual(str(b), 'foobar') self.assertEqual(b.pop(2), 'fo') self.assertEqual(str(b), 'obar') b.append('doh') self.assertEqual(b.pop(10), 'obardoh') self.assertEqual(str(b), '')
def __init__(self, driver=None, stdout=None, stderr=None, debug=0, connect_timeout=30, timeout=30, logfile=None, termtype='dumb', verify_fingerprint=True, account_factory=None): """ Constructor. The following events are provided: - data_received_event: A packet was received from the connected host. - otp_requested_event: The connected host requested a one-time-password to be entered. :keyword driver: Driver()|str :keyword stdout: Where to write the device response. Defaults to os.devnull. :keyword stderr: Where to write debug info. Defaults to stderr. :keyword debug: An integer between 0 (no debugging) and 5 (very verbose debugging) that specifies the amount of debug info sent to the terminal. The default value is 0. :keyword connect_timeout: Timeout for the initial TCP connection attempt :keyword timeout: See set_timeout(). The default value is 30. :keyword logfile: A file into which a log of the conversation with the device is dumped. :keyword termtype: The terminal type to request from the remote host, e.g. 'vt100'. :keyword verify_fingerprint: Whether to verify the host's fingerprint. :keyword account_factory: A function that produces a new :class:`Account`. """ self.data_received_event = Event() self.otp_requested_event = Event() self.os_guesser = OsGuesser() self.auto_driver = driver_map[self.guess_os()] self.proto_authenticated = False self.app_authenticated = False self.app_authorized = False self.manual_user_re = None self.manual_password_re = None self.manual_prompt_re = None self.manual_error_re = None self.manual_login_error_re = None self.driver_replaced = False self.host = None self.port = None self.last_account = None self.termtype = termtype self.verify_fingerprint = verify_fingerprint self.manual_driver = None self.debug = debug self.connect_timeout = connect_timeout self.timeout = timeout self.logfile = logfile self.response = None self.buffer = MonitoredBuffer() self.account_factory = account_factory self.send_data = None if stdout is None: self.stdout = open(os.devnull, 'w') else: self.stdout = stdout if stderr is None: self.stderr = sys.stderr else: self.stderr = stderr if logfile is None: self.log = None else: self.log = open(logfile, 'a') # set manual_driver if driver is not None: if isinstance(driver, str): if driver in driver_map: self.manual_driver = driver_map[driver] else: self._dbg(1, 'Invalid driver string given. Ignoring...') elif isinstance(driver, Driver): self.manual_driver = driver else: self._dbg(1, 'Invalid driver given. Ignoring...')
def testConstructor(self): MonitoredBuffer() with TemporaryFile() as f: MonitoredBuffer(f)