示例#1
0
    def test_state(self):
        tau0 = Taurus('0', 'listener0', server=self.server)
        tau1 = Taurus('1', 'listener1', server=self.server)

        assert tau0.state == {}
        assert tau1.state == {}

        packet1 = Packet({
            'dest': '0',
            'type': '1',
            'log': bool(random.randint(0, 1)),
            'video': bool(random.randint(0, 1)),
            'ant': bool(random.randint(0, 1)),
            'video_running': bool(random.randint(0, 1)),
            'video_recording': bool(random.randint(0, 1)),
            'powermeter_running': bool(random.randint(0, 1)),
            'heartrate_running': bool(random.randint(0, 1)),
            'speed_running': bool(random.randint(0, 1)),
            'calibration': bool(random.randint(0, 1))
        })
        self.server.manage_packet(packet1)

        assert tau0.state == packet1.jsonify
        assert tau1.state == {}

        packet2 = Packet({
            'dest': '1',
            'type': '1',
            'log': bool(random.randint(0, 1)),
            'video': bool(random.randint(0, 1)),
            'ant': bool(random.randint(0, 1)),
            'video_running': bool(random.randint(0, 1)),
            'video_recording': bool(random.randint(0, 1)),
            'powermeter_running': bool(random.randint(0, 1)),
            'heartrate_running': bool(random.randint(0, 1)),
            'speed_running': bool(random.randint(0, 1)),
            'calibration': bool(random.randint(0, 1))
        })
        self.server.manage_packet(packet2)

        assert tau0.state == packet1.jsonify
        assert tau1.state == packet2.jsonify

        packet3 = Packet({
            'dest': '1',
            'type': '1',
            'log': bool(random.randint(0, 1)),
            'video': bool(random.randint(0, 1)),
            'ant': bool(random.randint(0, 1)),
            'video_running': bool(random.randint(0, 1)),
            'video_recording': bool(random.randint(0, 1)),
            'powermeter_running': bool(random.randint(0, 1)),
            'heartrate_running': bool(random.randint(0, 1)),
            'speed_running': bool(random.randint(0, 1)),
            'calibration': bool(random.randint(0, 1))
        })
        self.server.manage_packet(packet3)

        assert tau0.state == packet1.jsonify
        assert tau1.state == packet3.jsonify
示例#2
0
    def test_receive(self):
        list_ = list()
        with pytest.raises(PacketInstanceException):
            self.bike.receive(dict())

        with pytest.raises(PacketInstanceException):
            self.bike.receive(str())

        setting = dict(test_packet[Packet.Type.SETTING])
        packet = Packet(setting)
        self.bike.receive(packet)
        list_.append(packet)

        signal = dict(test_packet[Packet.Type.SIGNAL])
        packet = Packet(signal)
        self.bike.receive(packet)
        list_.append(packet)

        message = dict(test_packet[Packet.Type.MESSAGE])
        packet = Packet(message)
        self.bike.receive(packet)
        list_.append(packet)

        raspberry = dict(test_packet[Packet.Type.RASPBERRY])
        packet = Packet(raspberry)
        self.bike.receive(packet)
        list_.append(packet)

        video = dict(test_packet[Packet.Type.VIDEO])
        packet = Packet(video)
        self.bike.receive(packet)
        list_.append(packet)

        assert list(self.bike.packets) == list_
示例#3
0
    def test_fields_packet(self):
        with pytest.raises(InvalidFieldsException):
            Packet({'dest': '0', 'type': '0'})

        tester = dict(test_packet[Packet.Type.DATA])
        tester.pop('gear')
        with pytest.raises(InvalidFieldsException):
            Packet(tester)
示例#4
0
    def test_data(self):
        tau0 = Taurus('0', 'listener0', server=self.server)
        tau1 = Taurus('1', 'listener1', server=self.server)

        assert tau0.data == {}
        assert tau1.data == {}

        packet1 = Packet({
            'dest': '0',
            'type': '0',
            'heartrate': str(random.random()),
            'power': str(random.random()),
            'cadence': str(random.random()),
            'distance': str(random.random()),
            'speed': str(random.random()),
            'time': str(random.random()),
            'gear': str(random.random())
        })
        self.server.manage_packet(packet1)

        assert tau0.data == packet1.jsonify
        assert tau1.data == {}

        packet2 = Packet({
            'dest': '1',
            'type': '0',
            'heartrate': str(random.random()),
            'power': str(random.random()),
            'cadence': str(random.random()),
            'distance': str(random.random()),
            'speed': str(random.random()),
            'time': str(random.random()),
            'gear': str(random.random())
        })
        self.server.manage_packet(packet2)

        assert tau0.data == packet1.jsonify
        assert tau1.data == packet2.jsonify

        packet3 = Packet({
            'dest': '1',
            'type': '0',
            'heartrate': str(random.random()),
            'power': str(random.random()),
            'cadence': str(random.random()),
            'distance': str(random.random()),
            'speed': str(random.random()),
            'time': str(random.random()),
            'gear': str(random.random())
        })
        self.server.manage_packet(packet3)

        assert tau0.data == packet1.jsonify
        assert tau1.data == packet3.jsonify

        # test per la funzione di history
        assert tau0.history == [packet1.jsonify]
        assert tau1.history == [packet2.jsonify, packet3.jsonify]
