예제 #1
0
    def test_read_pkg_HeaderAndData(self):
        header = a2b('000a05bb8100aa')
        data = a2b('bb810000cc')
        self.ser.read.side_effect = [header, data]

        assert self.dev.read_pkg() == header + data
        assert self.ser.read.call_args_list == [call(7), call(5)]
예제 #2
0
    def test_read_pkg_HeaderAndData(self):
        header = a2b('000a05bb8100aa')
        data = a2b('bb810000cc')
        self.ser.read.side_effect = [header, data]
        self.dev.is_open = True

        assert self.dev.read_pkg() == header + data
        assert self.ser.read.call_args_list == [call(7), call(5)]
예제 #3
0
    def test_read_pkg_OnlyHeader(self):
        header = a2b('fd0200bb81002d')
        self.ser.read.side_effect = [header]
        self.dev.is_open = True

        assert self.dev.read_pkg() == header
        assert self.ser.read.call_args_list == [call(7)]
예제 #4
0
 def test_write_pkg_FailsIfDeviceIsNotOpen(self):
     packet = a2b('ffffff')
     self.dev.is_open = False
     with pytest.raises(DeviceError,
                        message="Couldn't write packet, device is closed!"):
         self.dev.write_pkg(packet)
     self.ser.write.assert_not_called()
예제 #5
0
 def test_read(self):
     pkg = a2b('ff')
     self.ser.read.return_value = pkg
     self.dev.is_open = True
     assert self.dev.read() == pkg
     self.ser.read.assert_called_once_with(1)
     self.ser.flushInput.assert_called_once_with()
예제 #6
0
    def test_sync(self):
        address = 16777215
        table = 'SYSTEM_PARAMETER_TABLE'
        param = 'Baudrate'
        baudrate = 9600
        value = baudrate // 100
        ad_param = 0
        package = a2b('fd0b05ffffffaf0400600054')

        expected_calls = [
            call.cmd.set_parameter(address, table, param, [value], ad_param),
            call.dev.close_device(),
            call.dev.open_device(baudrate=1200),
            call.dev.write_pkg(package),
            call.dev.close_device(),
            call.dev.open_device(baudrate=2400),
            call.dev.write_pkg(package),
            call.dev.close_device(),
            call.dev.open_device(baudrate=4800),
            call.dev.write_pkg(package),
            call.dev.close_device(),
            call.dev.open_device(baudrate=9600),
            call.dev.write_pkg(package),
            call.dev.close_device(),
            call.dev.open_device(baudrate=baudrate)
        ]

        self.cmd.set_parameter.return_value = package
        self.dev.write_pkg.return_value = True

        self.bus.sync(baudrate=baudrate)
        assert self.bus.bus_synced
        assert self.manager.mock_calls == expected_calls
예제 #7
0
 def test__unpack_data_ToLong(self):
     data = '\xff' * 253
     crc = self.crc.calc_crc(data)
     pkg = a2b('fd3cffbb8100e0') + data + crc
     with pytest.raises(PackageError) as e:
         self.pkg.unpack(pkg)
     assert e.value.message == "Data block bigger than 252Bytes!"
예제 #8
0
 def test__unpack_head_FaultyCRC(self):
     data = '\xff' * 252
     crc = self.crc.calc_crc(data)
     pkg = a2b('fd3cffbb8100f0') + data + crc
     with pytest.raises(PackageError) as e:
         self.pkg.unpack(pkg)
     assert e.value.message == "Package with faulty header CRC!"
예제 #9
0
 def test_set_parameter_WrongTable(self):
     pkg = a2b('0011001a790095')
     serno = 31002
     table = 'SYSTEM_PARAMETER_TABLE'
     with pytest.raises(ResponceError,
                        message="Wrong set command in responce!"):
         self.res.set_parameter(pkg, table, serno)
예제 #10
0
 def test_set_parameter_WrongSerno(self):
     pkg = a2b('0011001a790095')
     serno = 31003
     table = 'PROBE_CONFIGURATION_PARAMETER_TABLE'
     with pytest.raises(ResponceError,
                        message="Wrong serial number in responce!"):
         self.res.set_parameter(pkg, table, serno)
예제 #11
0
 def test__unpack_head_WithProbeErrorState(self):
     data = '\xff' * 252
     crc = self.crc.calc_crc(data)
     pkg = a2b('853cffbb8100d9') + data + crc
     with pytest.raises(PackageError) as e:
         self.pkg.unpack(pkg)
     assert e.value.message == "actual moisture is too small in DAC"
