Commit c2bc1b06597b19dc0ecb8f1e733f724325d54951

Authored by Georg Hopp
1 parent ffe14cc4

the new testing framework works and the tests for cclass are done...actually i t…

…ry to figure out how to tell autotools to build this correctly.
... ... @@ -10,7 +10,7 @@
10 10
11 11
12 12 void
13   -setHashString(struct DYNTYPE_HASH * hash, const char * key, const char * value)
  13 +setHashString(struct DYNTYPE_HASH * hash, const char * const key, const char * value)
14 14 {
15 15 struct DYNTYPE * dyn;
16 16
... ... @@ -19,7 +19,7 @@ setHashString(struct DYNTYPE_HASH * hash, const char * key, const char * value)
19 19 }
20 20
21 21 void
22   -setHashInt(struct DYNTYPE_HASH * hash, const char * key, const int value)
  22 +setHashInt(struct DYNTYPE_HASH * hash, const char * const key, const int value)
23 23 {
24 24 struct DYNTYPE * dyn;
25 25
... ... @@ -27,12 +27,21 @@ setHashInt(struct DYNTYPE_HASH * hash, const char * key, const int value)
27 27 dyntype_hash_set(hash, key, dyn);
28 28 }
29 29
  30 +void
  31 +deleteHashValue(struct DYNTYPE_HASH * hash, const char * const key)
  32 +{
  33 + struct DYNTYPE * dyn = dyntype_hash_get(hash, key);
  34 +
  35 + delete(&dyn);
  36 +}
  37 +