示例#5
0
    def test_load(self):
        for tipo in test_packet.keys():
            tester = dict(test_packet[tipo])
            tester_tuple = tuple([tester[val] for val in tester.keys()])
            tester_list = list(tester_tuple)
            tester_str = ';'.join(map(str, tester.values()))

            # load con dizionario
            p1 = Packet(tester)
            print(p1)
            assert p1.jsonify == json.dumps(tester)
            assert p1.dictify == tester
            assert p1.content == tester_tuple
            assert len(p1) == len(tester)
            assert str(p1) == str(tester)

            # per la comparazione di due packetti uguali
            p2 = Packet(tester)

            assert p1.jsonify == p2.jsonify
            assert p1.dictify == p2.dictify
            assert p1.content == p2.content
            assert p1.value == p2.value
            assert p1.encode == p2.encode

            # load con tupla
            p3 = Packet(tester_tuple)

            assert p1.jsonify == p3.jsonify
            assert p1.dictify == p3.dictify
            assert p1.content == p3.content
            assert p1.value == p3.value
            assert p1.encode == p3.encode

            # load con lista
            p4 = Packet(tester_list)

            assert p1.jsonify == p4.jsonify
            assert p1.dictify == p4.dictify
            assert p1.content == p4.content
            assert p1.value == p4.value
            assert p1.encode == p4.encode

            # load con stringa
            p5 = Packet(tester_str)

            assert p1.content == p5.content
            assert p1.value == p5.value
            assert p1.encode == p5.encode
            assert p1.jsonify == p5.jsonify
            assert p1.dictify == p5.dictify
示例#6
0
    def test_protocol(self):
        with open(json_path) as p:
            protocol = p.read()

        Packet.protocol(protocol)
        assert Packet._PACKETS == json.loads(protocol)

        protocol = {'primo': {'ciao': '', 'type': 'ciao'}}
        result = Packet.protocol(protocol)

        assert result == protocol
        assert Packet._PACKETS == protocol

        # reset protocollo
        result = Packet.protocol()
        assert result == Packet._PACKETS
示例#7
0
    def test_secret_key(self):
        key = b"test"

        _ = Taurus('X', 'listenerX', '/dev/ttyUSB0', secret_key=key)
        p = Packet()

        assert p.secret_key == key
示例#8
0
    def test_secret_key(self):
        key = b"test"

        c = Client()
        _ = Bike('Y', 'serverY', c, secret_key=key)
        p = Packet()

        assert p.secret_key == key
示例#9
0
    def test_content(self):
        tester = dict(test_packet[Packet.Type.DATA])
        p1 = Packet(tester)

        assert p1.content == tuple(tester.values())

        tester2 = dict(test_packet[Packet.Type.DATA])
        tester2['gear'] = 11
        p2 = Packet(tester2)

        assert p1.content != p2.content
        assert p1.value != p2.value
        assert len(p1) == len(p2)

        tester2['dest'] = 2
        p3 = Packet(tester2)

        assert p2.content != p3.content
        assert p2.value == p3.value
        assert len(p2) == len(p3)
示例#10
0
    def test_receive(self):
        with pytest.raises(PacketInstanceException):
            self.taurus.receive(dict())

        with pytest.raises(PacketInstanceException):
            self.taurus.receive(str())

        data = dict(test_packet[Packet.Type.DATA])
        packet = Packet(data)
        self.taurus.receive(packet)
        assert self.taurus.data == packet.jsonify

        state = dict(test_packet[Packet.Type.STATE])
        packet = Packet(state)
        self.taurus.receive(packet)
        assert self.taurus.state == packet.jsonify

        setting = dict(test_packet[Packet.Type.SETTING])
        packet = Packet(setting)
        self.taurus.receive(packet)
        assert self.taurus.setting == packet.jsonify
