From 5b7b99ed81cca371a6fda44f8ccdcf7fea28a397 Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Wed, 20 May 2020 14:35:52 -0700 Subject: [PATCH] Add tests for iperf_api functions. --- src/Makefile.am | 12 +++-- src/private.pem | 27 +++++++++++ src/public.pem | 9 ++++ src/t_auth.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 src/private.pem create mode 100644 src/public.pem create mode 100644 src/t_auth.c diff --git a/src/Makefile.am b/src/Makefile.am index 6c5d947..11d3e17 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,9 @@ lib_LTLIBRARIES = libiperf.la # Build and install an iperf library bin_PROGRAMS = iperf3 # Build and install an iperf binary if ENABLE_PROFILING -noinst_PROGRAMS = t_timer t_units t_uuid t_api iperf3_profile # Build, but don't install the test programs and a profiled version of iperf3 +noinst_PROGRAMS = t_timer t_units t_uuid t_api t_auth iperf3_profile # Build, but don't install the test programs and a profiled version of iperf3 else -noinst_PROGRAMS = t_timer t_units t_uuid t_api # Build, but don't install the test programs +noinst_PROGRAMS = t_timer t_units t_uuid t_api t_auth # Build, but don't install the test programs endif include_HEADERS = iperf_api.h # Defines the headers that get installed with the program @@ -84,6 +84,11 @@ t_api_CFLAGS = -g t_api_LDFLAGS = t_api_LDADD = libiperf.la +t_auth_SOURCES = t_auth.c +t_auth_CFLAGS = -g +t_auth_LDFLAGS = +t_auth_LDADD = libiperf.la + # Specify which tests to run during a "make check" @@ -91,6 +96,7 @@ TESTS = \ t_timer \ t_units \ t_uuid \ - t_api + t_api \ + t_auth dist_man_MANS = iperf3.1 libiperf.3 diff --git a/src/private.pem b/src/private.pem new file mode 100644 index 0000000..8b1c5b6 --- /dev/null +++ b/src/private.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAwVbvPf/eDIZKmEVth9+VPgSx1RkXOAPCJ5tl51bcYoy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ktlkuj82fBlGWpH2+F +kPbsBWp2WylI3YaeQT4DZyRjQ3JEHglrOppZ0qMX180S2sJW9Eh2+Gw+lQvM9rSd +uhTVypYldNo6Ux+GnlDGwQKBgQC/6W4uvCyjcvXN8y8z08yysw1yzEaY6DFBqd0I +jUlH9Ktb9sABtXG9nbSTSssX85HQTw8bOeXWlISZo/TB1m4eFHMORgemnviq0cfL +4hoaOAtCJq1vnPJbqQe8c11mfj3mi0d+MZvzmO7ly+NGzlt92q0wqwJb13VgelGa +CWdL8QKBgQD1ABK8WJuMowihrQsLaUxNO/1hsN8CP/rjoi9D5NdSFt5zzcC3D/RA +m42ScOaAFIh44Js5aAROvbDqIlelhwRlmutP/lYyQRZDZm7X6u/bKuqK4H/sM6ZB +SICe2eWFvbKexI7QnreRltOWIFrNoZy9FdELM8DS+kePAlVt7c83Uw== +-----END RSA PRIVATE KEY----- diff --git a/src/public.pem b/src/public.pem new file mode 100644 index 0000000..71f4f13 --- /dev/null +++ b/src/public.pem @@ -0,0 +1,9 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwVbvPf/eDIZKmEVth9+V +PgSx1RkXOAPCJ5tl51bcYoy9P10NnoutsTK/64VclIuyUDUdAw81Vu5tYRcBeB8J +llp02xL8kPo0IROsT3wmMYbPziUG/I2988sAP9mL8QbtKydnADHMikfadfyPkfxW +2naFtquWT/vKKVkhC2LHJyTpmAVjpp6R9LDDu/YY0/kb5DvYPpe62xgNWjNVIhAB +u3R+StAAL25SaLXuaGSVpDe6SphnTqVf+LmKCkZmSMSfQoozXomNyFpLU8ODoV3Q +yHYi6QSWWFtU6gu0uso/9pfRFjbNGV9lxokKNz/cZGyu0SFddMiTGrt24NROi0RH +wwIDAQAB +-----END PUBLIC KEY----- diff --git a/src/t_auth.c b/src/t_auth.c new file mode 100644 index 0000000..ff9cffe --- /dev/null +++ b/src/t_auth.c @@ -0,0 +1,125 @@ +/* + * iperf, Copyright (c) 2020, The Regents of the University of + * California, through Lawrence Berkeley National Laboratory (subject + * to receipt of any required approvals from the U.S. Dept. of + * Energy). All rights reserved. + * + * If you have questions about your rights to use or distribute this + * software, please contact Berkeley Lab's Technology Transfer + * Department at TTD@lbl.gov. + * + * NOTICE. This software is owned by the U.S. Department of Energy. + * As such, the U.S. Government has been granted for itself and others + * acting on its behalf a paid-up, nonexclusive, irrevocable, + * worldwide license in the Software to reproduce, prepare derivative + * works, and perform publicly and display publicly. Beginning five + * (5) years after the date permission to assert copyright is obtained + * from the U.S. Department of Energy, and subject to any subsequent + * five (5) year renewals, the U.S. Government is granted for itself + * and others acting on its behalf a paid-up, nonexclusive, + * irrevocable, worldwide license in the Software to reproduce, + * prepare derivative works, distribute copies to the public, perform + * publicly and display publicly, and to permit others to do so. + * + * This code is distributed under a BSD style license, see the LICENSE + * file for complete information. + */ +#include "iperf_config.h" + +#include +#ifdef HAVE_STDINT_H +#include +#endif +#include +#include + +#include "iperf.h" +#include "iperf_api.h" +#if defined(HAVE_SSL) +#include "iperf_auth.h" +#endif /* HAVE_SSL */ + +#include "version.h" + +#include "units.h" + +#if defined(HAVE_SSL) +int test_authtoken(const char *authUser, const char *authPassword, EVP_PKEY *pubkey, EVP_PKEY *privkey); + +int +main(int argc, char **argv) +{ + /* sha256 */ + void sha256(const char *string, char outputBuffer[65]); + const char sha256String[] = "This is a SHA256 test."; + const char sha256Digest[] = "4816482f8b4149f687a1a33d61a0de6b611364ec0fb7adffa59ff2af672f7232"; /* echo -n "This is a SHA256 test." | shasum -a256 */ + char sha256Output[65]; + + sha256(sha256String, sha256Output); + assert(strcmp(sha256Output, sha256Digest) == 0); + + /* Base64{Encode,Decode} */ + int Base64Encode(const unsigned char* buffer, const size_t length, char** b64text); + int Base64Decode(const char* b64message, unsigned char** buffer, size_t* length); + const char base64String[] = "This is a Base64 test."; + char *base64Text; + char *base64Decode; + size_t base64DecodeLength; + const char base64EncodeCheck[] = "VGhpcyBpcyBhIEJhc2U2NCB0ZXN0Lg=="; /* echo -n "This is a Base64 test." | b64encode -r - */ + + assert(Base64Encode((unsigned char *) base64String, strlen(base64String), &base64Text) == 0); + assert(strcmp(base64Text, base64EncodeCheck) == 0); + assert(Base64Decode(base64Text, (unsigned char **) &base64Decode, &base64DecodeLength) == 0); + assert(strcmp(base64String, base64Decode) == 0); + + /* public/private key tests */ + const char *pubkeyfile = "public.pem"; + const char *privkeyfile = "private.pem"; + + /* built-in tests */ + assert(test_load_pubkey_from_file(pubkeyfile) == 0); + assert(test_load_private_key_from_file(privkeyfile) == 0); + + /* load public key pair for use in further tests */ + EVP_PKEY *pubkey, *privkey; + pubkey = load_pubkey_from_file(pubkeyfile); + assert(pubkey); + privkey = load_privkey_from_file(privkeyfile); + assert(privkey); + + /* authentication token tests */ + assert(test_authtoken("kilroy", "fubar", pubkey, privkey) == 0); + + /* This should fail because the data is way too long for the RSA key */ + /* assert(test_authtoken("kilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroykilroy", "fubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubarfubar", pubkey, privkey) < 0); */ + + return 0; +} + +int +test_authtoken(const char *authUser, const char *authPassword, EVP_PKEY *pubkey, EVP_PKEY *privkey) { + char *authToken; + char *decodeUser; + char *decodePassword; + time_t decodeTime; + + assert(encode_auth_setting(authUser, authPassword, pubkey, &authToken) == 0); + assert(decode_auth_setting(0, authToken, privkey, &decodeUser, &decodePassword, &decodeTime) == 0); + + assert(strcmp(decodeUser, authUser) == 0); + assert(strcmp(decodePassword, authPassword) == 0); + + time_t now = time(NULL); + + assert(now - decodeTime >= 0); /* time has to go forwards */ + assert(now - decodeTime <= 1); /* shouldn't take more than a second to run */ + + return 0; +} +#else +int +main(int argc, char **argv) +{ + return 0; +} +#endif /* HAVE_SSL */