int main() { char* pingBenc = "d1:q4:ping4:txid4:abcde"; struct Allocator* alloc = MallocAllocator_new(1<<22); struct TestFramework* tf = TestFramework_setUp("0123456789abcdefghijklmnopqrstuv", alloc, NULL); struct Ducttape_pvt* dt = Identity_cast((struct Ducttape_pvt*) tf->ducttape); struct Allocator* allocator = MallocAllocator_new(85000); uint16_t buffLen = sizeof(struct Ducttape_IncomingForMe) + 8 + strlen(pingBenc); uint8_t* buff = allocator->calloc(buffLen, 1, allocator); struct Headers_SwitchHeader* sh = (struct Headers_SwitchHeader*) buff; sh->label_be = Endian_hostToBigEndian64(4); struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) &sh[1]; uint8_t herPublicKey[32]; Base32_decode(herPublicKey, 32, (uint8_t*) "0z5tscp8td1sc6cv4htp7jbls79ltqxw9pbg190x0kbm1lguqtx0", 52); AddressCalc_addressForPublicKey(ip6->sourceAddr, herPublicKey); struct Headers_UDPHeader* udp = (struct Headers_UDPHeader*) &ip6[1]; ip6->hopLimit = 0; ip6->nextHeader = 17; udp->sourceAndDestPorts = 0; udp->length_be = Endian_hostToBigEndian16(strlen(pingBenc)); strncpy((char*)(udp + 1), pingBenc, strlen(pingBenc)); dt->switchInterface.receiveMessage = catchResponse; dt->switchInterface.receiverContext = NULL; // bad checksum udp->checksum_be = 1; struct Message m = { .bytes = buff, .length = buffLen, .padding = 0 }; Ducttape_injectIncomingForMe(&m, &dt->public, herPublicKey); Assert_always(!dt->switchInterface.receiverContext); // zero checksum udp->checksum_be = 0; struct Message m2 = { .bytes = buff, .length = buffLen, .padding = 0 }; Ducttape_injectIncomingForMe(&m2, &dt->public, herPublicKey); Assert_always(dt->switchInterface.receiverContext); // good checksum udp->checksum_be = Checksum_udpIp6(ip6->sourceAddr, (uint8_t*) udp, strlen(pingBenc) + Headers_UDPHeader_SIZE); struct Message m3 = { .bytes = buff, .length = buffLen, .padding = 0 }; Ducttape_injectIncomingForMe(&m3, &dt->public, herPublicKey); Assert_always(dt->switchInterface.receiverContext); }
int main() { struct Allocator* alloc = MallocAllocator_new(1<<20); struct TestFramework* tf = TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69" "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a", alloc, NULL, NULL, NULL); CryptoAuth_addUser(String_CONST("passwd"), 1, String_CONST("TEST"), tf->cryptoAuth); struct Message* message; struct Interface iface = { .sendMessage = messageFromInterface, .senderContext = &message, .allocator = alloc }; SwitchCore_setRouterInterface(&iface, tf->switchCore); //////////////////////// int ret = reconnectionNewEndpointTest(tf->ifController, tf->publicKey, &message, alloc, tf->eventBase, tf->logger, &iface, tf->rand); Allocator_free(alloc); return ret; }
static void repeatHello() { uint8_t* expectedOutput = "0000000101641c99f7719f5700000000a693a9fd3f0e27e81ab1100b57b37259" "4c2adca8671f1fdd050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0" "74bee22a90642a6ba8555be84c5e35970c5270e8f31f2a5978e0fbdee4542882" "97568f25a3fc2801aa707d954c78eccb970bcc8cb26867e9dbf0c9d6ef1b3f27" "24e7e550"; struct Allocator* alloc = MallocAllocator_new(1<<20); struct Context* ctx = setUp(NULL, HERPUBKEY, "password", alloc); struct Message* msg = Message_new(0, CryptoHeader_SIZE + HELLOWORLDLEN, alloc); Message_push(msg, HELLOWORLD, HELLOWORLDLEN, NULL); Assert_true(!CryptoAuth_encrypt(ctx->sess, msg)); Message_reset(msg); Message_push(msg, HELLOWORLD, HELLOWORLDLEN, NULL); Assert_true(!CryptoAuth_encrypt(ctx->sess, msg)); char* actual = Hex_print(msg->bytes, msg->length, alloc); if (CString_strcmp(actual, expectedOutput)) { Assert_failure("Test failed.\n" "Expected %s\n" " Got %s\n", expectedOutput, actual); } Allocator_free(alloc); }
int main(int argc, char** argv) { if (argc > 1) { respondToPipe(argv[1]); return 0; } struct Allocator* alloc = MallocAllocator_new(1<<20); char* path = Process_getPath(alloc); Assert_true(path != NULL); Assert_true(strstr(path, "/Process_test")); Assert_true(path[0] == '/'); int fds[2]; Assert_true(!Pipe_createUniPipe(fds)); char fdName[32]; snprintf(fdName, 32, "%d", fds[1]); char* args[] = { fdName, NULL }; Assert_true(!Process_spawn(path, args)); char output[32] = {0}; ssize_t len = read(fds[0], output, 31); Assert_true(len == strlen(MESSAGE)); Assert_true(!strcmp(MESSAGE, output)); }
static void testDuplicates(struct Random* rand) { uint16_t randomShorts[8192]; uint16_t out[8192]; struct ReplayProtector rp = {.bitfield = 0}; Random_bytes(rand, (uint8_t*)randomShorts, sizeof(randomShorts)); uint32_t outIdx = 0; for (uint32_t i = 0; i < 1024; i++) { if (ReplayProtector_checkNonce((randomShorts[i] % (i + 20)), &rp)) { out[outIdx] = (randomShorts[i] % (i + 20)); outIdx++; } } for (uint32_t i = 0; i < outIdx; i++) { for (uint32_t j = i + 1; j < outIdx; j++) { Assert_always(out[i] != out[j]); } } } int main() { struct Allocator* alloc = MallocAllocator_new(4096); struct Random* rand = Random_new(alloc, NULL, NULL); for (int i = 0; i < CYCLES; i++) { testDuplicates(rand); } return 0; }
int main(int argc, char** argv) { printf("init test"); struct Allocator* alloc = MallocAllocator_new(1<<20); struct Log* logger = FileWriterLog_new(stdout, alloc); struct EventBase* base = EventBase_new(alloc); char* ifName; struct Iface* iface = TAPInterface_new(NULL, &ifName, NULL, logger, base, alloc); struct NDPServer* ndp = NDPServer_new(iface, alloc); ndp->generic.receiveMessage = receiveMessage; ndp->generic.receiverContext = alloc; ndp->advertisePrefix[0] = 0xfd; ndp->prefixLen = AddressCalc_ADDRESS_PREFIX_BITS; struct Sockaddr_storage ss; Assert_true(!Sockaddr_parse("fd00::1", &ss)); NetDev_addAddress(ifName, &ss.addr, AddressCalc_ADDRESS_PREFIX_BITS, logger, NULL); Timeout_setTimeout(fail, alloc, 10000, base, alloc); EventBase_beginLoop(base); printf("Test ended\n"); return 0; }
int main() { struct Allocator* alloc = MallocAllocator_new(1<<22); struct Random* rand = Random_new(alloc, NULL, NULL); uint8_t ip[16]; uint8_t printedIp[40]; uint8_t printedShortIp[40]; uint8_t ipFromFull[16]; uint8_t ipFromShort[16]; for (int i = 0; i < 1024; ++i) { Random_bytes(rand, ip, 16); AddrTools_printIp(printedIp, ip); AddrTools_printShortIp(printedShortIp, ip); printf("%s\n%s\n\n", printedIp, printedShortIp); AddrTools_parseIp(ipFromFull, printedIp); AddrTools_parseIp(ipFromShort, printedShortIp); Assert_true(0 == Bits_memcmp(ip, ipFromFull, 16)); Assert_true(0 == Bits_memcmp(ipFromFull, ipFromShort, 16)); } return 0; }
int main() { struct Allocator* alloc = MallocAllocator_new(1<<22); struct Random* rand = Random_new(alloc, NULL, NULL); struct Log* log = FileWriterLog_new(stdout, alloc); uint8_t ip[16]; uint8_t printedIp[40]; uint8_t printedShortIp[40]; uint8_t ipFromFull[16]; uint8_t ipFromShort[16]; for (int i = 0; i < 1024; ++i) { Random_bytes(rand, ip, 16); for (int j = 0; j < 16; j++) { // make the random result have lots of zeros since that's what we're looking for. ip[j] = (ip[j] % 2) ? 0 : ip[j]; } AddrTools_printIp(printedIp, ip); AddrTools_printShortIp(printedShortIp, ip); //printf("%s\n%s\n\n", printedIp, printedShortIp); AddrTools_parseIp(ipFromFull, printedIp); AddrTools_parseIp(ipFromShort, printedShortIp); Log_debug(log, "print/parse %s", printedIp); Assert_true(0 == Bits_memcmp(ip, ipFromFull, 16)); Assert_true(0 == Bits_memcmp(ipFromFull, ipFromShort, 16)); } Allocator_free(alloc); return 0; }
int main(int argc, char** argv) { struct Allocator* alloc = MallocAllocator_new(1<<20); struct EventBase* base = EventBase_new(alloc); struct Log* log = FileWriterLog_new(stdout, alloc); struct Sockaddr* addrA = Sockaddr_fromBytes(TUNTools_testIP6AddrA, Sockaddr_AF_INET6, alloc); struct Sockaddr* addrB = Sockaddr_fromBytes(TUNTools_testIP6AddrB, Sockaddr_AF_INET6, alloc); char assignedIfName[TUNInterface_IFNAMSIZ]; struct Iface* tap = TUNInterface_new(NULL, assignedIfName, 1, base, log, NULL, alloc); struct TAPWrapper* tapWrapper = TAPWrapper_new(tap, log, alloc); // Now setup the NDP server so the tun will work correctly. struct NDPServer* ndp = NDPServer_new(&tapWrapper->internal, log, TAPWrapper_LOCAL_MAC, alloc); struct ARPServer* arp = ARPServer_new(&ndp->internal, log, TAPWrapper_LOCAL_MAC, alloc); addrA->flags |= Sockaddr_flags_PREFIX; addrA->prefix = 126; NetDev_addAddress(assignedIfName, addrA, log, NULL); TUNTools_echoTest(addrA, addrB, TUNTools_genericIP6Echo, &arp->internal, base, log, alloc); Allocator_free(alloc); return 0; }
int main() { struct Allocator* alloc = MallocAllocator_new(1048576); struct Log* logger = FileWriterLog_new(stdout, alloc); struct Random* rand = Random_new(alloc, logger, NULL); uint8_t curve25519private[32]; Random_bytes(rand, curve25519private, 32); uint8_t curve25519public[32]; crypto_scalarmult_curve25519_base(curve25519public, curve25519private); uint8_t signingKeyPair[64]; Sign_signingKeyPairFromCurve25519(signingKeyPair, curve25519private); struct Message* msg = Message_new(0, 512, alloc); Message_push(msg, "hello world", 12, NULL); Sign_signMsg(signingKeyPair, msg, rand); uint8_t curve25519publicB[32]; Assert_true(!Sign_verifyMsg(&signingKeyPair[32], msg)); Assert_true(!Sign_publicSigningKeyToCurve25519(curve25519publicB, &signingKeyPair[32])); Assert_true(!Bits_memcmp(curve25519publicB, curve25519public, 32)); Allocator_free(alloc); return 0; }
int main(int argc, char** argv) { if (isatty(STDIN_FILENO)) { printf("Usage: %s < cjdroute.conf > compliant.json\n", argv[0]); printf("Cjdns accepts configuration which is not valid json but only outputs json\n" "which is valid. This tool deserialies and reserialized a conf file or other " "json file.\n"); printf("In honor of thefinn93, thanks for all of your hard work helping people.\n"); return 0; } struct Allocator* allocator = MallocAllocator_new(1<<20); struct Reader* stdinReader = FileReader_new(stdin, allocator); Dict config; if (JsonBencSerializer_get()->parseDictionary(stdinReader, allocator, &config)) { fprintf(stderr, "Failed to parse configuration.\n"); return -1; } struct Writer* stdoutWriter = FileWriter_new(stdout, allocator); JsonBencSerializer_get()->serializeDictionary(stdoutWriter, &config); printf("\n"); Allocator_free(allocator); }
struct AdminTestFramework* AdminTestFramework_setUp() { struct Allocator* alloc = MallocAllocator_new(1<<20); struct Writer* logwriter = FileWriter_new(stdout, alloc); Assert_always(logwriter); struct Log* logger = alloc->clone(sizeof(struct Log), alloc, &(struct Log) { .writer = logwriter });
static void fromName() { struct Allocator* alloc = MallocAllocator_new(20000); Sockaddr_fromName("localhost", alloc); // This will fail in some cases (eg dns hijacking) //Assert_always(!Sockaddr_fromName("hasjklgyolgbvlbiogi", alloc)); Allocator_free(alloc); }
struct AdminTestFramework* AdminTestFramework_setUp(int argc, char** argv, char* testName) { if (argc > 2 && !strcmp(testName, argv[1]) && !strcmp("angel", argv[2])) { exit(AngelInit_main(argc-1, &argv[1])); } struct Allocator* alloc = MallocAllocator_new(1<<20); struct Writer* logwriter = FileWriter_new(stdout, alloc); Assert_true(logwriter); struct Log* logger = WriterLog_new(logwriter, alloc); struct EventBase* eventBase = EventBase_new(alloc); struct Random* rand = Random_new(alloc, logger, NULL); char asClientPipeName[32] = {0}; Random_base32(rand, (uint8_t*)asClientPipeName, 31); struct Pipe* asClientPipe = Pipe_named(asClientPipeName, eventBase, NULL, alloc); asClientPipe->logger = logger; char asCorePipeName[32] = {0}; Random_base32(rand, (uint8_t*)asCorePipeName, 31); struct Pipe* asCorePipe = Pipe_named(asCorePipeName, eventBase, NULL, alloc); asCorePipe->logger = logger; struct Interface* asCoreIface = FramingInterface_new(65535, &asCorePipe->iface, alloc); spawnAngel(testName, asClientPipeName, eventBase, alloc); Log_info(logger, "Initializing Angel"); initAngel(asClientPipe, asCoreIface, (char*)asCorePipe->name, eventBase, logger, alloc, rand); struct Sockaddr_storage addr; Assert_true(!Sockaddr_parse("127.0.0.1", &addr)); Log_info(logger, "Binding UDP admin socket"); struct AddrInterface* udpAdmin = UDPAddrInterface_new(eventBase, &addr.addr, alloc, NULL, logger); String* password = String_new("abcd", alloc); struct Admin* admin = Admin_new(udpAdmin, alloc, logger, eventBase, password); // Now setup the client. struct AdminClient* client = AdminClient_new(udpAdmin->addr, password, eventBase, logger, alloc); Assert_true(client); return Allocator_clone(alloc, (&(struct AdminTestFramework) { .admin = admin, .client = client, .alloc = alloc, .eventBase = eventBase, .logger = logger, .addr = Sockaddr_clone(udpAdmin->addr, alloc), .angelInterface = asCoreIface }));
int main() { struct Allocator* alloc = MallocAllocator_new(20000000); struct Random* rand = Random_new(alloc, NULL, NULL); packUnpack(rand); return 0; }
static int benchmark() { struct Allocator* alloc = MallocAllocator_new(1<<22); struct EventBase* base = EventBase_new(alloc); struct Writer* logWriter = FileWriter_new(stdout, alloc); struct Log* logger = WriterLog_new(logWriter, alloc); CryptoAuth_benchmark(base, logger, alloc); return 0; }
static int benchmark() { struct Allocator* alloc = MallocAllocator_new(1<<22); struct event_base* base = event_base_new(); struct Writer* logwriter = FileWriter_new(stdout, alloc); struct Log logger = { .writer = logwriter }; CryptoAuth_benchmark(base, &logger, alloc); return 0; }
int testInputHandler() { struct DHTMessage theMessage; struct Context context = { .theMessage = &theMessage, .ret = -1 }; struct Context context2 = { .theMessage = &theMessage, .ret = -1 }; struct DHTModule module = { .name = "TestModule", .context = &context, .handleIncoming = handleIncoming }; struct DHTModule module2 = { .name = "TestModule2", .context = &context2, .handleIncoming = handleIncoming }; struct Allocator* allocator = MallocAllocator_new(2048); struct DHTModuleRegistry* reg = DHTModules_new(allocator); DHTModules_register(&module, reg); DHTModules_register(&module2, reg); DHTModules_handleIncoming(&theMessage, reg); /* This should be ignored. */ DHTModules_handleOutgoing(&theMessage, reg); if (context.ret == -1) { printf("message not received"); } else if (context.ret == -2){ printf("wrong message received"); } else if (context2.ret == -1) { printf("message not received by all handlers."); } else if (context2.ret == -2) { printf("wrong message received by second handler."); } else { return 0; } return -1; } int main() { return testInputHandler(); }
int main() { struct Allocator* alloc = MallocAllocator_new(1<<20); struct Random* rand = Random_new(alloc, NULL, NULL); // mock interface controller. struct Context ctx = { .ic = { .registerPeer = registerPeer, .getPeerState = getPeerState } }; struct Interface externalIf = { .sendMessage = sendMessage, .allocator = alloc, .senderContext = &ctx }; /*struct MultiInterface* mif = */MultiInterface_new(KEY_SIZE, &externalIf, &ctx.ic); struct Entry* entries = Allocator_malloc(alloc, sizeof(struct Entry) * ENTRY_COUNT); Random_bytes(rand, (uint8_t*)entries, ENTRY_COUNT * sizeof(struct Entry)); struct Interface** ifaces = Allocator_calloc(alloc, sizeof(char*), ENTRY_COUNT); // seed the list with some near collisions. for (int i = 0; i < 10; i++) { int rnd = (((uint32_t*)entries)[i] >> 1) % ENTRY_COUNT; ((uint32_t*) (&entries[rnd]))[0] = ((uint32_t*) (&entries[i]))[0]; } for (int i = 0; i < CYCLES; i++) { int rnd = ((uint32_t*)entries)[i] % ENTRY_COUNT; struct Entry* entry = &entries[rnd]; struct Interface* iface = ifaces[rnd]; struct Message* msg; Message_STACK(msg, 0, 128); Message_push(msg, "hello world", 12); Message_push(msg, entry, 16); externalIf.receiveMessage(msg, &externalIf); //printf("Received message for iface [%u] from [%p]\n", rnd, (void*)ctx.receivedOn); if (iface) { Assert_always(ctx.receivedOn == iface); } else { ifaces[rnd] = ctx.receivedOn; } } Allocator_free(alloc); }
static void expectConvert(char* address, char* expectedOutput) { struct Sockaddr_storage ss; Assert_always(!Sockaddr_parse(address, &ss)); struct Allocator* alloc = MallocAllocator_new(20000); char* outAddr = Sockaddr_print(&ss.addr, alloc); Assert_always(outAddr); Assert_always(strlen(outAddr) == strlen(expectedOutput)); Assert_always(!strcmp(outAddr, expectedOutput)); Allocator_free(alloc); }
int main(int argc, char** argv) { struct Allocator* alloc = MallocAllocator_new(1<<20); struct EventBase* base = EventBase_new(alloc); struct Writer* logWriter = FileWriter_new(stdout, alloc); struct Log* logger = WriterLog_new(logWriter, alloc); struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET6, alloc); char assignedIfName[TUNInterface_IFNAMSIZ]; struct Interface* tun = TUNInterface_new(NULL, assignedIfName, base, logger, NULL, alloc); NetDev_addAddress(assignedIfName, addrA, 126, logger, NULL); struct Sockaddr_storage addr; Assert_always(!Sockaddr_parse("[fd00::1]", &addr)); #ifdef freebsd // tun is not setup synchronously in bsd but it lets you bind to the tun's // address anyway. sleep(1); #endif // Mac OSX and BSD do not set up their TUN devices synchronously. // We'll just keep on trying until this works. struct AddrInterface* udp = NULL; for (int i = 0; i < 20; i++) { if ((udp = setupUDP(base, &addr.addr, alloc, logger))) { break; } } Assert_always(udp); struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc); uint8_t* addrBytes; Assert_always(16 == Sockaddr_getAddress(dest, &addrBytes)); Bits_memcpy(addrBytes, testAddrB, 16); struct Message* msg; Message_STACK(msg, 0, 64); Message_push(msg, "Hello World", 12, NULL); Message_push(msg, dest, dest->addrLen, NULL); udp->generic.receiveMessage = receiveMessageUDP; udp->generic.receiverContext = alloc; tun->receiveMessage = receiveMessageTUN; udp->generic.sendMessage(msg, &udp->generic); Timeout_setTimeout(fail, NULL, 10000, base, alloc); EventBase_beginLoop(base); return 0; }
/** Check if nodes A and C can communicate via B without A knowing that C exists. */ int main() { struct Allocator* alloc = MallocAllocator_new(1<<22); struct Writer* logwriter = FileWriter_new(stdout, alloc); struct Log* logger = WriterLog_new(logwriter, alloc); struct Random* rand = Random_new(alloc, logger, NULL); struct EventBase* base = EventBase_new(alloc); start(alloc, logger, base, rand, runTest); EventBase_beginLoop(base); Allocator_free(alloc); return 0; }
/** Check if nodes A and C can communicate via B without A knowing that C exists. */ void Benchmark_runAll() { struct Allocator* alloc = MallocAllocator_new(1<<22); struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1); Identity_set(ctx); ctx->alloc = alloc; ctx->base = EventBase_new(alloc); struct Log* log = ctx->log = FileWriterLog_new(stdout, alloc); ctx->rand = Random_new(alloc, log, NULL); cryptoAuth(ctx); switching(ctx); }
int main() { char out[512]; struct Allocator* alloc = CanaryAllocator_new(MallocAllocator_new(1<<20), NULL); struct Writer* writer = ArrayWriter_new(out, 512, alloc); struct Reader* reader = ArrayReader_new(out, 512, alloc); testSerialize(writer, reader); testParse(writer, reader, alloc); Allocator_free(alloc); }
static void encryptRndNonceTest() { uint8_t buff[44]; Bits_memset(buff, 0, 44); uint8_t nonce[24]; Bits_memset(nonce, 0, 24); uint8_t secret[32]; Bits_memset(secret, 0, 32); struct Message m = { .bytes=&buff[32], .length=12, .padding=32}; CString_strcpy((char*) m.bytes, "hello world"); CryptoAuth_encryptRndNonce(nonce, &m, secret); uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc"; uint8_t output[57]; Hex_encode(output, 57, m.bytes, m.length); //printf("\n%s\n%s\n", (char*) expected, (char*) output); Assert_true(!Bits_memcmp(expected, output, 56)); Assert_true(!CryptoAuth_decryptRndNonce(nonce, &m, secret)); Assert_true(m.length == 12 && !Bits_memcmp(m.bytes, "hello world", m.length)); } static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger) { struct RandomSeed* evilSeed = DeterminentRandomSeed_new(alloc); return Random_newWithSeed(alloc, logger, evilSeed, NULL); } static void createNew() { struct Allocator* allocator = MallocAllocator_new(BUFFER_SIZE); struct CryptoAuth* ca = CryptoAuth_new(allocator, privateKey, eventBase, NULL, evilRandom(allocator, NULL)); /*for (int i = 0; i < 32; i++) { printf("%.2x", ca->publicKey[i]); }*/ Assert_true(Bits_memcmp(ca->publicKey, publicKey, 32) == 0); Allocator_free(allocator); } static uint8_t receiveMessage(struct Message* message, struct Interface* iface) { Message_pop(message, NULL, 4, NULL); *((struct Message**)iface->receiverContext) = message; return Error_NONE; }
static struct CryptoAuth_Wrapper* setUp(uint8_t* myPrivateKey, uint8_t* herPublicKey, uint8_t* authPassword, struct Message** resultMessage) { struct Allocator* allocator = MallocAllocator_new(8192*2); struct Writer* writer = FileWriter_new(stdout, allocator); struct Log* logger = WriterLog_new(writer, allocator); struct CryptoAuth* ca = CryptoAuth_new(allocator, myPrivateKey, eventBase, logger, evilRandom(allocator, logger)); struct Interface* iface = Allocator_clone(allocator, (&(struct Interface) { .sendMessage = sendMessage, .senderContext = resultMessage }));
struct CryptoAuth_Wrapper* setUp(uint8_t* myPrivateKey, uint8_t* herPublicKey, uint8_t* authPassword, struct Message** resultMessage) { struct Allocator* allocator = MallocAllocator_new(8192*2); struct Log* logger = allocator->malloc(sizeof(struct Log), allocator); logger->writer = FileWriter_new(stdout, allocator); struct CryptoAuth* ca = CryptoAuth_new(NULL, allocator, myPrivateKey, eventBase, logger); struct Interface* iface = allocator->clone(sizeof(struct Interface), allocator, &(struct Interface) { .sendMessage = sendMessage, .senderContext = resultMessage });
static int parseEmptyList() { char* test = "d" "2:hi" "le" "e"; struct Allocator* alloc = MallocAllocator_new(1<<20); struct Reader* reader = ArrayReader_new(test, CString_strlen(test), alloc); Dict d; int ret = StandardBencSerializer_get()->parseDictionary(reader, alloc, &d); char out[256]; struct Writer* w = ArrayWriter_new(out, 256, alloc); ret |= StandardBencSerializer_get()->serializeDictionary(w, &d); ret |= Bits_memcmp(test, out, CString_strlen(test)); Allocator_free(alloc); return ret; }
static void initAppState(struct AppState* state) { state->allocator = MallocAllocator_new(64 * 1024); if (state->allocator == NULL) { fprintf(stderr, "Failed to create allocator\n"); abort(); } state->map = AddressMapper_new(state->allocator); if (state->map == NULL) { fprintf(stderr, "Failed to create address map\n"); abort(); } }
int main() { struct Allocator* alloc = MallocAllocator_new(1<<20); struct RandomSeed* seed = DeterminentRandomSeed_new(alloc, NULL); struct Writer* w = FileWriter_new(stdout, alloc); struct Log* logger = WriterLog_new(w, alloc); struct Random* rand = Random_newWithSeed(alloc, logger, seed, NULL); // < 8 length checkBytes(rand, 0, 3); checkBytes(rand, 3, 6); // unaligned beginning (length and align add up to a multiple of 8) checkBytes(rand, 3, 29); // 1 byte aligned checkBytes(rand, 6, 10); // 2 byte aligned checkBytes(rand, 4, 20); // 4 byte aligned // unaligned end checkBytes(rand, 0, 9); // 1 byte checkBytes(rand, 0, 14); // 2 byte checkBytes(rand, 0, 20); // 4 byte // both unaligned checkBytes(rand, 1, 28); // 1,1 byte aligned checkBytes(rand, 3, 19); // 1,2 byte aligned checkBytes(rand, 5, 23); // 1,4 byte aligned checkBytes(rand, 2, 9); // 2,1 byte aligned checkBytes(rand, 6, 12); // 2,2 byte aligned checkBytes(rand, 6, 14); // 2,4 byte aligned checkBytes(rand, 4, 19); // 4,1 byte aligned checkBytes(rand, 4, 10); // 4,2 byte aligned checkBytes(rand, 4, 32); // 4,4 byte aligned test179(alloc, logger); /* torture uint8_t selections[2]; for (int i = 0; i < 100000; i++) { Random_bytes(rand, selections, 2); checkBytes(rand, selections[0] % 8, selections[1] % 128); }*/ Allocator_free(alloc); return 0; }