30 38 int
31 39 main(int argc, char * argv[])
32 40 {
33 41 struct CRYPT * crypt;
34 42 struct PACKET * packet;
35 43 struct DYNTYPE_HASH * data;
  44 + struct DYNTYPE * _clear;
36 45 struct json_object * json;
37 46 const char * json_str;
38 47 char * encrypted;
... ... @@ -67,7 +76,7 @@ main(int argc, char * argv[])
67 76 crypt = new(CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB);
68 77 encrypted = crypt_encrypt(crypt, json_str, pass, &length);
69 78
70   - delete(crypt);
  79 + delete(&crypt);
71 80 json_object_put(json);
72 81
73 82 b64d = calloc(BASE64_LENGTH(length), sizeof(char));
... ... @@ -80,13 +89,15 @@ main(int argc, char * argv[])
80 89 printf("%s\n", b64d);
81 90 free(b64d);
82 91
83   - delete(dyntype_hash_get(data, "#C#"));
84   - delete(dyntype_hash_get(data, "usr"));
85   - delete(dyntype_hash_get(data, "pas"));
86   - delete(dyntype_hash_get(data, "val"));
87   - delete(packet_getHeader(packet));
88   - delete(packet_getData(packet));
89   - delete(packet);
  92 + deleteHashValue(data, "#C#");
  93 + deleteHashValue(data, "usr");
  94 + deleteHashValue(data, "pas");
  95 + deleteHashValue(data, "val");
  96 + _clear = packet_getHeader(packet);
  97 + delete(&_clear);
  98 + _clear = packet_getData(packet);
  99 + delete(&_clear);
  100 + delete(&packet);
90 101
91 102
92 103 return 0;
... ...
... ... @@ -60,13 +60,14 @@ newFromJson(const void * _class, struct json_object * json)
60 60 void
61 61 delete(void * _object)
62 62 {
63   - const struct CCLASS ** class = _object;
  63 + const struct CCLASS ** class = *(void**)_object;
64 64
65   - if (_object && *class && (*class)->__destruct) {
66   - (*class)->__destruct(_object);
  65 + if (*(void**)_object && *class && (*class)->__destruct) {
  66 + (*class)->__destruct(*(void**)_object);
67 67 }
68 68
69   - free(_object);
  69 + free(*(void**)_object);
  70 + *(void**)_object = NULL;
70 71 }
71 72
72 73 struct json_object *
... ...
... ... @@ -97,7 +97,7 @@ __destruct(struct DYNTYPE * _this)
97 97 break;
98 98
99 99 case DYNTYPE_TYPE_HASH:
100   - delete((_this->data)._hash);
  100 + delete(&((_this->data)._hash));
101 101 break;
102 102
103 103 default:
... ...
1 1 ACLOCAL_AMFLAGS = -I m4
2 2
3   -TESTS = crypt decrypt dyntype hash packet
4   -check_PROGRAMS = crypt decrypt dyntype hash packet
  3 +TESTS = crypt decrypt dyntype hash packet runtest
  4 +check_PROGRAMS = crypt decrypt dyntype hash packet runtest
  5 +noinst_LTLIBRARIES = cclassTest.la
5 6
6 7 crypt_SOURCES = crypt.c ../base64.c
7 8 crypt_LDADD = ../src/libtoken.la $(LIBOBJS)
... ... @@ -22,3 +23,11 @@ hash_CFLAGS = -I ../include -I ..
22 23 packet_SOURCES = packet.c
23 24 packet_LDADD = ../src/libtoken.la $(LIBOBJS)
24 25 packet_CFLAGS = -I ../include -I ..
  26 +
  27 +cclassTest_la_SOURCES = cclassTest.c
  28 +cclassTest_la_LDFLAGS = -avoid-version -module -shared -export-dynamic
  29 +cclassTest_la_CFLAGS = -nostartfiles -fPIC -I. -I../include
  30 +
  31 +runtest_SOURCES = runtest.c
  32 +runtest_LDADD = ../src/libtoken.la $(LIBOBJS) -ldl
  33 +runtest_CFLAGS = -rdynamic
... ...
  1 +#include <sys/types.h>
  2 +#include <json/json.h>
  3 +
  4 +#include "runtest.h"
  5 +#include "mock/class.h"
  6 +#include "token/cclass.h"
  7 +
  8 +
  9 +char testname[] = "cclassTest";
  10 +struct MOCK_CLASS * mock = NULL;
  11 +
  12 +void
  13 +setUp()
  14 +{
  15 + mock = NULL;
  16 + _reset();
  17 +}
  18 +
  19 +void
  20 +tearDown()
  21 +{
  22 + if (NULL != mock) {
  23 + delete(&mock);
  24 + }
  25 +}
  26 +
  27 +int
  28 +testNew(void)
  29 +{
  30 + mock = new(MOCK_CLASS, 123);
  31 +
  32 + ASSERT_NOT_NULL(mock);
  33 + ASSERT_EQUAL(1, _called);
  34 + ASSERT_EQUAL(123, mock_class_getValue(mock));
  35 +
  36 + return TEST_OK;
  37 +}
  38 +
  39 +int
  40 +testNewFromJson(void)
  41 +{
  42 + struct json_object * json = json_object_new_int(123);
  43 +
  44 + mock = newFromJson(MOCK_CLASS, json);
  45 + json_object_put(json);
  46 +
  47 + ASSERT_NOT_NULL(mock);
  48 + ASSERT_EQUAL(1, _called);
  49 + ASSERT_EQUAL(123, mock_class_getValue(mock));
  50 +
  51 + return TEST_OK;
  52 +}
  53 +
  54 +int
  55 +testDelete(void)
  56 +{
  57 + mock = new(MOCK_CLASS, 123);
  58 +
  59 + ASSERT_NOT_NULL(mock);
  60 +
  61 + _reset();
  62 + delete(&mock);
  63 +
  64 + ASSERT_NULL(mock);
  65 + ASSERT_EQUAL(1, _called);
  66 +
  67 + return TEST_OK;
  68 +}
  69 +
  70 +int
  71 +testToJson(void)
  72 +{
  73 + struct json_object * json = NULL;
  74 + mock = new(MOCK_CLASS, 123);
  75 + int value;
  76 +
  77 + json = toJson(mock);
  78 +
  79 + ASSERT_NOT_NULL(json);
  80 + value = json_object_get_int(json);
  81 + json_object_put(json);
  82 +
  83 + ASSERT_EQUAL(123, value);
  84 + ASSERT_EQUAL(1, _called);
  85 +
  86 + return TEST_OK;
  87 +}
  88 +
  89 +testfunc tests[] = {
  90 + testNew,
  91 + testNewFromJson,
  92 + testDelete,
  93 + testToJson
  94 +};
  95 +size_t count = FUNCS_COUNT(tests);
  96 +
  97 +// vim: set et ts=4 sw=4:
... ...
1   -#include <sys/types.h>
2   -
3   -#include "runtest.h"
4   -
5   -
6   -char testname[] = "cclass_test";
7   -
8   -int
9   -dummy_ok(void)
10   -{
11   - return TEST_OK;
12   -}
13   -
14   -int
15   -dummy_failed(void)
16   -{
17   - return TEST_FAILED;
18   -}
19   -
20   -int
21   -dummy_error(void)
22   -{
23   - return TEST_ERROR;
24   -}
25   -
26   -testfunc tests[] = {
27   - dummy_ok,
28   - dummy_failed,
29   - dummy_error
30   -};
31   -size_t count = FUNCS_COUNT(tests);
32   -
33   -// vim: set et ts=4 sw=4:
... ... @@ -22,7 +22,7 @@ main(int argc, char * argv[])
22 22
23 23 crypt = new(CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB);
24 24 encrypted = crypt_encrypt(crypt, data, pass, &length);
25   - delete(crypt);
  25 + delete(&crypt);
26 26
27 27 b64d = calloc(BASE64_LENGTH(length), sizeof(char));
28 28 base64_encode(encrypted, length, b64d, BASE64_LENGTH(length));
... ...
... ... @@ -28,7 +28,7 @@ main(int argc, char * argv[])
28 28
29 29 crypt = new(CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB);
30 30 decrypted = crypt_decrypt(crypt, data, pass, &length);
31   - delete(crypt);
  31 + delete(&crypt);
32 32 free(data);
33 33
34 34 printf("%s\n", decrypted);
... ...
... ... @@ -20,7 +20,7 @@ main(int argc, char * argv[])
20 20
21 21 printf("%d\n", (dyn->data)._int);
22 22
23   - delete(dyn);
  23 + delete(&dyn);
24 24 json_object_put(json);
25 25
26 26 dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), 321);
... ... @@ -28,7 +28,7 @@ main(int argc, char * argv[])
28 28
29 29 printf("%s\n", json_object_to_json_string(json));
30 30
31   - delete(dyn);
  31 + delete(&dyn);
32 32 json_object_put(json);
33 33
34 34 json = json_object_new_string(TEST_STR);
... ... @@ -36,7 +36,7 @@ main(int argc, char * argv[])
36 36
37 37 printf("%s\n", (dyn->data)._string);
38 38
39   - delete(dyn);
  39 + delete(&dyn);
40 40 json_object_put(json);
41 41
42 42 dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(TEST_STR), TEST_STR);
... ... @@ -44,7 +44,7 @@ main(int argc, char * argv[])
44 44
45 45 printf("%s\n", json_object_to_json_string(json));
46 46
47   - delete(dyn);
  47 + delete(&dyn);
48 48 json_object_put(json);
49 49
50 50 json = json_tokener_parse("{\"key1\":123,\"key2\":321,\"key3\":\"" TEST_STR "\"}");
... ... @@ -61,18 +61,18 @@ main(int argc, char * argv[])
61 61
62 62 value = dyntype_hash_get(hash, TEST_KEY1);
63 63 printf("%d\n", (value->data)._int);
64   - delete(value);
  64 + delete(&value);
65 65
66 66 value = dyntype_hash_get(hash, TEST_KEY2);
67 67 printf("%d\n", (value->data)._int);
68   - delete(value);
  68 + delete(&value);
69 69
70 70 value = dyntype_hash_get(hash, TEST_KEY3);
71 71 printf("%s\n", (value->data)._string);
72   - delete(value);
  72 + delete(&value);
73 73 }
74 74
75   - delete(dyn);
  75 + delete(&dyn);
76 76
77 77 return 0;
78 78 }
... ...
... ... @@ -29,13 +29,13 @@ main(int argc, char * argv[])
29 29
30 30 dyn = dyntype_hash_get(hash, TEST_KEY1);
31 31 printf("%d\n", (dyn->data)._int);
32   - delete(dyn);
  32 + delete(&dyn);
33 33
34 34 dyn = dyntype_hash_get(hash, TEST_KEY2);
35 35 printf("%d\n", (dyn->data)._int);
36   - delete(dyn);
  36 + delete(&dyn);
37 37
38   - delete(hash);
  38 + delete(&hash);
39 39
40 40 json = json_tokener_parse("{\"key1\":123,\"key2\":321}");
41 41 hash = newFromJson(DYNTYPE_HASH, json);
... ... @@ -43,13 +43,13 @@ main(int argc, char * argv[])
43 43
44 44 dyn = dyntype_hash_get(hash, TEST_KEY1);
45 45 printf("%d\n", (dyn->data)._int);
46   - delete(dyn);
  46 + delete(&dyn);
47 47
48 48 dyn = dyntype_hash_get(hash, TEST_KEY2);
49 49 printf("%d\n", (dyn->data)._int);
50   - delete(dyn);
  50 + delete(&dyn);
51 51
52   - delete(hash);
  52 + delete(&hash);
53 53
54 54 return 0;
55 55 }
... ...
  1 +#include <assert.h>
  2 +#include <json/json.h>
  3 +
  4 +#include "token/cclass.h"
  5 +#include "class.h"
  6 +
  7 +char _called;
  8 +
  9 +void
  10 +inline
  11 +_reset()
  12 +{
  13 + _called = 0;
  14 +}
  15 +
  16 +static
  17 +void
  18 +__construct(struct MOCK_CLASS * _this, va_list * params)
  19 +{
  20 + _called = 1;
  21 + _this->value = va_arg(* params, int);
  22 +}
  23 +
  24 +static
  25 +void
  26 +__jsonConst(struct MOCK_CLASS * _this, json_object * json)
  27 +{
  28 + _called = 1;
  29 + assert(json_type_int == json_object_get_type(json));
  30 +
  31 + _this->value = json_object_get_int(json);
  32 +}
  33 +
  34 +static
  35 +void
  36 +__destruct(struct MOCK_CLASS * _this)
  37 +{
  38 + _called = 1;
  39 +}
  40 +
  41 +static
  42 +struct json_object *
  43 +__toJson(struct MOCK_CLASS * _this)
  44 +{
  45 + struct json_object * json = json_object_new_int(_this->value);
  46 +
  47 + _called = 1;
  48 + return json;
  49 +}
  50 +
  51 +static const
  52 +struct CCLASS _mock_class = {
  53 + sizeof(struct MOCK_CLASS),
  54 + (ctor)__construct,
  55 + (jCtor)__jsonConst,
  56 + (dtor)__destruct,
  57 + (jTo)__toJson
  58 +};
  59 +
  60 +const struct CCLASS * const MOCK_CLASS = &_mock_class;
  61 +
  62 +/**
  63 + * ~~~ method implementations ~~~~~~~~
  64 + */
  65 +
  66 +int
  67 +mock_class_getValue(struct MOCK_CLASS * _this)
  68 +{
  69 + return _this->value;
  70 +}
  71 +
  72 +void
  73 +mock_class_setValue(struct MOCK_CLASS * _this, int value)
  74 +{
  75 + _this->value = value;
  76 +}
  77 +
  78 +/**
  79 + * ~~~ helper for mock assertions ~~~~~~~~
  80 + */
  81 +void *
  82 +getConstruct()
  83 +{
  84 + return __construct;
  85 +}
  86 +
  87 +void *
  88 +getJsonConst()
  89 +{
  90 + return __jsonConst;
  91 +}
  92 +// vim: set et ts=4 sw=4:
... ...
  1 +#ifndef __MOCK_CLASS_H__
  2 +#define __MOCK_CLASS_H__
  3 +
  4 +#include "token/cclass.h"
  5 +
  6 +
  7 +extern char _called;
  8 +
  9 +extern void inline
  10 +_reset()
  11 +{
  12 + _called = 0;
  13 +}
  14 +
  15 +struct MOCK_CLASS {
  16 + const struct CCLASS * const class;
  17 + int value;
  18 +};
  19 +
  20 +extern const struct CCLASS * const MOCK_CLASS;
  21 +
  22 +/**
  23 + * ~~~ method declarations ~~~~~~~~
  24 + */
  25 +
  26 +int mock_class_getValue(struct MOCK_CLASS * _this);
  27 +void mock_class_setValue(struct MOCK_CLASS * _this, int value);
  28 +
  29 +#endif//__MOCK_CLASS_H__
  30 +// vim: set et ts=4 sw=4:
