Exemplo n.º 1
0
    def test_requesting_redundant(self):
        if not 'FILE_BLOCKS' in SHARED_STATE:
            print "You must run test_inserting() before this test."
            self.assertTrue(False)

        ctx, update_sm, start_state = self.setup_request_sm()

        blocks = []
        for entry in SHARED_STATE['FILE_BLOCKS']:
            blocks.append((entry[1], tuple(break_primary(entry[2]))))

        self.verify_not_cached(ctx, blocks)
        start_state.blocks = tuple(blocks)

        start(update_sm, ctx)
        run_until_quiescent(update_sm, POLL_SECS)
        self.assertTrue(update_sm.get_state(QUIESCENT).
                        arrived_from(((FINISHING,))))

        self.verify_cached(ctx, blocks)
Exemplo n.º 2
0
    def test_requesting_redundant(self):
        if not 'FILE_BLOCKS' in SHARED_STATE:
            print "You must run test_inserting() before this test."
            self.assertTrue(False)

        ctx, update_sm, start_state = self.setup_request_sm()

        blocks = []
        for entry in SHARED_STATE['FILE_BLOCKS']:
            blocks.append((entry[1], tuple(break_primary(entry[2]))))

        self.verify_not_cached(ctx, blocks)
        start_state.blocks = tuple(blocks)

        start(update_sm, ctx)
        run_until_quiescent(update_sm, POLL_SECS)
        self.assertTrue(
            update_sm.get_state(QUIESCENT).arrived_from(((FINISHING, ))))

        self.verify_cached(ctx, blocks)
Exemplo n.º 3
0
    def test_inserting(self):
        # Takes longer to insert existing blocks?
        offset = random.randrange(0, 256)
        print "offset: ", offset
        lengths = (FREENET_BLOCK_LEN - 1,
                   FREENET_BLOCK_LEN,
                   FREENET_BLOCK_LEN + 1,
                   1,
                   FREENET_BLOCK_LEN + 11235,
                   )

        insert_files = []
        for index, length in enumerate(lengths):
            full_path = os.path.join(self.tmp_dir,
                                     "%i.bin" % index)
            out_file = open(full_path, 'wb')
            out_file.write(bytes(length, offset))
            out_file.close()
            self.assertTrue(os.path.getsize(full_path) == length)
            insert_files.append(full_path)

        update_sm = self.make_state_machine()
        self.assertTrue(not 'TEST_STATE' in update_sm.states)
        update_sm.states['TEST_STATE'] = (
            InsertingRedundantBlocks(update_sm,
                                     'TEST_STATE',
                                     FINISHING,
                                     FAILING))


        ctx = ArchiveUpdateContext(update_sm, FakeUI())
        ctx.update({'ARCHIVE_CACHE_DIR':self.tmp_dir,
                    'REQUEST_URI':SOME_USK,
                    'ARCHIVE_BLOCK_FILES':insert_files,
                    'START_STATE':'TEST_STATE'})

        create_dirs(ctx.ui_,
                    ctx['ARCHIVE_CACHE_DIR'],
                    ctx['REQUEST_URI'])

        start(update_sm, ctx)
        run_until_quiescent(update_sm, POLL_SECS)
        self.assertTrue(update_sm.get_state(QUIESCENT).
                        arrived_from(((FINISHING,))))

        blocks = update_sm.states['TEST_STATE'].files
        for index, entry in enumerate(blocks):
            print "block [%i]: len: %i" % (index, entry[1])
            for chk in entry[2]:
                print "   ", chk

        # FREENET_BLOCK_LEN - 1, first is unpadded
        self.checkCHK(blocks[0][2][0], blocks[0][1], blocks[0][1],
                      bytes(blocks[0][1], offset))
        # FREENET_BLOCK_LEN - 1, second is padded
        self.checkCHK(blocks[0][2][1], blocks[0][1], blocks[0][1] + 1,
                      bytes(blocks[0][1], offset))

        # FREENET_BLOCK_LEN first is padded
        self.checkCHK(blocks[1][2][0], blocks[1][1], blocks[1][1] + 1,
                      bytes(blocks[1][1], offset))
        # FREENET_BLOCK_LEN second is padded
        self.checkCHK(blocks[1][2][1], blocks[1][1], blocks[1][1] + 1,
                      bytes(blocks[1][1], offset))

        # FREENET_BLOCK_LEN + 1, first is unpadded
        self.checkCHK(blocks[2][2][0], blocks[2][1], blocks[2][1],
                      bytes(blocks[2][1], offset))
        # FREENET_BLOCK_LEN + 1, second is unpadded
        self.checkCHK(blocks[2][2][1], blocks[2][1], blocks[2][1],
                      bytes(blocks[2][1], offset))

        # 1, first is unpadded
        self.checkCHK(blocks[3][2][0], blocks[3][1], blocks[3][1],
                      bytes(blocks[3][1], offset))

        # 1, second is padded
        self.checkCHK(blocks[3][2][1], blocks[3][1], blocks[3][1] + 1,
                      bytes(blocks[3][1], offset))


        # FREENET_BLOCK_LEN + 11235, first is unpadded
        self.checkCHK(blocks[4][2][0], blocks[4][1], blocks[4][1],
                      bytes(blocks[4][1], offset))

        # FREENET_BLOCK_LEN + 11235, second is unpadded
        self.checkCHK(blocks[4][2][1], blocks[4][1], blocks[4][1],
                      bytes(blocks[4][1], offset))

        # Save info for use in request testing
        SHARED_STATE['FILE_BLOCKS'] =  blocks
        SHARED_STATE['OFFSET'] = offset
