Skip to content

Commit 9b67acb

Browse files
committed
tests: moved common routines to test.[ch]
1 parent b04c40d commit 9b67acb

File tree

5 files changed

+145
-136
lines changed

5 files changed

+145
-136
lines changed

Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ tests_check_parser_CFLAGS = @check_CFLAGS@ $(PARSER_CFLAGS) $(STROPHE_FLAGS) \
7474
tests_check_parser_LDADD = @check_LIBS@ $(STROPHE_LIBS)
7575
tests_check_parser_LDFLAGS = -static
7676

77-
tests_test_rand_SOURCES = tests/test_rand.c src/sha1.c
77+
tests_test_rand_SOURCES = tests/test_rand.c tests/test.c src/sha1.c
7878
tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
7979

80-
tests_test_scram_SOURCES = tests/test_scram.c src/sha1.c
80+
tests_test_scram_SOURCES = tests/test_scram.c tests/test.c src/sha1.c
8181
tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src

tests/test.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* test.c
2+
* strophe XMPP client library -- common routines for tests
3+
*
4+
* Copyright (C) 2014 Dmitry Podgorny <pasis.ua@gmail.com>
5+
*
6+
* This software is provided AS-IS with no warranty, either express
7+
* or implied.
8+
*
9+
* This program is dual licensed under the MIT and GPLv3 licenses.
10+
*/
11+
12+
#include <assert.h>
13+
#include <string.h>
14+
15+
#include "test.h"
16+
17+
static uint8_t char_to_bin(char c)
18+
{
19+
return c <= '9' ? (uint8_t)(c - '0') :
20+
c <= 'Z' ? (uint8_t)(c - 'A' + 10) :
21+
(uint8_t)(c - 'a' + 10);
22+
}
23+
24+
void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len)
25+
{
26+
size_t len = strlen(hex);
27+
size_t i;
28+
29+
assert(len % 2 == 0);
30+
31+
for (i = 0; i < len / 2; ++i) {
32+
bin[i] = char_to_bin(hex[i * 2]) * 16 + char_to_bin(hex[i * 2 + 1]);
33+
}
34+
*bin_len = len / 2;
35+
}
36+
37+
const char *test_bin_to_hex(uint8_t *bin, size_t len)
38+
{
39+
static char buf[2048];
40+
size_t i;
41+
42+
static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
43+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
44+
45+
assert(ARRAY_SIZE(buf) > len * 2);
46+
47+
for (i = 0; i < len; ++i) {
48+
buf[i * 2] = hex[(bin[i] >> 4) & 0x0f];
49+
buf[i * 2 + 1] = hex[bin[i] & 0x0f];
50+
}
51+
buf[len * 2] = '\0';
52+
53+
return buf;
54+
}

tests/test.h

+64-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
1+
/* test.h
2+
** libstrophe XMPP client library -- common routines for tests
3+
**
4+
** Copyright (C) 2005-2009 Collecta, Inc.
5+
**
6+
** This software is provided AS-IS with no warranty, either express
7+
** or implied.
8+
**
9+
** This program is dual licensed under the MIT and GPLv3 licenses.
10+
*/
11+
112
#ifndef __LIBSTROPHE_TEST_H__
213
#define __LIBSTROPHE_TEST_H__
314