예제 #12
0
 def test__unpack_data_ToLong(self):
     data = b'\xff' * 253
     crc = self.crc.calc_crc(data)
     pkg = a2b('fd3cffbb8100e0') + data + crc
     with pytest.raises(PackageError,
                        message="Data block bigger than 252Bytes!"):
         self.pkg.unpack(pkg)
예제 #13
0
    def test_read_bytes(self):
        pkg = a2b('ffff')
        self.ser.read.side_effect = [pkg]
        self.dev.is_open = True

        assert self.dev.read_bytes(2) == pkg
        self.ser.read.assert_called_once_with(2)
예제 #14
0
 def test__unpack_head_FaultyCRC(self):
     data = b'\xff' * 252
     crc = self.crc.calc_crc(data)
     pkg = a2b('fd3cffbb8100f0') + data + crc
     with pytest.raises(PackageError,
                        message="Package with faulty header CRC!"):
         self.pkg.unpack(pkg)
예제 #15
0
 def test_set_parameter_WrongTable(self):
     pkg = a2b('0011001a790095')
     serno = 31002
     table = 'SYSTEM_PARAMETER_TABLE'
     with pytest.raises(ResponceError) as e:
         self.res.set_parameter(pkg, table, serno)
     assert e.value.message == "Wrong set command in responce!"
예제 #16
0
 def test_set_parameter_WrongSerno(self):
     pkg = a2b('0011001a790095')
     serno = 31003
     table = 'PROBE_CONFIGURATION_PARAMETER_TABLE'
     with pytest.raises(ResponceError) as e:
         self.res.set_parameter(pkg, table, serno)
     assert e.value.message == "Wrong serial number in responce!"
예제 #17
0
 def test__unpack_head_WithProbeErrorState(self):
     data = b'\xff' * 252
     crc = self.crc.calc_crc(data)
     pkg = a2b('853cffbb8100d9') + data + crc
     with pytest.raises(PackageError,
                        message="actual moisture is too small in DAC"):
         self.pkg.unpack(pkg)
예제 #18
0
    def test_read_pkg_OnlyHeader(self):
        header = a2b('fd0200bb81002d')
        self.ser.read.side_effect = [header]
        self.dev.is_open = True

        assert self.dev.read_pkg() == header
        assert self.ser.read.call_args_list == [call(7)]
예제 #19
0
 def test_read(self):
     pkg = a2b('ff')
     self.ser.read.return_value = pkg
     self.dev.is_open = True
     assert self.dev.read() == pkg
     self.ser.read.assert_called_once_with(1)
     self.ser.flushInput.assert_called_once_with()
예제 #20
0
    def test_read_bytes(self):
        pkg = a2b('ffff')
        self.ser.read.side_effect = [pkg]
        self.dev.is_open = True

        assert self.dev.read_bytes(2) == pkg
        self.ser.read.assert_called_once_with(2)
예제 #21
0
    def test_read_pkg_HeaderAndDataWithTimeout(self):
        pkg = a2b('000a05bb8100aa')
        self.ser.read.side_effect = [pkg, b'']
        with pytest.raises(DeviceError, message='Timeout reading data!'):
            self.dev.is_open = True
            self.dev.read_pkg()

        assert self.ser.read.call_args_list == [call(7), call(5)]
예제 #22
0
    def test_read_pkg_HeaderAndDataWithTimeout(self):
        pkg = a2b('000a05bb8100aa')
        self.ser.read.side_effect = [pkg, b'']
        with pytest.raises(DeviceError) as e:
            self.dev.read_pkg()

        assert e.value.message == 'Timeout reading data!'
        assert self.ser.read.call_args_list == [call(7), call(5)]
예제 #23
0
    def test_find_single_module(self):
        serno = 31002
        package = a2b('fd0800ffffff60')
        bytes_recv = a2b('000805ffffffd91a79000042')

        expected_calls = [
            call.cmd.get_negative_ack(),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_negative_ack(bytes_recv)
        ]

        self.cmd.get_negative_ack.return_value = package
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_negative_ack.return_value = serno

        assert self.bus.find_single_module() == serno
        assert self.manager.mock_calls == expected_calls