Exemplo n.º 4
0
    def test_inserting(self):
        # Takes longer to insert existing blocks?
        offset = random.randrange(0, 256)
        print "offset: ", offset
        lengths = (
            FREENET_BLOCK_LEN - 1,
            FREENET_BLOCK_LEN,
            FREENET_BLOCK_LEN + 1,
            1,
            FREENET_BLOCK_LEN + 11235,
        )

        insert_files = []
        for index, length in enumerate(lengths):
            full_path = os.path.join(self.tmp_dir, "%i.bin" % index)
            out_file = open(full_path, 'wb')
            out_file.write(bytes(length, offset))
            out_file.close()
            self.assertTrue(os.path.getsize(full_path) == length)
            insert_files.append(full_path)

        update_sm = self.make_state_machine()
        self.assertTrue(not 'TEST_STATE' in update_sm.states)
        update_sm.states['TEST_STATE'] = (InsertingRedundantBlocks(
            update_sm, 'TEST_STATE', FINISHING, FAILING))

        ctx = ArchiveUpdateContext(update_sm, FakeUI())
        ctx.update({
            'ARCHIVE_CACHE_DIR': self.tmp_dir,
            'REQUEST_URI': SOME_USK,
            'ARCHIVE_BLOCK_FILES': insert_files,
            'START_STATE': 'TEST_STATE'
        })

        create_dirs(ctx.ui_, ctx['ARCHIVE_CACHE_DIR'], ctx['REQUEST_URI'])

        start(update_sm, ctx)
        run_until_quiescent(update_sm, POLL_SECS)
        self.assertTrue(
            update_sm.get_state(QUIESCENT).arrived_from(((FINISHING, ))))

        blocks = update_sm.states['TEST_STATE'].files
        for index, entry in enumerate(blocks):
            print "block [%i]: len: %i" % (index, entry[1])
            for chk in entry[2]:
                print "   ", chk

        # FREENET_BLOCK_LEN - 1, first is unpadded
        self.checkCHK(blocks[0][2][0], blocks[0][1], blocks[0][1],
                      bytes(blocks[0][1], offset))
        # FREENET_BLOCK_LEN - 1, second is padded
        self.checkCHK(blocks[0][2][1], blocks[0][1], blocks[0][1] + 1,
                      bytes(blocks[0][1], offset))

        # FREENET_BLOCK_LEN first is padded
        self.checkCHK(blocks[1][2][0], blocks[1][1], blocks[1][1] + 1,
                      bytes(blocks[1][1], offset))
        # FREENET_BLOCK_LEN second is padded
        self.checkCHK(blocks[1][2][1], blocks[1][1], blocks[1][1] + 1,
                      bytes(blocks[1][1], offset))

        # FREENET_BLOCK_LEN + 1, first is unpadded
        self.checkCHK(blocks[2][2][0], blocks[2][1], blocks[2][1],
                      bytes(blocks[2][1], offset))
        # FREENET_BLOCK_LEN + 1, second is unpadded
        self.checkCHK(blocks[2][2][1], blocks[2][1], blocks[2][1],
                      bytes(blocks[2][1], offset))

        # 1, first is unpadded
        self.checkCHK(blocks[3][2][0], blocks[3][1], blocks[3][1],
                      bytes(blocks[3][1], offset))

        # 1, second is padded
        self.checkCHK(blocks[3][2][1], blocks[3][1], blocks[3][1] + 1,
                      bytes(blocks[3][1], offset))

        # FREENET_BLOCK_LEN + 11235, first is unpadded
        self.checkCHK(blocks[4][2][0], blocks[4][1], blocks[4][1],
                      bytes(blocks[4][1], offset))

        # FREENET_BLOCK_LEN + 11235, second is unpadded
        self.checkCHK(blocks[4][2][1], blocks[4][1], blocks[4][1],
                      bytes(blocks[4][1], offset))

        # Save info for use in request testing
        SHARED_STATE['FILE_BLOCKS'] = blocks
        SHARED_STATE['OFFSET'] = offset