Esempio n. 1
0
    def test_e3x(self):
        mynode1 = MyNode(123)
        mynode2 = MyNode(123)
        assert len(mynode1.public) == 64
        assert len(mynode1.private) == 64
        d = "223.221.22.55"
        dummy_ip = mynode1.get_ip_address(d)
        assert dummy_ip == d

        jloc = mynode1.get_my_location()
        logging.debug("my node loc : %s" % jloc)
        loc = json.loads(jloc)
        assert loc["hashname"] == mynode1.get_my_id()
        assert loc["port"] == 123
        logging.debug("my node public : %s" % mynode1.public)
        logging.debug("my node private : %s" % mynode1.private)
        logging.debug("my node ip address : %s" % mynode1.ip_address)
        logging.debug("my node ip address : %s" % mynode1.get_my_location())

        e3x1 = E3x(mynode1)
        e3x2 = E3x(mynode2)

        result = e3x1.encrypt_channel_message("test")
        assert result is None
        result = e3x1.decrypt_channel_message("test")
        assert result is None

        public1 = base64.b64encode(mynode1.public).decode()
        public2 = base64.b64encode(mynode2.public).decode()

        msg = json.dumps({"public_key": public1})
        logging.debug(msg)
        enc_msg = e3x1.encrypt_handshake(msg, public2)
        logging.debug("encypted handshake : %s" % enc_msg)
        msg_ = e3x2.decrypt_handshake(enc_msg)
        logging.debug("decypted handshake : %s" % msg_)
        assert msg == msg_

        msg = json.dumps({"public_key": public2})
        enc_msg = e3x2.encrypt_handshake(msg, public1)
        logging.debug("encypted handshake : %s" % enc_msg)
        msg_ = e3x1.decrypt_handshake(enc_msg)
        logging.debug("decypted handshake : %s" % msg_)
        assert msg == msg_

        msg = json.dumps({"public_key": public1})
        logging.debug(msg)
        enc_msg = e3x1.encrypt_handshake(msg, public2)
        m = bytearray(enc_msg)
        m[len(m) - 1] = 0
        msg_ = e3x2.decrypt_handshake(bytes(m))
        assert msg_ is None

        enc_msg = e3x1.encrypt_handshake("aaa:", public2)
        msg_ = e3x2.decrypt_handshake(enc_msg)
        assert msg_ is None

        enc_msg = e3x1.encrypt_handshake('{"aaa":"aa"}', public2)
        msg_ = e3x2.decrypt_handshake(enc_msg)
        assert msg_ is None

        assert e3x1.channel_enckey == e3x2.channel_deckey
        assert e3x2.channel_enckey == e3x1.channel_deckey

        msg = "good"
        enc_msg = e3x1.encrypt_channel_message(msg)
        logging.debug("encypted channel message : %s" % enc_msg)
        msg_ = e3x2.decrypt_channel_message(enc_msg)
        logging.debug("decypted channel message : %s" % msg_)
        assert msg == msg_

        msg = "day"
        enc_msg = e3x2.encrypt_channel_message(msg)
        logging.debug("encypted channel message : %s" % enc_msg)
        msg_ = e3x1.decrypt_channel_message(enc_msg)
        logging.debug("decypted channel message : %s" % msg_)
        assert msg == msg_

        enc_msg = e3x2.encrypt_channel_message(msg)
        logging.debug("encypted channel message : %s" % enc_msg)
        m = bytearray(enc_msg)
        m[-1] = 0
        msg_ = e3x1.decrypt_channel_message(bytes(m))
        assert msg_ is None