예제 #24
0
 def _load_skipped_mk(self, name, other_name):
     skipped_hk_mk = dict()
     with self.db as db:
         rows = db.execute(
             '''
             SELECT
                 *
             FROM
                 skipped_mk
             WHERE
                 my_identity = ? AND
                 to_identity = ?''', (name, other_name))
     for row in rows:
         mk = a2b(row['mk'])
         skipped_hk_mk[mk] = SkippedMessageKey(mk,
                                               hk=a2b(row['hkr']),
                                               timestamp=row['timestamp'])
     return skipped_hk_mk
예제 #25
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_find_single_module(self):
        serno = 31002
        package = a2b('fd0800ffffff60')
        bytes_recv = a2b('000805ffffffd91a79000042')

        expected_calls = [
            call.cmd.get_negative_ack(),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_negative_ack(bytes_recv)
        ]

        self.cmd.get_negative_ack.return_value = package
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_negative_ack.return_value = serno

        assert self.bus.find_single_module() == serno
        assert self.manager.mock_calls == expected_calls
예제 #26
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_probe_module_short(self):
        serno = 31002
        package = a2b('fd04001a790003')
        bytes_recv = a2b('24')

        expected_calls = [
            call.cmd.get_short_ack(serno),
            call.dev.write_pkg(package),
            call.dev.read_bytes(1),
            call.res.get_short_ack(bytes_recv, serno)
        ]

        self.cmd.get_short_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_bytes.return_value = bytes_recv
        self.res.get_short_ack.return_value = True

        assert self.bus.probe_module_short(serno)
        assert self.manager.mock_calls == expected_calls
예제 #27
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_probe_module_long(self):
        serno = 31002
        package = a2b('fd02001a79009f')
        bytes_recv = a2b('0002001a7900a7')

        expected_calls = [
            call.cmd.get_long_ack(serno),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_long_ack(bytes_recv, serno)
        ]

        self.cmd.get_long_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_long_ack.return_value = True

        assert self.bus.probe_module_long(serno)
        assert self.manager.mock_calls == expected_calls
예제 #28
0
    def test_probe_range(self):
        broadcast = 0b111100000000000000000000
        package = a2b('fd06000000f0d0')
        bytes_recv = a2b('ff')

        expected_calls = [
            call.cmd.get_range_ack(broadcast),
            call.dev.write_pkg(package),
            call.dev.read(),
            call.res.get_range_ack(bytes_recv)
        ]

        self.cmd.get_range_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read.return_value = bytes_recv
        self.res.get_range_ack.return_value = True

        assert self.bus.probe_range(broadcast)
        assert self.manager.mock_calls == expected_calls
예제 #29
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_probe_range(self):
        broadcast = 0b111100000000000000000000
        package = a2b('fd06000000f0d0')
        bytes_recv = a2b('ff')

        expected_calls = [
            call.cmd.get_range_ack(broadcast),
            call.dev.write_pkg(package),
            call.dev.read(),
            call.res.get_range_ack(bytes_recv)
        ]

        self.cmd.get_range_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read.return_value = bytes_recv
        self.res.get_range_ack.return_value = True

        assert self.bus.probe_range(broadcast)
        assert self.manager.mock_calls == expected_calls
예제 #30
0
    def test_probe_module_long(self):
        serno = 31002
        package = a2b('fd02001a79009f')
        bytes_recv = a2b('0002001a7900a7')

        expected_calls = [
            call.cmd.get_long_ack(serno),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_long_ack(bytes_recv, serno)
        ]

        self.cmd.get_long_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_long_ack.return_value = True

        assert self.bus.probe_module_long(serno)
        assert self.manager.mock_calls == expected_calls
예제 #31
0
    def test_probe_module_short(self):
        serno = 31002
        package = a2b('fd04001a790003')
        bytes_recv = a2b('24')

        expected_calls = [
            call.cmd.get_short_ack(serno),
            call.dev.write_pkg(package),
            call.dev.read_bytes(1),
            call.res.get_short_ack(bytes_recv, serno)
        ]

        self.cmd.get_short_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_bytes.return_value = bytes_recv
        self.res.get_short_ack.return_value = True

        assert self.bus.probe_module_short(serno)
        assert self.manager.mock_calls == expected_calls
