Esempio n. 1
0
 def test_verify(self):
     conn = MagicMock()
     peer_session = PeerSession(conn)
     keys_auth = EllipticalKeysAuth(self.path)
     peer_session.key_id = keys_auth.get_key_id()
     peer_session.p2p_service.verify_sig = keys_auth.verify
     msg = MessageStopGossip()
     assert not peer_session.verify(msg)
     msg.sig = keys_auth.sign(msg.get_short_hash())
     assert peer_session.verify(msg)
Esempio n. 2
0
    def test_add_task_header(self):
        config = self._get_config_desc()
        keys_auth = EllipticalKeysAuth(self.path)
        keys_auth_2 = EllipticalKeysAuth(os.path.join(self.path, "2"))

        self.ts = ts = TaskServer(Node(), config, keys_auth, self.client,
                                  use_docker_machine_manager=False)

        task_header = get_example_task_header()
        task_header["task_id"] = "xyz"

        with self.assertRaises(Exception) as raised:
            ts.add_task_header(task_header)
            self.assertEqual(raised.exception.message, "Invalid signature")
            self.assertEqual(len(ts.get_tasks_headers()), 0)

        task_header["task_owner_key_id"] = keys_auth_2.key_id
        task_header["signature"] = keys_auth_2.sign(TaskHeader.dict_to_binary(task_header))

        self.assertIsNotNone(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_tasks_headers()), 1)

        task_header = get_example_task_header()
        task_header["task_id"] = "xyz_2"
        task_header["task_owner_key_id"] = keys_auth_2.key_id
        task_header["signature"] = keys_auth_2.sign(TaskHeader.dict_to_binary(task_header))

        self.assertIsNotNone(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_tasks_headers()), 2)

        self.assertIsNotNone(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_tasks_headers()), 2)

        new_header = dict(task_header)
        new_header["task_owner"]["pub_port"] = 9999
        new_header["signature"] = keys_auth_2.sign(TaskHeader.dict_to_binary(new_header))

        self.assertIsNotNone(ts.add_task_header(new_header))
        self.assertEqual(len(ts.get_tasks_headers()), 2)
        saved_task = next(th for th in ts.get_tasks_headers() if th["task_id"] == "xyz_2")
        self.assertEqual(saved_task["signature"], new_header["signature"])
Esempio n. 3
0
    def test_verify(self):
        keys_auth = EllipticalKeysAuth(self.path)
        conn = Mock()
        ts = TaskSession(conn)
        ts.task_server = Mock()
        ts.task_server.verify_sig = keys_auth.verify

        msg = message.MessageRemoveTask()
        assert not ts.verify(msg)
        msg.sig = keys_auth.sign(msg.get_short_hash())
        ts.key_id = keys_auth.get_key_id()
        assert ts.verify(msg)