static void testRecvFail(CuTest *tc) { NTPSock*listenSock; NTPSock*connectSock; NTPSock*acceptSock; char buf[20]; int result; uint16_t port = 38712; //connect the sockets connectUtil(tc, &listenSock, &connectSock, &acceptSock, port); //close one socket NTPDisconnect(&connectSock); CuAssert(tc, "disconnect to NULL", connectSock==NULL); //try to recv() on the other socket result = NTPRecv(acceptSock, buf, sizeof(buf)); CuAssert(tc, "recv should fail", result<=0); //cleanup NTPDisconnect(&listenSock); NTPDisconnect(&acceptSock); CuAssert(tc, "disconnect NULL", listenSock==NULL); CuAssert(tc, "disconnect NULL", acceptSock==NULL); }
int TestSetThreadCount(CuTest *ct) { /* SKIP */ /* Run this test only when hash is supported. */ #ifdef HAVE_HASH DB_ENV *dbenv; DB *db; CuAssert(ct, "db_env_create", db_env_create(&dbenv, 0) == 0); dbenv->set_errpfx(dbenv, "TestSetThreadCount"); CuAssert(ct, "set_thread_count", dbenv->set_thread_count(dbenv, 2) == 0); CuAssert(ct, "env->open", dbenv->open(dbenv, ".", DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE | DB_THREAD, 0) == 0); CuAssert(ct, "db_create", db_create(&db, dbenv, 0) == 0); CuAssert(ct, "DB->open", db->open( db, NULL, NULL, "TestSetThreadCount", DB_HASH, DB_CREATE, 0) == 0); db->close(db, 0); dbenv->close(dbenv, 0); #else printf("TestSetThreadCount is not supported by the build.\n"); #endif /* HAVE_HASH */ return (0); }
/* * To verify the integrity of the output file produced * I hand walked through the complete file ensuring that * it held to the file schema--Bill */ void t_file___write_block(CuTest *tc) { FILE *fp, *f; m_uword cnt; m_uword tmp; int i, j; m_file_block block; miniat *m; m = malloc(sizeof(miniat)); fp = fopen("write_block.miniat", "w+"); CuAssert(tc, "File pointer not equal to NULL", fp != NULL); /*write the header*/ cnt = (rand() % 20) + 10; m_file_write_block_count (m, fp, cnt); /*start building the body data to write*/ for(i=0; i<cnt; i++) { block.number = i; block.id = (rand() % m_block_type_cnt); block.start_address = ((rand() << 16) | (rand())); block.length = rand()%32; block.data = (m_uword *)malloc(sizeof(m_uword) * block.length); CuAssert(tc, "Block data not equal to NULL", block.data != NULL); /*make some data*/ for(j=0; j < block.length; j++) { block.data[j] = ((rand() << 16) | (rand())); } /*write the block*/ m_file_write_block (m, fp, &block); /*free the block.data*/ if(block.data) { for(j=0; j < block.length; j++) { //free(block.data[j]); } //free(block.data); block.data = NULL; } } /*rewind the file pointer and read the blocks to verify*/ rewind(fp); /*check the counts*/ m_file_read_block_count (m, fp, &tmp); CuAssert(tc, "Block count matches previous block count", cnt == tmp); if(fp) { fclose(fp); } if(m) { free(m); } remove("write_block.miniat"); return; }
static void testSendFail(CuTest *tc) { NTPSock *listenSock; NTPSock *connectSock; NTPSock *acceptSock; char buf[] = "So remember, remember the Red River Valley"; int result; uint16_t port = 23551; //connect the sockets connectUtil(tc, &listenSock, &connectSock, &acceptSock, port); //close one socket NTPDisconnect(&acceptSock); CuAssert(tc, "disconnect to NULL", acceptSock==NULL); //try to send() on the other socket. Keep trying //because it doesn't always fail the first time. int i; for(i=0;i<20;i++) { result = NTPSend(connectSock, buf, NTPstrlen(buf)); } CuAssert(tc, "Send should fail", result<=0); //cleanup NTPDisconnect(&listenSock); NTPDisconnect(&connectSock); CuAssert(tc, "disconnect NULL", listenSock==NULL); CuAssert(tc, "disconnect NULL", acceptSock==NULL); }
static void testSerializePublicationsFile(CuTest *tc) { int res; KSI_PublicationsFile *pubFile = NULL; char *raw = NULL; unsigned raw_len = 0; FILE *f = NULL; int symbol = 0; unsigned i= 0; KSI_ERR_clearErrors(ctx); setFileMockResponse(tc, getFullResourcePath(TEST_PUBLICATIONS_FILE)); res = KSI_PublicationsFile_fromFile(ctx, getFullResourcePath(TEST_PUBLICATIONS_FILE), &pubFile); CuAssert(tc, "Unable to read publications file", res == KSI_OK && pubFile != NULL); res = KSI_PublicationsFile_serialize(ctx, pubFile, &raw, &raw_len); CuAssert(tc, "Unable to serialize publications file", res == KSI_OK && raw != NULL && raw_len != 0); f = fopen(getFullResourcePath(TEST_PUBLICATIONS_FILE), "rb"); CuAssert(tc, "Unable to open publications file", res == KSI_OK && f != NULL); while ((symbol = getc(f)) != EOF && i<raw_len){ CuAssert(tc, "Serialized publications file mismatch", (char)symbol == raw[i]); i++; } CuAssert(tc, "Serialized publications file length mismatch", i == raw_len); KSI_PublicationsFile_free(pubFile); KSI_free(raw); if (f) fclose(f); }
static void expect_authentic (CuTest *tc, const int expected_status, SignedVotedBallot const svbs, VoterRoll const vr, BlankBallot const bb) { char *raw_ballot_box = 0; CuAssert (tc, (0 == expected_status ? "VHTI_authenticate should have accepted valid input" : "VHTI_authenticate should have rejected invalid input"), expected_status == VHTI_authenticate (svbs, vr, bb, &raw_ballot_box)); if (0 != expected_status) { if (raw_ballot_box) fprintf (stderr, "Hmm: `%s'\n", raw_ballot_box); CuAssertPtrEquals (tc, NULL, raw_ballot_box); } else { CuAssertPtrNotNull (tc, raw_ballot_box); CuAssert (tc, "VHTI_authenticate should have returned a RawBallotBox", 0 == VHTI_validate (RAW_BALLOT_BOX, raw_ballot_box)); VHTI_free (raw_ballot_box); } }
static void testVerifySignatureWithUserPublication(CuTest *tc) { int res; KSI_Signature *sig = NULL; const char pubStr[] = "AAAAAA-CTOQBY-AAMJYH-XZPM6T-UO6U6V-2WJMHQ-EJMVXR-JEAGID-2OY7P5-XFFKYI-QIF2LG-YOV7SO"; const char pubStr_bad[] = "AAAAAA-CT5VGY-AAPUCF-L3EKCC-NRSX56-AXIDFL-VZJQK4-WDCPOE-3KIWGB-XGPPM3-O5BIMW-REOVR4"; KSI_PublicationData *pubData = NULL; KSI_PublicationData *pubData_bad = NULL; KSI_ERR_clearErrors(ctx); res = KSI_PublicationData_fromBase32(ctx, pubStr, &pubData); CuAssert(tc, "Unable to parse publication string.", res == KSI_OK && pubData != NULL); res = KSI_PublicationData_fromBase32(ctx, pubStr_bad, &pubData_bad); CuAssert(tc, "Unable to parse publication string.", res == KSI_OK && pubData_bad != NULL); res = KSI_Signature_fromFile(ctx, getFullResourcePath("resource/tlv/ok-sig-2014-04-30.1-extended.ksig"), &sig); CuAssert(tc, "Unable to read signature from file.", res == KSI_OK && sig != NULL); res = KSI_Signature_verifyWithPublication(sig, ctx, pubData); CuAssert(tc, "Unable to verify signature with publication.", res == KSI_OK); res = KSI_Signature_verifyWithPublication(sig, ctx, pubData_bad); CuAssert(tc, "Unable to verify signature with publication.", res != KSI_OK); KSI_PublicationData_free(pubData); KSI_PublicationData_free(pubData_bad); KSI_Signature_free(sig); }
void TestSessionKeyTableAPI( CuTest* tc ) { dssl_SessionKeyTable* tbl = NULL; DSSL_Session* sess = NULL; DSSL_SessionKeyData* kd = NULL; tbl = dssl_SessionKT_Create( 100, TEST_CACHE_TIMEOUT ); CuAssert( tc, "SessionKeyTable object should be not NULL", tbl != NULL ); sess = (DSSL_Session*) malloc( sizeof( DSSL_Session ) ); CuAssert( tc, "DSSL_Session object should be not NULL", sess != NULL ); memset( sess, 0, sizeof(*sess) ); dssl_SessionKT_Add( tbl, sess ); CuAssertTrue( tc, tbl->count == 1 ); kd = dssl_SessionKT_Find( tbl, sess->session_id ); CuAssertTrue( tc, kd != NULL ); CuAssertTrue( tc, kd->refcount == 1 ); CuAssertTrue( tc, kd->released_time == 0 ); kd = NULL; dssl_SessionKT_Release( tbl, sess->session_id ); ThreadSleep( (TEST_CACHE_TIMEOUT+1)*1000 ); dssl_SessionKT_CleanSessionCache( tbl ); CuAssertTrue( tc, tbl->count == 0 ); free( sess ); dssl_SessionKT_Destroy( tbl ); }
static void testExtractingNotExisting(CuTest* tc) { int res; KSI_Signature *sig = NULL; KSI_MultiSignature *ms = NULL; KSI_DataHash *hsh = NULL; KSI_ERR_clearErrors(ctx); res = KSI_Signature_fromFile(ctx, getFullResourcePath(TEST_SIGNATURE_FILE), &sig); CuAssert(tc, "Unable to read signature from file.", res == KSI_OK && sig != NULL); res = KSI_MultiSignature_new(ctx, &ms); CuAssert(tc, "Unable to create multi signature container.", res == KSI_OK && ms != NULL); res = KSI_MultiSignature_add(ms, sig); CuAssert(tc, "Unable to add signature to multi signature container.", res == KSI_OK); KSI_Signature_free(sig); sig = NULL; KSITest_DataHash_fromStr(ctx, "01db27c0db0aebb8d3963c3a720985cedb600f91854cdb1e45ad631611c39284dd", &hsh); res = KSI_MultiSignature_get(ms, hsh, &sig); CuAssert(tc, "Get should fail with KSI_MULTISIG_NOT_FOUND", res == KSI_MULTISIG_NOT_FOUND && sig == NULL); KSI_DataHash_free(hsh); KSI_MultiSignature_free(ms); KSI_Signature_free(sig); }
static void testVerifyDocument(CuTest *tc) { int res; unsigned char in[0x1ffff]; unsigned in_len = 0; char doc[] = "LAPTOP"; FILE *f = NULL; KSI_Signature *sig = NULL; KSI_ERR_clearErrors(ctx); f = fopen(getFullResourcePath(TEST_SIGNATURE_FILE), "rb"); CuAssert(tc, "Unable to open signature file.", f != NULL); in_len = (unsigned)fread(in, 1, sizeof(in), f); CuAssert(tc, "Nothing read from signature file.", in_len > 0); fclose(f); res = KSI_Signature_parse(ctx, in, in_len, &sig); CuAssert(tc, "Failed to parse signature", res == KSI_OK && sig != NULL); res = KSI_Signature_verifyDocument(sig, ctx, doc, strlen(doc)); CuAssert(tc, "Failed to verify valid document", res == KSI_OK); res = KSI_Signature_verifyDocument(sig, ctx, doc, sizeof(doc)); CuAssert(tc, "Verification did not fail with expected error.", res == KSI_VERIFICATION_FAILURE); KSI_Signature_free(sig); }
void potion_test_empty(CuTest *T) { PN empty = PN_TUP0(); CuAssert(T, "empty isn't a tuple", PN_IS_TUPLE(empty)); CuAssert(T, "empty isn't a ref", PN_IS_PTR(empty)); CuAssertIntEquals(T, "tuple length is off", 0, PN_INT(potion_send(empty, potion_str(P, "length")))); }
void potion_test_allocated(CuTest *T) { struct PNMemory *M = P->mem; void *prev = NULL; void *scanptr = (void *)((char *)M->birth_lo + PN_ALIGN(sizeof(struct PNMemory), 8)); while ((PN)scanptr < (PN)M->birth_cur) { if (((struct PNFwd *)scanptr)->fwd != POTION_FWD && ((struct PNFwd *)scanptr)->fwd != POTION_COPIED) { if (((struct PNObject *)scanptr)->vt > PN_TUSER) { vPN(Object) o = (struct PNObject *)scanptr; fprintf(stderr, "error: scanning heap from %p to %p\n", M->birth_lo, M->birth_cur); fprintf(stderr, "%p in %s region\n", scanptr, IS_GC_PROTECTED(scanptr) ? "protected" : IN_BIRTH_REGION(scanptr) ? "birth" : IN_OLDER_REGION(scanptr) ? "older" : "gc"); fprintf(stderr, "%p { uniq:0x%08x vt:0x%08x ivars[0]:0x%08lx type:0x%x}\n", scanptr, o->uniq, o->vt, o->ivars[0], potion_type((PN)scanptr)); fprintf(stderr, "prev %p: size=%d, type:0x%x (%s)\n", prev, potion_type_size(P, prev), potion_type((PN)prev), AS_STR(PN_VTABLE(PN_TYPE((PN)prev)))); #ifdef DEBUG //potion_dump_stack(P); #endif } CuAssert(T, "wrong type for allocated object", ((struct PNObject *)scanptr)->vt <= PN_TUSER); } prev = scanptr; scanptr = (void *)((char *)scanptr + potion_type_size(P, scanptr)); CuAssert(T, "allocated object goes beyond GC pointer", (PN)scanptr <= (PN)M->birth_cur); } }
void createIndex(CuTest* tc, Directory* dir, bool multiSegment) { WhitespaceAnalyzer whitespaceAnalyzer; IndexWriter w(dir, &whitespaceAnalyzer, true); w.setMergePolicy(_CLNEW LogDocMergePolicy()); Document doc; for (int i = 0; i < 100; i++) { createDocument(doc, i, 4); w.addDocument(&doc); if (multiSegment && (i % 10) == 0) { w.flush(); } } if (!multiSegment) { w.optimize(); } w.close(); IndexReader* r = IndexReader::open(dir); if (multiSegment) { CuAssert(tc,_T("check is multi"), strcmp(r->getObjectName(),"MultiSegmentReader")==0); } else { CuAssert(tc,_T("check is segment"), strcmp(r->getObjectName(),"SegmentReader")==0); } r->close(); _CLDELETE(r); }
static void addInput(CuTest *tc, KSI_BlockSigner *bs, int genMeta) { int res = KSI_UNKNOWN_ERROR; size_t i; KSI_DataHash *hsh = NULL; KSI_MetaData *md = NULL; for (i = 0; input_data[i] != NULL; i++) { res = KSI_DataHash_create(ctx, input_data[i], strlen(input_data[i]), KSI_HASHALG_SHA2_256, &hsh); CuAssert(tc, "Unable to create data hash.", res == KSI_OK && hsh != NULL); if (genMeta) { char clientId[100]; KSI_snprintf(clientId, sizeof(clientId), "Client-%d", i); res = createMetaData(clientId, &md); CuAssert(tc, "Unable to create metadata.", res == KSI_OK && md != NULL); res = KSI_BlockSigner_addLeaf(bs, hsh, 0, md, NULL); CuAssert(tc, "Unable to add leaf with meta data.", res == KSI_OK); KSI_MetaData_free(md); md = NULL; } else { res = KSI_BlockSigner_add(bs, hsh); CuAssert(tc, "Unable to add data hash to the block signer.", res == KSI_OK); } KSI_DataHash_free(hsh); hsh = NULL; } }
static void rbtree_1(CuTest *tc) { /* tree should be empty on start. */ CuAssert(tc, "empty tree?", (rbtree_first((rbtree_t*)tree) == &rbtree_null_node)); CuAssert(tc, "empty tree?", (rbtree_last((rbtree_t*)tree) == &rbtree_null_node)); test_tree_integrity(tc, tree); }
static void testVerifyCalendarChainAlgoChange(CuTest *tc) { int res; unsigned char in[0x1ffff]; unsigned in_len = 0; FILE *f = NULL; KSI_Signature *sig = NULL; KSI_ERR_clearErrors(ctx); f = fopen(getFullResourcePath("resource/tlv/cal_algo_switch.ksig"), "rb"); CuAssert(tc, "Unable to open signature file.", f != NULL); in_len = (unsigned)fread(in, 1, sizeof(in), f); CuAssert(tc, "Nothing read from signature file.", in_len > 0); fclose(f); res = KSI_Signature_parse(ctx, in, in_len, &sig); CuAssert(tc, "Failed to parse signature", res == KSI_OK && sig != NULL); KSITest_setFileMockResponse(tc, getFullResourcePath("resource/tlv/cal_algo_switch-extend_resposne.tlv")); ctx->requestCounter = 0; res = KSI_Signature_verifyOnline(sig, ctx); CuAssert(tc, "Failed to verify valid document", res == KSI_OK); KSI_Signature_free(sig); }
static void testGetOldest(CuTest *tc) { int res; KSI_MultiSignature *ms = NULL; KSI_DataHash *hsh = NULL; KSI_Signature *sig = NULL; KSI_Integer *tm = NULL; res = KSI_MultiSignature_fromFile(ctx, getFullResourcePath("resource/multi_sig/test2.mksi"), &ms); CuAssert(tc, "Unable to read multi signature container from file.", res == KSI_OK && ms != NULL); KSITest_DataHash_fromStr(ctx, "0111a700b0c8066c47ecba05ed37bc14dcadb238552d86c659342d1d7e87b8772d", &hsh); res = KSI_MultiSignature_get(ms, hsh, &sig); CuAssert(tc, "Unable to get signature from container.", res == KSI_OK && sig != NULL); res = KSI_verifySignature(ctx, sig); CuAssert(tc, "Unable to verify signature extracted from container.", res == KSI_OK); res = KSI_Signature_getSigningTime(sig, &tm); CuAssert(tc, "Wrong signing time (probably returning the newer signature).", res == KSI_OK && KSI_Integer_equalsUInt(tm, 1398866256)); KSI_Signature_free(sig); KSI_DataHash_free(hsh); KSI_MultiSignature_free(ms); }
static void testLoadPublicationsFileWithNoCerts(CuTest *tc) { int res; KSI_PublicationsFile *pubFile = NULL; KSI_LIST(KSI_CertificateRecord) *certList = NULL; KSI_PKICertificate *cert = NULL; unsigned char dummy[] = {0xca, 0xfe, 0xba, 0xbe}; KSI_OctetString *certId = NULL; KSI_ERR_clearErrors(ctx); res = KSI_PublicationsFile_fromFile(ctx, getFullResourcePath("resource/publications/publications-nocerts.bin"), &pubFile); CuAssert(tc, "Unable to read publications file", res == KSI_OK && pubFile != NULL); res = KSI_PublicationsFile_getCertificates(pubFile, &certList); CuAssert(tc, "Unable to get certificate list", res == KSI_OK); CuAssert(tc, "Unexpected certificate list length.", KSI_CertificateRecordList_length(certList) == 0); res = KSI_OctetString_new(ctx, dummy, sizeof(dummy), &certId); CuAssert(tc, "Creating an octetstring failed", res == KSI_OK && certId != NULL); res = KSI_PublicationsFile_getPKICertificateById(pubFile, certId, &cert); CuAssert(tc, "Searching for a non existend certificate failed", res == KSI_OK && cert == NULL); KSI_OctetString_free(certId); KSI_PublicationsFile_free(pubFile); }
static void testVerifyPublicationsFileWithAttributeNotPresent(CuTest *tc) { int res; KSI_PublicationsFile *pubFile = NULL; KSI_CertConstraint arr[] = { {NULL, NULL}, {NULL, NULL} }; KSI_ERR_clearErrors(ctx); res = KSI_PublicationsFile_fromFile(ctx, getFullResourcePath(TEST_PUBLICATIONS_FILE), &pubFile); CuAssert(tc, "Unable to read publications file", res == KSI_OK && pubFile != NULL); arr[0].oid = "2.5.4.9"; arr[0].val = "Local pub"; res = KSI_CTX_setDefaultPubFileCertConstraints(ctx, arr); CuAssert(tc, "Unable to delete OID 2.5.4.9", res == KSI_OK); /* Verification should fail. */ res = KSI_PublicationsFile_verify(pubFile, ctx); CuAssert(tc, "Publications file must verify with address.", res != KSI_OK); arr[0].oid = KSI_CERT_EMAIL; arr[0].val = "*****@*****.**"; res = KSI_CTX_setDefaultPubFileCertConstraints(ctx, arr); CuAssert(tc, "Unable to set OID 2.5.4.9 back to normal", res == KSI_OK); /* Verification should not fail. */ res = KSI_PublicationsFile_verify(pubFile, ctx); CuAssert(tc, "Publications file must verify.", res == KSI_OK); KSI_PublicationsFile_free(pubFile); }
static void testVerifyPublicationsFileWithNoConstraints(CuTest *tc) { int res; KSI_PublicationsFile *pubFile = NULL; KSI_CertConstraint arr[] = { {NULL, NULL}, {NULL, NULL} }; KSI_ERR_clearErrors(ctx); res = KSI_PublicationsFile_fromFile(ctx, getFullResourcePath(TEST_PUBLICATIONS_FILE), &pubFile); CuAssert(tc, "Unable to read publications file", res == KSI_OK && pubFile != NULL); res = KSI_CTX_setDefaultPubFileCertConstraints(ctx, arr); CuAssert(tc, "Unable to delete OID 1.2.840.113549.1.9.1", res == KSI_OK); /* Verification should not fail. */ res = KSI_PublicationsFile_verify(pubFile, ctx); CuAssert(tc, "Publications file may not verify with no constraints.", res == KSI_PUBFILE_VERIFICATION_NOT_CONFIGURED); arr[0].oid = KSI_CERT_EMAIL; arr[0].val = "*****@*****.**"; res = KSI_CTX_setDefaultPubFileCertConstraints(ctx, arr); CuAssert(tc, "Unable to set OID 1.2.840.113549.1.9.1 back to normal", res == KSI_OK); /* Verification should not fail. */ res = KSI_PublicationsFile_verify(pubFile, ctx); CuAssert(tc, "Publications file must verify with e-mail.", res == KSI_OK); KSI_PublicationsFile_free(pubFile); }
static void test_fail_read_flush(CuTest *tc) { apr_file_t *f; const char *fname = "data/testflush.dat"; apr_status_t rv; char buf[2]; apr_file_remove(fname, p); apr_assert_success(tc, "open test file", apr_file_open(&f, fname, APR_CREATE|APR_READ|APR_BUFFERED, APR_UREAD|APR_UWRITE, p)); /* this write should be buffered. */ apr_assert_success(tc, "buffered write should succeed", apr_file_puts("hello", f)); /* Now, trying a read should fail since the write must be flushed, * and should fail with something other than EOF since the file is * opened read-only. */ rv = apr_file_read_full(f, buf, 2, NULL); CuAssert(tc, "read should flush buffered write and fail", rv != APR_SUCCESS && rv != APR_EOF); /* Likewise for gets */ rv = apr_file_gets(buf, 2, f); CuAssert(tc, "gets should flush buffered write and fail", rv != APR_SUCCESS && rv != APR_EOF); apr_file_close(f); apr_file_remove(fname, p); }
void potion_test_int3(CuTest *T) { PN neg = PN_NUM(-4343); CuAssert(T, "negative numbers invalid", PN_INT(neg) == -4343); CuAssert(T, "negative not a number", PN_IS_INT(neg)); CuAssert(T, "negative is a ref", !PN_IS_PTR(neg)); CuAssert(T, "negative bad add", -3853 == PN_INT(potion_send(neg, potion_str(P, "+"), num))); }
void potion_test_int2(CuTest *T) { PN pos = PN_NUM(10891); CuAssert(T, "positive numbers invalid", PN_INT(pos) == 10891); CuAssert(T, "positive not a number", PN_IS_INT(pos)); CuAssert(T, "positive is a ref", !PN_IS_PTR(pos)); CuAssert(T, "positive bad add", 11381 == PN_INT(potion_send(pos, potion_str(P, "+"), num))); }
void potion_test_int1(CuTest *T) { PN zero = PN_ZERO; CuAssert(T, "zero isn't zero", PN_INT(zero) == 0); CuAssert(T, "zero isn't a number", PN_IS_INT(zero)); CuAssert(T, "zero is a ref", !PN_IS_PTR(zero)); CuAssert(T, "zero bad add", 490 == PN_INT(potion_send(zero, potion_str(P, "+"), num))); }
static void rbtree_3(CuTest *tc) { /* check RBTREE_NULL contents */ CuAssert(tc, "RBTREE_NULL)->parent == RBTREE_NULL", ((RBTREE_NULL)->parent == RBTREE_NULL)); CuAssert(tc, "RBTREE_NULL)->left == RBTREE_NULL", ((RBTREE_NULL)->left == RBTREE_NULL)); CuAssert(tc, "RBTREE_NULL)->right == RBTREE_NULL", ((RBTREE_NULL)->right == RBTREE_NULL)); CuAssert(tc, "RBTREE_NULL)->color == 0", ((RBTREE_NULL)->color == 0)); CuAssert(tc, "RBTREE_NULL)->key == 0", ((RBTREE_NULL)->key == 0)); }
void potion_test_tuple(CuTest *T) { PN tup = potion_tuple_with_size(P, 3); PN_TUPLE_AT(tup, 0) = PN_NIL; PN_TUPLE_AT(tup, 1) = PN_string; PN_TUPLE_AT(tup, 2) = tup; CuAssert(T, "tuple isn't a tuple", PN_IS_TUPLE(tup)); CuAssert(T, "tuple isn't a ref", PN_IS_PTR(tup)); CuAssertIntEquals(T, "tuple length is off", 3, PN_INT(potion_send(tup, potion_str(P, "length")))); }
static void testMaskingInput(CuTest *tc) { static const unsigned char diceRolls[] = {0xd5, 0x58, 0xaf, 0xfa, 0x80, 0x67, 0xf4, 0x2c, 0xd9, 0x48, 0x36, 0x21, 0xd1, 0xab, 0xae, 0x23, 0xed, 0xd6, 0xca, 0x04, 0x72, 0x7e, 0xcf, 0xc7, 0xdb, 0xc7, 0x6b, 0xde, 0x34, 0x77, 0x1e, 0x53}; int res; KSI_BlockSigner *bs = NULL; KSI_OctetString *iv = NULL; KSI_DataHash *zero = NULL; size_t i; struct { KSI_CTX *ctx; KSI_HashAlgorithm algo_id; KSI_DataHash *prevHash; KSI_OctetString *iv; KSI_BlockSigner **bs; int expectedRes; } tests[] = { {NULL, KSI_HASHALG_SHA3_512, NULL, NULL, NULL, KSI_INVALID_ARGUMENT}, {NULL, KSI_HASHALG_SHA3_512, NULL, NULL, &bs, KSI_INVALID_ARGUMENT}, {NULL, KSI_HASHALG_SHA3_512, NULL, iv, &bs, KSI_INVALID_ARGUMENT}, {NULL, KSI_HASHALG_SHA3_512, zero, NULL, &bs, KSI_INVALID_ARGUMENT}, {ctx, KSI_HASHALG_SHA3_512, NULL, NULL, &bs, KSI_UNAVAILABLE_HASH_ALGORITHM}, {NULL, KSI_HASHALG_SHA2_512, NULL, NULL, NULL, KSI_INVALID_ARGUMENT}, {NULL, KSI_HASHALG_SHA2_512, NULL, NULL, &bs, KSI_INVALID_ARGUMENT}, {NULL, KSI_HASHALG_SHA2_512, NULL, iv, &bs, KSI_INVALID_ARGUMENT}, {NULL, KSI_HASHALG_SHA2_512, zero, NULL, &bs, KSI_INVALID_ARGUMENT}, {ctx, KSI_HASHALG_SHA2_512, zero, NULL, &bs, KSI_OK}, {NULL, -1, NULL, NULL, NULL, -1} }; /* Create zero hash. */ res = KSI_DataHash_createZero(ctx, KSI_HASHALG_SHA2_512, &zero); CuAssert(tc, "Unable to create zero hash.", res == KSI_OK && zero != NULL); /* Create random initial vector. */ res = KSI_OctetString_new(ctx, diceRolls, sizeof(diceRolls), &iv); CuAssert(tc, "Unable to create initial vector.", res == KSI_OK && iv != NULL); res = KSI_BlockSigner_new(ctx, KSI_HASHALG_SHA1, zero, iv, &bs); CuAssert(tc, "Unable to create block signer instance with masking.", res == KSI_OK && bs != NULL); for (i = 0; tests[i].expectedRes != -1; i++) { res = KSI_BlockSigner_new(tests[i].ctx, tests[i].algo_id, tests[i].prevHash, tests[i].iv, tests[i].bs); KSI_BlockSigner_free(bs); bs = NULL; if (res != tests[i].expectedRes) { char buf[1000]; KSI_snprintf(buf, sizeof(buf), "Unexpected result @%i (expected = '%s', but was '%s').", i, KSI_getErrorString(tests[i].expectedRes), KSI_getErrorString(res)); CuFail(tc, buf); } } KSI_OctetString_free(iv); KSI_DataHash_free(zero); }
void Image_loadFromFile_shouldYieldRightDimensions(CuTest* tc) { Image* image = Image_new(); Image_loadFromFile(image, "res/test.png"); CuAssert(tc, "Image dimensions incorrect.", image->width == 6 && image->height == 8); CuAssertTrue(tc, image->channels == 3); CuAssertTrue(tc, image->bpp == 4); CuAssert(tc, "Image size incorrect.", 6*8*4 == image->size); Image_delete(image); }
void TestHashAddValue(CuTest* tc) { hash_t *hash = hash_create(); int number = 20; hash_add_value_for_key(hash, "test", &number, sizeof(number)); void *value = hash_get_value_for_key(hash, "test"); CuAssert(tc, "hash_get_value_for_key does not return NULL for valid key", value != NULL); CuAssert(tc, "hash_get_value_for_key returns correct value", *((int *)value) == 20); hash_destroy(hash); }
void potion_test_sig(CuTest *T) { // test the simple parser entry point yy_sig, not the compiler transformation potion_sig_compile PN sig = potion_sig(P, "num1=N,num2=N"); CuAssert(T, "signature isn't a tuple", PN_IS_TUPLE(sig)); CuAssertIntEquals(T, "len=2", 2, PN_INT(PN_TUPLE_LEN(sig))); CuAssertIntEquals(T, "arity=2", 2, potion_sig_arity(P, sig)); CuAssertStrEquals(T, "num1=N,num2=N", //roundtrip PN_STR_PTR(potion_sig_string(P,0,sig))); CuAssertStrEquals(T, "(num1, 78, num2, 78)", PN_STR_PTR(potion_send(sig, PN_string))); CuAssertStrEquals(T, "num1", PN_STR_PTR(potion_send(PN_TUPLE_AT(sig,0), PN_string))); CuAssertIntEquals(T, "num1=N", 'N', PN_INT(PN_TUPLE_AT(sig,1))); CuAssertStrEquals(T, "num2", PN_STR_PTR(potion_send(PN_TUPLE_AT(sig,2), PN_string))); CuAssertIntEquals(T, "num2=N", 'N', PN_INT(PN_TUPLE_AT(sig,3))); sig = potion_sig(P, "x=N|y=N"); CuAssertStrEquals(T, "(x, 78, 124, y, 78)", PN_STR_PTR(potion_send(sig, PN_string))); CuAssertIntEquals(T, "arity=2", 2, potion_sig_arity(P, sig)); sig = potion_sig(P, "x=N,y=N|r=N"); CuAssert(T, "signature isn't a tuple", PN_IS_TUPLE(sig)); CuAssertStrEquals(T, "(x, 78, y, 78, 124, r, 78)", PN_STR_PTR(potion_send(sig, PN_string))); CuAssertStrEquals(T, "x=N,y=N|r=N", PN_STR_PTR(potion_sig_string(P,0,sig))); CuAssertIntEquals(T, "arity=3", 3, potion_sig_arity(P, sig)); { // roundtrips char *sigs[] = { "", "x,y", "x", "x=N", "x,y", "x=N,y=o", "x|y", "x|y,z", "x=o|y,z", "x|y=o", "x=N,y=N|r=N", /*optional */ "x:=1", "|x:=1", "x|y:=0", /* defaults */ "x,y.z", /* the dot */ }; int size = sizeof(sigs)/sizeof(char *); int i; for (i=0; i< size; i++) { CuAssertStrEquals(T, sigs[i], PN_STR_PTR(potion_sig_string(P,0,potion_sig(P, sigs[i])))); } } CuAssertIntEquals(T, "arity nil", 0, potion_sig_arity(P, PN_NIL)); // sig "" returns PN_FALSE, which throws an error //CuAssertIntEquals(T, "arity ''", 0, potion_sig_arity(P, potion_sig(P, ""))); CuAssertIntEquals(T, "arity x:=1", 1, potion_sig_arity(P, potion_sig(P, "x:=1"))); CuAssertIntEquals(T, "arity |x:=1", 1, potion_sig_arity(P, potion_sig(P, "|x:=1"))); CuAssertIntEquals(T, "arity x|y:=1", 2, potion_sig_arity(P, potion_sig(P, "x|y:=1"))); }