예제 #32
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_get(self):
        serno = 31002
        table = 'SYSTEM_PARAMETER_TABLE'
        param = 'SerialNum'
        package = a2b('fd0a031a7900290100c4')
        bytes_recv = a2b('000a051a7900181a79000042')

        expected_calls = [
            call.cmd.get_parameter(serno, table, param),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_parameter(bytes_recv, table, param)
        ]

        self.cmd.get_parameter.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_parameter.return_value = (31002,)

        assert self.bus.get(serno, table, param) == (serno,)
        assert self.manager.mock_calls == expected_calls
예제 #33
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_get_eeprom_page(self):
        serno = 30001
        page_nr = 0
        page = [17, 47, 196, 78, 55, 2, 243, 231, 251, 61]
        package = a2b('fd3c0331750029ff0081')
        bytes_recv = a2b('003c0b1a790015112fc44e3702f3e7fb3dc5')

        expected_calls = [
            call.cmd.get_epr_page(serno, page_nr),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_epr_page(bytes_recv)
        ]

        self.cmd.get_epr_page.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_epr_page.return_value = page

        assert self.bus.get_eeprom_page(serno, page_nr) == page
        assert self.manager.mock_calls == expected_calls
예제 #34
0
    def test_get_eeprom_page(self):
        serno = 30001
        page_nr = 0
        page = [17, 47, 196, 78, 55, 2, 243, 231, 251, 61]
        package = a2b('fd3c0331750029ff0081')
        bytes_recv = a2b('003c0b1a790015112fc44e3702f3e7fb3dc5')

        expected_calls = [
            call.cmd.get_epr_page(serno, page_nr),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_epr_page(bytes_recv)
        ]

        self.cmd.get_epr_page.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_epr_page.return_value = page

        assert self.bus.get_eeprom_page(serno, page_nr) == page
        assert self.manager.mock_calls == expected_calls
예제 #35
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_set_eeprom_page(self):
        serno = 30001
        page_nr = 7
        page = [0, 0, 0, 0, 0, 0, 0, 0, 35, 255, 255, 0]
        package = a2b('fd3d0f317500f6ff07000000000000000023ffff007b')
        bytes_recv = a2b('003d001a79004c')

        expected_calls = [
            call.cmd.set_epr_page(serno, page_nr, page),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.set_epr_page(bytes_recv)
        ]

        self.cmd.set_epr_page.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.set_epr_page.return_value = True

        assert self.bus.set_eeprom_page(serno, page_nr, page)
        assert self.manager.mock_calls == expected_calls
예제 #36
0
    def test_get(self):
        serno = 31002
        table = 'SYSTEM_PARAMETER_TABLE'
        param = 'SerialNum'
        package = a2b('fd0a031a7900290100c4')
        bytes_recv = a2b('000a051a7900181a79000042')

        expected_calls = [
            call.cmd.get_parameter(serno, table, param),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.get_parameter(bytes_recv, table, param)
        ]

        self.cmd.get_parameter.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.get_parameter.return_value = (31002, )

        assert self.bus.get(serno, table, param) == (serno, )
        assert self.manager.mock_calls == expected_calls
예제 #37
0
    def test_set_eeprom_page(self):
        serno = 30001
        page_nr = 7
        page = [0, 0, 0, 0, 0, 0, 0, 0, 35, 255, 255, 0]
        package = a2b('fd3d0f317500f6ff07000000000000000023ffff007b')
        bytes_recv = a2b('003d001a79004c')

        expected_calls = [
            call.cmd.set_epr_page(serno, page_nr, page),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.set_epr_page(bytes_recv)
        ]

        self.cmd.set_epr_page.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.set_epr_page.return_value = True

        assert self.bus.set_eeprom_page(serno, page_nr, page)
        assert self.manager.mock_calls == expected_calls
