From aaea686fedfba7efe24da024c2856d370d9f9de5 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 14 Nov 2011 22:55:29 +0100 Subject: [PATCH] some smaller fixes as well as a complete test create_token implementation. --- bigpoint_dyntype.c | 2 +- bigpoint_hash.c | 8 ++-- bigpoint_packet.c | 22 +++++----- bigpoint_packet.h | 4 +- createToken.c | 99 +++++++++++++++++++++++++++++++++++++++++++ tests/packet_test.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 218 insertions(+), 17 deletions(-) create mode 100644 createToken.c create mode 100644 tests/packet_test.c diff --git a/bigpoint_dyntype.c b/bigpoint_dyntype.c index c262fa1..ba7b13e 100644 --- a/bigpoint_dyntype.c +++ b/bigpoint_dyntype.c @@ -25,7 +25,7 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params) break; case BIGPOINT_DYNTYPE_HASH: - (_this->data)._hash = new(BIGPOINT_HASH); + (_this->data)._hash = va_arg(* params, struct BIGPOINT_HASH *); break; default: diff --git a/bigpoint_hash.c b/bigpoint_hash.c index 254a23a..fa1d778 100644 --- a/bigpoint_hash.c +++ b/bigpoint_hash.c @@ -85,15 +85,15 @@ _updateHashSize(struct BIGPOINT_HASH * _this) _this->keys = realloc(_this->keys, sizeof(char*) * _this->size); memset(_this->keys + (_this->used * sizeof(char*)), - HASH_ENTRY_CHUNK_SIZE * sizeof(char*), - 0); + 0, + HASH_ENTRY_CHUNK_SIZE * sizeof(char*)); _this->values = realloc( _this->values, sizeof(struct BIGPOINT_DYNTYPE *) * _this->size); memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)), - HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *), - 0); + 0, + HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *)); } } diff --git a/bigpoint_packet.c b/bigpoint_packet.c index 7fb0d99..eba2957 100644 --- a/bigpoint_packet.c +++ b/bigpoint_packet.c @@ -1,13 +1,13 @@ #include -#include "bigpoint_container_packet.h" +#include "bigpoint_packet.h" static void __construct(struct BIGPOINT_PACKET * _this, va_list * params) { - bigpoint_container_packet_set_default_content(_this); + bigpoint_packet_set_default_content(_this); } static @@ -53,38 +53,40 @@ __toJson(struct BIGPOINT_PACKET * _this) } static const -struct BIGPOINT_CLASS _bigpoint_packet = { +struct BIGPOINT_CCLASS _bigpoint_packet = { sizeof(struct BIGPOINT_PACKET), (ctor)__construct, - __jsonConst, + (jCtor)__jsonConst, (dtor)__destruct, - __toJson + (jTo)__toJson }; const struct BIGPOINT_CCLASS * const BIGPOINT_PACKET = &_bigpoint_packet; -void * +struct BIGPOINT_DYNTYPE * bigpoint_packet_getHeader( struct BIGPOINT_PACKET * _this) { return _this->content[BIGPOINT_PACKET_HEADER]; } -void * +struct BIGPOINT_DYNTYPE * bigpoint_packet_getData( struct BIGPOINT_PACKET * _this) { return _this->content[BIGPOINT_PACKET_DATA]; } -void bigpoint_packet_setHeader( +void +bigpoint_packet_setHeader( struct BIGPOINT_PACKET * _this, struct BIGPOINT_DYNTYPE * header) { _this->content[BIGPOINT_PACKET_HEADER] = header; } -void bigpoint_packet_setData( +void +bigpoint_packet_setData( struct BIGPOINT_PACKET * _this, struct BIGPOINT_DYNTYPE * data) { @@ -92,7 +94,7 @@ void bigpoint_packet_setData( } void -bigpoint_container_packet_set_default_content( +bigpoint_packet_set_default_content( struct BIGPOINT_PACKET * _this) { _this->content[BIGPOINT_PACKET_HEADER] = NULL; diff --git a/bigpoint_packet.h b/bigpoint_packet.h index 14d6284..5833e99 100644 --- a/bigpoint_packet.h +++ b/bigpoint_packet.h @@ -17,10 +17,10 @@ struct BIGPOINT_PACKET { extern const struct BIGPOINT_CCLASS * const BIGPOINT_PACKET; -void * bigpoint_packet_getHeader( +struct BIGPOINT_DYNTYPE * bigpoint_packet_getHeader( struct BIGPOINT_PACKET * _this); -void * bigpoint_packet_getData( +struct BIGPOINT_DYNTYPE * bigpoint_packet_getData( struct BIGPOINT_PACKET * _this); void bigpoint_packet_setHeader( diff --git a/createToken.c b/createToken.c new file mode 100644 index 0000000..46799c7 --- /dev/null +++ b/createToken.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include + +#include "bigpoint_cclass.h" +#include "bigpoint_packet.h" +#include "bigpoint_dyntype.h" +#include "bigpoint_hash.h" +#include "bigpoint_crypt.h" +#include "base64.h" + + +void +setHashString(struct BIGPOINT_HASH * hash, const char * key, const char * value) +{ + struct BIGPOINT_DYNTYPE * dyn; + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_STRING, strlen(value), value); + bigpoint_hash_set(hash, key, dyn); +} + +void +setHashInt(struct BIGPOINT_HASH * hash, const char * key, const int value) +{ + struct BIGPOINT_DYNTYPE * dyn; + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), value); + bigpoint_hash_set(hash, key, dyn); +} + +int +main(int argc, char * argv[]) +{ + struct BIGPOINT_CRYPT * crypt; + struct BIGPOINT_PACKET * packet; + struct BIGPOINT_HASH * data; + struct json_object * json; + const char * json_str; + char * encrypted; + char * b64d; + char pass[] = "1234"; + size_t length; + + packet = new(BIGPOINT_PACKET); + + bigpoint_packet_setHeader( + packet, + new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), time(NULL))); + + data = new(BIGPOINT_HASH); + + setHashString(data, "#C#", "this comes from C"); + setHashString(data, "usr", "ppohg"); + setHashString(data, "pas", "yyyyy"); + setHashInt(data, "val", 321); + + bigpoint_packet_setData( + packet, + new(BIGPOINT_DYNTYPE, + BIGPOINT_DYNTYPE_HASH, + sizeof(struct BIGPOINT_HASH *), + data)); + + json = toJson(packet); + json_str = json_object_to_json_string(json); + length = strlen(json_str); + + crypt = new(BIGPOINT_CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB); + encrypted = bigpoint_crypt_encrypt(crypt, json_str, pass, &length); + + delete(crypt); + json_object_put(json); + + b64d = calloc(BASE64_LENGTH(length), sizeof(char)); + base64_encode(encrypted, length, b64d, BASE64_LENGTH(length)); + free(encrypted); + + b64d = realloc(b64d, BASE64_LENGTH(length) + 1); + b64d[BASE64_LENGTH(length)] = '\0'; + + printf("%s\n", b64d); + free(b64d); + + delete(bigpoint_hash_get(data, "#C#")); + delete(bigpoint_hash_get(data, "usr")); + delete(bigpoint_hash_get(data, "pas")); + delete(bigpoint_hash_get(data, "val")); + delete(bigpoint_packet_getHeader(packet)); + delete(bigpoint_packet_getData(packet)); + delete(packet); + + + return 0; +} + +// vim: set et ts=4 sw=4: diff --git a/tests/packet_test.c b/tests/packet_test.c new file mode 100644 index 0000000..32b628a --- /dev/null +++ b/tests/packet_test.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + +#include "../bigpoint_cclass.h" +#include "../bigpoint_packet.h" +#include "../bigpoint_dyntype.h" +#include "../bigpoint_hash.h" + + +void +setHashString(struct BIGPOINT_HASH * hash, const char * key, const char * value) +{ + struct BIGPOINT_DYNTYPE * dyn; + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_STRING, strlen(value), value); + bigpoint_hash_set(hash, key, dyn); +} + +void +setHashInt(struct BIGPOINT_HASH * hash, const char * key, const int value) +{ + struct BIGPOINT_DYNTYPE * dyn; + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), value); + bigpoint_hash_set(hash, key, dyn); +} + +int +main(int argc, char * argv[]) +{ + struct BIGPOINT_PACKET * packet; + struct BIGPOINT_HASH * data; + struct json_object * json; + char * json_str; + + packet = new(BIGPOINT_PACKET); + + bigpoint_packet_setHeader( + packet, + new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), time(NULL))); + + data = new(BIGPOINT_HASH); + + setHashString(data, "#C#", "this comes from C"); + setHashString(data, "usr", "ppohg"); + setHashString(data, "pas", "yyyyy"); + setHashInt(data, "val", 321); + + bigpoint_packet_setData( + packet, + new(BIGPOINT_DYNTYPE, + BIGPOINT_DYNTYPE_HASH, + sizeof(struct BIGPOINT_HASH *), + data)); + + json = toJson(packet); + + json_str = calloc( + strlen(json_object_to_json_string(json)) + 1, + sizeof(char)); + memcpy(json_str, + json_object_to_json_string(json), + strlen(json_object_to_json_string(json))); + + printf("%s\n", json_str); + + delete(bigpoint_hash_get(data, "#C#")); + delete(bigpoint_hash_get(data, "usr")); + delete(bigpoint_hash_get(data, "pas")); + delete(bigpoint_hash_get(data, "val")); + delete(bigpoint_packet_getHeader(packet)); + delete(bigpoint_packet_getData(packet)); + delete(packet); + + json_object_put(json); + + json = json_tokener_parse(json_str); + packet = newFromJson(BIGPOINT_PACKET, json); + + printf("%s\n", json_object_to_json_string(json)); + + data = (bigpoint_packet_getData(packet)->data)._hash; + delete(bigpoint_hash_get(data, "#C#")); + delete(bigpoint_hash_get(data, "usr")); + delete(bigpoint_hash_get(data, "pas")); + delete(bigpoint_hash_get(data, "val")); + delete(bigpoint_packet_getHeader(packet)); + delete(bigpoint_packet_getData(packet)); + delete(packet); + free(json_str); + + json_object_put(json); + + return 0; +} + +// vim: set et ts=4 sw=4: