Beispiel #1
0
    def test_transfer(self, capsys):
        """ Test transfer of ownership of a dns entry
        """
        with capsys.disabled():
            self.basic_creation()

            # Transfer domain

            t = self.create_transaction(chains.DNS_Data('t', 'seclab.oth', ''),
                                        0, 1)

            self.chain.new_transaction(t)
            self.chain.process_message(('mine', self.sender_verify, 'local'))

            # Verify that old user can't change entry anymore

            t = self.create_transaction(
                chains.DNS_Data('u', 'seclab.oth', '127.0.0.2'), 0, 20)

            assert not self.chain.validate_transaction(t, False)

            # Verify that new user can change entry

            self.sender_verify = self.receiver_verify
            self.sender_sign = self.receiver_sign

            self.chain.process_message(('mine', self.sender_verify, 'local'))

            t = self.create_transaction(
                chains.DNS_Data('u', 'seclab.oth', '127.0.0.3'),
                0,
                20,
            )

            assert self.chain.validate_transaction(t, False)
Beispiel #2
0
    def test_resolve_conflict(self):
        """ Test that resolve conflict works with the dns chain
        """

        # Initial chain

        self.basic_creation()

        # Secondary chain

        bchain2 = chains.DNSBlockChain(VERSION, Queue(), Queue())

        bchain2.process_message(('mine', self.sender_verify, 'local'))

        t = self.create_transaction(
            chains.DNS_Data('r', 'seclab.oth', '127.0.0.1'), 0, 20)

        bchain2.new_transaction(t)

        for _ in range(5):
            bchain2.process_message(('mine', self.sender_verify, 'local'))

        t = self.create_transaction(chains.DNS_Data('t', 'seclab.oth', ''), 0,
                                    5, '0')

        bchain2.new_transaction(t)

        bchain2.process_message(('mine', self.sender_verify, 'local'))

        # Check new_chain of the initial blockchain

        self.chain.resolve_conflict(bchain2.get_header_chain())

        assert bchain2.latest_header() == self.chain.nc_latest_header()

        # Add to secondary chain, to test "pre-filling" of new_chain

        for _ in range(3):
            bchain2.process_message(('mine', self.sender_verify, 'local'))

        self.chain.resolve_conflict(bchain2.get_header_chain())
        assert bchain2.latest_header() == self.chain.nc_latest_header()

        # Chain exchange

        for b in bchain2.get_block_chain():
            self.chain.new_block(b)

        assert bchain2.latest_block() == self.chain.latest_block()
Beispiel #3
0
    def test_auction(self, capsys):
        """ Test the auction system of the dns chain
        """
        with capsys.disabled():
            self.basic_creation()

            # Create auction

            t = self.create_transaction(chains.DNS_Data('t', 'seclab.oth', ''),
                                        0, 5, '0')

            self.chain.new_transaction(t)
            self.chain.process_message(('mine', self.sender_verify, 'local'))

            # Bid on auction

            self.sender_verify = self.receiver_verify
            self.sender_sign = self.receiver_sign

            t = self.create_transaction(chains.DNS_Data('b', 'seclab.oth', ''),
                                        5, 1, '0')

            self.chain.new_transaction(t)
            self.chain.process_message(('mine', self.sender_verify, 'local'))

            # Increase bid

            t = self.create_transaction(chains.DNS_Data('b', 'seclab.oth', ''),
                                        9, 1, '0')

            self.chain.new_transaction(t)

            # Mine till auction is over

            for _ in range(5):
                self.chain.process_message(
                    ('mine', self.sender_verify, 'local'))

            # Verify that new owner can update the dns entry

            t = self.create_transaction(
                chains.DNS_Data('u', 'seclab.oth', '127.0.0.3'),
                0,
                20,
            )

            assert self.chain.validate_transaction(t, False)
Beispiel #4
0
    def test_basic_transaction(self):
        """ Test that the DNS chain can handle basic transactions.

        Basic transaction = Transferring coins between users
        """

        self.chain.process_message(('mine', self.sender_verify, 'local'))

        t = self.create_transaction(chains.DNS_Data('', '', ''), 1, 1)
        self.chain.new_transaction(t)

        assert t in self.chain.transaction_pool

        # Compare new transaction against transaction pool

        t2 = self.create_transaction(chains.DNS_Data('', '', ''), 1, 1)
        self.chain.new_transaction(t2)

        assert t2 in self.chain.transaction_pool
Beispiel #5
0
    def basic_creation(self):
        """ Setup the blockchain.

        Adds an initial entry: seclab.oth -> 127.0.0.1
        """

        self.chain.process_message(('mine', self.sender_verify, 'local'))

        t = self.create_transaction(
            chains.DNS_Data('r', 'seclab.oth', '127.0.0.1'), 0, 20)
        self.chain.new_transaction(t)

        self.chain.process_message(('mine', self.sender_verify, 'local'))
Beispiel #6
0
    def test_update(self, capsys):
        """ Test updating of a dns entry
        """
        with capsys.disabled():
            self.basic_creation()

            t = self.create_transaction(
                chains.DNS_Data('u', 'seclab.oth', '127.0.0.2'), 0, 20)
            self.chain.new_transaction(t)
            self.chain.process_message(('mine', self.sender_verify, 'local'))

        self.chain.process_message(('dns_lookup', 'seclab.oth', 'local'))

        captured = capsys.readouterr()
        assert captured.out == '127.0.0.2\n'
Beispiel #7
0
def create_object(data: dict):
    """ Create an object out of a dict.

    Args:
        data: Dictionary containing the data.

    Returns:
        Object that was created.
    """
    if isinstance(data, list):
        return [create_object(e) for e in data]

    # Chains

    if data['__type__'] == 'Transaction':
        return chains.Transaction(**create_object(data['__data__']))

    if data['__type__'] == 'Header':
        return chains.Header(**create_object(data['__data__']))

    if data['__type__'] == 'Block':
        return chains.Block(create_object(data['__header__']),
                            create_object(data['__transactions__']))

    if data['__type__'] == 'DNS_Transaction':
        return chains.DNS_Transaction(**create_object(data['__data__']))

    if data['__type__'] == 'DNS_Data':
        return chains.DNS_Data(**create_object(data['__data__']))

    if data['__type__'] == 'DDosHeader':
        return chains.DDosHeader(**create_object(data['__data__']))

    if data['__type__'] == 'DDosTransaction':
        return chains.DDosTransaction(**create_object(data['__data__']))

    if data['__type__'] == 'DDosData':
        return chains.DDosData(**create_object(data['__data__']))

    # General

    if data['__type__'] == 'Tuple':
        return tuple(
            create_object(data[f'__{i}__']) for i in range(len(data) - 1))

    if data['__type__'] == 'List':
        return [create_object(data[f'__{i}__']) for i in range(len(data) - 1)]

    if data['__type__'] == 'Dict':
        return dict((k, create_object(v)) for (k, v) in data.items()
                    if k != '__type__')

    if data['__type__'] == 'Bytes':
        return data['__data__'].encode(data['__encoding__'])

    if data['__type__'] == 'Hex-Bytes':
        return nacl.encoding.HexEncoder.decode(data['__data__'].encode(
            data['__encoding__']))

    if data['__type__'] == 'SigningKey':
        return nacl.signing.SigningKey(
            data['__seed__'].encode(data['__encoding__']),
            nacl.encoding.HexEncoder)

    return data['__data__']