예제 #38
0
def drbg_test(tpm):
    """Runs DRBG test case.

  Args:
    tpm: a tpm object used to communicate with the device

  Raises:
    subcmd.TpmTestError: on unexpected target responses
  """

    for test in test_inputs:
        drbg_op, drbg_params = test
        if drbg_op == DRBG_INIT:
            entropy, nonce, perso = drbg_params
            cmd = _drbg_init_cmd(drbg_op, a2b(entropy), a2b(nonce), a2b(perso))
            response = tpm.command(tpm.wrap_ext_command(subcmd.DRBG_TEST, cmd))
            if response != EMPTY_DRBG_RESPONSE:
                raise subcmd.TpmTestError(
                    "Unexpected response to DRBG_INIT: %s" %
                    (utils.hex_dump(wrapped_response)))
        elif drbg_op == DRBG_RESEED:
            entropy, inp1, inp2 = drbg_params
            cmd = _drbg_init_cmd(drbg_op, a2b(entropy), a2b(inp1), a2b(inp2))
            response = tpm.command(tpm.wrap_ext_command(subcmd.DRBG_TEST, cmd))
            if response != EMPTY_DRBG_RESPONSE:
                raise subcmd.TpmTestError(
                    "Unexpected response to DRBG_RESEED: %s" %
                    (utils.hex_dump(wrapped_response)))
        elif drbg_op == DRBG_GENERATE:
            inp, expected = drbg_params
            cmd = _drbg_gen_cmd(a2b(inp), a2b(expected))
            response = tpm.command(tpm.wrap_ext_command(subcmd.DRBG_TEST, cmd))
            if expected != '':
                result = response[12:]
                if a2b(expected) != result:
                    raise subcmd.TpmTestError(
                        'error:\nexpected %s\nreceived %s' % (utils.hex_dump(
                            a2b(expected)), utils.hex_dump(result)))
    print('%sSUCCESS: %s' % (utils.cursor_back(), 'DRBG test'))
예제 #39
0
 def test__unpack_head(self):
     # e.g. responce to probe_module_long(33211)
     data = {
         'header': {
             'state': 0,
             'cmd': 11,
             'length': 0,
             'serno': 33211
         },
         'data': None
     }
     pkg = a2b('000b00bb8100e6')
     assert self.pkg.unpack(pkg) == data
예제 #40
0
 def test__unpack_head_AndData(self):
     # e.g. responce to get_serial(33211)
     data = {
         'header': {
             'state': 0,
             'cmd': 10,
             'length': 5,
             'serno': 33211
         },
         'data': b'\xbb\x81\x00\x00'
     }
     pkg = a2b('000a05bb8100aabb810000cc')
     assert self.pkg.unpack(pkg) == data
예제 #41
0
    def test_set(self):
        serno = 31002
        table = 'PROBE_CONFIGURATION_PARAMETER_TABLE'
        param = 'DeviceSerialNum'
        value = [31003]
        ad_param = 0
        package = a2b('fd11071a79002b0c001b790000b0')
        bytes_recv = a2b('0011001a790095')

        expected_calls = [
            call.cmd.set_parameter(serno, table, param, value, ad_param),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.set_parameter(bytes_recv, table, serno)
        ]

        self.cmd.set_parameter.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.set_parameter.return_value = True

        assert self.bus.set(serno, table, param, value)
        assert self.manager.mock_calls == expected_calls
예제 #42
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_set(self):
        serno = 31002
        table = 'PROBE_CONFIGURATION_PARAMETER_TABLE'
        param = 'DeviceSerialNum'
        value = [31003]
        ad_param = 0
        package = a2b('fd11071a79002b0c001b790000b0')
        bytes_recv = a2b('0011001a790095')

        expected_calls = [
            call.cmd.set_parameter(serno, table, param, value, ad_param),
            call.dev.write_pkg(package),
            call.dev.read_pkg(),
            call.res.set_parameter(bytes_recv, table, serno)
        ]

        self.cmd.set_parameter.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.return_value = bytes_recv
        self.res.set_parameter.return_value = True

        assert self.bus.set(serno, table, param, value)
        assert self.manager.mock_calls == expected_calls
예제 #43
0
    def test_find_single_module_FindNothing(self):
        package = a2b('fd0800ffffff60')
        bytes_recv = DeviceError('Timeout reading header!')

        expected_calls = [
            call.cmd.get_negative_ack(),
            call.dev.write_pkg(package),
            call.dev.read_pkg()
        ]

        self.cmd.get_negative_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.side_effect = bytes_recv

        assert not self.bus.find_single_module()
        assert self.manager.mock_calls == expected_calls
예제 #44
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_find_single_module_FindNothing(self):
        package = a2b('fd0800ffffff60')
        bytes_recv = DeviceError('Timeout reading header!')

        expected_calls = [
            call.cmd.get_negative_ack(),
            call.dev.write_pkg(package),
            call.dev.read_pkg()
        ]

        self.cmd.get_negative_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_pkg.side_effect = bytes_recv

        assert not self.bus.find_single_module()
        assert self.manager.mock_calls == expected_calls
