int main(void) { if (!try_setup_ns(CLONE_NEWNET)) { atomic_printf("EXIT-SUCCESS"); return 0; } /* * For this test, we'll be manually doing the following: * iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j MASQUERADE */ int sock_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); struct ipt_getinfo info; memset(&info, 0, sizeof(info)); strcpy(info.name, "nat"); uint32_t getinfo_size = sizeof(info); test_assert( 0 == getsockopt(sock_fd, SOL_IP, IPT_SO_GET_INFO, &info, &getinfo_size)); test_assert(getinfo_size == sizeof(info)); atomic_printf("%d existing entries in nat table\n", info.num_entries); struct ipt_get_entries* entries = malloc(sizeof(struct ipt_get_entries) + info.size); strcpy(entries->name, "nat"); entries->size = info.size; uint32_t getentries_size = sizeof(struct ipt_get_entries) + entries->size; test_assert(0 == getsockopt(sock_fd, SOL_IP, IPT_SO_GET_ENTRIES, entries, &getentries_size)); test_assert(getentries_size == sizeof(struct ipt_get_entries) + info.size); // matches will be empty struct xt_entry_target target; const char* target_name = "MASQUERADE"; target.u.user.target_size = strlen(target_name) - 1; memcpy(target.u.user.name, target_name, strlen(target_name) - 1); struct ipt_entry entry; memset(&entry, 0, sizeof(struct ipt_entry)); entry.ip.src.s_addr = 0x10; entry.ip.smsk.s_addr = 0xffffff; entry.target_offset = 0x70; entry.next_offset = 0x98; // Allocate space to receive counters struct xt_counters* counters = malloc(sizeof(struct xt_counters) * info.num_entries); // We will check for at least some of these bytes being replaced by the kernel memset(counters, 0xff, sizeof(struct xt_counters) * info.num_entries); struct ipt_replace repl; strcpy(repl.name, "nat"); repl.num_entries = info.num_entries + 1; repl.size = info.size + entry.next_offset; memcpy(repl.hook_entry, info.hook_entry, sizeof(repl.hook_entry)); memcpy(repl.underflow, info.underflow, sizeof(repl.underflow)); repl.num_counters = info.num_entries; repl.counters = counters; size_t final_size = sizeof(struct ipt_replace) + repl.size; char* final = malloc(final_size); // Assemble structure memcpy(final, &repl, sizeof(struct ipt_replace)); // Copy over original entries and insert our new one as the second-to-last one char* src_ptr = (char*)entries->entrytable; char* dest_ptr = (char*)((struct ipt_replace*)final)->entries; for (size_t i = 0; i < info.num_entries; ++i) { if (i == info.num_entries - 2) { memcpy(dest_ptr, &entry, sizeof(struct ipt_entry)); dest_ptr += sizeof(struct ipt_entry); memcpy(dest_ptr, &target, sizeof(struct xt_entry_target)); dest_ptr += sizeof(struct xt_entry_target); size_t npad = entry.next_offset - sizeof(struct xt_entry_target); memset(dest_ptr, 0, npad); dest_ptr += npad; } struct ipt_entry* cur_entry = (struct ipt_entry*)src_ptr; memcpy(dest_ptr, src_ptr, cur_entry->next_offset); dest_ptr += cur_entry->next_offset; src_ptr += cur_entry->next_offset; } // Finally pass this off to the kernel test_assert( setsockopt(sock_fd, SOL_IP, IPT_SO_SET_REPLACE, final, final_size)); // Verify that the counters array was overwritten. Since we don't know the // exact value here, just make sure some bytes were written. After every byte // comparison we also call getuid if they were not the same, which should // catch any replay divergence, just from tick mismatch. int any_changed = 0; for (size_t i = 0; i < sizeof(struct xt_counters) * info.num_entries; ++i) { if (((uint8_t*)entries)[i] != 0xff) { any_changed = 1; (void)getuid(); } } test_assert(any_changed); atomic_printf("EXIT-SUCCESS"); }
/***********************************************************************//** * @brief Test GRmf class **************************************************************************/ void TestGXspec::test_GRmf(void) { // Test void constructor test_try("GRmf void constructor"); try { GRmf rmf; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test energy boundary constructor test_try("GRmf energy boundary constructor"); try { GEbounds ebds(10, GEnergy(0.1, "TeV"), GEnergy(10.0, "TeV")); GRmf rmf(ebds, ebds); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test filling and accessing GEbounds ebds(9, GEnergy(1.0, "TeV"), GEnergy(10.0, "TeV")); GRmf rmf(ebds, ebds); for (int i = 0; i < 9; ++i) { for (int k = i; k < i+3 && k < 9; ++k) { rmf(i, k) = 1.0; } } for (int i = 0; i < 9; ++i) { int k = 0; for (; k < i; ++k) { test_value(rmf.at(i,k), 0.0); test_value(rmf(i,k), 0.0); } for (; k < i+3 && k < 9; ++k) { test_value(rmf.at(i,k), 1.0); test_value(rmf(i,k), 1.0); } for (; k < 9; ++k) { test_value(rmf.at(i,k), 0.0); test_value(rmf(i,k), 0.0); } } // Test saving and loading rmf.save("rmf.fits", true); test_assert(rmf.filename() == "rmf.fits", "Unexpected filename \""+rmf.filename()+"\"."); rmf.load("rmf.fits"); test_assert(rmf.filename() == "rmf.fits", "Unexpected filename \""+rmf.filename()+"\"."); /* for (int i = 0; i < 9; ++i) { for (int k = 0; k < 9; ++k) { std::cout << rmf(i,k) << " "; } std::cout << std::endl; } */ for (int i = 0; i < 9; ++i) { int k = 0; for (; k < i; ++k) { test_value(rmf.at(i,k), 0.0); test_value(rmf(i,k), 0.0); } for (; k < i+3 && k < 9; ++k) { test_value(rmf.at(i,k), 1.0); test_value(rmf(i,k), 1.0); } for (; k < 9; ++k) { test_value(rmf.at(i,k), 0.0); test_value(rmf(i,k), 0.0); } } // Test constructing GRmf rmf2("rmf.fits"); test_assert(rmf2.filename() == "rmf.fits", "Unexpected filename \""+rmf2.filename()+"\"."); for (int i = 0; i < 9; ++i) { int k = 0; for (; k < i; ++k) { test_value(rmf.at(i,k), 0.0); test_value(rmf(i,k), 0.0); } for (; k < i+3 && k < 9; ++k) { test_value(rmf.at(i,k), 1.0); test_value(rmf(i,k), 1.0); } for (; k < 9; ++k) { test_value(rmf.at(i,k), 0.0); test_value(rmf(i,k), 0.0); } } //std::cout << rmf << std::endl; //std::cout << rmf2 << std::endl; // Return return; }
void copy_test(struct dstring *ds) { /* 1 + 32 == 33 */ test_assert(dstring_init(ds, 1)); /* 256 + 32 + 1 == 289 */ test_assert(dstring_cpys(ds, BIG_STRING)); test_assert(256 == ds->len); test_assert(289 == ds->a); test_assert(dstring_size(ds) == 256); test_assert(dstring_data(ds) != 0); /* ds->s[288] == ok, ds->s[289] == overflow */ test_assert('1' == ds->s[248]); test_assert('2' == ds->s[249]); test_assert('3' == ds->s[250]); test_assert('4' == ds->s[251]); test_assert('5' == ds->s[252]); test_assert('6' == ds->s[253]); test_assert('7' == ds->s[254]); test_assert('8' == ds->s[255]); test_assert('1' == ds->s[8]); test_assert('2' == ds->s[9]); test_assert('3' == ds->s[10]); test_assert('4' == ds->s[11]); test_assert('5' == ds->s[12]); test_assert('6' == ds->s[13]); test_assert('7' == ds->s[14]); test_assert('8' == ds->s[15]); }
int main() { irr::IrrlichtDevice* device = irr::createDevice(irr::video::EDT_OPENGL); irr::ITimer* timer = device->getTimer(); vik::Tween t(timer, vik::tweenLERP); timer->start(); timer->setTime(0); t.start(0, 1, 1000); test_assert(t.getPlaying() == true); test_assert(t.getCurrentValue() <= 0.001f); timer->setTime(600); t.update(); test_assert(t.getCurrentValue() > 0.5f); test_assert(t.getPlaying() == true); timer->setTime(2000); t.update(); test_assert(fabs(t.getCurrentValue() - 1.0f) <= 0.001f); test_assert(t.getPlaying() == false); t.restart(-2, 3, 0); t.update(); test_assert(t.getPlaying() == false); test_assert(fabs(t.getCurrentValue() - 3.0f) <= 0.001f); timer->setTime(0); t.restart(-2, 3, 5000); test_assert(t.getPlaying() == true); test_assert(fabs(t.getCurrentValue() - (-2.0f)) <= 0.001f); timer->setTime(4000); t.update(); test_assert(t.getPlaying() == true); test_assert(t.getCurrentValue() > 0.f); t.stop(); test_assert(t.getPlaying() == false); test_assert(fabs(t.getCurrentValue() - (-2.0f)) <= 0.001f); // final sanity check std::cout << "Tweening from -2 to 3 in 100ms using linear interpolation" << std::endl; timer->setTime(0); t.restart(-2, 3, 100); irr::f32 lastDisplayedValue = 1337.f; while (timer->getTime() <= 150) { t.update(); if (lastDisplayedValue != t.getCurrentValue()) { std::cout << t.getCurrentValue() << std::endl; lastDisplayedValue = t.getCurrentValue(); } timer->tick(); } test_assert(t.getPlaying() == false); test_assert(fabs(t.getCurrentValue() - 3.f) <= 0.001f); generateReport(); }
static void handle_usr1(int sig) { test_assert(SIGUSR1 == sig); caught_usr1 = 1; puts("caught usr1"); }
long checked_ptrace(enum __ptrace_request request, pid_t pid, void* addr, void* data) { long ret = ptrace(request, pid, addr, data); test_assert(ret != -1); return ret; }
int main(void) { const char *a = "AAAABBBBCCCCDDDD \nAAAA"; const char *str; unsigned long num = 0; str = a; num = scan_charsetn(str, "AB", 3); test_assert(num == 3); str = a; num = scan_charset(str, "AB"); test_assert(num == 8); str += num; num = scan_charset(str, "D"); test_assert(num == 0); num = scan_notcharsetn(str, "D", 2); test_assert(num == 2); num = scan_notcharset(str, "D"); test_assert(num == 4); str += num; num = scan_notcharset(str, "D"); test_assert(num == 0); num = scan_charset(str, "D"); test_assert(num == 4); str += num; num = scan_whitespace(str); test_assert(num == 4); str += num; num = scan_newline(str); test_assert(num == 1); /* will not cross 0 */ str += num; num = scan_charset(str, "A"); test_assert(num == 4); str += num; num = scan_charset(str, "A"); test_assert(num == 0); return 0; }
int main(void) { struct object obj; struct object *obp; const void *vp; unsigned long num; unsigned long cmp; squeue_init(&sq, buf, QUEUE_SIZE, sizeof(struct object)); printf("h t u\n"); dump(); /* check size is zero */ test_assert(squeue_size(&sq) == 0); test_assert(squeue_SIZE(&sq) == 0); printf("\n"); /* check enq works */ for (num = 0; num < QUEUE_SIZE; ++num) { obj.num = num << 1; test_assert(squeue_enq(&sq, &obj) == 1); dump(); } printf("\n"); /* check size is correct */ test_assert(squeue_bytes(&sq) == QUEUE_SIZE * sizeof(struct object)); test_assert(squeue_BYTES(&sq) == QUEUE_SIZE * sizeof(struct object)); test_assert(squeue_size(&sq) == QUEUE_SIZE); test_assert(squeue_SIZE(&sq) == QUEUE_SIZE); /* check deny overflow */ test_assert(squeue_enq(&sq, 0) == 0); dump(); printf("\n"); /* check deq works */ for (num = 0; num < QUEUE_SIZE; ++num) { test_assert(squeue_peek(&sq, (void **) &obp) == 1); cmp = obp->num; test_assert(cmp == num << 1); test_assert(squeue_deq(&sq, (void **) &obp)); cmp = obp->num; test_assert(cmp == num << 1); dump(); } printf("\n"); /* check deny underflow */ test_assert(squeue_deq(&sq, 0) == 0); dump(); printf("\n"); /* check enq works */ for (num = 0; num < QUEUE_SIZE; ++num) { obj.num = num << 1; test_assert(squeue_enq(&sq, &obj) == 1); dump(); } printf("\n"); /* check size is correct */ test_assert(squeue_bytes(&sq) == QUEUE_SIZE * sizeof(struct object)); test_assert(squeue_BYTES(&sq) == QUEUE_SIZE * sizeof(struct object)); test_assert(squeue_size(&sq) == QUEUE_SIZE); test_assert(squeue_SIZE(&sq) == QUEUE_SIZE); /* check deny overflow */ test_assert(squeue_enq(&sq, 0) == 0); dump(); printf("\n"); /* check deq works */ for (num = 0; num < QUEUE_SIZE; ++num) { test_assert(squeue_peek(&sq, (void **) &obp) == 1); cmp = obp->num; test_assert(cmp == num << 1); test_assert(squeue_deq(&sq, (void **) &obp)); cmp = obp->num; test_assert(cmp == num << 1); dump(); } printf("\n"); /* check data works */ vp = squeue_data(&sq); test_assert(vp == buf); vp = squeue_DATA(&sq); test_assert(vp == buf); return 0; }
/* Test Case: * Submit a job that generates output after a short time. * Before that output is generated, send a STDIO_UPDATE signal, to * direct the output to a different port. * Verify that the output arrives at the new port */ int test_stdio_update(void) { char *old_listener_url, *new_listener_url; char *old_job_contact; int rc; char *callback_contact; char *old_rsl, *new_rsl; test_monitor_t monitor; const char rsl_spec[] = "&(executable=/bin/sh)" "(arguments=-c 'sleep 30; echo hello;')" "(rsl_substitution = (TEST_GASS_URL %s))" "(stdout = $(TEST_GASS_URL)/out)"; const char stdio_update_rsl_spec[] = "&(stdout = %s/out)"; globus_mutex_init(&monitor.mutex, NULL); globus_cond_init(&monitor.cond, NULL); memset(monitor.old_output, 0, sizeof(monitor.old_output)); memset(monitor.new_output, 0, sizeof(monitor.new_output)); monitor.old_request = GLOBUS_NULL_HANDLE; monitor.new_request = GLOBUS_NULL_HANDLE; monitor.status = GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED; monitor.failure_code = 0; /* Create a pair of listeners and get their base URLs. The job will be * submitted with stdout directed to the first, then redirected to the * second via a stdio update signal */ rc = globus_gass_transfer_create_listener( &monitor.old_listener, NULL, "https"); test_assert_gram_rc_equals(rc, GLOBUS_SUCCESS); test_assert(monitor.old_listener != GLOBUS_NULL_HANDLE); old_listener_url = globus_gass_transfer_listener_get_base_url( monitor.old_listener); test_assert(old_listener_url != NULL); rc = globus_gass_transfer_register_listen( monitor.old_listener, test_l_old_listener_callback, &monitor); test_assert_gram_rc_equals(rc, GLOBUS_SUCCESS); rc = globus_gass_transfer_create_listener( &monitor.new_listener, NULL, "https"); test_assert_gram_rc_equals(rc, GLOBUS_SUCCESS); test_assert(monitor.new_listener != GLOBUS_NULL_HANDLE); new_listener_url = globus_gass_transfer_listener_get_base_url( monitor.new_listener); test_assert(new_listener_url != NULL); rc = globus_gass_transfer_register_listen( monitor.new_listener, test_l_new_listener_callback, &monitor); test_assert(rc == GLOBUS_SUCCESS); old_rsl = globus_common_create_string(rsl_spec, old_listener_url); test_assert(old_rsl != NULL); /* Submit the job, do the two-phase commit, then submit a restart * request with the new stdout destination */ rc = globus_gram_client_callback_allow( test_l_gram_callback, &monitor, &callback_contact); test_assert_gram_rc_equals(rc, GLOBUS_SUCCESS); test_assert(callback_contact != NULL); rc = globus_gram_client_job_request( contact_string, old_rsl, GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED| GLOBUS_GRAM_PROTOCOL_JOB_STATE_STAGE_IN| GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING| GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE| GLOBUS_GRAM_PROTOCOL_JOB_STATE_STAGE_OUT| GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE| GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED, callback_contact, &old_job_contact); test_assert_gram_rc_equals(rc, GLOBUS_SUCCESS); test_assert(old_job_contact != NULL); globus_mutex_lock(&monitor.mutex); while (monitor.status == GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED) { globus_cond_wait(&monitor.cond, &monitor.mutex); } new_rsl = globus_common_create_string( stdio_update_rsl_spec, new_listener_url); test_assert(new_rsl != NULL); rc = globus_gram_client_job_signal( old_job_contact, GLOBUS_GRAM_PROTOCOL_JOB_SIGNAL_STDIO_UPDATE, new_rsl, &monitor.status, &monitor.failure_code); test_assert_gram_rc_equals(rc, GLOBUS_SUCCESS); /* Wait for job to complete. After it's done, check to see which * destination got stdout */ while (monitor.status != GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE && monitor.status != GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED) { globus_cond_wait(&monitor.cond, &monitor.mutex); } if (monitor.old_request) { globus_gass_transfer_fail(monitor.old_request, test_l_gass_fail, NULL); } globus_mutex_unlock(&monitor.mutex); if (monitor.new_output[0] == 0) { fprintf(stderr, "Didn't get expected output to new handle\n"); test_assert(strcmp((char *) monitor.new_output, "hello\n") == 0); } if (monitor.old_output[0] != 0) { fprintf(stderr, "Unexpected output to old handle: %s", monitor.old_output); test_assert(monitor.old_output[0] == 0); } return rc; }
/** Run unit tests for our AES functionality */ static void test_crypto_aes(void) { char *data1 = NULL, *data2 = NULL, *data3 = NULL; crypto_cipher_env_t *env1 = NULL, *env2 = NULL; int i, j; char *mem_op_hex_tmp=NULL; data1 = tor_malloc(1024); data2 = tor_malloc(1024); data3 = tor_malloc(1024); /* Now, test encryption and decryption with stream cipher. */ data1[0]='\0'; for (i = 1023; i>0; i -= 35) strncat(data1, "Now is the time for all good onions", i); memset(data2, 0, 1024); memset(data3, 0, 1024); env1 = crypto_new_cipher_env(); test_neq(env1, 0); env2 = crypto_new_cipher_env(); test_neq(env2, 0); j = crypto_cipher_generate_key(env1); crypto_cipher_set_key(env2, crypto_cipher_get_key(env1)); crypto_cipher_encrypt_init_cipher(env1); crypto_cipher_decrypt_init_cipher(env2); /* Try encrypting 512 chars. */ crypto_cipher_encrypt(env1, data2, data1, 512); crypto_cipher_decrypt(env2, data3, data2, 512); test_memeq(data1, data3, 512); test_memneq(data1, data2, 512); /* Now encrypt 1 at a time, and get 1 at a time. */ for (j = 512; j < 560; ++j) { crypto_cipher_encrypt(env1, data2+j, data1+j, 1); } for (j = 512; j < 560; ++j) { crypto_cipher_decrypt(env2, data3+j, data2+j, 1); } test_memeq(data1, data3, 560); /* Now encrypt 3 at a time, and get 5 at a time. */ for (j = 560; j < 1024-5; j += 3) { crypto_cipher_encrypt(env1, data2+j, data1+j, 3); } for (j = 560; j < 1024-5; j += 5) { crypto_cipher_decrypt(env2, data3+j, data2+j, 5); } test_memeq(data1, data3, 1024-5); /* Now make sure that when we encrypt with different chunk sizes, we get the same results. */ crypto_free_cipher_env(env2); env2 = NULL; memset(data3, 0, 1024); env2 = crypto_new_cipher_env(); test_neq(env2, 0); crypto_cipher_set_key(env2, crypto_cipher_get_key(env1)); crypto_cipher_encrypt_init_cipher(env2); for (j = 0; j < 1024-16; j += 17) { crypto_cipher_encrypt(env2, data3+j, data1+j, 17); } for (j= 0; j < 1024-16; ++j) { if (data2[j] != data3[j]) { printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]); } } test_memeq(data2, data3, 1024-16); crypto_free_cipher_env(env1); env1 = NULL; crypto_free_cipher_env(env2); env2 = NULL; /* NIST test vector for aes. */ env1 = crypto_new_cipher_env(); /* IV starts at 0 */ crypto_cipher_set_key(env1, "\x80\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00"); crypto_cipher_encrypt_init_cipher(env1); crypto_cipher_encrypt(env1, data1, "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00", 16); test_memeq_hex(data1, "0EDD33D3C621E546455BD8BA1418BEC8"); /* Now test rollover. All these values are originally from a python * script. */ crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\x00\x00\x00\x00" "\xff\xff\xff\xff\xff\xff\xff\xff"); memset(data2, 0, 1024); crypto_cipher_encrypt(env1, data1, data2, 32); test_memeq_hex(data1, "335fe6da56f843199066c14a00a40231" "cdd0b917dbc7186908a6bfb5ffd574d3"); crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff"); memset(data2, 0, 1024); crypto_cipher_encrypt(env1, data1, data2, 32); test_memeq_hex(data1, "e627c6423fa2d77832a02b2794094b73" "3e63c721df790d2c6469cc1953a3ffac"); crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff"); memset(data2, 0, 1024); crypto_cipher_encrypt(env1, data1, data2, 32); test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a" "0EDD33D3C621E546455BD8BA1418BEC8"); /* Now check rollover on inplace cipher. */ crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff"); crypto_cipher_crypt_inplace(env1, data2, 64); test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a" "0EDD33D3C621E546455BD8BA1418BEC8" "93e2c5243d6839eac58503919192f7ae" "1908e67cafa08d508816659c2e693191"); crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff"); crypto_cipher_crypt_inplace(env1, data2, 64); test_assert(tor_mem_is_zero(data2, 64)); done: tor_free(mem_op_hex_tmp); if (env1) crypto_free_cipher_env(env1); if (env2) crypto_free_cipher_env(env2); tor_free(data1); tor_free(data2); tor_free(data3); }
int main(void) { int *p = foo(setglob); test_assert(glob == 23); return 0; }
/** Run unit tests for misc crypto formatting functionality (base64, base32, * fingerprints, etc) */ static void test_crypto_formats(void) { char *data1 = NULL, *data2 = NULL, *data3 = NULL; int i, j, idx; data1 = tor_malloc(1024); data2 = tor_malloc(1024); data3 = tor_malloc(1024); test_assert(data1 && data2 && data3); /* Base64 tests */ memset(data1, 6, 1024); for (idx = 0; idx < 10; ++idx) { i = base64_encode(data2, 1024, data1, idx); test_assert(i >= 0); j = base64_decode(data3, 1024, data2, i); test_eq(j,idx); test_memeq(data3, data1, idx); } strlcpy(data1, "Test string that contains 35 chars.", 1024); strlcat(data1, " 2nd string that contains 35 chars.", 1024); i = base64_encode(data2, 1024, data1, 71); test_assert(i >= 0); j = base64_decode(data3, 1024, data2, i); test_eq(j, 71); test_streq(data3, data1); test_assert(data2[i] == '\0'); crypto_rand(data1, DIGEST_LEN); memset(data2, 100, 1024); digest_to_base64(data2, data1); test_eq(BASE64_DIGEST_LEN, strlen(data2)); test_eq(100, data2[BASE64_DIGEST_LEN+2]); memset(data3, 99, 1024); test_eq(digest_from_base64(data3, data2), 0); test_memeq(data1, data3, DIGEST_LEN); test_eq(99, data3[DIGEST_LEN+1]); test_assert(digest_from_base64(data3, "###") < 0); /* Encoding SHA256 */ crypto_rand(data2, DIGEST256_LEN); memset(data2, 100, 1024); digest256_to_base64(data2, data1); test_eq(BASE64_DIGEST256_LEN, strlen(data2)); test_eq(100, data2[BASE64_DIGEST256_LEN+2]); memset(data3, 99, 1024); test_eq(digest256_from_base64(data3, data2), 0); test_memeq(data1, data3, DIGEST256_LEN); test_eq(99, data3[DIGEST256_LEN+1]); /* Base32 tests */ strlcpy(data1, "5chrs", 1024); /* bit pattern is: [35 63 68 72 73] -> * [00110101 01100011 01101000 01110010 01110011] * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011] */ base32_encode(data2, 9, data1, 5); test_streq(data2, "gvrwq4tt"); strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024); base32_encode(data2, 30, data1, 10); test_streq(data2, "772w2rfobvomsywe"); /* Base16 tests */ strlcpy(data1, "6chrs\xff", 1024); base16_encode(data2, 13, data1, 6); test_streq(data2, "3663687273FF"); strlcpy(data1, "f0d678affc000100", 1024); i = base16_decode(data2, 8, data1, 16); test_eq(i,0); test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8); /* now try some failing base16 decodes */ test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */ test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */ strlcpy(data1, "f0dz!8affc000100", 1024); test_eq(-1, base16_decode(data2, 8, data1, 16)); tor_free(data1); tor_free(data2); tor_free(data3); /* Add spaces to fingerprint */ { data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000"); test_eq(strlen(data1), 40); data2 = tor_malloc(FINGERPRINT_LEN+1); add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1); test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000"); tor_free(data1); tor_free(data2); } /* Check fingerprint */ { test_assert(crypto_pk_check_fingerprint_syntax( "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000")); test_assert(!crypto_pk_check_fingerprint_syntax( "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 000")); test_assert(!crypto_pk_check_fingerprint_syntax( "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000")); test_assert(!crypto_pk_check_fingerprint_syntax( "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 0000")); test_assert(!crypto_pk_check_fingerprint_syntax( "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 00000")); test_assert(!crypto_pk_check_fingerprint_syntax( "ACD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000")); } done: tor_free(data1); tor_free(data2); tor_free(data3); }
/** Run unit tests for our public key crypto functions */ static void test_crypto_pk(void) { crypto_pk_env_t *pk1 = NULL, *pk2 = NULL; char *encoded = NULL; char data1[1024], data2[1024], data3[1024]; size_t size; int i, j, p, len; /* Public-key ciphers */ pk1 = pk_generate(0); pk2 = crypto_new_pk_env(); test_assert(pk1 && pk2); test_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size)); test_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size)); test_eq(0, crypto_pk_cmp_keys(pk1, pk2)); test_eq(128, crypto_pk_keysize(pk1)); test_eq(1024, crypto_pk_num_bits(pk1)); test_eq(128, crypto_pk_keysize(pk2)); test_eq(1024, crypto_pk_num_bits(pk2)); test_eq(128, crypto_pk_public_encrypt(pk2, data1, sizeof(data1), "Hello whirled.", 15, PK_PKCS1_OAEP_PADDING)); test_eq(128, crypto_pk_public_encrypt(pk1, data2, sizeof(data1), "Hello whirled.", 15, PK_PKCS1_OAEP_PADDING)); /* oaep padding should make encryption not match */ test_memneq(data1, data2, 128); test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128, PK_PKCS1_OAEP_PADDING,1)); test_streq(data3, "Hello whirled."); memset(data3, 0, 1024); test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128, PK_PKCS1_OAEP_PADDING,1)); test_streq(data3, "Hello whirled."); /* Can't decrypt with public key. */ test_eq(-1, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128, PK_PKCS1_OAEP_PADDING,1)); /* Try again with bad padding */ memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */ test_eq(-1, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128, PK_PKCS1_OAEP_PADDING,1)); /* File operations: save and load private key */ test_assert(! crypto_pk_write_private_key_to_filename(pk1, get_fname("pkey1"))); /* failing case for read: can't read. */ test_assert(crypto_pk_read_private_key_from_filename(pk2, get_fname("xyzzy")) < 0); write_str_to_file(get_fname("xyzzy"), "foobar", 6); /* Failing case for read: no key. */ test_assert(crypto_pk_read_private_key_from_filename(pk2, get_fname("xyzzy")) < 0); test_assert(! crypto_pk_read_private_key_from_filename(pk2, get_fname("pkey1"))); test_eq(15, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128, PK_PKCS1_OAEP_PADDING,1)); /* Now try signing. */ strlcpy(data1, "Ossifrage", 1024); test_eq(128, crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10)); test_eq(10, crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128)); test_streq(data3, "Ossifrage"); /* Try signing digests. */ test_eq(128, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2), data1, 10)); test_eq(20, crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128)); test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128)); test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128)); /*XXXX test failed signing*/ /* Try encoding */ crypto_free_pk_env(pk2); pk2 = NULL; i = crypto_pk_asn1_encode(pk1, data1, 1024); test_assert(i>0); pk2 = crypto_pk_asn1_decode(data1, i); test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0); /* Try with hybrid encryption wrappers. */ crypto_rand(data1, 1024); for (i = 0; i < 3; ++i) { for (j = 85; j < 140; ++j) { memset(data2,0,1024); memset(data3,0,1024); if (i == 0 && j < 129) continue; p = (i==0)?PK_NO_PADDING: (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING; len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2), data1,j,p,0); test_assert(len>=0); len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3), data2,len,p,1); test_eq(len,j); test_memeq(data1,data3,j); } } /* Try copy_full */ crypto_free_pk_env(pk2); pk2 = crypto_pk_copy_full(pk1); test_assert(pk2 != NULL); test_neq_ptr(pk1, pk2); test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0); done: if (pk1) crypto_free_pk_env(pk1); if (pk2) crypto_free_pk_env(pk2); tor_free(encoded); }
/** Run unit tests for our SHA-1 functionality */ static void test_crypto_sha(void) { crypto_digest_env_t *d1 = NULL, *d2 = NULL; int i; char key[160]; char digest[32]; char data[50]; char d_out1[DIGEST_LEN], d_out2[DIGEST256_LEN]; char *mem_op_hex_tmp=NULL; /* Test SHA-1 with a test vector from the specification. */ i = crypto_digest(data, "abc", 3); test_memeq_hex(data, "A9993E364706816ABA3E25717850C26C9CD0D89D"); tt_int_op(i, ==, 0); /* Test SHA-256 with a test vector from the specification. */ i = crypto_digest256(data, "abc", 3, DIGEST_SHA256); test_memeq_hex(data, "BA7816BF8F01CFEA414140DE5DAE2223B00361A3" "96177A9CB410FF61F20015AD"); tt_int_op(i, ==, 0); /* Test HMAC-SHA-1 with test cases from RFC2202. */ /* Case 1. */ memset(key, 0x0b, 20); crypto_hmac_sha1(digest, key, 20, "Hi There", 8); test_streq(hex_str(digest, 20), "B617318655057264E28BC0B6FB378C8EF146BE00"); /* Case 2. */ crypto_hmac_sha1(digest, "Jefe", 4, "what do ya want for nothing?", 28); test_streq(hex_str(digest, 20), "EFFCDF6AE5EB2FA2D27416D5F184DF9C259A7C79"); /* Case 4. */ base16_decode(key, 25, "0102030405060708090a0b0c0d0e0f10111213141516171819", 50); memset(data, 0xcd, 50); crypto_hmac_sha1(digest, key, 25, data, 50); test_streq(hex_str(digest, 20), "4C9007F4026250C6BC8414F9BF50C86C2D7235DA"); /* Case 5. */ memset(key, 0xaa, 80); crypto_hmac_sha1(digest, key, 80, "Test Using Larger Than Block-Size Key - Hash Key First", 54); test_streq(hex_str(digest, 20), "AA4AE5E15272D00E95705637CE8A3B55ED402112"); /* Test HMAC-SHA256 with test cases from wikipedia and RFC 4231 */ /* Case empty (wikipedia) */ crypto_hmac_sha256(digest, "", 0, "", 0); test_streq(hex_str(digest, 32), "B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD"); /* Case quick-brown (wikipedia) */ crypto_hmac_sha256(digest, "key", 3, "The quick brown fox jumps over the lazy dog", 43); test_streq(hex_str(digest, 32), "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8"); /* "Test Case 1" from RFC 4231 */ memset(key, 0x0b, 20); crypto_hmac_sha256(digest, key, 20, "Hi There", 8); test_memeq_hex(digest, "b0344c61d8db38535ca8afceaf0bf12b" "881dc200c9833da726e9376c2e32cff7"); /* "Test Case 2" from RFC 4231 */ memset(key, 0x0b, 20); crypto_hmac_sha256(digest, "Jefe", 4, "what do ya want for nothing?", 28); test_memeq_hex(digest, "5bdcc146bf60754e6a042426089575c7" "5a003f089d2739839dec58b964ec3843"); /* "Test case 3" from RFC 4231 */ memset(key, 0xaa, 20); memset(data, 0xdd, 50); crypto_hmac_sha256(digest, key, 20, data, 50); test_memeq_hex(digest, "773ea91e36800e46854db8ebd09181a7" "2959098b3ef8c122d9635514ced565fe"); /* "Test case 4" from RFC 4231 */ base16_decode(key, 25, "0102030405060708090a0b0c0d0e0f10111213141516171819", 50); memset(data, 0xcd, 50); crypto_hmac_sha256(digest, key, 25, data, 50); test_memeq_hex(digest, "82558a389a443c0ea4cc819899f2083a" "85f0faa3e578f8077a2e3ff46729665b"); /* "Test case 5" from RFC 4231 */ memset(key, 0x0c, 20); crypto_hmac_sha256(digest, key, 20, "Test With Truncation", 20); test_memeq_hex(digest, "a3b6167473100ee06e0c796c2955552b"); /* "Test case 6" from RFC 4231 */ memset(key, 0xaa, 131); crypto_hmac_sha256(digest, key, 131, "Test Using Larger Than Block-Size Key - Hash Key First", 54); test_memeq_hex(digest, "60e431591ee0b67f0d8a26aacbf5b77f" "8e0bc6213728c5140546040f0ee37f54"); /* "Test case 7" from RFC 4231 */ memset(key, 0xaa, 131); crypto_hmac_sha256(digest, key, 131, "This is a test using a larger than block-size key and a " "larger than block-size data. The key needs to be hashed " "before being used by the HMAC algorithm.", 152); test_memeq_hex(digest, "9b09ffa71b942fcb27635fbcd5b0e944" "bfdc63644f0713938a7f51535c3a35e2"); /* Incremental digest code. */ d1 = crypto_new_digest_env(); test_assert(d1); crypto_digest_add_bytes(d1, "abcdef", 6); d2 = crypto_digest_dup(d1); test_assert(d2); crypto_digest_add_bytes(d2, "ghijkl", 6); crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest(d_out2, "abcdefghijkl", 12); test_memeq(d_out1, d_out2, DIGEST_LEN); crypto_digest_assign(d2, d1); crypto_digest_add_bytes(d2, "mno", 3); crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest(d_out2, "abcdefmno", 9); test_memeq(d_out1, d_out2, DIGEST_LEN); crypto_digest_get_digest(d1, d_out1, sizeof(d_out1)); crypto_digest(d_out2, "abcdef", 6); test_memeq(d_out1, d_out2, DIGEST_LEN); crypto_free_digest_env(d1); crypto_free_digest_env(d2); /* Incremental digest code with sha256 */ d1 = crypto_new_digest256_env(DIGEST_SHA256); test_assert(d1); crypto_digest_add_bytes(d1, "abcdef", 6); d2 = crypto_digest_dup(d1); test_assert(d2); crypto_digest_add_bytes(d2, "ghijkl", 6); crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256); test_memeq(d_out1, d_out2, DIGEST_LEN); crypto_digest_assign(d2, d1); crypto_digest_add_bytes(d2, "mno", 3); crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256); test_memeq(d_out1, d_out2, DIGEST_LEN); crypto_digest_get_digest(d1, d_out1, sizeof(d_out1)); crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256); test_memeq(d_out1, d_out2, DIGEST_LEN); done: if (d1) crypto_free_digest_env(d1); if (d2) crypto_free_digest_env(d2); tor_free(mem_op_hex_tmp); }
void on_release_func(caValue* data) { int slot = as_int(data); test_assert(as_bool(g_slots[slot])); set_bool(g_slots[slot], false); }
int main(int argc, char* argv[]) { struct sigaction sa; pthread_t thread; pthread_t w_thread; char ch; test_assert(0 == pipe(pipe_fds)); sa.sa_sigaction = handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_SIGINFO; sigaction(SIGSYS, &sa, NULL); pthread_create(&w_thread, NULL, waiting_thread, NULL); /* Prepare syscallbuf patch path. Need to do this after pthread_create since when we have more than one thread we take a different syscall path... */ open("/dev/null", O_RDONLY); test_assert(0 == prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); test_assert(1 == prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)); install_filter(); test_assert(2 == prctl(PR_GET_SECCOMP)); test_assert(1 == write(pipe_fds[1], "c", 1)); pthread_join(w_thread, NULL); test_assert(-1 == syscall(SYS_pipe, pipe_fds)); test_assert(ESRCH == errno); /* Spawning a thread will execute an rrcall_init_buffers syscall, which our filter tries to block but shouldn't be able to. */ pthread_create(&thread, NULL, run_thread, NULL); pthread_join(thread, NULL); /* Check that the ioctls used by syscallbuf aren't blocked */ test_assert(1 == write(pipe_fds[1], "c", 1)); test_assert(1 == read(pipe_fds[0], &ch, 1)); test_assert(1 == write(pipe_fds[1], "c", 1)); test_assert(1 == read(pipe_fds[0], &ch, 1)); syscall(SYS_geteuid); open("/dev/null", O_RDONLY); test_assert(count_SIGSYS == 2); atomic_puts("SUCCESS"); return 0; }
static void test_compression_handler(const struct compression_handler *handler) { const char *path = "test-compression.tmp"; struct istream *file_input, *input; struct ostream *file_output, *output; unsigned char buf[IO_BLOCK_SIZE]; const unsigned char *data; size_t size; struct sha1_ctxt sha1; unsigned char output_sha1[SHA1_RESULTLEN], input_sha1[SHA1_RESULTLEN]; unsigned int i; int fd; ssize_t ret; test_begin(t_strdup_printf("compression handler %s", handler->name)); /* write compressed data */ fd = open(path, O_TRUNC | O_CREAT | O_RDWR, 0600); if (fd == -1) i_fatal("creat(%s) failed: %m", path); file_output = o_stream_create_fd_file(fd, 0, FALSE); output = handler->create_ostream(file_output, 1); sha1_init(&sha1); /* 1) write lots of easily compressible data */ memset(buf, 0, sizeof(buf)); for (i = 0; i < 1024*1024*4 / sizeof(buf); i++) { sha1_loop(&sha1, buf, sizeof(buf)); test_assert(o_stream_send(output, buf, sizeof(buf)) == sizeof(buf)); } /* 2) write uncompressible data */ for (i = 0; i < 1024*128 / sizeof(buf); i++) { random_fill_weak(buf, sizeof(buf)); sha1_loop(&sha1, buf, sizeof(buf)); test_assert(o_stream_send(output, buf, sizeof(buf)) == sizeof(buf)); } /* 3) write semi-compressible data */ for (i = 0; i < sizeof(buf); i++) { if (rand () % 3 == 0) buf[i] = rand() % 4; else buf[i] = i; } for (i = 0; i < 1024*128 / sizeof(buf); i++) { sha1_loop(&sha1, buf, sizeof(buf)); test_assert(o_stream_send(output, buf, sizeof(buf)) == sizeof(buf)); } o_stream_destroy(&output); o_stream_destroy(&file_output); sha1_result(&sha1, output_sha1); /* read and uncompress the data */ sha1_init(&sha1); file_input = i_stream_create_fd(fd, IO_BLOCK_SIZE, FALSE); input = handler->create_istream(file_input, FALSE); while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) { sha1_loop(&sha1, data, size); i_stream_skip(input, size); } test_assert(ret == -1); i_stream_destroy(&input); i_stream_destroy(&file_input); sha1_result(&sha1, input_sha1); test_assert(memcmp(input_sha1, output_sha1, sizeof(input_sha1)) == 0); i_unlink(path); test_end(); }
static void vfstest_fd(void) { #define FD_BUFSIZE 5 #define BAD_FD 20 #define HUGE_FD 9999 int fd1, fd2; char buf[FD_BUFSIZE]; struct dirent d; syscall_success(mkdir("fd", 0)); syscall_success(chdir("fd")); /* read/write/close/getdents/dup nonexistent file descriptors */ syscall_fail(read(BAD_FD, buf, FD_BUFSIZE), EBADF); syscall_fail(read(HUGE_FD, buf, FD_BUFSIZE), EBADF); syscall_fail(read(-1, buf, FD_BUFSIZE), EBADF); syscall_fail(write(BAD_FD, buf, FD_BUFSIZE), EBADF); syscall_fail(write(HUGE_FD, buf, FD_BUFSIZE), EBADF); syscall_fail(write(-1, buf, FD_BUFSIZE), EBADF); syscall_fail(close(BAD_FD), EBADF); syscall_fail(close(HUGE_FD), EBADF); syscall_fail(close(-1), EBADF); syscall_fail(lseek(BAD_FD, 0, SEEK_SET), EBADF); syscall_fail(lseek(HUGE_FD, 0, SEEK_SET), EBADF); syscall_fail(lseek(-1, 0, SEEK_SET), EBADF); syscall_fail(getdents(BAD_FD, &d, sizeof(d)), EBADF); syscall_fail(getdents(HUGE_FD, &d, sizeof(d)), EBADF); syscall_fail(getdents(-1, &d, sizeof(d)), EBADF); syscall_fail(dup(BAD_FD), EBADF); syscall_fail(dup(HUGE_FD), EBADF); syscall_fail(dup(-1), EBADF); syscall_fail(dup2(BAD_FD, 10), EBADF); syscall_fail(dup2(HUGE_FD, 10), EBADF); syscall_fail(dup2(-1, 10), EBADF); /* dup2 has some extra cases since it takes a second fd */ syscall_fail(dup2(0, HUGE_FD), EBADF); syscall_fail(dup2(0, -1), EBADF); /* if the fds are equal, but the first is invalid or out of the * allowed range */ syscall_fail(dup2(BAD_FD, BAD_FD), EBADF); syscall_fail(dup2(HUGE_FD, HUGE_FD), EBADF); syscall_fail(dup2(-1, -1), EBADF); /* dup works properly in normal usage */ create_file("file01"); syscall_success(fd1 = open("file01", O_RDWR, 0)); syscall_success(fd2 = dup(fd1)); test_assert(fd1 < fd2, "dup(%d) returned %d", fd1, fd2); syscall_success(write(fd2, "hello", 5)); test_fpos(fd1, 5); test_fpos(fd2, 5); syscall_success(lseek(fd2, 0, SEEK_SET)); test_fpos(fd1, 0); test_fpos(fd2, 0); read_fd(fd1, 5, "hello"); test_fpos(fd1, 5); test_fpos(fd2, 5); syscall_success(close(fd2)); /* dup2 works properly in normal usage */ syscall_success(fd2 = dup2(fd1, 10)); test_assert(10 == fd2, "dup2(%d, 10) returned %d", fd1, fd2); test_fpos(fd1, 5); test_fpos(fd2, 5); syscall_success(lseek(fd2, 0, SEEK_SET)); test_fpos(fd1, 0); test_fpos(fd2, 0); syscall_success(close(fd2)); /* dup2-ing a file to itself works */ syscall_success(fd2 = dup2(fd1, fd1)); test_assert(fd1 == fd2, "dup2(%d, %d) returned %d", fd1, fd1, fd2); syscall_success(close(fd2)); /* dup2 closes previous file */ int fd3; create_file("file02"); syscall_success(fd3 = open("file02", O_RDWR, 0)); syscall_success(fd2 = dup2(fd1, fd3)); test_assert(fd2 == fd3, "dup2(%d, %d) returned %d", fd1, fd3, fd2); test_fpos(fd1, 0); test_fpos(fd2, 0); syscall_success(lseek(fd2, 5, SEEK_SET)); test_fpos(fd1, 5); test_fpos(fd2, 5); syscall_success(chdir("..")); }
void do_test(const TestOpts& opts) { std::cout << "Testing with parameters: " << opts.to_string() << std::endl; std::vector<std::thread> threads; TestContext ctx(opts); std::cout << "Running workers ... " << std::endl; for (size_t i = 0; i < opts.n_workers; ++i) { threads.emplace_back([i, &ctx] { ctx.barrier_start.wait(); for (size_t j = 0; j < ctx.opts.n_additions; ++j) { if ((j % ctx.opts.n_workers) != i) { continue; } cdebug("!ctx.hash_set_a.contains(" << j << ") before add"); test_assert(!ctx.hash_set_a.contains(j), "expected not to contain " << j << " before test"); cdebug("!ctx.hash_set_b.contains(" << j << ") before add"); test_assert(!ctx.hash_set_b.contains(j), "expected not to contain " << j << " before test"); cdebug("ctx.hash_set_a.add(" << j << ")"); ctx.hash_set_a.add(j); cdebug("ctx.hash_set_b.add(" << j << ")"); ctx.hash_set_b.add(j); cdebug("ctx.hash_set_a.contains(" << j << ") after add"); test_assert(ctx.hash_set_a.contains(j), "expected to contain " << j << " after addition"); cdebug("ctx.hash_set_b.contains(" << j << ") after add"); test_assert(ctx.hash_set_b.contains(j), "expected to contain " << j << " after addition"); cdebug("ctx.hash_set_a.add(" << j << ") once more"); ctx.hash_set_a.add(j); cdebug("ctx.hash_set_b.add(" << j << ") once more"); ctx.hash_set_b.add(j); cdebug("ctx.hash_set_a.remove(" << j << ")"); ctx.hash_set_a.remove(j); cdebug("ctx.hash_set_b.remove(" << j << ")"); ctx.hash_set_b.remove(j); cdebug("!ctx.hash_set_a.contains(" << j << ") after remove"); test_assert(!ctx.hash_set_a.contains(j), "expected not to contain " << j << " after removal"); cdebug("!ctx.hash_set_b.contains(" << j << ") after remove"); test_assert(!ctx.hash_set_b.contains(j), "expected not to contain " << j << " after removal"); cdebug("ctx.hash_set_a.add(" << j << ") second time"); ctx.hash_set_a.add(j); cdebug("ctx.hash_set_b.add(" << j << ") second time"); ctx.hash_set_b.add(j); cdebug("ctx.hash_set_a.contains(" << j << ") after second add"); test_assert(ctx.hash_set_a.contains(j), "expected to contain " << j << " after addition"); cdebug("ctx.hash_set_b.contains(" << j << ") after second add"); test_assert(ctx.hash_set_b.contains(j), "expected to contain " << j << " after addition"); cdebug("finished testing on item " << j); } }); } std::cout << "Done" << std::endl; std::cout << "Joining workers ... " << std::endl; for (auto& t : threads) { if (t.joinable()) { t.join(); } } std::cout << "Done" << std::endl; std::cout << "OK" << std::endl; }
/* * Tests open(), close(), and unlink() * - Accepts only valid combinations of flags * - Cannot open nonexistent file without O_CREAT * - Cannot write to readonly file * - Cannot read from writeonly file * - Cannot close non-existent file descriptor * - Lowest file descriptor is always selected * - Cannot unlink a directory # - Cannot unlink a non-existent file * - Cannot open a directory for writing * - File descriptors are correctly released when a proc exits */ static void vfstest_open(void) { #define OPEN_BUFSIZE 5 char buf[OPEN_BUFSIZE]; int fd, fd2; struct stat s; syscall_success(mkdir("open", 0777)); syscall_success(chdir("open")); /* No invalid combinations of O_RDONLY, O_WRONLY, and O_RDWR. Since * O_RDONLY is stupidly defined as 0, the only invalid possible * combination is O_WRONLY|O_RDWR. */ syscall_fail(open("file01", O_WRONLY | O_RDWR | O_CREAT, 0), EINVAL); syscall_fail(open("file01", O_RDONLY | O_RDWR | O_WRONLY | O_CREAT, 0), EINVAL); /* Cannot open nonexistent file without O_CREAT */ syscall_fail(open("file02", O_WRONLY, 0), ENOENT); syscall_success(fd = open("file02", O_RDONLY | O_CREAT, 0)); syscall_success(close(fd)); syscall_success(unlink("file02")); syscall_fail(stat("file02", &s), ENOENT); /* Cannot create invalid files */ create_file("tmpfile"); syscall_fail(open("tmpfile/test", O_RDONLY | O_CREAT, 0), ENOTDIR); syscall_fail(open("noent/test", O_RDONLY | O_CREAT, 0), ENOENT); syscall_fail(open(LONGNAME, O_RDONLY | O_CREAT, 0), ENAMETOOLONG); /* Cannot write to readonly file */ syscall_success(fd = open("file03", O_RDONLY | O_CREAT, 0)); syscall_fail(write(fd, "hello", 5), EBADF); syscall_success(close(fd)); /* Cannot read from writeonly file. Note that we do not unlink() it * from above, so we do not need O_CREAT set. */ syscall_success(fd = open("file03", O_WRONLY, 0)); syscall_fail(read(fd, buf, OPEN_BUFSIZE), EBADF); syscall_success(close(fd)); syscall_success(unlink("file03")); syscall_fail(stat("file03", &s), ENOENT); /* Lowest file descriptor is always selected. */ syscall_success(fd = open("file04", O_RDONLY | O_CREAT, 0)); syscall_success(fd2 = open("file04", O_RDONLY, 0)); test_assert(fd2 > fd, "open() did not return lowest fd"); syscall_success(close(fd)); syscall_success(close(fd2)); syscall_success(fd2 = open("file04", O_WRONLY, 0)); test_assert(fd2 == fd, "open() did not return correct fd"); syscall_success(close(fd2)); syscall_success(unlink("file04")); syscall_fail(stat("file04", &s), ENOENT); /* Cannot open a directory for writing */ syscall_success(mkdir("file05", 0)); syscall_fail(open("file05", O_WRONLY, 0), EISDIR); syscall_fail(open("file05", O_RDWR, 0), EISDIR); syscall_success(rmdir("file05")); /* Cannot unlink a directory */ syscall_success(mkdir("file06", 0)); syscall_fail(unlink("file06"), EPERM); syscall_success(rmdir("file06")); /* Cannot unlink a non-existent file */ syscall_fail(unlink("file07"), ENOENT); syscall_success(chdir("..")); }
void AlarmCallback(OSAlarm *alarm, OSContext *context) { test_assert(alarm == &sAlarm); test_report("AlarmCallback called."); }
static void vfstest_read(void) { #define READ_BUFSIZE 256 int fd, ret; char buf[READ_BUFSIZE]; struct stat s; syscall_success(mkdir("read", 0777)); syscall_success(chdir("read")); /* Can read and write to a file */ syscall_success(fd = open("file01", O_RDWR | O_CREAT, 0)); syscall_success(ret = write(fd, "hello", 5)); test_assert(5 == ret, "write(%d, \"hello\", 5) returned %d", fd, ret); syscall_success(ret = lseek(fd, 0, SEEK_SET)); test_assert(0 == ret, "lseek(%d, 0, SEEK_SET) returned %d", fd, ret); read_fd(fd, READ_BUFSIZE, "hello"); syscall_success(close(fd)); /* cannot read from a directory */ syscall_success(mkdir("dir01", 0)); syscall_success(fd = open("dir01", O_RDONLY, 0)); syscall_fail(read(fd, buf, READ_BUFSIZE), EISDIR); syscall_success(close(fd)); /* Can seek to beginning, middle, and end of file */ syscall_success(fd = open("file02", O_RDWR | O_CREAT, 0)); syscall_success(write(fd, "hello", 5)); #define test_lseek(expr, res) \ do { \ int __r = (expr); \ test_assert((res) == __r, # expr " returned %d, expected %d", __r, res);\ } while (0); test_lseek(lseek(fd, 0, SEEK_CUR), 5); read_fd(fd, 10, ""); test_lseek(lseek(fd, -1, SEEK_CUR), 4); read_fd(fd, 10, "o"); test_lseek(lseek(fd, 2, SEEK_CUR), 7); read_fd(fd, 10, ""); syscall_fail(lseek(fd, -8, SEEK_CUR), EINVAL); test_lseek(lseek(fd, 0, SEEK_SET), 0); read_fd(fd, 10, "hello"); test_lseek(lseek(fd, 3, SEEK_SET), 3); read_fd(fd, 10, "lo"); test_lseek(lseek(fd, 7, SEEK_SET), 7); read_fd(fd, 10, ""); syscall_fail(lseek(fd, -1, SEEK_SET), EINVAL); test_lseek(lseek(fd, 0, SEEK_END), 5); read_fd(fd, 10, ""); test_lseek(lseek(fd, -2, SEEK_END), 3); read_fd(fd, 10, "lo"); test_lseek(lseek(fd, 3, SEEK_END), 8); read_fd(fd, 10, ""); syscall_fail(lseek(fd, -8, SEEK_END), EINVAL); syscall_fail(lseek(fd, 0, SEEK_SET + SEEK_CUR + SEEK_END), EINVAL); syscall_success(close(fd)); /* O_APPEND works properly */ create_file("file03"); syscall_success(fd = open("file03", O_RDWR, 0)); test_fpos(fd, 0); syscall_success(write(fd, "hello", 5)); test_fpos(fd, 5); syscall_success(close(fd)); syscall_success(fd = open("file03", O_RDWR | O_APPEND, 0)); test_fpos(fd, 0); syscall_success(write(fd, "hello", 5)); test_fpos(fd, 10); syscall_success(lseek(fd, 0, SEEK_SET)); test_fpos(fd, 0); read_fd(fd, 10, "hellohello"); syscall_success(lseek(fd, 5, SEEK_SET)); test_fpos(fd, 5); syscall_success(write(fd, "again", 5)); test_fpos(fd, 15); syscall_success(lseek(fd, 0, SEEK_SET)); test_fpos(fd, 0); read_fd(fd, 15, "hellohelloagain"); syscall_success(close(fd)); /* seek and write beyond end of file */ create_file("file04"); syscall_success(fd = open("file04", O_RDWR, 0)); syscall_success(write(fd, "hello", 5)); test_fpos(fd, 5); test_lseek(lseek(fd, 10, SEEK_SET), 10); syscall_success(write(fd, "again", 5)); syscall_success(stat("file04", &s)); test_assert(s.st_size == 15, "actual size: %d", s.st_size); test_lseek(lseek(fd, 0, SEEK_SET), 0); test_assert(15 == read(fd, buf, READ_BUFSIZE), "unexpected number of bytes read"); test_assert(0 == memcmp(buf, "hello\0\0\0\0\0again", 15), "unexpected data read"); syscall_success(close(fd)); syscall_success(chdir("..")); }
static void test_md_cache(void *data) { or_options_t *options = NULL; microdesc_cache_t *mc = NULL ; smartlist_t *added = NULL, *wanted = NULL; microdesc_t *md1, *md2, *md3; char d1[DIGEST256_LEN], d2[DIGEST256_LEN], d3[DIGEST256_LEN]; const char *test_md3_noannotation = strchr(test_md3, '\n')+1; time_t time1, time2, time3; char *fn = NULL, *s = NULL; (void)data; options = get_options_mutable(); tt_assert(options); time1 = time(NULL); time2 = time(NULL) - 2*24*60*60; time3 = time(NULL) - 15*24*60*60; /* Possibly, turn this into a test setup/cleanup pair */ tor_free(options->DataDirectory); options->DataDirectory = tor_strdup(get_fname("md_datadir_test")); #ifdef _WIN32 tt_int_op(0, ==, mkdir(options->DataDirectory)); #else tt_int_op(0, ==, mkdir(options->DataDirectory, 0700)); #endif tt_assert(!strcmpstart(test_md3_noannotation, "onion-key")); crypto_digest256(d1, test_md1, strlen(test_md1), DIGEST_SHA256); crypto_digest256(d2, test_md2, strlen(test_md1), DIGEST_SHA256); crypto_digest256(d3, test_md3_noannotation, strlen(test_md3_noannotation), DIGEST_SHA256); mc = get_microdesc_cache(); added = microdescs_add_to_cache(mc, test_md1, NULL, SAVED_NOWHERE, 0, time1, NULL); tt_int_op(1, ==, smartlist_len(added)); md1 = smartlist_get(added, 0); smartlist_free(added); added = NULL; wanted = smartlist_new(); added = microdescs_add_to_cache(mc, test_md2, NULL, SAVED_NOWHERE, 0, time2, wanted); /* Should fail, since we didn't list test_md2's digest in wanted */ tt_int_op(0, ==, smartlist_len(added)); smartlist_free(added); added = NULL; smartlist_add(wanted, tor_memdup(d2, DIGEST256_LEN)); smartlist_add(wanted, tor_memdup(d3, DIGEST256_LEN)); added = microdescs_add_to_cache(mc, test_md2, NULL, SAVED_NOWHERE, 0, time2, wanted); /* Now it can work. md2 should have been added */ tt_int_op(1, ==, smartlist_len(added)); md2 = smartlist_get(added, 0); /* And it should have gotten removed from 'wanted' */ tt_int_op(smartlist_len(wanted), ==, 1); test_mem_op(smartlist_get(wanted, 0), ==, d3, DIGEST256_LEN); smartlist_free(added); added = NULL; added = microdescs_add_to_cache(mc, test_md3, NULL, SAVED_NOWHERE, 0, -1, NULL); /* Must fail, since SAVED_NOWHERE precludes annotations */ tt_int_op(0, ==, smartlist_len(added)); smartlist_free(added); added = NULL; added = microdescs_add_to_cache(mc, test_md3_noannotation, NULL, SAVED_NOWHERE, 0, time3, NULL); /* Now it can work */ tt_int_op(1, ==, smartlist_len(added)); md3 = smartlist_get(added, 0); smartlist_free(added); added = NULL; /* Okay. We added 1...3. Let's poke them to see how they look, and make * sure they're really in the journal. */ tt_ptr_op(md1, ==, microdesc_cache_lookup_by_digest256(mc, d1)); tt_ptr_op(md2, ==, microdesc_cache_lookup_by_digest256(mc, d2)); tt_ptr_op(md3, ==, microdesc_cache_lookup_by_digest256(mc, d3)); tt_int_op(md1->last_listed, ==, time1); tt_int_op(md2->last_listed, ==, time2); tt_int_op(md3->last_listed, ==, time3); tt_int_op(md1->saved_location, ==, SAVED_IN_JOURNAL); tt_int_op(md2->saved_location, ==, SAVED_IN_JOURNAL); tt_int_op(md3->saved_location, ==, SAVED_IN_JOURNAL); tt_int_op(md1->bodylen, ==, strlen(test_md1)); tt_int_op(md2->bodylen, ==, strlen(test_md2)); tt_int_op(md3->bodylen, ==, strlen(test_md3_noannotation)); test_mem_op(md1->body, ==, test_md1, strlen(test_md1)); test_mem_op(md2->body, ==, test_md2, strlen(test_md2)); test_mem_op(md3->body, ==, test_md3_noannotation, strlen(test_md3_noannotation)); tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs.new", options->DataDirectory); s = read_file_to_str(fn, RFTS_BIN, NULL); tt_assert(s); test_mem_op(md1->body, ==, s + md1->off, md1->bodylen); test_mem_op(md2->body, ==, s + md2->off, md2->bodylen); test_mem_op(md3->body, ==, s + md3->off, md3->bodylen); tt_ptr_op(md1->family, ==, NULL); tt_ptr_op(md3->family, !=, NULL); tt_int_op(smartlist_len(md3->family), ==, 3); tt_str_op(smartlist_get(md3->family, 0), ==, "nodeX"); /* Now rebuild the cache! */ tt_int_op(microdesc_cache_rebuild(mc, 1), ==, 0); tt_int_op(md1->saved_location, ==, SAVED_IN_CACHE); tt_int_op(md2->saved_location, ==, SAVED_IN_CACHE); tt_int_op(md3->saved_location, ==, SAVED_IN_CACHE); /* The journal should be empty now */ tor_free(s); s = read_file_to_str(fn, RFTS_BIN, NULL); tt_str_op(s, ==, ""); tor_free(s); tor_free(fn); /* read the cache. */ tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs", options->DataDirectory); s = read_file_to_str(fn, RFTS_BIN, NULL); test_mem_op(md1->body, ==, s + md1->off, strlen(test_md1)); test_mem_op(md2->body, ==, s + md2->off, strlen(test_md2)); test_mem_op(md3->body, ==, s + md3->off, strlen(test_md3_noannotation)); /* Okay, now we are going to forget about the cache entirely, and reload it * from the disk. */ microdesc_free_all(); mc = get_microdesc_cache(); md1 = microdesc_cache_lookup_by_digest256(mc, d1); md2 = microdesc_cache_lookup_by_digest256(mc, d2); md3 = microdesc_cache_lookup_by_digest256(mc, d3); test_assert(md1); test_assert(md2); test_assert(md3); test_mem_op(md1->body, ==, s + md1->off, strlen(test_md1)); test_mem_op(md2->body, ==, s + md2->off, strlen(test_md2)); test_mem_op(md3->body, ==, s + md3->off, strlen(test_md3_noannotation)); tt_int_op(md1->last_listed, ==, time1); tt_int_op(md2->last_listed, ==, time2); tt_int_op(md3->last_listed, ==, time3); /* Okay, now we are going to clear out everything older than a week old. * In practice, that means md3 */ microdesc_cache_clean(mc, time(NULL)-7*24*60*60, 1/*force*/); tt_ptr_op(md1, ==, microdesc_cache_lookup_by_digest256(mc, d1)); tt_ptr_op(md2, ==, microdesc_cache_lookup_by_digest256(mc, d2)); tt_ptr_op(NULL, ==, microdesc_cache_lookup_by_digest256(mc, d3)); md3 = NULL; /* it's history now! */ /* rebuild again, make sure it stays gone. */ tt_int_op(microdesc_cache_rebuild(mc, 1), ==, 0); tt_ptr_op(md1, ==, microdesc_cache_lookup_by_digest256(mc, d1)); tt_ptr_op(md2, ==, microdesc_cache_lookup_by_digest256(mc, d2)); tt_ptr_op(NULL, ==, microdesc_cache_lookup_by_digest256(mc, d3)); /* Re-add md3, and make sure we can rebuild the cache. */ added = microdescs_add_to_cache(mc, test_md3_noannotation, NULL, SAVED_NOWHERE, 0, time3, NULL); tt_int_op(1, ==, smartlist_len(added)); md3 = smartlist_get(added, 0); smartlist_free(added); added = NULL; tt_int_op(md1->saved_location, ==, SAVED_IN_CACHE); tt_int_op(md2->saved_location, ==, SAVED_IN_CACHE); tt_int_op(md3->saved_location, ==, SAVED_IN_JOURNAL); tt_int_op(microdesc_cache_rebuild(mc, 1), ==, 0); tt_int_op(md3->saved_location, ==, SAVED_IN_CACHE); done: if (options) tor_free(options->DataDirectory); microdesc_free_all(); smartlist_free(added); if (wanted) SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp)); smartlist_free(wanted); tor_free(s); tor_free(fn); }
static void vfstest_s5fs_vm(void) { int fd, newfd, ret; char buf[2048]; struct stat oldstatbuf, newstatbuf; void *addr; syscall_success(mkdir("s5fs", 0)); syscall_success(chdir("s5fs")); /* Open some stuff */ syscall_success(fd = open("oldchld", O_RDWR | O_CREAT, 0)); syscall_success(mkdir("parent", 0)); /* link/unlink tests */ syscall_success(link("oldchld", "newchld")); /* Make sure stats match */ syscall_success(stat("oldchld", &oldstatbuf)); syscall_success(stat("newchld", &newstatbuf)); test_assert(0 == memcmp(&oldstatbuf, &newstatbuf, sizeof(struct stat)), NULL); /* Make sure contents match */ syscall_success(newfd = open("newchld", O_RDWR, 0)); syscall_success(ret = write(fd, TESTSTR, strlen(TESTSTR))); test_assert(ret == (int)strlen(TESTSTR), NULL); syscall_success(ret = read(newfd, buf, strlen(TESTSTR))); test_assert(ret == (int)strlen(TESTSTR), NULL); test_assert(0 == strncmp(buf, TESTSTR, strlen(TESTSTR)), "string is %.*s, expected %s", strlen(TESTSTR), buf, TESTSTR); syscall_success(close(fd)); syscall_success(close(newfd)); /* Remove one, make sure the other remains */ syscall_success(unlink("oldchld")); syscall_fail(mkdir("newchld", 0), EEXIST); syscall_success(link("newchld", "oldchld")); /* Link/unlink error cases */ syscall_fail(link("oldchld", "newchld"), EEXIST); syscall_fail(link("oldchld", LONGNAME), ENAMETOOLONG); syscall_fail(link("parent", "newchld"), EPERM); /* only rename test */ /*syscall_success(rename("oldchld", "newchld"));*/ /* mmap/munmap tests */ syscall_success(fd = open("newchld", O_RDWR, 0)); test_assert(MAP_FAILED != (addr = mmap(0, strlen(TESTSTR), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)), NULL); /* Check contents of memory */ test_assert(0 == memcmp(addr, TESTSTR, strlen(TESTSTR)), NULL); /* Write to it -> we shouldn't pagefault */ memcpy(addr, SHORTSTR, strlen(SHORTSTR)); test_assert(0 == memcmp(addr, SHORTSTR, strlen(SHORTSTR)), NULL); /* mmap the same thing on top of it, but shared */ test_assert(MAP_FAILED != mmap(addr, strlen(TESTSTR), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0), NULL); /* Make sure the old contents were restored (the mapping was private) */ test_assert(0 == memcmp(addr, TESTSTR, strlen(TESTSTR)), NULL); /* Now change the contents */ memcpy(addr, SHORTSTR, strlen(SHORTSTR)); /* mmap it on, private, on top again */ test_assert(MAP_FAILED != mmap(addr, strlen(TESTSTR), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd, 0), NULL); /* Make sure it changed */ test_assert(0 == memcmp(addr, SHORTSTR, strlen(SHORTSTR)), NULL); /* Fork and try changing things */ if (!fork()) { /* Child changes private mapping */ memcpy(addr, TESTSTR, strlen(TESTSTR)); exit(0); } /* Wait until child is done */ syscall_success(wait(0)); /* Make sure it's actually private */ test_assert(0 == memcmp(addr, SHORTSTR, strlen(SHORTSTR)), NULL); /* Unmap it */ syscall_success(munmap(addr, 2048)); /* mmap errors */ test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, MAP_PRIVATE, 12, 0), NULL); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, MAP_PRIVATE, -1, 0), NULL); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, 0, fd, 0), NULL); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, MAP_FIXED, fd, 0), NULL); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, MAP_FIXED | MAP_PRIVATE, fd, 0), NULL); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, MAP_PRIVATE, fd, 0x12345), NULL); test_assert(MAP_FAILED == mmap((void *) 0x12345, 1024, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0), NULL); test_assert(MAP_FAILED == mmap(0, 0, PROT_READ, MAP_PRIVATE, fd, 0), NULL); test_assert(MAP_FAILED == mmap(0, -1, PROT_READ, MAP_PRIVATE, fd, 0), NULL); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0), NULL); syscall_success(close(fd)); syscall_success(fd = open("newchld", O_RDONLY, 0)); test_assert(MAP_FAILED == mmap(0, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0), NULL); syscall_success(close(fd)); /* TODO ENODEV (mmap a terminal) EOVERFLOW (mmap SO MUCH of /dev/zero that fpointer would overflow) */ /* Also should test opening too many file descriptors somewhere */ /* munmap errors */ syscall_fail(munmap((void *) 0x12345, 15), EINVAL); syscall_fail(munmap(0x0, 15), EINVAL); syscall_fail(munmap(addr, 0), EINVAL); syscall_fail(munmap(addr, -1), EINVAL); /* brk tests */ /* Set the break, and use the memory in question */ test_assert((void *) - 1 != (addr = sbrk(128)), NULL); memcpy(addr, TESTSTR, 128); test_assert(0 == memcmp(addr, TESTSTR, 128), NULL); /* Make sure that the brk is being saved properly */ test_assert((void *)((unsigned long) addr + 128) == sbrk(0), NULL); /* Knock the break back down */ syscall_success(brk(addr)); /* brk errors */ syscall_fail(brk((void *)(&"brk")), ENOMEM); syscall_fail(brk((void *) 1), ENOMEM); syscall_fail(brk((void *) &addr), ENOMEM); syscall_success(chdir("..")); }
/***********************************************************************//** * @brief Test GArf class **************************************************************************/ void TestGXspec::test_GArf(void) { // Test void constructor test_try("GArf void constructor"); try { GArf arf; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test energy boundary constructor test_try("GArf energy boundary constructor"); try { GEbounds ebds(10, GEnergy(0.1, "TeV"), GEnergy(10.0, "TeV")); GArf arf(ebds); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test filling and accessing GEbounds ebds(9, GEnergy(1.0, "TeV"), GEnergy(10.0, "TeV")); GArf arf(ebds); for (int i = 0; i < 9; i += 2) { arf[i] = 1.0; } for (int i = 0; i < 9; i += 2) { test_value(arf.at(i), 1.0); test_value(arf[i], 1.0); } for (int i = 1; i < 9; i += 2) { test_value(arf.at(i), 0.0); test_value(arf[i], 0.0); } arf[0] = 5.0; arf[1] = 3.7; test_value(arf[0], 5.0); test_value(arf[1], 3.7); // Test saving and loading arf.save("arf.fits", true); test_assert(arf.filename() == "arf.fits", "Unexpected filename \""+arf.filename()+"\"."); arf.load("arf.fits"); test_assert(arf.filename() == "arf.fits", "Unexpected filename \""+arf.filename()+"\"."); test_value(arf[0], 5.0, 1.0e-6); test_value(arf[1], 3.7, 1.0e-6); for (int i = 2; i < 9; i += 2) { test_value(arf.at(i), 1.0); test_value(arf[i], 1.0); } for (int i = 3; i < 9; i += 2) { test_value(arf.at(i), 0.0); test_value(arf[i], 0.0); } // Test constructing GArf arf2("arf.fits"); test_assert(arf2.filename() == "arf.fits", "Unexpected filename \""+arf2.filename()+"\"."); test_value(arf2[0], 5.0, 1.0e-6); test_value(arf2[1], 3.7, 1.0e-6); for (int i = 2; i < 9; i += 2) { test_value(arf2.at(i), 1.0); test_value(arf2[i], 1.0); } for (int i = 3; i < 9; i += 2) { test_value(arf2.at(i), 0.0); test_value(arf2[i], 0.0); } //std::cout << arf << std::endl; //std::cout << arf2 << std::endl; // Return return; }
static void test_message_address(void) { static const char *input[] = { "user@domain", NULL, "<user@domain>", "user@domain", "foo bar <user@domain>", NULL, "\"foo bar\" <user@domain>", "foo bar <user@domain>", "<@route:user@domain>", NULL, "<@route@route2:user@domain>", "<@route,@route2:user@domain>", "hello <@route ,@route2:user@domain>", "hello <@route,@route2:user@domain>", "user (hello)", NULL, "hello <user>", NULL, "@domain", NULL }; static struct message_address group_prefix = { NULL, NULL, NULL, "group", NULL, FALSE }; static struct message_address group_suffix = { NULL, NULL, NULL, NULL, NULL, FALSE }; static struct message_address output[] = { { NULL, NULL, NULL, "user", "domain", FALSE }, { NULL, NULL, NULL, "user", "domain", FALSE }, { NULL, "foo bar", NULL, "user", "domain", FALSE }, { NULL, "foo bar", NULL, "user", "domain", FALSE }, { NULL, NULL, "@route", "user", "domain", FALSE }, { NULL, NULL, "@route,@route2", "user", "domain", FALSE }, { NULL, "hello", "@route,@route2", "user", "domain", FALSE }, { NULL, "hello", NULL, "user", "", TRUE }, { NULL, "hello", NULL, "user", "", TRUE }, { NULL, NULL, NULL, "", "domain", TRUE } }; struct message_address *addr; string_t *str, *group; const char *wanted_string; unsigned int i; i_assert(N_ELEMENTS(input) == N_ELEMENTS(output)*2); test_begin("message address parsing"); str = t_str_new(128); group = t_str_new(256); str_append(group, "group: "); for (i = 0; i < N_ELEMENTS(output); i++) { addr = message_address_parse(pool_datastack_create(), (const unsigned char *)input[i*2], strlen(input[i*2]), -1U, FALSE); test_assert(addr != NULL && addr->next == NULL && cmp_addr(addr, &output[i])); if (!output[i].invalid_syntax) { str_truncate(str, 0); message_address_write(str, addr); wanted_string = input[i*2+1] != NULL ? input[i*2+1] : input[i*2]; test_assert(strcmp(str_c(str), wanted_string) == 0); if (i != 0) { if ((i % 2) == 0) str_append(group, ","); else str_append(group, " , \n "); } str_append(group, input[i*2]); } } str_append_c(group, ';'); test_end(); test_begin("message address parsing with groups"); addr = message_address_parse(pool_datastack_create(), str_data(group), str_len(group), -1U, FALSE); test_assert(addr != NULL && cmp_addr(addr, &group_prefix)); addr = addr->next; for (i = 0; i < N_ELEMENTS(output) && addr != NULL; i++) { if (output[i].invalid_syntax) continue; test_assert(cmp_addr(addr, &output[i])); addr = addr->next; } test_assert(addr != NULL && addr->next == NULL && cmp_addr(addr, &group_suffix)); test_end(); test_begin("message address parsing with empty group"); str_truncate(group, 0); str_append(group, "group:;"); addr = message_address_parse(pool_datastack_create(), str_data(group), str_len(group), -1U, FALSE); test_assert(addr != NULL && cmp_addr(addr, &group_prefix)); addr = addr->next; test_assert(addr != NULL && addr->next == NULL && cmp_addr(addr, &group_suffix)); test_end(); }
/***********************************************************************//** * @brief Test GPha class **************************************************************************/ void TestGXspec::test_GPha(void) { // Test void constructor test_try("GPha void constructor"); try { GPha pha; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test energy boundary constructor test_try("GPha energy boundary constructor"); try { GEbounds ebds(10, GEnergy(0.1, "TeV"), GEnergy(10.0, "TeV")); GPha pha(ebds); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test filling and accessing GEbounds ebds(9, GEnergy(1.0, "TeV"), GEnergy(10.0, "TeV"), false); GPha pha(ebds); for (int i = 0; i < 12; i += 2) { pha.fill(GEnergy(double(i), "TeV"), 1.0); } test_value(pha.counts(), 4.0); test_value(pha.underflow(), 1.0); test_value(pha.overflow(), 1.0); test_value(pha.outflow(), 0.0); for (int i = 0; i < 9; i += 2) { test_value(pha.at(i), 0.0); test_value(pha[i], 0.0); } for (int i = 1; i < 9; i += 2) { test_value(pha.at(i), 1.0); test_value(pha[i], 1.0); } pha[0] = 5.0; pha[1] = 3.7; test_value(pha[0], 5.0); test_value(pha[1], 3.7); // Test saving and loading pha.save("pha.fits", true); test_assert(pha.filename() == "pha.fits", "Unexpected filename \""+pha.filename()+"\"."); pha.load("pha.fits"); test_assert(pha.filename() == "pha.fits", "Unexpected filename \""+pha.filename()+"\"."); test_value(pha[0], 5.0, 1.0e-6); test_value(pha[1], 3.7, 1.0e-6); test_value(pha.counts(), 11.7, 1.0e-6); test_value(pha.underflow(), 0.0, 1.0e-6); test_value(pha.overflow(), 0.0, 1.0e-6); test_value(pha.outflow(), 0.0, 1.0e-6); for (int i = 2; i < 9; i += 2) { test_value(pha.at(i), 0.0); test_value(pha[i], 0.0); } for (int i = 3; i < 9; i += 2) { test_value(pha.at(i), 1.0); test_value(pha[i], 1.0); } // Test constructing GPha pha2("pha.fits"); test_assert(pha2.filename() == "pha.fits", "Unexpected filename \""+pha2.filename()+"\"."); test_value(pha2[0], 5.0, 1.0e-6); test_value(pha2[1], 3.7, 1.0e-6); test_value(pha2.counts(), 11.7, 1.0e-6); test_value(pha2.underflow(), 0.0, 1.0e-6); test_value(pha2.overflow(), 0.0, 1.0e-6); test_value(pha2.outflow(), 0.0, 1.0e-6); for (int i = 2; i < 9; i += 2) { test_value(pha2.at(i), 0.0); test_value(pha2[i], 0.0); } for (int i = 3; i < 9; i += 2) { test_value(pha2.at(i), 1.0); test_value(pha2[i], 1.0); } //std::cout << pha << std::endl; //std::cout << pha2 << std::endl; // Return return; }
~MyType() { test_assert(MyType_allocated > 0); MyType_allocated--; }
void chop_test(struct dstring *ds) { test_assert(dstring_cpys(ds, BIG_STRING)); /* len == 256 */ /* should do nothing */ dstring_chop(ds, 300); test_assert(256 == ds->len); test_assert(dstring_size(ds) == 256); test_assert(dstring_data(ds) != 0); /* should work */ dstring_chop(ds, 20); test_assert(20 == ds->len); test_assert(0 == ds->s[20]); test_assert(dstring_size(ds) == 20); test_assert(dstring_data(ds) != 0); /* edge case */ test_assert(dstring_cpys(ds, BIG_STRING)); while (ds->len != ds->a - 1) test_assert(dstring_catb(ds, "X", 1)); dstring_chop(ds, ds->a); test_assert(288 == ds->len); test_assert(289 == ds->a); test_assert(dstring_size(ds) == 288); test_assert(dstring_data(ds) != 0); dstring_trunc(ds); test_assert(0 == ds->len); test_assert(289 == ds->a); test_assert(dstring_size(ds) == 0); test_assert(dstring_data(ds) != 0); dstring_trunc(ds); test_assert(0 == ds->len); test_assert(289 == ds->a); test_assert(dstring_size(ds) == 0); test_assert(dstring_data(ds) != 0); dstring_free(ds); test_assert(dstring_size(ds) == 0); test_assert(dstring_data(ds) == 0); }
void test_starcode_3 (void) // Test 'addmatch', 'transfer_counts_and_update_canonicals'. { useq_t *u1 = new_useq(1, "B}d2)$ChPyDC=xZ D-C", NULL); useq_t *u2 = new_useq(2, "RCD67vQc80:~@FV`?o%D", NULL); // Add match to 'u1'. addmatch(u1, u2, 1, 1); test_assert(u1->count == 1); test_assert(u2->count == 2); // This should not transfer counts but update canonical. transfer_counts_and_update_canonicals(u2); test_assert(u1->count == 1); test_assert(u2->count == 2); test_assert(u1->canonical == NULL); test_assert(u2->canonical == u2); // This should transfer the counts from 'u1' to 'u2'. transfer_counts_and_update_canonicals(u1); test_assert(u1->count == 0); test_assert(u2->count == 3); test_assert(u1->canonical == u2); test_assert(u2->canonical == u2); destroy_useq(u1); destroy_useq(u2); useq_t *u3 = new_useq(1, "{Lu[T}FOCMs}L_zx", NULL); useq_t *u4 = new_useq(2, "|kAV|Ch|RZ]h~WjCoDpX", NULL); useq_t *u5 = new_useq(2, "}lzHolUky", NULL); // Add matches to 'u3'. addmatch(u3, u4, 1, 1); addmatch(u3, u5, 1, 1); test_assert(u3->count == 1); test_assert(u4->count == 2); test_assert(u5->count == 2); transfer_counts_and_update_canonicals(u3); test_assert(u3->count == 0); test_assert(u3->count == 0); test_assert(u3->canonical == NULL); test_assert(u4->canonical == u4); test_assert(u5->canonical == u5); destroy_useq(u3); destroy_useq(u4); destroy_useq(u5); }