int main()
{
    srand(time(0));
    debug(4, "Test case scenario: write random parts of memory, then read it");
    debug(4, "Initializing library");
    ASSERT_SYSCALL(page_sim_init(PAGE_SIZE, MEM_SIZE, ADDRESS_SPACE_SIZE,
                MAX_CONCURRENT_OPERATIONS, callback));
    local_version = malloc(TOTAL_MEMORY_LENGTH);
    debug(4, "Filling memory with zeros");
    int i;
    for(i=0; i<TOTAL_MEMORY_LENGTH; ++i){
        ASSERT_SYSCALL(page_sim_set(i, 0));
        local_version[i] = 0;
    }
    debug(4, "Spawning %d threads", THREADS_COUNT);
    pthread_t threads[THREADS_COUNT];
    for(i=0; i<THREADS_COUNT; ++i){
        ASSERT_PTHREAD(pthread_create(threads+i, 0, thread_procedure, 0));
    }
    debug(4, "Threads spawned, now joining with them");
    for(i=0; i<THREADS_COUNT; ++i){
        ASSERT_PTHREAD(pthread_join(threads[i], 0));
    }
    debug(4, "Joined all threads");
    debug(4, "Testing content of whole available memory");
    for(i=0; i<TOTAL_MEMORY_LENGTH; ++i){
        debug(4, "Reading byte from address %u:", i);
        uint8_t v;
        ASSERT_SYSCALL(page_sim_get(i, &v));
        debug(4, "Read byte: %d from address %u", v, i);
        assert(local_version[i] == v);
        debug(4, "Byte correct");
    }
    debug(4, "All memory was OK");
    debug(4, "Releasing library");
    ASSERT_SYSCALL(page_sim_end());
    test_callback();
    return 0;
}
Ejemplo n.º 2
0
int main()
{
    srand(time(0));
    debug(5, "Running test case %d", TEST_CASE);
    const char* TEST_LIB = "libtest.so";
    struct call_cnt* desc;
    debug(5, "Trying to intercept %s calls", TEST_LIB);
    ASSERT_CALL_CNT_LIB(intercept(&desc, TEST_LIB));

    /* Run some code here */
    #if TEST_CASE < 100
        /* One-threaded cases */
        entry_point();
    #elif TEST_CASE < 200
        /* Multi-threaded cases */
        pthread_t threads[THREADS_NUM];
        pthread_attr_t attr;
        ASSERT_PTHREAD(pthread_attr_init(&attr));
        ASSERT_PTHREAD(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
        size_t i;
        for(i=0; i<THREADS_NUM; ++i){
            debug(7, "Starting thread num %d", i);
            ASSERT_PTHREAD(pthread_create(&threads[i], &attr, entry_point, NULL));
        }

        for(i=0; i<THREADS_NUM; ++i){
            ASSERT_PTHREAD(pthread_join(threads[i], NULL));
            debug(7, "Joined with thread num %d", i);
        }
        ASSERT_PTHREAD(pthread_attr_destroy(&attr));
    #elif TEST_CASE == 201
        /* double intercept() call */
        struct call_cnt *desc2;
        debug(5, "Trying to intercept %s calls again", TEST_LIB);
        ASSERT_CALL_CNT_LIB(intercept(&desc2, TEST_LIB));
        entry_point();
        debug(5, "Undoing the inner-most interception of %s", TEST_LIB);
        ASSERT_CALL_CNT_LIB(stop_intercepting(desc2));
        #if DEBUG > 0
            ASSERT_CALL_CNT_LIB(print_stats_to_stream(stdout, desc2));
        #endif
    #elif TEST_CASE == 202
        entry_point();
    #else
        fatal("No such test case");
    #endif

    debug(5, "Stopping interception of %s calls", TEST_LIB);
    ASSERT_CALL_CNT_LIB(stop_intercepting(desc));
    #if DEBUG > 0
        ASSERT_CALL_CNT_LIB(print_stats_to_stream(stdout, desc));
    #endif

    int intern_calls;
    ASSERT_CALL_CNT_LIB(intern_calls = get_num_intern_calls(desc));
    int extern_calls;
    ASSERT_CALL_CNT_LIB(extern_calls = get_num_extern_calls(desc));

    /* Perform assertions here */
    debug(5, "intern_calls = %d, expected_intern_calls = %d", intern_calls, expected_intern_calls());
    assert(intern_calls == expected_intern_calls());
    debug(5, "extern_calls = %d, expected_extern_calls = %d", extern_calls, expected_extern_calls());
    assert(extern_calls == expected_extern_calls());
    debug(5, "Assertions OK");

    #if TEST_CASE == 201
        int intern_calls2;
        ASSERT_CALL_CNT_LIB(intern_calls2 = get_num_intern_calls(desc));
        int extern_calls2;
        ASSERT_CALL_CNT_LIB(extern_calls2 = get_num_extern_calls(desc));

        /* Perform assertions here */
        debug(5, "intern_calls2 = %d, expected_intern_calls = %d", intern_calls2, expected_intern_calls());
        assert(intern_calls2 == expected_intern_calls());
        debug(5, "extern_calls2 = %d, expected_extern_calls = %d", extern_calls2, expected_extern_calls());
        assert(extern_calls2 == expected_extern_calls());
        debug(5, "Assertions #2 OK");
        debug(5, "Releasing stats #2");
        ASSERT_CALL_CNT_LIB(release_stats(desc2));
        desc2 = NULL;
    #elif TEST_CASE == 202
        debug(5, "Now calling library %s after stop_intercepting()", TEST_LIB);
        uint32_t was_expected_intern_calls = expected_intern_calls();
        uint32_t was_expected_extern_calls = expected_extern_calls();
        entry_point();
        debug(5, "intern_calls = %d, was_expected_intern_calls = %d", intern_calls, was_expected_intern_calls);
        assert(intern_calls == was_expected_intern_calls);
        debug(5, "extern_calls = %d, was_expected_extern_calls = %d", extern_calls, was_expected_extern_calls);
        assert(extern_calls == was_expected_extern_calls);
        debug(5, "Assertions #2 OK");
    #endif


    debug(5, "Releasing stats");
    ASSERT_CALL_CNT_LIB(release_stats(desc));
    desc = NULL;

    debug(5, "Test case %d complete", TEST_CASE);
    return 0;
}