예제 #45
0
파일: test_bus.py 프로젝트: mhubig/implib2
    def test_probe_module_short_ButGetDeviceError(self):
        serno = 31002
        package = a2b('fd04001a790003')
        bytes_recv = DeviceError('Timeout reading header!')

        expected_calls = [
            call.cmd.get_short_ack(serno),
            call.dev.write_pkg(package),
            call.dev.read_bytes(1)
        ]

        self.cmd.get_short_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_bytes.side_effect = bytes_recv

        assert not self.bus.probe_module_short(serno)
        assert self.manager.mock_calls == expected_calls
예제 #46
0
    def test_probe_module_short_ButGetDeviceError(self):
        serno = 31002
        package = a2b('fd04001a790003')
        bytes_recv = DeviceError('Timeout reading header!')

        expected_calls = [
            call.cmd.get_short_ack(serno),
            call.dev.write_pkg(package),
            call.dev.read_bytes(1)
        ]

        self.cmd.get_short_ack.return_value = package
        self.dev.write_pkg.return_value = True
        self.dev.read_bytes.side_effect = bytes_recv

        assert not self.bus.probe_module_short(serno)
        assert self.manager.mock_calls == expected_calls
예제 #47
0
    def test_wakeup(self):
        address = 16777215
        table = 'ACTION_PARAMETER_TABLE'
        param = 'EnterSleep'
        value = 0
        ad_param = 0
        package = a2b('fd1504fffffffe05000035')

        expected_calls = [
            call.dev.open_device(),
            call.cmd.set_parameter(address, table, param, [value], ad_param),
            call.dev.write_pkg(package),
        ]

        self.cmd.set_parameter.return_value = package
        self.dev.write_pkg.return_value = True

        assert self.bus.wakeup()
        assert self.manager.mock_calls == expected_calls
예제 #48
0
    def test_wakeup(self):
        address = 16777215
        table = 'ACTION_PARAMETER_TABLE'
        param = 'EnterSleep'
        value = 0
        ad_param = 0
        package = a2b('fd1504fffffffe05000035')

        expected_calls = [
            call.cmd.set_parameter(address, table, param, [value], ad_param),
            call.dev.open_device(),
            call.dev.write_pkg(package),
        ]

        self.cmd.set_parameter.return_value = package
        self.dev.write_pkg.return_value = True

        assert self.bus.wakeup()
        assert self.manager.mock_calls == expected_calls
예제 #49
0
    def test_sync(self):
        address = 16777215
        table = 'SYSTEM_PARAMETER_TABLE'
        param = 'Baudrate'
        baudrate = 9600
        value = baudrate/100
        ad_param = 0
        package = a2b('fd0b05ffffffaf0400600054')

        expected_calls = [
            call.dev.open_device(),
            call.cmd.set_parameter(address, table, param, [value], ad_param),
            call.dev.close_device(),

            call.dev.open_device(baudrate=1200),
            call.dev.write_pkg(package),
            call.dev.close_device(),

            call.dev.open_device(baudrate=2400),
            call.dev.write_pkg(package),
            call.dev.close_device(),

            call.dev.open_device(baudrate=4800),
            call.dev.write_pkg(package),
            call.dev.close_device(),

            call.dev.open_device(baudrate=9600),
            call.dev.write_pkg(package),
            call.dev.close_device(),

            call.dev.open_device(baudrate=baudrate)
        ]

        self.cmd.set_parameter.return_value = package
        self.dev.write_pkg.return_value = True

        self.bus.sync(baudrate=baudrate)
        assert self.bus.bus_synced
        assert self.manager.mock_calls == expected_calls
