int msgThreadEntry(int argc, const char **argv)
{
   // Give main thread a chance to block on the message queue
   OSMessage msg;
   sMessagesRead = 0;

   while (sMessagesRead < NumMessages) {
      // Receive message
      test_eq(OSReceiveMessage(&sQueue, &msg, OS_MESSAGE_FLAGS_BLOCKING), TRUE);
      test_eq(msg.message, (void *)1);
      test_eq(msg.args[0], 2 + sMessagesRead);
      test_eq(msg.args[1], 3);
      test_eq(msg.args[2], 4);
      ++sMessagesRead;
   }

   return 0;
}
Esempio n. 2
0
int main()
{
    test<forward_iterator<const int*> >();
    test<bidirectional_iterator<const int*> >();
    test<random_access_iterator<const int*> >();
    test<const int*>();
    test_eq();

    constexpr_test();
}
Esempio n. 3
0
static void test_equalities(uint32_t n) {
  uint32_t k, l;
  arena_t m;

  init_arena(&m);
  for (k=0; k<n; k++) {
    arena_push(&m);
    for (l=0; l<n; l++) {
      show_label(k);
      if (k != l) show_label(l);

      test_eq(&m, pos_occ(k), pos_occ(l));
      test_eq(&m, pos_occ(k), neg_occ(l));
      test_eq(&m, neg_occ(k), pos_occ(l));
      test_eq(&m, neg_occ(k), neg_occ(l));
      printf("\n");
    }
    arena_pop(&m);
  }
  delete_arena(&m);
}
Esempio n. 4
0
File: iopt.c Progetto: seL4/sel4test
static int
map_iopt_from_iospace(env_t env, seL4_CPtr iospace, seL4_CPtr *iopt, seL4_CPtr *frame)
{
    int error;
    *frame = vka_alloc_frame_leaky(&env->vka, seL4_PageBits);
    test_assert(*frame);
    error = seL4_ARM_Page_MapIO(*frame, iospace, seL4_AllRights, IOPT_MAP_BASE);

    if (error == seL4_FailedLookup) {
        *iopt = vka_alloc_io_page_table_leaky(&env->vka);
        test_assert(*iopt);
        error = seL4_ARM_IOPageTable_Map(*iopt, iospace, IOPT_MAP_BASE);
        test_eq(error, seL4_NoError);
        error = seL4_ARM_Page_MapIO(*frame, iospace, seL4_AllRights, IOPT_MAP_BASE);
        test_eq(error, seL4_NoError);
    }
    test_eq(error, seL4_NoError);

    return error;

}
int tc_libcxx_algorithms_alg_min_max_min_element_comp(void)
{
    TC_ASSERT_FUNC((test<forward_iterator<const int*> >()));
    TC_ASSERT_FUNC((test<bidirectional_iterator<const int*> >()));
    TC_ASSERT_FUNC((test<random_access_iterator<const int*> >()));
    TC_ASSERT_FUNC((test<const int*>()));
    TC_ASSERT_FUNC((test_eq()));

    constexpr_test();
    TC_SUCCESS_RESULT();
    return 0;
}
Esempio n. 6
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_basic_iopt(env_t env)
{
    int error;
    seL4_CPtr iospace, frame;
    iopt_cptrs_t pts;
    error = map_iopt_set(env, &iospace, &pts, &frame);
    test_eq(error, seL4_NoError);

    delete_iospace(env, iospace);
    return sel4test_get_result();
}
Esempio n. 7
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_basic_map_unmap(env_t env)
{
    int error;
    int i;
    seL4_CPtr iospace, pt, frame;
    seL4_SlotRegion caps = env->io_space_caps;
    int cap_count = caps.end - caps.start + 1;

    for (i = 0; i < cap_count; i++) {
        iospace = caps.start + i;
        error = map_iopt_set(env, iospace, &pt, &frame);
        test_eq(error, seL4_NoError);

        error = seL4_ARM_Page_Unmap(frame);
        test_eq(error, seL4_NoError);

        error = seL4_ARM_IOPageTable_Unmap(pt);
        test_eq(error, seL4_NoError);

        error = map_iopt_from_iospace(env, iospace, &pt, &frame);
        test_eq(error, seL4_NoError);

        error = seL4_ARM_IOPageTable_Unmap(pt);
        test_eq(error, seL4_NoError);
        error = seL4_ARM_Page_Unmap(frame);
        test_eq(error, seL4_NoError);
        delete_iospace(env, iospace);
    }
    return sel4test_get_result();
}
Esempio n. 8
0
/** Run unit tests for Diffie-Hellman functionality. */
static void
test_crypto_dh(void)
{
  crypto_dh_t *dh1 = crypto_dh_new(DH_TYPE_CIRCUIT);
  crypto_dh_t *dh2 = crypto_dh_new(DH_TYPE_CIRCUIT);
  char p1[DH_BYTES];
  char p2[DH_BYTES];
  char s1[DH_BYTES];
  char s2[DH_BYTES];
  ssize_t s1len, s2len;

  test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);

  memset(p1, 0, DH_BYTES);
  memset(p2, 0, DH_BYTES);
  test_memeq(p1, p2, DH_BYTES);
  test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  test_memneq(p1, p2, DH_BYTES);
  test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  test_memneq(p1, p2, DH_BYTES);

  memset(s1, 0, DH_BYTES);
  memset(s2, 0xFF, DH_BYTES);
  s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p2, DH_BYTES, s1, 50);
  s2len = crypto_dh_compute_secret(LOG_WARN, dh2, p1, DH_BYTES, s2, 50);
  test_assert(s1len > 0);
  test_eq(s1len, s2len);
  test_memeq(s1, s2, s1len);

  {
    /* XXXX Now fabricate some bad values and make sure they get caught,
     * Check 0, 1, N-1, >= N, etc.
     */
  }

 done:
  crypto_dh_free(dh1);
  crypto_dh_free(dh2);
}
Esempio n. 9
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_basic_map_unmap(env_t env)
{
    int error;
    int i;
    iopt_cptrs_t pts;
    seL4_CPtr iospace, frame;
    error = map_iopt_set(env, &iospace, &pts, &frame);
    test_eq(error, seL4_NoError);

    error = seL4_X86_Page_Unmap(frame);
    test_eq(error, seL4_NoError);
    for (i = pts.depth - 1; i >= 0; i--) {
        error = seL4_X86_IOPageTable_Unmap(pts.pts[i]);
        test_eq(error, seL4_NoError);
    }

    error = map_iopt_from_iospace(env, iospace, &pts, &frame);
    test_eq(error, seL4_NoError);

    for (i = 0; i < pts.depth; i++) {
        error = seL4_X86_IOPageTable_Unmap(pts.pts[i]);
        test_eq(error, seL4_NoError);
    }

    error = seL4_X86_Page_Unmap(frame);
    test_eq(error, seL4_NoError);

    delete_iospace(env, iospace);
    return sel4test_get_result();
}
Esempio n. 10
0
File: iopt.c Progetto: seL4/sel4test
static int
map_iopt_from_iospace(env_t env, seL4_CPtr iospace, iopt_cptrs_t *pts, seL4_CPtr *frame)
{
    int error = seL4_NoError;

    pts->depth = 0;
    /* Allocate and map page tables until we can map a frame */
    *frame = vka_alloc_frame_leaky(&env->vka, seL4_PageBits);
    test_assert(*frame);

    while (seL4_X86_Page_MapIO(*frame, iospace, seL4_AllRights, IOPT_MAP_BASE) == seL4_FailedLookup) {
        test_assert(pts->depth < MAX_IOPT_DEPTH);
        pts->pts[pts->depth] = vka_alloc_io_page_table_leaky(&env->vka);
        test_assert(pts->pts[pts->depth]);
        error = seL4_X86_IOPageTable_Map(pts->pts[pts->depth], iospace, IOPT_MAP_BASE);
        test_eq(error, seL4_NoError);
        pts->depth++;
    }
    test_eq(error, seL4_NoError);

    return error;
}
Esempio n. 11
0
static int
run_test (int /*argc*/, char* /*argv*/ [])
{
    test_eq ();
    test_lt ();

    //test_ne ();
    //test_gt ();
    //test_le ();
    //test_ge ();

    return 0;
}
Esempio n. 12
0
/** Test tor_addr_port_parse(). */
static void
test_addr_parse(void)
{
  int r;
  tor_addr_t addr;
  char buf[TOR_ADDR_BUF_LEN];
  uint16_t port = 0;

  /* Correct call. */
  r= tor_addr_port_parse(LOG_DEBUG,
                         "192.0.2.1:1234",
                         &addr, &port);
  test_assert(r == 0);
  tor_addr_to_str(buf, &addr, sizeof(buf), 0);
  test_streq(buf, "192.0.2.1");
  test_eq(port, 1234);

  /* Domain name. */
  r= tor_addr_port_parse(LOG_DEBUG,
                         "torproject.org:1234",
                         &addr, &port);
  test_assert(r == -1);

  /* Only IP. */
  r= tor_addr_port_parse(LOG_DEBUG,
                         "192.0.2.2",
                         &addr, &port);
  test_assert(r == -1);

  /* Bad port. */
  r= tor_addr_port_parse(LOG_DEBUG,
                         "192.0.2.2:66666",
                         &addr, &port);
  test_assert(r == -1);

  /* Only domain name */
  r= tor_addr_port_parse(LOG_DEBUG,
                         "torproject.org",
                         &addr, &port);
  test_assert(r == -1);

  /* Bad IP address */
  r= tor_addr_port_parse(LOG_DEBUG,
                         "192.0.2:1234",
                         &addr, &port);
  test_assert(r == -1);

 done:
  ;
}
Esempio n. 13
0
int main()
{
    int tx_port = 0;
    char *source = "test/test_defs.b";

    sys_init(0);
    tx_server(source, "bin/state", &tx_port);
    vol_init(0, "bin/volume");

    char *code = sys_load(source);
    env = env_new(source, code);
    mem_free(code);

    int len = 0;
    char **files = sys_list("test/data", &len);

    vars = vars_new(len);
    rvars = vars_new(len);
    for (int i = 0; i < len; ++i) {
        vars_add(rvars, files[i], 0, NULL);
        vars_add(vars, files[i], 0, NULL);
    }
    vars_add(vars, "___param", 0, NULL);

    test_vars();
    test_load();
    test_param();
    test_clone();
    test_eq();
    test_store();
    test_select();
    test_rename();
    test_extend();
    test_join();
    test_project();
    test_semidiff();
    test_summary();
    test_union();
    test_compound();
    test_call();

    tx_free();
    env_free(env);
    mem_free(files);
    vars_free(vars);
    vars_free(rvars);

    return 0;
}
Esempio n. 14
0
static void
test_replaycache_hit(void)
{
  replaycache_t *r = NULL;
  int result;

  r = replaycache_new(600, 300);
  test_assert(r != NULL);

  result =
    replaycache_add_and_test_internal(1200, r, test_buffer,
        (int)strlen(test_buffer), NULL);
  test_eq(result, 0);

  result =
    replaycache_add_and_test_internal(1300, r, test_buffer,
        (int)strlen(test_buffer), NULL);
  test_eq(result, 1);

 done:
  if (r) replaycache_free(r);

  return;
}
Esempio n. 15
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_basic_iopt(env_t env)
{
    int error;
    seL4_CPtr pt = 0;
    seL4_CPtr frame = 0;
    seL4_SlotRegion caps = env->io_space_caps;
    int cap_count = caps.end - caps.start + 1;
    seL4_CPtr cap = caps.start;

    for (int i = 0; i < cap_count; i++) {
        error = map_iopt_set(env, cap + i, &pt, &frame);
        test_eq(error, seL4_NoError);
        delete_iospace(env, cap + i);
    }
    return sel4test_get_result();
}
Esempio n. 16
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_no_overlapping_4k(env_t env)
{
    int error;
    iopt_cptrs_t pts;
    seL4_CPtr iospace, frame;
    error = map_iopt_set(env, &iospace, &pts, &frame);
    test_eq(error, seL4_NoError);

    frame = vka_alloc_frame_leaky(&env->vka, seL4_PageBits);
    test_assert(frame);
    error = seL4_X86_Page_MapIO(frame, iospace, seL4_AllRights, IOPT_MAP_BASE);
    test_assert(error != seL4_NoError);

    delete_iospace(env, iospace);
    return sel4test_get_result();
}
Esempio n. 17
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_no_overlapping_pt(env_t env)
{
    int error;
    iopt_cptrs_t pts;
    seL4_CPtr iospace, frame, pt;
    error = map_iopt_set(env, &iospace, &pts, &frame);
    test_eq(error, seL4_NoError);

    /* Mapping in a new PT should fail */
    pt = vka_alloc_io_page_table_leaky(&env->vka);
    test_assert(pt);
    error = seL4_X86_IOPageTable_Map(pt, iospace, IOPT_MAP_BASE);
    test_assert(error != seL4_NoError);

    delete_iospace(env, iospace);
    return sel4test_get_result();
}
Esempio n. 18
0
File: iopt.c Progetto: seL4/sel4test
static int
map_iopt_set(env_t env, seL4_CPtr *iospace, iopt_cptrs_t *pts, seL4_CPtr *frame)
{
    int error;
    cspacepath_t master_path, iospace_path;

    /* Allocate a random device ID that hopefully doesn't exist have any
     * RMRR regions */
    error = vka_cspace_alloc(&env->vka, iospace);
    test_assert(!error);
    vka_cspace_make_path(&env->vka, *iospace, &iospace_path);
    vka_cspace_make_path(&env->vka, env->io_space, &master_path);
    error = vka_cnode_mint(&iospace_path, &master_path, seL4_AllRights,(DOMAIN_ID << 16) | FAKE_PCI_DEVICE);
    test_eq(error, seL4_NoError);

    error = map_iopt_from_iospace(env, *iospace, pts, frame);

    return error;
}
Esempio n. 19
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_no_overlapping_pt(env_t env)
{
    int error;
    int i;
    seL4_CPtr iospace, pt, frame;
    seL4_SlotRegion caps = env->io_space_caps;
    int cap_count = caps.end - caps.start + 1;
    for (i = 0; i < cap_count; i++) {
        iospace = caps.start + i;
        error = map_iopt_set(env, iospace, &pt, &frame);
        test_eq(error, seL4_NoError);

        /* Mapping in a new PT should fail */
        pt = vka_alloc_io_page_table_leaky(&env->vka);
        test_assert(pt);
        error = seL4_ARM_IOPageTable_Map(pt, iospace, IOPT_MAP_BASE);
        test_assert(error != seL4_NoError);
        delete_iospace(env, iospace);
    }
    return sel4test_get_result();
}
Esempio n. 20
0
File: iopt.c Progetto: seL4/sel4test
static int
test_iopt_no_overlapping_4k(env_t env)
{
    int error;
    int i;
    seL4_CPtr iospace, pt, frame;
    seL4_SlotRegion caps = env->io_space_caps;
    int cap_count = caps.end - caps.start + 1;
    for (i = 0; i < cap_count; i++) {
        iospace = caps.start + i;
        error = map_iopt_set(env, iospace, &pt, &frame);
        test_eq(error, seL4_NoError);

        frame = vka_alloc_frame_leaky(&env->vka, seL4_PageBits);
        test_assert(frame);
        /* mapping in a new frame should fail */
        error = seL4_ARM_Page_MapIO(frame, iospace, seL4_AllRights, IOPT_MAP_BASE);
        test_assert(error != seL4_NoError);
        delete_iospace(env, iospace);
    }

    return sel4test_get_result();
}
Esempio n. 21
0
/** Run unit tests for basic dynamic-sized array functionality. */
static void
test_container_smartlist_basic(void)
{
  smartlist_t *sl;

  /* XXXX test sort_digests, uniq_strings, uniq_digests */

  /* Test smartlist add, del_keeporder, insert, get. */
  sl = smartlist_new();
  smartlist_add(sl, (void*)1);
  smartlist_add(sl, (void*)2);
  smartlist_add(sl, (void*)3);
  smartlist_add(sl, (void*)4);
  smartlist_del_keeporder(sl, 1);
  smartlist_insert(sl, 1, (void*)22);
  smartlist_insert(sl, 0, (void*)0);
  smartlist_insert(sl, 5, (void*)555);
  test_eq_ptr((void*)0,   smartlist_get(sl,0));
  test_eq_ptr((void*)1,   smartlist_get(sl,1));
  test_eq_ptr((void*)22,  smartlist_get(sl,2));
  test_eq_ptr((void*)3,   smartlist_get(sl,3));
  test_eq_ptr((void*)4,   smartlist_get(sl,4));
  test_eq_ptr((void*)555, smartlist_get(sl,5));
  /* Try deleting in the middle. */
  smartlist_del(sl, 1);
  test_eq_ptr((void*)555, smartlist_get(sl, 1));
  /* Try deleting at the end. */
  smartlist_del(sl, 4);
  test_eq(4, smartlist_len(sl));

  /* test isin. */
  test_assert(smartlist_contains(sl, (void*)3));
  test_assert(!smartlist_contains(sl, (void*)99));

 done:
  smartlist_free(sl);
}
Esempio n. 22
0
/** Test AES-CTR encryption and decryption with IV. */
static void
test_crypto_aes_iv(void *arg)
{
  char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
  char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
  char key1[16], key2[16];
  ssize_t encrypted_size, decrypted_size;

  int use_evp = !strcmp(arg,"evp");
  evaluate_evp_for_aes(use_evp);

  plain = tor_malloc(4095);
  encrypted1 = tor_malloc(4095 + 1 + 16);
  encrypted2 = tor_malloc(4095 + 1 + 16);
  decrypted1 = tor_malloc(4095 + 1);
  decrypted2 = tor_malloc(4095 + 1);

  crypto_rand(plain, 4095);
  crypto_rand(key1, 16);
  crypto_rand(key2, 16);
  crypto_rand(plain_1, 1);
  crypto_rand(plain_15, 15);
  crypto_rand(plain_16, 16);
  crypto_rand(plain_17, 17);
  key1[0] = key2[0] + 128; /* Make sure that contents are different. */
  /* Encrypt and decrypt with the same key. */
  encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
                                                 plain, 4095);

  test_eq(encrypted_size, 16 + 4095);
  tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
                                   * greater than 0, but its truth is not
                                   * obvious to all analysis tools. */
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
                                             encrypted1, encrypted_size);

  test_eq(decrypted_size, 4095);
  tt_assert(decrypted_size > 0);
  test_memeq(plain, decrypted1, 4095);
  /* Encrypt a second time (with a new random initialization vector). */
  encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
                                             plain, 4095);

  test_eq(encrypted_size, 16 + 4095);
  tt_assert(encrypted_size > 0);
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
                                             encrypted2, encrypted_size);
  test_eq(decrypted_size, 4095);
  tt_assert(decrypted_size > 0);
  test_memeq(plain, decrypted2, 4095);
  test_memneq(encrypted1, encrypted2, encrypted_size);
  /* Decrypt with the wrong key. */
  decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
                                             encrypted1, encrypted_size);
  test_memneq(plain, decrypted2, decrypted_size);
  /* Alter the initialization vector. */
  encrypted1[0] += 42;
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
                                             encrypted1, encrypted_size);
  test_memneq(plain, decrypted2, 4095);
  /* Special length case: 1. */
  encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
                                             plain_1, 1);
  test_eq(encrypted_size, 16 + 1);
  tt_assert(encrypted_size > 0);
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
                                             encrypted1, encrypted_size);
  test_eq(decrypted_size, 1);
  tt_assert(decrypted_size > 0);
  test_memeq(plain_1, decrypted1, 1);
  /* Special length case: 15. */
  encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
                                             plain_15, 15);
  test_eq(encrypted_size, 16 + 15);
  tt_assert(encrypted_size > 0);
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
                                             encrypted1, encrypted_size);
  test_eq(decrypted_size, 15);
  tt_assert(decrypted_size > 0);
  test_memeq(plain_15, decrypted1, 15);
  /* Special length case: 16. */
  encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
                                             plain_16, 16);
  test_eq(encrypted_size, 16 + 16);
  tt_assert(encrypted_size > 0);
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
                                             encrypted1, encrypted_size);
  test_eq(decrypted_size, 16);
  tt_assert(decrypted_size > 0);
  test_memeq(plain_16, decrypted1, 16);
  /* Special length case: 17. */
  encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
                                             plain_17, 17);
  test_eq(encrypted_size, 16 + 17);
  tt_assert(encrypted_size > 0);
  decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
                                             encrypted1, encrypted_size);
  test_eq(decrypted_size, 17);
  tt_assert(decrypted_size > 0);
  test_memeq(plain_17, decrypted1, 17);

 done:
  /* Free memory. */
  tor_free(plain);
  tor_free(encrypted1);
  tor_free(encrypted2);
  tor_free(decrypted1);
  tor_free(decrypted2);
}
Esempio n. 23
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);
  }

 done:
  tor_free(data1);
  tor_free(data2);
  tor_free(data3);
}
Esempio n. 24
0
/** Run unit tests for our public key crypto functions */
static void
test_crypto_pk(void)
{
  crypto_pk_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_pk_new();
  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));

  /* comparison between keys and NULL */
  tt_int_op(crypto_pk_cmp_keys(NULL, pk1), <, 0);
  tt_int_op(crypto_pk_cmp_keys(NULL, NULL), ==, 0);
  tt_int_op(crypto_pk_cmp_keys(pk1, NULL), >, 0);

  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_pk_free(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 < 2; ++i) {
    for (j = 85; j < 140; ++j) {
      memset(data2,0,1024);
      memset(data3,0,1024);
      p = (i==0)?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_pk_free(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_pk_free(pk1);
  if (pk2)
    crypto_pk_free(pk2);
  tor_free(encoded);
}
Esempio n. 25
0
/** Run unit tests for fp_pair-to-void* map functions */
static void
test_container_fp_pair_map(void)
{
  fp_pair_map_t *map;
  fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  void *v;
  fp_pair_map_iter_t *iter;
  fp_pair_t k;

  map = fp_pair_map_new();
  test_assert(map);
  test_eq(fp_pair_map_size(map), 0);
  test_assert(fp_pair_map_isempty(map));

  memset(fp1.first, 0x11, DIGEST_LEN);
  memset(fp1.second, 0x12, DIGEST_LEN);
  memset(fp2.first, 0x21, DIGEST_LEN);
  memset(fp2.second, 0x22, DIGEST_LEN);
  memset(fp3.first, 0x31, DIGEST_LEN);
  memset(fp3.second, 0x32, DIGEST_LEN);
  memset(fp4.first, 0x41, DIGEST_LEN);
  memset(fp4.second, 0x42, DIGEST_LEN);
  memset(fp5.first, 0x51, DIGEST_LEN);
  memset(fp5.second, 0x52, DIGEST_LEN);
  memset(fp6.first, 0x61, DIGEST_LEN);
  memset(fp6.second, 0x62, DIGEST_LEN);

  v = fp_pair_map_set(map, &fp1, (void*)99);
  test_eq(v, NULL);
  test_assert(!fp_pair_map_isempty(map));
  v = fp_pair_map_set(map, &fp2, (void*)101);
  test_eq(v, NULL);
  v = fp_pair_map_set(map, &fp1, (void*)100);
  test_eq(v, (void*)99);
  test_eq_ptr(fp_pair_map_get(map, &fp1), (void*)100);
  test_eq_ptr(fp_pair_map_get(map, &fp2), (void*)101);
  test_eq_ptr(fp_pair_map_get(map, &fp3), NULL);
  fp_pair_map_assert_ok(map);

  v = fp_pair_map_remove(map, &fp2);
  fp_pair_map_assert_ok(map);
  test_eq_ptr(v, (void*)101);
  test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  test_eq_ptr(fp_pair_map_remove(map, &fp2), NULL);

  fp_pair_map_set(map, &fp2, (void*)101);
  fp_pair_map_set(map, &fp3, (void*)102);
  fp_pair_map_set(map, &fp4, (void*)103);
  test_eq(fp_pair_map_size(map), 4);
  fp_pair_map_assert_ok(map);
  fp_pair_map_set(map, &fp5, (void*)104);
  fp_pair_map_set(map, &fp6, (void*)105);
  fp_pair_map_assert_ok(map);

  /* Test iterator. */
  iter = fp_pair_map_iter_init(map);
  while (!fp_pair_map_iter_done(iter)) {
    fp_pair_map_iter_get(iter, &k, &v);
    test_eq_ptr(v, fp_pair_map_get(map, &k));

    if (tor_memeq(&fp2, &k, sizeof(fp2))) {
      iter = fp_pair_map_iter_next_rmv(map, iter);
    } else {
      iter = fp_pair_map_iter_next(map, iter);
    }
  }

  /* Make sure we removed fp2, but not the others. */
  test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  test_eq_ptr(fp_pair_map_get(map, &fp5), (void*)104);

  fp_pair_map_assert_ok(map);
  /* Clean up after ourselves. */
  fp_pair_map_free(map, NULL);
  map = NULL;

 done:
  if (map)
    fp_pair_map_free(map, NULL);
}
Esempio n. 26
0
/** Run unit tests for string-to-void* map functions */
static void
test_container_strmap(void)
{
  strmap_t *map;
  strmap_iter_t *iter;
  const char *k;
  void *v;
  char *visited = NULL;
  smartlist_t *found_keys = NULL;

  map = strmap_new();
  test_assert(map);
  test_eq(strmap_size(map), 0);
  test_assert(strmap_isempty(map));
  v = strmap_set(map, "K1", (void*)99);
  test_eq_ptr(v, NULL);
  test_assert(!strmap_isempty(map));
  v = strmap_set(map, "K2", (void*)101);
  test_eq_ptr(v, NULL);
  v = strmap_set(map, "K1", (void*)100);
  test_eq_ptr(v, (void*)99);
  test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  strmap_assert_ok(map);

  v = strmap_remove(map,"K2");
  strmap_assert_ok(map);
  test_eq_ptr(v, (void*)101);
  test_eq_ptr(strmap_get(map,"K2"), NULL);
  test_eq_ptr(strmap_remove(map,"K2"), NULL);

  strmap_set(map, "K2", (void*)101);
  strmap_set(map, "K3", (void*)102);
  strmap_set(map, "K4", (void*)103);
  test_eq(strmap_size(map), 4);
  strmap_assert_ok(map);
  strmap_set(map, "K5", (void*)104);
  strmap_set(map, "K6", (void*)105);
  strmap_assert_ok(map);

  /* Test iterator. */
  iter = strmap_iter_init(map);
  found_keys = smartlist_new();
  while (!strmap_iter_done(iter)) {
    strmap_iter_get(iter,&k,&v);
    smartlist_add(found_keys, tor_strdup(k));
    test_eq_ptr(v, strmap_get(map, k));

    if (!strcmp(k, "K2")) {
      iter = strmap_iter_next_rmv(map,iter);
    } else {
      iter = strmap_iter_next(map,iter);
    }
  }

  /* Make sure we removed K2, but not the others. */
  test_eq_ptr(strmap_get(map, "K2"), NULL);
  test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  /* Make sure we visited everyone once */
  smartlist_sort_strings(found_keys);
  visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  test_streq(visited, "K1:K2:K3:K4:K5:K6");

  strmap_assert_ok(map);
  /* Clean up after ourselves. */
  strmap_free(map, NULL);
  map = NULL;

  /* Now try some lc functions. */
  map = strmap_new();
  strmap_set_lc(map,"Ab.C", (void*)1);
  test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  strmap_assert_ok(map);
  test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  strmap_assert_ok(map);
  test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);

 done:
  if (map)
    strmap_free(map,NULL);
  if (found_keys) {
    SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
    smartlist_free(found_keys);
  }
  tor_free(visited);
}
Esempio n. 27
0
/** Run unit tests for heap-based priority queue functions. */
static void
test_container_pqueue(void)
{
  smartlist_t *sl = smartlist_new();
  int (*cmp)(const void *, const void*);
  const int offset = STRUCT_OFFSET(pq_entry_t, idx);
#define ENTRY(s) pq_entry_t s = { #s, -1 }
  ENTRY(cows);
  ENTRY(zebras);
  ENTRY(fish);
  ENTRY(frogs);
  ENTRY(apples);
  ENTRY(squid);
  ENTRY(daschunds);
  ENTRY(eggplants);
  ENTRY(weissbier);
  ENTRY(lobsters);
  ENTRY(roquefort);
  ENTRY(chinchillas);
  ENTRY(fireflies);

#define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)

  cmp = compare_strings_for_pqueue_;
  smartlist_pqueue_add(sl, cmp, offset, &cows);
  smartlist_pqueue_add(sl, cmp, offset, &zebras);
  smartlist_pqueue_add(sl, cmp, offset, &fish);
  smartlist_pqueue_add(sl, cmp, offset, &frogs);
  smartlist_pqueue_add(sl, cmp, offset, &apples);
  smartlist_pqueue_add(sl, cmp, offset, &squid);
  smartlist_pqueue_add(sl, cmp, offset, &daschunds);
  smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  smartlist_pqueue_add(sl, cmp, offset, &roquefort);

  OK();

  test_eq(smartlist_len(sl), 11);
  test_eq_ptr(smartlist_get(sl, 0), &apples);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
  test_eq(smartlist_len(sl), 10);
  OK();
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
  smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  OK();
  smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  OK();
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
  OK();
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
  OK();
  test_eq(smartlist_len(sl), 3);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
  test_eq(smartlist_len(sl), 0);
  OK();

  /* Now test remove. */
  smartlist_pqueue_add(sl, cmp, offset, &cows);
  smartlist_pqueue_add(sl, cmp, offset, &fish);
  smartlist_pqueue_add(sl, cmp, offset, &frogs);
  smartlist_pqueue_add(sl, cmp, offset, &apples);
  smartlist_pqueue_add(sl, cmp, offset, &squid);
  smartlist_pqueue_add(sl, cmp, offset, &zebras);
  test_eq(smartlist_len(sl), 6);
  OK();
  smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  test_eq(smartlist_len(sl), 5);
  OK();
  smartlist_pqueue_remove(sl, cmp, offset, &cows);
  test_eq(smartlist_len(sl), 4);
  OK();
  smartlist_pqueue_remove(sl, cmp, offset, &apples);
  test_eq(smartlist_len(sl), 3);
  OK();
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  test_eq(smartlist_len(sl), 0);
  OK();

#undef OK

 done:

  smartlist_free(sl);
}
Esempio n. 28
0
	void run(){
		test_eq(ofTrimFront("    trim this string    "),"trim this string    ","trimfront");
		test_eq(ofTrimBack("    trim this string    "),"    trim this string","trimback");
		test_eq(ofTrim("    trim this string    "),"trim this string","trim");

		auto split0 = ofSplitString("hi this is a split test", " ");
		test_eq(split0.size(),6u,"split size");
		test_eq(split0[0],"hi","split 0");
		test_eq(split0[1],"this","split 1");
		test_eq(split0[2],"is","split 2");
		test_eq(split0[3],"a","split 3");
		test_eq(split0[4],"split","split 4");
		test_eq(split0[5],"test","split 5");


		auto split1 = ofSplitString(" hi this is a split test ", " ");
		test_eq(split1.size(),8u,"split no trim size");
		test_eq(split1[0],"","split no trim 0");
		test_eq(split1[1],"hi","split no trim 1");
		test_eq(split1[2],"this","split no trim 2");
		test_eq(split1[3],"is","split no trim 3");
		test_eq(split1[4],"a","split no trim 4");
		test_eq(split1[5],"split","split no trim 5");
		test_eq(split1[6],"test","split no trim 6");
		test_eq(split1[7],"","split no trim 7");

		auto split2 = ofSplitString(" hi this is a split test ", " ", true, true);
		test_eq(split2.size(),6u,"split trim size");
		test_eq(split2[0],"hi","split trim 0");
		test_eq(split2[1],"this","split trim 1");
		test_eq(split2[2],"is","split trim 2");
		test_eq(split2[3],"a","split trim 3");
		test_eq(split2[4],"split","split trim 4");
		test_eq(split2[5],"test","split trim 5");

		auto split3 = ofSplitString(" hi      this is a split test ", " ", true, true);
		test_eq(split2.size(),6u,"split trim2 size");
		test_eq(split2[0],"hi","split trim2 0");
		test_eq(split2[1],"this","split trim2 1");
		test_eq(split2[2],"is","split trim2 2");
		test_eq(split2[3],"a","split trim2 3");
		test_eq(split2[4],"split","split trim2 4");
		test_eq(split2[5],"test","split trim2 5");

		test_eq(ofJoinString({"hi","this","is","a","join","test"}," "),"hi this is a join test","join string");
		test_eq(ofJoinString({"hi"}," "),"hi","join string 1 element");
		test_eq(ofJoinString({}," "),"","join string 0 elements");

		std::string replace = "hi this is a replace test";
		ofStringReplace(replace,"replace","replaceeee");
		test_eq(replace , "hi this is a replaceeee test","replace string element");

        test_eq(ofToLower("AbCéÉBbCcc"),"abcéébbccc","tolower");
        test_eq(ofToUpper("AbCéÉBbCcc"),"ABCÉÉBBCCC","toupper");

		// test #4363
		std::vector<string> strs;
		strs.push_back("hi");
		strs.push_back("this");
		strs.push_back("is");
		strs.push_back("a");
		strs.push_back("join");
		strs.push_back("test");
		test_eq(ofJoinString(strs,","),"hi,this,is,a,join,test","test #4363");
	}
    void run() {
        ofPixels pixels;
        const int w = 320;
        const int h = 240;

        for(ofPixelFormat pixelFormat=OF_PIXELS_GRAY; pixelFormat<OF_PIXELS_NUM_FORMATS; pixelFormat = (ofPixelFormat)(pixelFormat+1)) {
            pixels.allocate(w,h,pixelFormat);
            int bpp = bitsPerPixel(pixelFormat);
            string format = formatName(pixelFormat);
            test_eq(pixels.getBitsPerChannel(),8,"getBitsPerChannel() " + format);
            test_eq(pixels.getBitsPerPixel(),bpp,"getBitsPerPixel() " + format);
            test_eq(pixels.getBytesPerChannel(),1,"getBytesPerChannel() " + format);
            test_eq(pixels.getBytesPerPixel(),bpp/8,"getBytesPerPixel() " + format);
            test_eq(pixels.getBytesStride(),w*bpp/8,"getBytesStride() " + format);
            test_eq(pixels.getWidth(),w,"getWidth() " + format);
            test_eq(pixels.getHeight(),h,"getHeight() " + format);
            if(hasImageFormat(pixelFormat)) {
                test_eq(pixels.getImageType(),imageType(pixelFormat),"getImageType() " + format);
            }
            if(hasChannels(pixelFormat)) {
                test_eq(pixels.getNumChannels(),numChannels(pixelFormat),"getNumChannels() " + format);
            }
            test_eq(pixels.getNumPlanes(),getNumPlanes(pixelFormat),"getNumPlanes() " + format);
            test_eq(pixels.getPixelFormat(),pixelFormat,"getPixelFormat() " + format);
            test_eq((uint64_t)pixels.getPlane(0).getData(), (uint64_t)pixels.getData(),"getPlane(0)==getData() " + format);
            test_eq(pixels.getTotalBytes(),w*h*bpp/8,"getTotalBytes() " + format);

            if(hasWorkingIterators(pixelFormat)) {
                test_eq((uint64_t)pixels.getLine(0).begin(), (uint64_t)pixels.getData(),"getLine(0).begin()==getData() " + format);
                test_eq((uint64_t)pixels.getLine(0).end(), (uint64_t)pixels.getData()+(w*bpp/8),"getLine(0).end()==getData()+(w*3) " + format);
                test_eq((uint64_t)pixels.getLine(h-1).begin(), (uint64_t)pixels.getData()+(w*bpp/8*(h-1)),"getLine(h-1).begin()==getData()+(w*bpp/8*(h-1)) " + format);
                test_eq((uint64_t)pixels.getLine(h-1).end(), (uint64_t)pixels.end(),"getLine(h-1).end()==end() " + format);
                test_eq((uint64_t)&pixels.getLine(0).getPixel(10)[0], (uint64_t)pixels.getData()+(10*bpp/8),"getLine(0).getPixel(10)[0]==pixels.getData()+(10*bpp/8)");
            }
        }
    }
Esempio n. 30
0
/** Run unit tests for smartlist-of-strings functionality. */
static void
test_container_smartlist_strings(void)
{
  smartlist_t *sl = smartlist_new();
  char *cp=NULL, *cp_alloc=NULL;
  size_t sz;

  /* Test split and join */
  test_eq(0, smartlist_len(sl));
  smartlist_split_string(sl, "abc", ":", 0, 0);
  test_eq(1, smartlist_len(sl));
  test_streq("abc", smartlist_get(sl, 0));
  smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  test_eq(4, smartlist_len(sl));
  test_streq("a", smartlist_get(sl, 1));
  test_streq("bc", smartlist_get(sl, 2));
  test_streq("", smartlist_get(sl, 3));
  cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
  test_streq(cp_alloc, "abcabc");
  tor_free(cp_alloc);
  cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
  test_streq(cp_alloc, "abc!a!bc!");
  tor_free(cp_alloc);
  cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  test_streq(cp_alloc, "abcXYaXYbcXY");
  tor_free(cp_alloc);
  cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  test_streq(cp_alloc, "abcXYaXYbcXYXY");
  tor_free(cp_alloc);
  cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
  test_streq(cp_alloc, "abcabc");
  tor_free(cp_alloc);

  smartlist_split_string(sl, "/def/  /ghijk", "/", 0, 0);
  test_eq(8, smartlist_len(sl));
  test_streq("", smartlist_get(sl, 4));
  test_streq("def", smartlist_get(sl, 5));
  test_streq("  ", smartlist_get(sl, 6));
  test_streq("ghijk", smartlist_get(sl, 7));
  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);

  smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  test_eq(3, smartlist_len(sl));
  test_streq("a", smartlist_get(sl,0));
  test_streq("bbd", smartlist_get(sl,1));
  test_streq("cdef", smartlist_get(sl,2));
  smartlist_split_string(sl, " z <> zhasd <>  <> bnud<>   ", "<>",
                         SPLIT_SKIP_SPACE, 0);
  test_eq(8, smartlist_len(sl));
  test_streq("z", smartlist_get(sl,3));
  test_streq("zhasd", smartlist_get(sl,4));
  test_streq("", smartlist_get(sl,5));
  test_streq("bnud", smartlist_get(sl,6));
  test_streq("", smartlist_get(sl,7));

  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);

  smartlist_split_string(sl, " ab\tc \td ef  ", NULL,
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  test_eq(4, smartlist_len(sl));
  test_streq("ab", smartlist_get(sl,0));
  test_streq("c", smartlist_get(sl,1));
  test_streq("d", smartlist_get(sl,2));
  test_streq("ef", smartlist_get(sl,3));
  smartlist_split_string(sl, "ghi\tj", NULL,
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  test_eq(6, smartlist_len(sl));
  test_streq("ghi", smartlist_get(sl,4));
  test_streq("j", smartlist_get(sl,5));

  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);

  cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  test_streq(cp_alloc, "");
  tor_free(cp_alloc);
  cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  test_streq(cp_alloc, "XY");
  tor_free(cp_alloc);

  smartlist_split_string(sl, " z <> zhasd <>  <> bnud<>   ", "<>",
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  test_eq(3, smartlist_len(sl));
  test_streq("z", smartlist_get(sl, 0));
  test_streq("zhasd", smartlist_get(sl, 1));
  test_streq("bnud", smartlist_get(sl, 2));
  smartlist_split_string(sl, " z <> zhasd <>  <> bnud<>   ", "<>",
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  test_eq(5, smartlist_len(sl));
  test_streq("z", smartlist_get(sl, 3));
  test_streq("zhasd <>  <> bnud<>", smartlist_get(sl, 4));
  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);

  smartlist_split_string(sl, "abcd\n", "\n",
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  test_eq(1, smartlist_len(sl));
  test_streq("abcd", smartlist_get(sl, 0));
  smartlist_split_string(sl, "efgh", "\n",
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  test_eq(2, smartlist_len(sl));
  test_streq("efgh", smartlist_get(sl, 1));

  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);

  /* Test swapping, shuffling, and sorting. */
  smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  test_eq(7, smartlist_len(sl));
  smartlist_sort(sl, compare_strs_);
  cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  test_streq(cp_alloc,"and,arma,by,nickm,onion,router,the");
  tor_free(cp_alloc);
  smartlist_swap(sl, 1, 5);
  cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  test_streq(cp_alloc,"and,router,by,nickm,onion,arma,the");
  tor_free(cp_alloc);
  smartlist_shuffle(sl);
  test_eq(7, smartlist_len(sl));
  test_assert(smartlist_contains_string(sl, "and"));
  test_assert(smartlist_contains_string(sl, "router"));
  test_assert(smartlist_contains_string(sl, "by"));
  test_assert(smartlist_contains_string(sl, "nickm"));
  test_assert(smartlist_contains_string(sl, "onion"));
  test_assert(smartlist_contains_string(sl, "arma"));
  test_assert(smartlist_contains_string(sl, "the"));

  /* Test bsearch. */
  smartlist_sort(sl, compare_strs_);
  test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
                                        compare_without_first_ch_));
  test_streq("and", smartlist_bsearch(sl, " AND", compare_without_first_ch_));
  test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", compare_without_first_ch_));

  /* Test bsearch_idx */
  {
    int f;
    smartlist_t *tmp = NULL;

    test_eq(0, smartlist_bsearch_idx(sl," aaa",compare_without_first_ch_,&f));
    test_eq(f, 0);
    test_eq(0, smartlist_bsearch_idx(sl," and",compare_without_first_ch_,&f));
    test_eq(f, 1);
    test_eq(1, smartlist_bsearch_idx(sl," arm",compare_without_first_ch_,&f));
    test_eq(f, 0);
    test_eq(1, smartlist_bsearch_idx(sl," arma",compare_without_first_ch_,&f));
    test_eq(f, 1);
    test_eq(2, smartlist_bsearch_idx(sl," armb",compare_without_first_ch_,&f));
    test_eq(f, 0);
    test_eq(7, smartlist_bsearch_idx(sl," zzzz",compare_without_first_ch_,&f));
    test_eq(f, 0);

    /* Test trivial cases for list of length 0 or 1 */
    tmp = smartlist_new();
    test_eq(0, smartlist_bsearch_idx(tmp, "foo",
                                     compare_strs_for_bsearch_, &f));
    test_eq(f, 0);
    smartlist_insert(tmp, 0, (void *)("bar"));
    test_eq(1, smartlist_bsearch_idx(tmp, "foo",
                                     compare_strs_for_bsearch_, &f));
    test_eq(f, 0);
    test_eq(0, smartlist_bsearch_idx(tmp, "aaa",
                                     compare_strs_for_bsearch_, &f));
    test_eq(f, 0);
    test_eq(0, smartlist_bsearch_idx(tmp, "bar",
                                     compare_strs_for_bsearch_, &f));
    test_eq(f, 1);
    /* ... and one for length 2 */
    smartlist_insert(tmp, 1, (void *)("foo"));
    test_eq(1, smartlist_bsearch_idx(tmp, "foo",
                                     compare_strs_for_bsearch_, &f));
    test_eq(f, 1);
    test_eq(2, smartlist_bsearch_idx(tmp, "goo",
                                     compare_strs_for_bsearch_, &f));
    test_eq(f, 0);
    smartlist_free(tmp);
  }

  /* Test reverse() and pop_last() */
  smartlist_reverse(sl);
  cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  test_streq(cp_alloc,"the,router,onion,nickm,by,arma,and");
  tor_free(cp_alloc);
  cp_alloc = smartlist_pop_last(sl);
  test_streq(cp_alloc, "and");
  tor_free(cp_alloc);
  test_eq(smartlist_len(sl), 6);
  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);
  cp_alloc = smartlist_pop_last(sl);
  test_eq_ptr(cp_alloc, NULL);

  /* Test uniq() */
  smartlist_split_string(sl,
                     "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
                     ",", 0, 0);
  smartlist_sort(sl, compare_strs_);
  smartlist_uniq(sl, compare_strs_, tor_free_);
  cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  test_streq(cp_alloc, "50,a,canal,man,noon,panama,plan,radar");
  tor_free(cp_alloc);

  /* Test contains_string, contains_string_case and contains_int_as_string */
  test_assert(smartlist_contains_string(sl, "noon"));
  test_assert(!smartlist_contains_string(sl, "noonoon"));
  test_assert(smartlist_contains_string_case(sl, "nOOn"));
  test_assert(!smartlist_contains_string_case(sl, "nooNooN"));
  test_assert(smartlist_contains_int_as_string(sl, 50));
  test_assert(!smartlist_contains_int_as_string(sl, 60));

  /* Test smartlist_choose */
  {
    int i;
    int allsame = 1;
    int allin = 1;
    void *first = smartlist_choose(sl);
    test_assert(smartlist_contains(sl, first));
    for (i = 0; i < 100; ++i) {
      void *second = smartlist_choose(sl);
      if (second != first)
        allsame = 0;
      if (!smartlist_contains(sl, second))
        allin = 0;
    }
    test_assert(!allsame);
    test_assert(allin);
  }
  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_clear(sl);

  /* Test string_remove and remove and join_strings2 */
  smartlist_split_string(sl,
                    "Some say the Earth will end in ice and some in fire",
                    " ", 0, 0);
  cp = smartlist_get(sl, 4);
  test_streq(cp, "will");
  smartlist_add(sl, cp);
  smartlist_remove(sl, cp);
  tor_free(cp);
  cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  test_streq(cp_alloc, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  tor_free(cp_alloc);
  smartlist_string_remove(sl, "in");
  cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
  test_streq(cp_alloc, "Some+say+the+Earth+fire+end+some+ice+and");
  test_eq((int)sz, 40);

 done:

  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  smartlist_free(sl);
  tor_free(cp_alloc);
}