Пример #1
0
int main( int argc, char ** argv)
{
    struct bootconfig bc;
    enum bt_ll_parttype which = kernel;
    int res;

    {
        uint32_t channels, levels;
        get_log_config(&channels, &levels);
        set_log_config(channels, BC_LOG_STDERR);
    }

    MOCK_CB_SET( mtd_read, my_mtd_read );


    initialised = 1;

    bc.info.eb_cnt = 6;
    bc.info.min_io_size = 9876;
    bc.blocks = _alloc_blocks( bc.info.eb_cnt );
    bc.dev = "/test/device";

    memcpy( &bc.blocks[0], "BAD!", 4); bc.blocks[0].epoch = 99;
    memcpy( &bc.blocks[2], "Boot", 4); bc.blocks[2].epoch = 12;
    memcpy( &bc.blocks[3], "Boot", 4); bc.blocks[3].epoch =  2;

    bc.blocks[2].kernel.partition = 1;
    bc.blocks[2].kernel.n_booted  = 1;
    bc.blocks[2].kernel.n_healthy = 0;
    bc.blocks[2].rootfs.partition = 0;
    bc.blocks[2].rootfs.n_booted  = 1;
    bc.blocks[2].rootfs.n_healthy = 1;

    bc.blocks[1].kernel.partition = 0;
    bc.blocks[1].kernel.n_booted  = 0;
    bc.blocks[1].kernel.n_healthy = 0;
    bc.blocks[1].rootfs.partition = 1;
    bc.blocks[1].rootfs.n_booted  = 0;
    bc.blocks[1].rootfs.n_healthy = 0;

    MOCK_3_CALL( -1, mtd_is_bad, &bc.info, bc.fd, 3 );
    MOCK_3_CALL(  0, mtd_is_bad, &bc.info, bc.fd, 4 );
    MOCK_4_CALL( -1, mtd_erase, bc.mtd, &bc.info, bc.fd, 4 );
    MOCK_3_CALL(  1, mtd_is_bad, &bc.info, bc.fd, 4 );

    MOCK_3_CALL(  0, mtd_is_bad, &bc.info, bc.fd, 5 );
    MOCK_4_CALL(  0, mtd_erase, bc.mtd, &bc.info, bc.fd, 5 );
    MOCK_10_CALL(-1, mtd_write, bc.mtd, &bc.info, bc.fd, 5, 0, DONT_CHECK_PARAM, bc.info.min_io_size,
            NULL, 0, 0 );
    MOCK_3_CALL( -1, mtd_is_bad, &bc.info, bc.fd, 5 );

    MOCK_3_CALL(  0, mtd_is_bad, &bc.info, bc.fd, 1 );
    MOCK_4_CALL(  0, mtd_erase, bc.mtd, &bc.info, bc.fd, 1 );
    MOCK_10_CALL( 0, mtd_write, bc.mtd, &bc.info, bc.fd, 1, 0, DONT_CHECK_PARAM, bc.info.min_io_size,
            NULL, 0, 0 );

    MOCK_3_CALL( -1, mtd_is_bad, &bc.info, bc.fd, 0 );
    MOCK_3_CALL(  0, mtd_is_bad, &bc.info, bc.fd, 1 );
    MOCK_6_CALL( 0, mtd_read, &bc.info, bc.fd, 1, 0, &bc.blocks[1], sizeof( *bc.blocks ) );
    MOCK_3_CALL(  0, mtd_is_bad, &bc.info, bc.fd, 2 );
    MOCK_6_CALL( 0, mtd_read, &bc.info, bc.fd, 2, 0, &bc.blocks[2], sizeof( *bc.blocks ) );
    MOCK_3_CALL( -1, mtd_is_bad, &bc.info, bc.fd, 3 );
    MOCK_3_CALL( -1, mtd_is_bad, &bc.info, bc.fd, 4 );
    MOCK_3_CALL( -1, mtd_is_bad, &bc.info, bc.fd, 5 );

    res = bc_ll_set_partition( &bc, kernel, 1 );

    TEST_ASSERT( 0, res, int);
    TEST_ASSERT( 1, !!(0 != memcmp( &bc.blocks[0], "BAD!", 4)), int ); 
    TEST_ASSERT( 0, memcmp(         &bc.blocks[1], "Boot", 4) ,int );
    TEST_ASSERT( 0, memcmp(         &bc.blocks[2], "Boot", 4) ,int );
    TEST_ASSERT( 1, !!(0 != memcmp( &bc.blocks[3], "BAD!", 4)),int );
    TEST_ASSERT( 0, memcmp(         &bc.blocks[4], "BAD!", 4), int );
    TEST_ASSERT( 1, !!(0 != memcmp( &bc.blocks[5], "BAD!", 4)),int );

    TEST_ASSERT( 1, bc.blocks[1].kernel.partition, int );
    TEST_ASSERT( 1, bc.blocks[1].kernel.n_booted, int);
    TEST_ASSERT( 1, bc.blocks[1].kernel.n_healthy, int);
    TEST_ASSERT( 0, bc.blocks[1].rootfs.partition, int)
    TEST_ASSERT( 1, bc.blocks[1].rootfs.n_booted, int);
    TEST_ASSERT( 1, bc.blocks[1].rootfs.n_healthy, int);

    TEST_ASSERT( 11, _mtd_is_bad_called_count, int );
    TEST_ASSERT( 2, _mtd_erase_called_count, int );
    TEST_ASSERT( 1, _mtd_write_called_count, int );

    return 0;
}
Пример #2
0
static void test_add( void )
{
	struct mtd_blktrans_ops test_ops;
	struct mtd_info		test_mtd;
	struct mtd_block_map	test_map;

	const unsigned int 	nr_of_ebs = 4;
	uint32_t   block_map_mem[ nr_of_ebs ];

#ifdef CONFIG_MTD_BLOCK_ROM_DELAYED_INIT
	struct task_struct task;
	test_ops.name  = "spargel";
	test_mtd.index = 1954;
#endif

	test_mtd.erasesize   = 128 * 1024;
	test_mtd.size 	     = test_mtd.erasesize * nr_of_ebs;
	test_mtd.block_isbad = mtd_block_isbad;

	memset( &test_map, 	0x00, sizeof(test_map));
	memset( &block_map_mem, 0x00, sizeof(block_map_mem));
	MOCK_2_CALL( &test_map,      kzalloc, sizeof(test_map), GFP_KERNEL );
	MOCK_2_CALL( &block_map_mem, kzalloc, sizeof(block_map_mem), GFP_KERNEL );

#ifdef CONFIG_MTD_BLOCK_ROM_DELAYED_INIT
	MOCK_5_CALL( &task, kthread_run, blockrom_add_bottom, &test_map, DONT_CHECK_PARAM,
			test_ops.name, test_mtd.index );
	MOCK_CB_SET( kthread_run, my_kthread_run );
#endif

	MOCK_2_CALL( 0, mtd_block_isbad, &test_mtd, test_mtd.erasesize * 0);
	MOCK_2_CALL( 0, mtd_block_isbad, &test_mtd, test_mtd.erasesize * 1);
	MOCK_2_CALL( 1, mtd_block_isbad, &test_mtd, test_mtd.erasesize * 2);
	MOCK_2_CALL( 0, mtd_block_isbad, &test_mtd, test_mtd.erasesize * 3);

	MOCK_1_CALL( 0, add_mtd_blktrans_dev, &test_map.dev );

	blockrom_tr.add_mtd( &test_ops, &test_mtd );

	TEST_ASSERT( test_map.dev.mtd, 	 &test_mtd, struct mtd_info *);
	TEST_ASSERT( test_map.block_map, (uint32_t *)&block_map_mem, uint32_t *);
	TEST_ASSERT( test_map.dev.tr, 	 &test_ops, struct mtd_blktrans_ops* );

	TEST_ASSERT( nr_of_ebs, test_map.blocks_total, uint32_t);
	TEST_ASSERT( 1, test_map.blocks_bad, uint32_t);

	/* check HDD size (in 512 byte blocks); we have 1 bad eraseblock */
	TEST_ASSERT( test_map.dev.size, (test_mtd.size - test_mtd.erasesize) / 512, uint32_t);
	TEST_ASSERT( test_map.dev.readonly, 1, int);

	TEST_ASSERT( test_map.block_map[ 0 ], 0, uint32_t );
	TEST_ASSERT( test_map.block_map[ 1 ], 1, uint32_t );
	TEST_ASSERT( test_map.block_map[ 2 ], 3, uint32_t );
	TEST_ASSERT( test_map.block_map[ 3 ], 4, uint32_t );

	TEST_ASSERT_CALLED( kzalloc, 2);
	TEST_ASSERT_CALLED( mtd_block_isbad, nr_of_ebs);
	TEST_ASSERT_CALLED( add_mtd_blktrans_dev, 1);

	MOCK_RESET( kzalloc );
	MOCK_RESET( mtd_block_isbad );
	MOCK_RESET( add_mtd_blktrans_dev );
#ifdef CONFIG_MTD_BLOCK_ROM_DELAYED_INIT
	MOCK_RESET( kthread_run );
	MOCK_CB_CLEAR( kthread_run );
#endif
}