Added testing framework

master
John Jones 2017-02-20 15:58:19 -05:00
parent fcb0cce437
commit 4e281ddd08
4 changed files with 138 additions and 41 deletions

View File

@ -48,7 +48,7 @@ struct MultiAddress* multiaddress_new_from_string(const char* straddress); //Con
void multiaddress_free(struct MultiAddress* in);
int multiaddress_copy(const struct MultiAddress* source, struct MultiAddress* destination);
struct MultiAddress* multiaddress_copy(const struct MultiAddress* source);
int multiaddress_encapsulate(struct MultiAddress * result, char * string);

View File

@ -98,23 +98,33 @@ void multiaddress_free(struct MultiAddress* in) {
* @param out the destination. NOTE: memory for out should be preallocated
* @returns true(1) on success, otherwise false(0)
*/
int multiaddress_copy(const struct MultiAddress* in, struct MultiAddress* out) {
if (in != NULL && out != NULL) {
// memory allocation
out->bytes = malloc(in->bsize);
if (out->bytes != NULL) {
out->string = malloc(strlen(in->string) + 1);
if (out->string != NULL) {
// copy
struct MultiAddress* multiaddress_copy(const struct MultiAddress* in) {
struct MultiAddress* out = NULL;
if (in != NULL) {
out = (struct MultiAddress*)malloc(sizeof(struct MultiAddress));
if (out != NULL) {
if (in->bsize > 0) {
out->bytes = malloc(in->bsize);
if (out->bytes == NULL) {
free(out);
return NULL;
}
out->bsize = in->bsize;
memcpy(out->bytes, in->bytes, out->bsize);
} // bytes need to be copied
if (in->string != NULL) {
out->string = malloc(strlen(in->string) + 1);
if (out->string == NULL) {
if (out->bsize > 0)
free(out->bytes);
free(out);
return NULL;
}
strcpy(out->string, in->string);
return 1;
} // string allocated
free(out->bytes);
} // bytes allocated
} // string needs to be copied
} // structure allocated
} // good parameters
return 0;
return out;
}
/**

48
test_multiaddr.h Normal file
View File

@ -0,0 +1,48 @@
#pragma once
#include "multiaddr/multiaddr.h"
int test_new_from_string() {
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
for(int i = 0; i < a->bsize; i++) {
printf("%02x ", a->bytes[i]);
}
printf(" End of bytes\n");
multiaddress_free(a);
return 1;
}
int test_full() {
char addrstr[100];
strcpy(addrstr,"/ip4/192.168.1.1/tcp/8080/");
printf("INITIAL: %s\n",addrstr);
struct MultiAddress* a;
a= multiaddress_new_from_string(addrstr);
printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes));
//Remember, Decapsulation happens from right to left, never in reverse!
printf("A STRING:%s\n",a->string);
multiaddress_encapsulate(a,"/ip4/192.131.200.111/udp/3333/");
printf("A STRING ENCAPSULATED:%s\n",a->string);
multiaddress_decapsulate(a,"udp");
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
multiaddress_encapsulate(a,"/tcp/8080");
printf("A STRING ENCAPSULATED TCP:%s\n",a->string);
struct MultiAddress* beta;
beta = multiaddress_new_from_bytes(a->bytes,a->bsize);
printf("B STRING: %s\n",beta->string);
multiaddress_free(a);
multiaddress_free(beta);
return 1;
}
int test_hex_to_val() {
return 0;
}

View File

@ -1,31 +1,70 @@
#include "multiaddr/multiaddr.h"
//ADD TO PROTOUTILS.H
#include <stdio.h>
int main()
{
char addrstr[100];
strcpy(addrstr,"/ip4/192.168.1.1/tcp/8080/");
printf("INITIAL: %s\n",addrstr);
struct MultiAddress* a;
a= multiaddress_new_from_string(addrstr);
printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes));
//Remember, Decapsulation happens from right to left, never in reverse!
printf("A STRING:%s\n",a->string);
multiaddress_encapsulate(a,"/ip4/192.131.200.111/udp/3333/");
printf("A STRING ENCAPSULATED:%s\n",a->string);
multiaddress_decapsulate(a,"udp");
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
multiaddress_encapsulate(a,"/tcp/8080");
printf("A STRING ENCAPSULATED TCP:%s\n",a->string);
#include "test_multiaddr.h"
struct MultiAddress* beta;
beta = multiaddress_new_from_bytes(a->bytes,a->bsize);
printf("B STRING: %s\n",beta->string);
const char* names[] = {
"test_new_from_string",
"test_full"
};
multiaddress_free(a);
multiaddress_free(beta);
int (*funcs[])(void) = {
test_new_from_string,
test_full
};
int testit(const char* name, int (*func)(void)) {
printf("Testing %s...\n", name);
int retVal = func();
if (retVal)
printf("%s success!\n", name);
else
printf("** Uh oh! %s failed.**\n", name);
return retVal;
}
int main(int argc, char** argv) {
int counter = 0;
int tests_ran = 0;
char* test_wanted;
int only_one = 0;
if(argc > 1) {
only_one = 1;
if (argv[1][0] == '\'') { // some shells put quotes around arguments
argv[1][strlen(argv[1])-1] = 0;
test_wanted = &(argv[1][1]);
}
else
test_wanted = argv[1];
}
int array_length = sizeof(funcs) / sizeof(funcs[0]);
int array2_length = sizeof(names) / sizeof(names[0]);
if (array_length != array2_length) {
printf("Test arrays are not of the same length. Funcs: %d, Names: %d\n", array_length, array2_length);
}
for (int i = 0; i < array_length; i++) {
if (only_one) {
const char* currName = names[i];
if (strcmp(currName, test_wanted) == 0) {
tests_ran++;
counter += testit(names[i], funcs[i]);
}
}
else
if (!only_one) {
tests_ran++;
counter += testit(names[i], funcs[i]);
}
}
if (tests_ran == 0)
printf("***** No tests found *****\n");
else {
if (tests_ran - counter > 0) {
printf("***** There were %d failed test(s) (%d successful) *****\n", tests_ran - counter, counter);
} else {
printf("All %d tests passed\n", tests_ran);
}
}
return 1;
}