예제 #50
0
def main():
    global a

    class Chat_Server(threading.Thread):
        def __init__(self):
            print "Chat_Server init"
            threading.Thread.__init__(self)
            self.running = 1
            self.conn = None
            self.addr = None
            self.host = '127.0.0.1'
            self.port = None

        def run(self):
            print "running chat server"
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind((self.host, self.port))
            s.listen(1)
            print("waiting for connection from client")
            self.conn, addr = s.accept()
            while self.running == True:
                data = self.conn.recv(1024)

                if data:
                    data = a.decrypt(data)
                    if data == 'exit':
                        self.running = 0
                    else:
                        print "Client Says >> " + data
                else:
                    break
            time.sleep(0)

        def kill(self):
            self.running = 0

    class Chat_Client(threading.Thread):
        def __init__(self):
            print "Chat Client init"
            threading.Thread.__init__(self)
            self.host = None
            self.sock = None
            self.running = 1
            self.port = None

        def run(self):
            print "Chat Client Run"
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.connect((self.host, self.port))
            while self.running == True:

                rcv = self.sock.recv(1024)

                data = '' + rcv
                if data:
                    data = a.decrypt(data)
                    if data == 'exit':
                        self.running = 0
                    else:
                        print "Server Says >> " + data
                else:
                    break
            time.sleep(0)

        def kill(self):
            self.running = 0

    class Text_Input(threading.Thread):
        def __init__(self):
            print "text input init"
            threading.Thread.__init__(self)
            self.running = 1

        def run(self):
            print "text input run "
            while self.running == True:
                text = raw_input('')
                try:
                    text = text.replace('\n', '') + '\n'
                    text = a.encrypt(text)
                    chat_client.sock.sendall(text)
                except:
                    Exception
                try:
                    text = text.replace('\n', '') + '\n'
                    text = a.encrypt(text)
                    chat_server.conn.sendall(text)
                except:
                    Exception
                time.sleep(0.1)

        def kill(self):
            self.running = 0

    try:
        mode = sys.argv[1]
    except:
        exit(1)

    if mode == '-s':
        s = socket.socket()
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                     1)  # Socket object
        host = '127.0.0.1'  # Get host name
        port = 8000
        myName = raw_input("What is your name: ")
        otherName = raw_input("What is the other name: ")
        masterkey = raw_input("what is your previously decided on master key"
                              )  # Reserve best port.
        a = Axolotl(myName,
                    dbname=otherName + '.db',
                    dbpassphrase=None,
                    nonthreaded_sql=False)
        a.createState(other_name=otherName,
                      mkey=hashlib.sha256(masterkey).digest(),
                      mode=False)
        rkey = b2a(a.state['DHRs']).strip()
        print "Send this ratchet key to your client: ", rkey
        print 'Server started'
        print 'Waiting for cients to connect...'

        s.bind((host, port))  # Bind to the port
        s.listen(3)  # Now wait for client connection.
        c, addr = s.accept()  # Establish connection with client.
        print 'Got connection from', addr
        secret = raw_input("Enter shared secret: ")
        smpr = smp.SMP(secret)
        buffer = c.recv(4096)[4:]

        buffer = smpr.step2(buffer)
        tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer
        c.send(tempBuffer)

        buffer = c.recv(4096)[4:]

        buffer = smpr.step4(buffer)
        tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer
        c.send(tempBuffer)

        if smpr.match:
            print "match"
        else:
            print "no match"
            s.close()
            sys.exit()

        chat_server = Chat_Server()
        chat_server.port = int(raw_input("Enter port to listen on: "))
        chat_server.start()
        text_input = Text_Input()
        text_input.start()

    elif mode == '-c':
        s = socket.socket()
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                     1)  # Socket object
        host = '127.0.0.1'  # Get host name
        port = 8000
        # Reserve best port.
        myName = raw_input("What is your name: ")
        otherName = raw_input("What is the other name: ")
        masterkey = raw_input("what is your previously decided on master key"
                              )  # Reserve best port.
        rkey = raw_input(
            "what is the ratchet key you received from your partner:")
        print 'Connect to ', host, port
        s.connect((host, port))
        secret = raw_input("Enter shared secret: ")
        smpr = smp.SMP(secret)

        buffer = smpr.step1()
        #print "buffer = {}\n".format(  buffer )
        tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer

        s.send(tempBuffer)

        buffer = s.recv(4096)[4:]
        buffer = smpr.step3(buffer)
        tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer
        s.send(tempBuffer)

        buffer = s.recv(4096)[4:]
        smpr.step5(buffer)
        if smpr.match:
            print "match"
        else:
            print "no match"
            s.close()
            sys.exit()
        a = Axolotl(myName,
                    dbname=otherName + '.db',
                    dbpassphrase=None,
                    nonthreaded_sql=False)
        a.createState(other_name=otherName,
                      mkey=hashlib.sha256(masterkey).digest(),
                      mode=True,
                      other_ratchetKey=a2b(rkey))
        chat_client = Chat_Client()
        chat_client.host = raw_input("Enter host to connect to: ")
        chat_client.port = int(raw_input("Enter port to connect to: "))
        chat_client.start()
        text_input = Text_Input()
        text_input.start()
