END_TEST /* test vmi_translate_uv2p */ //TODO figure out how to test this //START_TEST (test_libvmi_uv2p) //{ // vmi_translate_uv2p(vmi, vaddr, pid) //} //END_TEST /* test vmi_translate_ksym2v */ START_TEST (test_libvmi_ksym2v) { vmi_instance_t vmi = NULL; vmi_init(&vmi, VMI_AUTO | VMI_INIT_COMPLETE, get_testvm()); addr_t va = 0; if (VMI_OS_WINDOWS == vmi_get_ostype(vmi)){ va = vmi_translate_ksym2v(vmi, "PsInitialSystemProcess"); } else if (VMI_OS_LINUX == vmi_get_ostype(vmi)){ va = vmi_translate_ksym2v(vmi, "init_task"); } else{ fail_unless(0, "vmi set to invalid os type"); } fail_unless(va != 0, "ksym2v translation failed"); vmi_destroy(vmi); }
END_TEST #if ENABLE_KVM == 1 /* test vmi_get_dgvma */ // we use vmi_read_va() to verify vmi_get_dgvma() START_TEST (test_vmi_get_dgvma) { vmi_instance_t vmi = NULL; vmi_init(&vmi, VMI_AUTO | VMI_INIT_COMPLETE, get_testvm()); vmi_shm_snapshot_create(vmi); addr_t va = 0x0; size_t count = 4096; unsigned long max_size = 0xffff; void *buf_readva = malloc(count); void *buf_dgvma = NULL; for (; va + count <= max_size; va += count) { size_t read_va = vmi_read_va(vmi, va, 0, buf_readva, count); size_t read_dgvma = vmi_get_dgvma(vmi, va, 0, &buf_dgvma, count); fail_unless(read_va == read_dgvma, "vmi_get_dgvma(0x%"PRIx64 ") read size %d dosn't conform to %d of vmi_read_va()", va, read_dgvma, read_va); int cmp = memcmp(buf_readva, buf_dgvma, read_va); fail_unless(0 == cmp, "vmi_get_dgvma(0x%"PRIx64 ") contents dosn't conform to vmi_read_va()", va); } free(buf_readva); vmi_shm_snapshot_destroy(vmi); vmi_destroy(vmi); }
END_TEST /* test determine mode and init function */ START_TEST (test_libvmi_init2) { vmi_instance_t vmi = NULL; vmi_mode_t mode; status_t ret = vmi_get_access_mode(vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, &mode); fail_unless(ret == VMI_SUCCESS, "vmi_get_access_mode failed to identify the hypervisor"); ret = vmi_init(&vmi, mode, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, NULL); fail_unless(ret == VMI_SUCCESS, "vmi_init failed"); fail_unless(vmi != NULL, "vmi_init failed to initialize vmi instance struct"); vmi_destroy(vmi); }
END_TEST /* test auto complete init */ START_TEST (test_libvmi_init1) { vmi_instance_t vmi = NULL; status_t ret = vmi_init(&vmi, VMI_AUTO | VMI_INIT_COMPLETE, get_testvm()); fail_unless(ret == VMI_SUCCESS, "vmi_init failed with AUTO | COMPLETE"); fail_unless(vmi != NULL, "vmi_init failed to initialize vmi instance struct"); vmi_destroy(vmi); }
END_TEST /* test auto complete init */ START_TEST (test_libvmi_init1) { vmi_instance_t vmi = NULL; status_t ret = vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL); fail_unless(ret == VMI_SUCCESS, "vmi_init failed with VMI_INIT_DOMAINNAME and global config"); fail_unless(vmi != NULL, "vmi_init failed to initialize vmi instance struct"); vmi_destroy(vmi); }
END_TEST /* test partial init and init_complete function */ START_TEST (test_libvmi_init2) { vmi_instance_t vmi = NULL; status_t ret = vmi_init(&vmi, VMI_AUTO | VMI_INIT_PARTIAL, get_testvm()); fail_unless(ret == VMI_SUCCESS, "vmi_init failed with AUTO | PARTIAL"); fail_unless(vmi != NULL, "vmi_init failed to initialize vmi instance struct"); ret = vmi_init_complete(&vmi, NULL); fail_unless(ret == VMI_SUCCESS, "vmi_init_complete failed"); fail_unless(vmi != NULL, "vmi_init_complete failed to initialize vmi instance struct"); vmi_destroy(vmi); }
END_TEST /* test vmi_translate_kv2p */ START_TEST (test_libvmi_kv2p) { vmi_instance_t vmi = NULL; vmi_init(&vmi, VMI_AUTO | VMI_INIT_COMPLETE, get_testvm()); addr_t va = 0; if (VMI_OS_WINDOWS == vmi_get_ostype(vmi)){ va = vmi_translate_ksym2v(vmi, "PsInitialSystemProcess"); } else if (VMI_OS_LINUX == vmi_get_ostype(vmi)){ va = vmi_translate_ksym2v(vmi, "init_task"); } else{ fail_unless(0, "vmi set to invalid os type"); } addr_t pa = vmi_translate_kv2p(vmi, va); fail_unless(pa != 0, "kv2p translation failed"); vmi_destroy(vmi); }
END_TEST START_TEST (test_vmi_get_memsize_max_phys_addr) { vmi_instance_t vmi = NULL; uint64_t memsize = 0; addr_t max_physical_addr = 0; vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, NULL); memsize = vmi_get_memsize(vmi); max_physical_addr = vmi_get_max_physical_address(vmi); fail_unless(memsize > 0, "guest ram size is 0"); fail_unless(max_physical_addr > 0, "max physical address is 0"); fail_unless(max_physical_addr >= memsize, "max physical address is less than memsize"); vmi_destroy(vmi); }
END_TEST /* test vmi_get_dgpma */ // we use vmi_read_pa() to verify vmi_get_dgpma() START_TEST (test_vmi_get_dgpma) { vmi_instance_t vmi = NULL; vmi_init(&vmi, VMI_AUTO | VMI_INIT_COMPLETE, get_testvm()); vmi_shm_snapshot_create(vmi); addr_t pa = 0x1000; // just because vmi_read_page() deny to fetch frame 0. size_t count = 4096; unsigned long max_size = vmi_get_max_physical_address(vmi); void *buf_readpa = malloc(count); void *buf_dgpma = NULL; for (; pa + count <= max_size; pa += count) { size_t read_pa = vmi_read_pa(vmi, pa, buf_readpa, count); size_t read_dgpma = vmi_get_dgpma(vmi, pa, &buf_dgpma, count); if (read_pa == 0 && read_dgpma == 0) { continue; } fail_unless(read_pa == read_dgpma, "vmi_get_dgpma(0x%"PRIx64 ") read size %d dosn't conform to %d of vmi_read_pa()", pa, read_dgpma, read_pa); int cmp = memcmp(buf_readpa, buf_dgpma, read_pa); fail_unless(0 == cmp, "vmi_get_dgpma(0x%"PRIx64 ") contents dosn't conform to vmi_read_pa()", pa); } free(buf_readpa); vmi_shm_snapshot_destroy(vmi); vmi_destroy(vmi); }
END_TEST /* test init_complete with passed config */ START_TEST (test_libvmi_init3) { FILE *f = NULL; const char *ptr = NULL; char location[100]; const char *sudo_user = NULL; struct passwd *pw_entry = NULL; vmi_instance_t vmi = NULL; /* read the config entry from the config file */ /* first check home directory of sudo user */ if ((sudo_user = getenv("SUDO_USER")) != NULL) { if ((pw_entry = getpwnam(sudo_user)) != NULL) { snprintf(location, 100, "%s/etc/libvmi.conf\0", pw_entry->pw_dir); if ((f = fopen(location, "r")) != NULL) { goto success; } } } /* next check home directory for current user */ snprintf(location, 100, "%s/etc/libvmi.conf\0", getenv("HOME")); if ((f = fopen(location, "r")) != NULL) { goto success; } /* finally check in /etc */ snprintf(location, 100, "/etc/libvmi.conf\0"); if ((f = fopen(location, "r")) != NULL) { goto success; } fail_unless(0, "failed to find config file"); success: /* strip path for memory image files */ if ((ptr = strrchr(get_testvm(), '/')) == NULL) { ptr = get_testvm(); } else { ptr++; } /* check file size */ fseek(f, 0L, SEEK_END); long sz = ftell(f); fseek(f, 0L, SEEK_SET); /* read entry in from file */ char *buf = malloc(sz); fread(buf, sz, 1, f); long pos = 0; size_t max_len = strnlen(ptr, 100); int found = 0; for (pos = 0; pos < sz; ++pos) { if (strncmp(buf + pos, ptr, max_len) == 0) { found = 1; break; } } if (!found) { fail_unless(0, "failed to find config entry"); } long start = pos + max_len; found = 0; for ( ; pos < sz; ++pos) { if (buf[pos] == '}') { found = 1; break; } } if (!found) { fail_unless(0, "failed to find end of config entry"); } long end = pos + 1; long entry_length = end - start; char *config = malloc(entry_length); memcpy(config, buf + start, entry_length); free(buf); status_t ret = vmi_init_complete(&vmi, (void*)get_testvm(), VMI_INIT_DOMAINNAME, NULL, VMI_CONFIG_STRING, (void*)config, NULL); free(config); fail_unless(ret == VMI_SUCCESS, "vmi_init_complete failed"); fail_unless(vmi != NULL, "vmi_init_complete failed to initialize vmi instance struct"); vmi_destroy(vmi); }