... ...
... ... @@ -11,7 +11,7 @@
11 11
12 12
13 13 void
14   -setHashString(struct DYNTYPE_HASH * hash, const char * key, const char * value)
  14 +setHashString(struct DYNTYPE_HASH * hash, const char * const key, const char * value)
15 15 {
16 16 struct DYNTYPE * dyn;
17 17
... ... @@ -20,7 +20,7 @@ setHashString(struct DYNTYPE_HASH * hash, const char * key, const char * value)
20 20 }
21 21
22 22 void
23   -setHashInt(struct DYNTYPE_HASH * hash, const char * key, const int value)
  23 +setHashInt(struct DYNTYPE_HASH * hash, const char * const key, const int value)
24 24 {
25 25 struct DYNTYPE * dyn;
26 26
... ... @@ -28,13 +28,22 @@ setHashInt(struct DYNTYPE_HASH * hash, const char * key, const int value)
28 28 dyntype_hash_set(hash, key, dyn);
29 29 }
30 30
  31 +void
  32 +deleteHashValue(struct DYNTYPE_HASH * hash, const char * const key)
  33 +{
  34 + struct DYNTYPE * dyn = dyntype_hash_get(hash, key);
  35 +
  36 + delete(&dyn);
  37 +}
  38 +
31 39 int
32 40 main(int argc, char * argv[])
33 41 {
34   - struct PACKET * packet;
35   - struct DYNTYPE_HASH * data;
36   - struct json_object * json;
37   - char * json_str;
  42 + struct PACKET * packet;
  43 + struct DYNTYPE_HASH * data;
  44 + struct DYNTYPE * _clear;
  45 + struct json_object * json;
  46 + char * json_str;
38 47
39 48 packet = new(PACKET);
40 49
... ... @@ -67,13 +76,15 @@ main(int argc, char * argv[])
67 76
68 77 printf("%s\n", json_str);
69 78
70   - delete(dyntype_hash_get(data, "#C#"));
71   - delete(dyntype_hash_get(data, "usr"));
72   - delete(dyntype_hash_get(data, "pas"));
73   - delete(dyntype_hash_get(data, "val"));
74   - delete(packet_getHeader(packet));
75   - delete(packet_getData(packet));
76   - delete(packet);
  79 + deleteHashValue(data, "#C#");
  80 + deleteHashValue(data, "usr");
  81 + deleteHashValue(data, "pas");
  82 + deleteHashValue(data, "val");
  83 + _clear = packet_getHeader(packet);
  84 + delete(&_clear);
  85 + _clear = packet_getData(packet);
  86 + delete(&_clear);
  87 + delete(&packet);
77 88
78 89 json_object_put(json);
79 90
... ... @@ -83,13 +94,15 @@ main(int argc, char * argv[])
83 94 printf("%s\n", json_object_to_json_string(json));
84 95
85 96 data = (packet_getData(packet)->data)._hash;
86   - delete(dyntype_hash_get(data, "#C#"));
87   - delete(dyntype_hash_get(data, "usr"));
88   - delete(dyntype_hash_get(data, "pas"));
89   - delete(dyntype_hash_get(data, "val"));
90   - delete(packet_getHeader(packet));
91   - delete(packet_getData(packet));
92   - delete(packet);
  97 + deleteHashValue(data, "#C#");
  98 + deleteHashValue(data, "usr");
  99 + deleteHashValue(data, "pas");
  100 + deleteHashValue(data, "val");
  101 + _clear = packet_getHeader(packet);
  102 + delete(&_clear);
  103 + _clear = packet_getData(packet);
  104 + delete(&_clear);
  105 + delete(&packet);
93 106 free(json_str);
94 107
95 108 json_object_put(json);
... ...
... ... @@ -27,7 +27,6 @@ load_symbol(void * dlhandle, const char * const symbol)
27 27
28 28 if ((error = dlerror()) != NULL) {
29 29 fprintf(stderr, "%s\n", error);
30   - exit(EXIT_FAILURE);
31 30 }
32 31
33 32 return sym;
... ... @@ -44,27 +43,51 @@ runtests(
44 43 testfunc * tests;
45 44 const char * const testname;
46 45 //char * const * funcnames;
  46 + char testfile[NAME_MAX+3] = "./";
  47 +
  48 + strcat(testfile, filename);
  49 +
  50 + void (*setUp)() = NULL;
  51 + void (*tearDown)() = NULL;
47 52
48 53 size_t index;
49 54 void * dlhandle;
50 55
51   - dlhandle = dlopen("./cclass.test", RTLD_LAZY);
  56 + dlhandle = dlopen(testfile, RTLD_LAZY);
52 57 if (!dlhandle) {
53 58 fprintf(stderr, "%s\n", dlerror());
54 59 exit(EXIT_FAILURE);
55 60 }
56 61
57   - * (void **) (&count) = load_symbol(dlhandle, "count");
58   - * (void **) (&tests) = load_symbol(dlhandle, "tests");
59   - * (void **) (&testname) = load_symbol(dlhandle, "testname");
  62 + * (void **) (&count) = load_symbol(dlhandle, "count");
  63 + * (void **) (&tests) = load_symbol(dlhandle, "tests");
  64 + * (void **) (&testname) = load_symbol(dlhandle, "testname");
60 65 // * (void **) (&funcnames) = load_symbol(dlhandle, "funcnames");
  66 +
  67 + * (void **) (&setUp) = load_symbol(dlhandle, "setUp");
  68 + * (void **) (&tearDown) = load_symbol(dlhandle, "tearDown");
61 69
  70 + if (NULL == count || NULL == tests || NULL == testname) {
  71 + *errors = TEST_ERROR;
  72 + return;
  73 + }
  74 +
62 75 *_count += *count;
63 76
64 77 printf("running tests for %s\n", testname);
65 78
66 79 for (index=0; index<*count; index++) {
67   - int result = tests[index]();
  80 + int result;
  81 +
  82 + if (NULL != setUp) {
  83 + setUp();
  84 + }
  85 +
  86 + result = tests[index]();
  87 +
  88 + if (NULL != setUp) {
  89 + tearDown();
  90 + }
68 91
69 92 switch (result) {
70 93 case TEST_FAILED: (*failures)++; break;
... ... @@ -99,7 +122,7 @@ main(int argc, char * argv[])
99 122
100 123 dirent = readdir(dir);
101 124 while (dirent) {
102   - if (0 == strcmp(".test", dirent->d_name + strlen(dirent->d_name) - 5)) {
  125 + if (0 == strcmp("Test.la", dirent->d_name + strlen(dirent->d_name) - 7)) {
103 126 runtests(dirent->d_name, &count, &failures, &errors);
104 127 }
105 128
... ... @@ -113,7 +136,7 @@ main(int argc, char * argv[])
113 136 failures,
114 137 errors);
115 138
116   - return 0;
  139 + return failures + errors;
117 140 }
118 141
119 142 // vim: set et ts=4 sw=4:
... ...
... ... @@ -10,6 +10,12 @@ enum RESULT_TYPES {
10 10 TEST_ERROR
11 11 };
12 12
  13 +#define ASSERT_NULL(value) if (NULL != (value)) return TEST_FAILED
  14 +#define ASSERT_NOT_NULL(value) if (NULL == (value)) return TEST_FAILED
  15 +#define ASSERT_EQUAL(val1,val2) if ((val1) != (val2)) return TEST_FAILED
  16 +#define ASSERT_NOT_EQUAL(val1,val2) if ((val1) == (val2)) return TEST_FAILED
  17 +
  18 +
13 19 typedef int (* testfunc)(void);
14 20 #define FUNCS_COUNT(array) (sizeof((array)) / sizeof(testfunc))
15 21
... ...
Please register or login to post a comment