4-
#define TEST_MAIN \
5-
int main(int argc, char **argv) {\
6-
int num_failed;\
7-
Suite *s = parser_suite();\
8-
SRunner *sr = srunner_create(s);\
9-
srunner_run_all(sr, CK_NORMAL);\
10-
num_failed = srunner_ntests_failed(sr);\
11-
srunner_free(sr);\
12-
return (num_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;\
13-
}\
14-
15+
#include <stddef.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
#include "ostypes.h"
19+
20+
#define TEST_MAIN \
21+
int main(int argc, char **argv) { \
22+
int num_failed; \
23+
Suite *s = parser_suite(); \
24+
SRunner *sr = srunner_create(s); \
25+
srunner_run_all(sr, CK_NORMAL); \
26+
num_failed = srunner_ntests_failed(sr); \
27+
srunner_free(sr); \
28+
return (num_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; \
29+
}
30+
31+
#ifndef ARRAY_SIZE
32+
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
33+
#endif
34+
35+
#define COMPARE(v1, v2) \
36+
do { \
37+
const char *__v1 = v1; \
38+
const char *__v2 = v2; \
39+
if (strcmp(__v1, __v2) != 0) { \
40+
printf("%s differs!\n" \
41+
"expected: %s\n" \
42+
"got: %s\n", \
43+
#v1, __v1, __v2); \
44+
exit(1); \
45+
} \
46+
} while (0)
47+
48+
#define COMPARE_BUF(v1, len1, v2, len2) \
49+
do { \
50+
const uint8_t *__v1 = (uint8_t *)(v1); \
51+
const uint8_t *__v2 = (uint8_t *)(v2); \
52+
size_t __len1 = len1; \
53+
size_t __len2 = len2; \
54+
if (__len1 != __len2 || \
55+
memcmp(__v1, __v2, __len1) != 0) \
56+
{ \
57+
printf("%s differs!\n", #v1); \
58+
printf("expected: 0x%s\n", \
59+
test_bin_to_hex(__v1, __len1); \
60+
printf("got: 0x%s\n", \
61+
test_bin_to_hex(__v2, __len2); \
62+
exit(1); \
63+
} \
64+
} while (0)
65+
66+
void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len);
67+
const char *test_bin_to_hex(uint8_t *bin, size_t len);
1568

1669
#endif /* __LIBSTROPHE_TEST_H__ */

tests/test_rand.c

+15-63
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
* This program is dual licensed under the MIT and GPLv3 licenses.
1010
*/
1111

12-
/* gcc -o test_rand -I. -I./src tests/test_rand.c src/sha1.c */
12+
/* gcc -o test_rand -I./src tests/test_rand.c tests/test.c src/sha1.c */
1313

1414
#include <assert.h>
1515
#include <stdio.h>
1616
#include <stdlib.h>
1717

18+
#include "test.h"
19+
1820
/* include rand.c to access private structures and functions */
1921
#include "rand.c"
2022

21-
#ifndef ARRAY_SIZE
22-
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
23-
#endif
24-
2523
/* stubs to build test without whole libstrophe */
2624
void *xmpp_alloc(const xmpp_ctx_t * const ctx, const size_t size) {
2725
return NULL;
@@ -103,53 +101,6 @@ static struct {
103101
},
104102
};
105103

106-
static uint8_t char_to_bin(char c)
107-
{
108-
return c <= '9' ? (uint8_t)(c - '0') :
109-
c <= 'Z' ? (uint8_t)(c - 'A' + 10) :
110-
(uint8_t)(c - 'a' + 10);
111-
}
112-
113-
static void hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len)
114-
{
115-
size_t len = strlen(hex);
116-
size_t i;
117-
118-
assert(len % 2 == 0);
119-
120-
for (i = 0; i < len / 2; ++i) {
121-
bin[i] = char_to_bin(hex[i * 2]) * 16 + char_to_bin(hex[i * 2 + 1]);
122-
}
123-
*bin_len = len / 2;
124-
}
125-
126-
static const char *bin_to_hex(uint8_t *bin, size_t len)
127-
{
128-
static char buf[1024];
129-
size_t i;
130-
131-
assert(ARRAY_SIZE(buf) > len * 2);
132-
133-
for (i = 0; i < len; ++i) {
134-
sprintf(buf + i * 2, "%02x", bin[i]);
135-
}
136-
137-
return buf;
138-
}
139-
140-
#define COMPARE(v1, v2) \
141-
do { \
142-
const char *__v1 = v1; \
143-
const char *__v2 = v2; \
144-
if (strcmp(__v1, __v2) != 0) { \
145-
printf("%s differs!\n" \
146-
"expected: %s\n" \
147-
"got: %s\n", \
148-
#v1, __v1, __v2); \
149-
exit(1); \
150-
} \
151-
} while (0)
152-
153104
int main()
154105
{
155106
size_t i;
@@ -160,30 +111,31 @@ int main()
160111
uint8_t output[1024];
161112
Hash_DRBG_CTX ctx;
162113

114+
printf("Hash_DRBG tests.\n");
163115
for (i = 0; i < ARRAY_SIZE(test_vectors); ++i) {
164116
printf("Test #%d: ", (int)i + 1);
165-
hex_to_bin(test_vectors[i].entropy_input, entropy_input,
166-
&entropy_input_len);
167-
hex_to_bin(test_vectors[i].nonce, nonce, &nonce_len);
117+
test_hex_to_bin(test_vectors[i].entropy_input, entropy_input,
118+
&entropy_input_len);
119+
test_hex_to_bin(test_vectors[i].nonce, nonce, &nonce_len);
168120

169121
Hash_DRBG_Instantiate(&ctx, entropy_input, entropy_input_len,
170122
nonce, nonce_len);
171-
COMPARE(test_vectors[i].V1, bin_to_hex(ctx.V, sizeof(ctx.V)));
172-
COMPARE(test_vectors[i].C1, bin_to_hex(ctx.C, sizeof(ctx.C)));
123+
COMPARE(test_vectors[i].V1, test_bin_to_hex(ctx.V, sizeof(ctx.V)));
124+
COMPARE(test_vectors[i].C1, test_bin_to_hex(ctx.C, sizeof(ctx.C)));
173125
assert(ctx.reseed_counter == 1);
174126

175127
Hash_DRBG_Generate(&ctx, output, test_vectors[i].returned_bytes);
176-
COMPARE(test_vectors[i].V2, bin_to_hex(ctx.V, sizeof(ctx.V)));
177-
COMPARE(test_vectors[i].C2, bin_to_hex(ctx.C, sizeof(ctx.C)));
128+
COMPARE(test_vectors[i].V2, test_bin_to_hex(ctx.V, sizeof(ctx.V)));
129+
COMPARE(test_vectors[i].C2, test_bin_to_hex(ctx.C, sizeof(ctx.C)));
178130
assert(ctx.reseed_counter == 2);
179131

180132
Hash_DRBG_Generate(&ctx, output, test_vectors[i].returned_bytes);
181-
COMPARE(test_vectors[i].V3, bin_to_hex(ctx.V, sizeof(ctx.V)));
182-
COMPARE(test_vectors[i].C3, bin_to_hex(ctx.C, sizeof(ctx.C)));
133+
COMPARE(test_vectors[i].V3, test_bin_to_hex(ctx.V, sizeof(ctx.V)));
134+
COMPARE(test_vectors[i].C3, test_bin_to_hex(ctx.C, sizeof(ctx.C)));
183135
COMPARE(test_vectors[i].output,
184-
bin_to_hex(output, test_vectors[i].returned_bytes));
136+
test_bin_to_hex(output, test_vectors[i].returned_bytes));
185137
assert(ctx.reseed_counter == 3);
186-
printf("ok\n");
138+
printf("ok\n");
187139
}
188140

189141
return 0;

tests/test_scram.c

+10-60
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,18 @@
99
* This program is dual licensed under the MIT and GPLv3 licenses.
1010
*/
1111

12-
/* gcc -o test_scram -I. -I./src tests/test_scram.c src/sha1.c */
12+
/* gcc -o test_scram -I./src tests/test_scram.c tests/test.c src/sha1.c */
1313

1414
#include <assert.h>
1515
#include <string.h>
1616
#include <stdio.h>
1717
#include <stdlib.h>
1818

19+
#include "test.h"
20+
1921
/* include scram.c to access static functions */
2022
#include "scram.c"
2123

22-
/* TODO Make common code for tests. */
23-
24-
#ifndef ARRAY_SIZE
25-
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
26-
#endif
27-
28-
static uint8_t char_to_bin(char c)
29-
{
30-
return c <= '9' ? (uint8_t)(c - '0') :
31-
c <= 'Z' ? (uint8_t)(c - 'A' + 10) :
32-
(uint8_t)(c - 'a' + 10);
33-
}
34-
35-
static void hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len)
36-
{
37-
size_t len = strlen(hex);
38-
size_t i;
39-
40-
assert(len % 2 == 0);
41-
42-
for (i = 0; i < len / 2; ++i) {
43-
bin[i] = char_to_bin(hex[i * 2]) * 16 + char_to_bin(hex[i * 2 + 1]);
44-
}
45-
*bin_len = len / 2;
46-
}
47-
48-
static const char *bin_to_hex(uint8_t *bin, size_t len)
49-
{
50-
static char buf[1024];
51-
size_t i;
52-
53-
assert(ARRAY_SIZE(buf) > len * 2);
54-
55-
for (i = 0; i < len; ++i) {
56-
sprintf(buf + i * 2, "%02x", bin[i]);
57-
}
58-
return buf;
59-
}
60-
61-
#define COMPARE(v1, v2) \
62-
do { \
63-
const char *__v1 = v1; \
64-
const char *__v2 = v2; \
65-
if (strcmp(__v1, __v2) != 0) { \
66-
printf("%s differs!\n" \
67-
"expected: %s\n" \
68-
"got: %s\n", \
69-
#v1, __v1, __v2); \
70-
exit(1); \
71-
} \
72-
} while (0)
73-
7424
/*
7525
* Test vectors for derivation function (RFC6070).
7626
*/
@@ -120,7 +70,7 @@ static void test_df(void)
12070
SCRAM_SHA1_Hi((uint8_t *)df_vectors[i].P, df_vectors[i].P_len,
12171
(uint8_t *)df_vectors[i].S, df_vectors[i].S_len,
12272
df_vectors[i].c, dk);
123-
s = bin_to_hex(dk, sizeof(dk));
73+
s = test_bin_to_hex(dk, sizeof(dk));
12474
COMPARE(df_vectors[i].DK, s);
12575
printf("ok\n");
12676
}
@@ -141,9 +91,9 @@ static const struct {
14191
.initial = "n,,n=juliet,r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AA",
14292
.challenge = "r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AAe124695b-69a9-4de6-9c30"
14393
"-b51b3808c59e,s=NjhkYTM0MDgtNGY0Zi00NjdmLTkxMmUtNDlmNTNmN"
144-
"DNkMDMz,i=4096",
94+
"DNkMDMz,i=4096",
14595
.response = "c=biws,r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AAe124695b-69a9-4de"
146-
"6-9c30-b51b3808c59e",
96+
"6-9c30-b51b3808c59e",
14797
.salt = "36386461333430382d346634662d34363766"
14898
"2d393132652d343966353366343364303333",
14999
.i = 4096,
@@ -168,7 +118,7 @@ static void test_scram(void)
168118
snprintf(auth, sizeof(auth), "%s,%s,%s",
169119
scram_vectors[i].initial + 3, scram_vectors[i].challenge,
170120
scram_vectors[i].response);
171-
hex_to_bin(scram_vectors[i].salt, salt, &salt_len);
121+
test_hex_to_bin(scram_vectors[i].salt, salt, &salt_len);
172122

173123
SCRAM_SHA1_ClientKey((uint8_t *)scram_vectors[i].password,
174124
strlen(scram_vectors[i].password),
@@ -177,9 +127,9 @@ static void test_scram(void)
177127
for (j = 0; j < SHA1_DIGEST_SIZE; j++) {
178128
sign[j] ^= key[j];
179129
}
180-
s = bin_to_hex(sign, SHA1_DIGEST_SIZE);
181-
COMPARE(scram_vectors[i].sign, s);
182-
printf("ok\n");
130+
s = test_bin_to_hex(sign, SHA1_DIGEST_SIZE);
131+
COMPARE(scram_vectors[i].sign, s);
132+
printf("ok\n");
183133
}
184134
}
185135

0 commit comments

Comments
 (0)