示例#11
0
    def test_digest(self):
        Packet.secret_key = b"test_key"
        protected_type = Packet().protected_type

        for tipo in protected_type:
            tester = dict(test_packet[tipo])

            p1 = Packet(tester, nonce=False)
            p2 = Packet(tester, nonce=False)

            assert p1.secret_key == p2.secret_key

            assert p1.content == p2.content
            assert p1.value == p2.value
            assert p1.encode == p2.encode
            assert p1.jsonify == p2.jsonify
            assert p1.dictify == p2.dictify

        for tipo in protected_type:
            tester = dict(test_packet[tipo])

            p1 = Packet(tester)
            p2 = Packet(tester)

            assert p1.secret_key == p2.secret_key

            assert p1.nonce < p2.nonce
            assert p1.content != p2.content
            assert p1.value != p2.value
            assert p1.encode != p2.encode
            assert p1.jsonify != p2.jsonify
            assert p1.dictify != p2.dictify

        for tipo in protected_type:
            tester = dict(test_packet[tipo])

            Packet.secret_key = b"test_key"
            p1 = Packet(tester)

            assert p1.secret_key == b"test_key"

            Packet.secret_key = b"test_key2"
            p2 = Packet(tester)

            assert p2.secret_key == b"test_key2"

            # aggiornando la secret_key si aggiorna per tutti
            assert p1.secret_key == p2.secret_key

            assert p1.content != p2.content
            assert p1.value != p2.value
            assert p1.encode != p2.encode
            assert p1.jsonify != p2.jsonify
            assert p1.dictify != p2.dictify
            assert p1.digest != p2.digest
示例#12
0
    def test_hashing(self):
        Packet.secret_key = b"test_key"
        protected_type = Packet().protected_type

        for tipo in protected_type:
            tester = dict(test_packet[tipo])

            p1 = Packet(tester)
            h1 = blake2s(key=Packet.secret_key, digest_size=16)
            h1.update(json.dumps(p1.raw_data).encode('utf-8'))

            p2 = Packet(tester, nonce=False)
            h2 = blake2s(key=Packet.secret_key, digest_size=16)
            h2.update(json.dumps(p2.raw_data).encode('utf-8'))

            assert p1.digest == h1.hexdigest()
            assert p1.digest != h2.hexdigest()

            assert p2.digest != h1.hexdigest()
            assert p2.digest == h2.hexdigest()

            assert p1.digest != p2.digest
示例#13
0
    def test_manage_packet(self):
        server = Server()
        dest = Taurus('X', 'listenerX', server=server)

        with pytest.raises(PacketInstanceException):
            server.manage_packet(dict())

        with pytest.raises(PacketInstanceException):
            server.manage_packet(str())

        data = dict(test_packet[Packet.Type.DATA])
        packet = Packet(data)
        server.manage_packet(packet)
        assert dest.data == packet.jsonify

        state = dict(test_packet[Packet.Type.STATE])
        packet = Packet(state)
        server.manage_packet(packet)
        assert dest.state == packet.jsonify

        setting = dict(test_packet[Packet.Type.SETTING])
        packet = Packet(setting)
        server.manage_packet(packet)
        assert dest.setting == packet.jsonify
示例#14
0
    def test_wrong_type(self):
        values = ['19', '-1', 'a', '=', ';', '', ',', 'é']

        for char in values:
            with pytest.raises(InvalidTypeException):
                Packet('0;' + char)
示例#15
0
    def test_setting(self):
        tau0 = Taurus('0', 'listener0', server=self.server)
        tau1 = Taurus('1', 'listener1', server=self.server)

        assert tau0.setting == {}
        assert tau1.setting == {}

        packet1 = Packet({
            'dest': '0',
            'type': '3',
            'circonferenza': str(random.random()),
            'run': str(random.random()),
            'log': bool(random.randint(0, 1)),
            'csv': bool(random.randint(0, 1)),
            'ant': bool(random.randint(0, 1)),
            'potenza': str(random.random()),
            'led': str(random.random()),
            'calibration_value': str(random.random()),
            'update': str(random.random()),
            'p13': bool(random.randint(0, 1))
        })
        self.server.manage_packet(packet1)

        assert tau0.setting == packet1.jsonify
        assert tau1.setting == {}

        packet2 = Packet({
            'dest': '1',
            'type': '3',
            'circonferenza': str(random.random()),
            'run': str(random.random()),
            'log': bool(random.randint(0, 1)),
            'csv': bool(random.randint(0, 1)),
            'ant': bool(random.randint(0, 1)),
            'potenza': str(random.random()),
            'led': str(random.random()),
            'calibration_value': str(random.random()),
            'update': str(random.random()),
            'p13': bool(random.randint(0, 1))
        })
        self.server.manage_packet(packet2)

        assert tau0.setting == packet1.jsonify
        assert tau1.setting == packet2.jsonify

        packet3 = Packet({
            'dest': '1',
            'type': '3',
            'circonferenza': str(random.random()),
            'run': str(random.random()),
            'log': bool(random.randint(0, 1)),
            'csv': bool(random.randint(0, 1)),
            'ant': bool(random.randint(0, 1)),
            'potenza': str(random.random()),
            'led': str(random.random()),
            'calibration_value': str(random.random()),
            'update': str(random.random()),
            'p13': bool(random.randint(0, 1))
        })
        self.server.manage_packet(packet3)

        assert tau0.setting == packet1.jsonify
        assert tau1.setting == packet3.jsonify