Increase code Coverage (#897)

* Make it clear which hash we are testing

* Coverage for all pearson hash variants

* Initial coverage for auth.c

* Add some more auth.c coverage

* Ensure new bins are added to both build systems

* Ensure new source files are fully linted
This commit is contained in:
Hamish Coleman 2021-11-10 06:02:05 +00:00 committed by GitHub
parent e7ad089d24
commit e4e2aba420
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 231 additions and 4 deletions

View File

@ -199,6 +199,8 @@ target_link_libraries(n2n-benchmark n2n)
add_executable(n2n-keygen tools/n2n-keygen.c)
target_link_libraries(n2n-keygen n2n)
add_executable(tests-auth tools/tests-auth.c)
target_link_libraries(tests-auth n2n)
add_executable(tests-compress tools/tests-compress.c)
target_link_libraries(tests-compress n2n)
add_executable(tests-elliptic tools/tests-elliptic.c)

View File

@ -95,6 +95,7 @@ LINT_CCODE=\
src/transform_cc20.c \
src/tuntap_linux.c \
src/wire.c \
tools/tests-auth.c \
tools/tests-compress.c \
tools/tests-elliptic.c \
tools/tests-hashing.c \

View File

@ -4,6 +4,7 @@
# in the tests dir.
TESTS="
tests-auth
tests-compress
tests-elliptic
tests-hashing

31
tests/tests-auth.expected Normal file
View File

@ -0,0 +1,31 @@
bin_to_ascii: input size = 0x10
000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | |
bin_to_ascii: output: 00420mG51WS82GeB30qE3m
ascii_to_bin: input = 00420mG51WS82GeB30qE3m
ascii_to_bin: output:
000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | |
010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |
generate_private_key: input = 00420mG51WS82GeB30qE3m
generate_private_key: output:
000: a4 f4 b5 1c 8a 0a 09 f9 7e 98 22 ca 8a cc b3 f9 | ~ " |
010: 4d 5a 0d 02 0b 9d 08 ea 03 9b 46 41 8e 3c 0d 49 |MZ FA < I|
generate_public_key: input:
000: a4 f4 b5 1c 8a 0a 09 f9 7e 98 22 ca 8a cc b3 f9 | ~ " |
010: 4d 5a 0d 02 0b 9d 08 ea 03 9b 46 41 8e 3c 0d 49 |MZ FA < I|
generate_public_key: output:
000: ca 58 61 6f f9 25 d0 cd 1d a5 62 48 a0 15 5e ad | Xao % bH ^ |
010: a9 f3 5c 10 5f 20 b6 42 b0 a9 7c 1e 0e d7 e9 4b | \ _ B | K|
generate_shared_secret: input: prv
000: a4 f4 b5 1c 8a 0a 09 f9 7e 98 22 ca 8a cc b3 f9 | ~ " |
010: 4d 5a 0d 02 0b 9d 08 ea 03 9b 46 41 8e 3c 0d 49 |MZ FA < I|
generate_shared_secret: input: pub
000: ca 58 61 6f f9 25 d0 cd 1d a5 62 48 a0 15 5e ad | Xao % bH ^ |
010: a9 f3 5c 10 5f 20 b6 42 b0 a9 7c 1e 0e d7 e9 4b | \ _ B | K|
generate_shared_secret: output:
000: 5d 94 7b 0b db 54 e8 70 8a 09 b0 db 6f 0b 0d 31 |] { T p o 1|
010: 1b b8 5f ba 57 74 34 bd 3b c5 c4 6c d5 ae a4 84 | _ Wt4 ; l |

View File

@ -32,5 +32,16 @@ environment: input size = 0x200
1e0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | |
1f0: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f | |
pearson: output = 0xb2d98fa82ea108be
pearson_hash_256: output:
000: 40 09 5c ca 28 6b fb 93 4c 4a f7 c0 79 a8 04 5a |@ \ (k LJ y Z|
010: b5 3d cf b3 a7 ed 18 56 b2 d9 8f a8 2e a1 08 be | = V . |
pearson_hash_128: output:
000: b5 3d cf b3 a7 ed 18 56 b2 d9 8f a8 2e a1 08 be | = V . |
pearson_hash_64: output = 0xb2d98fa82ea108be
pearson_hash_32: output = 0x2ea108be
pearson_hash_16: output = 0x8be

View File

@ -19,6 +19,7 @@ TOOLS+=@ADDITIONAL_TOOLS@
TESTS=tests-compress tests-elliptic tests-hashing tests-transform
TESTS+=tests-wire
TESTS+=tests-auth
.PHONY: all clean install
all: $(TOOLS) $(TESTS)

128
tools/tests-auth.c Normal file
View File

@ -0,0 +1,128 @@
/*
* (C) 2007-21 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>
*
*/
#include <inttypes.h>
#include "n2n.h"
#include "hexdump.h"
uint8_t PKT_CONTENT1[]={
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
};
char *PKT_CONTENT2 = "00420mG51WS82GeB30qE3m";
void test_bin_to_ascii (void *buf, unsigned int bufsize) {
char *test_name = "bin_to_ascii";
char out[32];
printf("%s: input size = 0x%x\n", test_name, bufsize);
fhexdump(0, buf, bufsize, stdout);
bin_to_ascii(out, buf, bufsize);
printf("%s: output: %s\n", test_name, out);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_ascii_to_bin (char *buf) {
char *test_name = "ascii_to_bin";
uint8_t out[32];
memset(out, 0, sizeof(out));
printf("%s: input = %s\n", test_name, buf);
ascii_to_bin(out, buf);
// TODO:
// - it would be nice if the function returned the bufsize,
// - or took an allocation size as input
printf("%s: output:\n", test_name);
fhexdump(0, out, sizeof(out), stdout);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_generate_private_key (char *in, n2n_private_public_key_t prv) {
char *test_name = "generate_private_key";
printf("%s: input = %s\n", test_name, in);
generate_private_key(prv, in);
printf("%s: output:\n", test_name);
fhexdump(0, prv, sizeof(n2n_private_public_key_t), stdout);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_generate_public_key (n2n_private_public_key_t prv, n2n_private_public_key_t pub) {
char *test_name = "generate_public_key";
printf("%s: input:\n", test_name);
fhexdump(0, prv, sizeof(n2n_private_public_key_t), stdout);
generate_public_key(pub, prv);
printf("%s: output:\n", test_name);
fhexdump(0, pub, sizeof(n2n_private_public_key_t), stdout);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_generate_shared_secret (n2n_private_public_key_t prv, n2n_private_public_key_t pub) {
char *test_name = "generate_shared_secret";
n2n_private_public_key_t out;
printf("%s: input: prv\n", test_name);
fhexdump(0, prv, sizeof(n2n_private_public_key_t), stdout);
printf("%s: input: pub\n", test_name);
fhexdump(0, pub, sizeof(n2n_private_public_key_t), stdout);
generate_shared_secret(out, prv, pub);
printf("%s: output:\n", test_name);
fhexdump(0, out, sizeof(out), stdout);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
int main (int argc, char * argv[]) {
test_bin_to_ascii(PKT_CONTENT1, sizeof(PKT_CONTENT1));
test_ascii_to_bin(PKT_CONTENT2);
n2n_private_public_key_t prv;
memset(prv, 0, sizeof(prv));
n2n_private_public_key_t pub;
memset(pub, 0, sizeof(pub));
test_generate_private_key(PKT_CONTENT2, prv);
test_generate_public_key(prv, pub);
test_generate_shared_secret(prv, pub);
return 0;
}

View File

@ -41,8 +41,30 @@ uint8_t PKT_CONTENT[]={
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
};
void test_pearson (void *buf, unsigned int bufsize) {
char *test_name = "pearson";
void test_pearson_16 (void *buf, unsigned int bufsize) {
char *test_name = "pearson_hash_16";
uint16_t hash = pearson_hash_16(buf, bufsize);
printf("%s: output = 0x%" PRIx16 "\n", test_name, hash);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_pearson_32 (void *buf, unsigned int bufsize) {
char *test_name = "pearson_hash_32";
uint32_t hash = pearson_hash_32(buf, bufsize);
printf("%s: output = 0x%" PRIx32 "\n", test_name, hash);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_pearson_64 (void *buf, unsigned int bufsize) {
char *test_name = "pearson_hash_64";
uint64_t hash = pearson_hash_64(buf, bufsize);
@ -52,6 +74,32 @@ void test_pearson (void *buf, unsigned int bufsize) {
printf("\n");
}
void test_pearson_128 (void *buf, unsigned int bufsize) {
char *test_name = "pearson_hash_128";
uint8_t hash[16];
pearson_hash_128(hash, buf, bufsize);
printf("%s: output:\n", test_name);
fhexdump(0, hash, sizeof(hash), stdout);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
void test_pearson_256 (void *buf, unsigned int bufsize) {
char *test_name = "pearson_hash_256";
uint8_t hash[32];
pearson_hash_256(hash, buf, bufsize);
printf("%s: output:\n", test_name);
fhexdump(0, hash, sizeof(hash), stdout);
fprintf(stderr, "%s: tested\n", test_name);
printf("\n");
}
int main (int argc, char * argv[]) {
pearson_hash_init();
@ -60,7 +108,11 @@ int main (int argc, char * argv[]) {
fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout);
printf("\n");
test_pearson(PKT_CONTENT, sizeof(PKT_CONTENT));
test_pearson_256(PKT_CONTENT, sizeof(PKT_CONTENT));
test_pearson_128(PKT_CONTENT, sizeof(PKT_CONTENT));
test_pearson_64(PKT_CONTENT, sizeof(PKT_CONTENT));
test_pearson_32(PKT_CONTENT, sizeof(PKT_CONTENT));
test_pearson_16(PKT_CONTENT, sizeof(PKT_CONTENT));
return 0;
}