Example #1
0
int main( int argc, char ** argv)
{
    struct bootconfig bc;
    unsigned int i, j;
    int res;

    initialised = 1;

    bc.info.eb_cnt = 33;
    bc.info.min_io_size = 2342;
    bc.blocks = _alloc_blocks( bc.info.eb_cnt );
    bc.dev = "/test/trallalla";

    bc.fd = 9;

    /* first run: some mdt_is_bad errors, some read errors, some bad blocks */
    j=0;
    for ( i = 0; i < bc.info.eb_cnt; i++ ) {
        int ret = 0;
    
        if ( 0 == ( i % 5 ) )
            ret = 1;

        if ( 0 == ( i % 11 ) )
            ret = -1;

        MOCK_3_CALL( ret, mtd_is_bad, &bc.info, bc.fd, i );
        if ( 0 == ret ) {
            int ret2 = ( 0 == ( i % 9 ) ) ? -1 : 0;
            MOCK_6_CALL( ret2, mtd_read, &bc.info, bc.fd, i, 0, &bc.blocks[i], sizeof(struct btblock) );
            j++;
        }
    }

    res = bc_ll_reread( &bc );

    TEST_ASSERT( 0, res, int);

    TEST_ASSERT( i - 1, _mtd_is_bad_called_count, int );
    TEST_ASSERT( j - 1, _mtd_read_called_count, int );

    for ( i = 0; i < bc.info.eb_cnt; i++ ) {
        if (    ( 0 == ( i %  5 ) )
            && !( 0 == ( i % 11 ) ) ) {
            TEST_ASSERT( 0, memcmp( bc.blocks[i].magic, "BAD!", 4 ), int);
        } else {
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;
}