예제 #51
0
    elif mode == '-c':
        axolotl = Axolotl(NICK,
                          dbname=OTHER_NICK + '.db',
                          dbpassphrase=None,
                          nonthreaded_sql=False)
        tor_process = tor(TOR_CLIENT_PORT, TOR_CLIENT_CONTROL_PORT,
                          '/tmp/tor.client', '')
        print 'Exchanging credentials via tor...'
        creds = credentialsReceive(mkey)
        if not creds:
            print 'Master Key Mismatch!'
            print 'Exiting...'
            sys.exit()
        cookie, rkey, onion = creds.split('___')
        controller = clientController(cookie, onion)
        axolotl.createState(other_name=OTHER_NICK,
                            mkey=hash_(mkey),
                            mode=True,
                            other_ratchetKey=a2b(rkey))

        print 'Credentials received, connecting to the other party...'
        with torcontext() as s:
            s.connect((onion, PORT))
            print 'Connected...'
            smp_match = doSMP(s, False)
            chatThread(s, smp_match, onion)

    else:
        usage()
예제 #52
0
 def test_check_crc(self):
     data = a2b('FD15ED09f3')
     assert self.crc.check_crc(data)
예제 #53
0
 def test_calc_crc(self):
     crc = a2b('f3')
     assert self.crc.calc_crc(a2b('FD15ED09')) == crc
예제 #54
0
 def test_set_epr_page(self):
     page = a2b('000000000000000023ffff00')
     pkg = self.cmd.set_epr_page(30001, 7, page)
     assert pkg == a2b('fd3d0f317500f6ff07000000000000000023ffff007b')
예제 #55
0
 def test_get_epr_page(self):
     pkg = self.cmd.get_epr_page(30001, 0)
     assert pkg == a2b('fd3c0331750029ff0081')
예제 #56
0
 def test_do_tdr_scan(self):
     pkg = self.cmd.do_tdr_scan(30001, 1, 126, 2, 64)
     assert pkg == a2b('fd1e06317500d3017e024000a4')
예제 #57
0
 def test_set_parameter(self):
     pkg = self.cmd.set_parameter(31002,
                                  'PROBE_CONFIGURATION_PARAMETER_TABLE',
                                  'DeviceSerialNum', [31003])
     assert pkg == a2b('fd11071a79002b0c001b790000b0')
예제 #58
0
 def test_get_parameter(self):
     pkg = self.cmd.get_parameter(31002, 'SYSTEM_PARAMETER_TABLE',
                                  'SerialNum')
     assert pkg == a2b('fd0a031a7900290100c4')
예제 #59
0
 def test_get_negative_ack(self):
     pkg = self.cmd.get_negative_ack()
     assert pkg == a2b('fd0800ffffff60')
예제 #60
0
# 1             LSB INFO LEN
# INFO_LEN      INFO
#
_ECIES_CMD_FORMAT = '{o:c}{inl:s}{input}{al:s}{iv}{xl:s}{x}{yl:s}{y}{sl:s}{s}{il:s}{i}'


_DEFAULT_SALT = 'Salt!'
_DEFAULT_INFO = 'Info!'
_STATIC_IV = ''.join([chr(x) for x in range(16)])

_ECIES_INPUTS = (
  (
    '',
    'Test message!!',
    _STATIC_IV,
    a2b('6fdaf5e2e11dd61c116222c748d99b45f69031c9d4d3d5787a9a0fdd3b9c471a'),
    a2b('98e76f53febd6bedc8fa19ce1543cb3f8f5cbc72c74602f1bfdee88c19d3d9d0'),
    a2b('8750c295cd33be5846868e2869bf2c8cfeefbc4a574874c7388bf40f74e8e0e6'),
    _DEFAULT_SALT,
    _DEFAULT_INFO,
    'SIMPLE'
  ),
  (
    '',
    'Multi block test message!!!!',
    _STATIC_IV,
    a2b('6fdaf5e2e11dd61c116222c748d99b45f69031c9d4d3d5787a9a0fdd3b9c471a'),
    a2b('98e76f53febd6bedc8fa19ce1543cb3f8f5cbc72c74602f1bfdee88c19d3d9d0'),
    a2b('8750c295cd33be5846868e2869bf2c8cfeefbc4a574874c7388bf40f74e8e0e6'),
    _DEFAULT_SALT,
    _DEFAULT_INFO,