From 7eab94db8b14a6c13d772694cc480106c731b6cf Mon Sep 17 00:00:00 2001 From: Julien 'Lta' BALLET Date: Tue, 25 Apr 2017 17:12:05 +0200 Subject: [PATCH 1/3] Initial setup of unit testing, with one test Signed-off-by: Julien 'Lta' BALLET --- .gitignore | 1 + Makefile.am | 11 +++++++++ configure.ac | 25 ++++++++++++++----- tests/all_tests.c | 14 +++++++++++ tests/test_hmac.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.h | 43 +++++++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 tests/all_tests.c create mode 100644 tests/test_hmac.c create mode 100644 tests/tests.h diff --git a/.gitignore b/.gitignore index a3b3ad9..a725b64 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ dsm dsm_discover dsm_lookup dsm_inverse +all_tests configure .*/ build*/ diff --git a/Makefile.am b/Makefile.am index 4a78c3c..74b44e0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -113,6 +113,17 @@ dsm_inverse_SOURCES = bin/inverse.c dsm_lookup_SOURCES = bin/lookup.c +if TESTS +bin_PROGRAMS += all_tests + +all_tests_SOURCES = tests/all_tests.c \ + tests/test_hmac.c \ + $(libdsm_la_SOURCES) + +all_tests_CFLAGS = @CMOCKA_CFLAGS@ +all_tests_LDADD = @CMOCKA_LIBS@ libcompat.la $(TASN1_LIBS) @LTLIBICONV@ +endif + LDADD = libdsm.la clean-local: diff --git a/configure.ac b/configure.ac index 56ea1d7..b25aaaf 100644 --- a/configure.ac +++ b/configure.ac @@ -44,8 +44,13 @@ AC_ARG_ENABLE([debug], AS_HELP_STRING([--enable-debug], [Additional debugging features [default=no]]) ) +AC_ARG_ENABLE([tests], +AS_HELP_STRING([--enable-tests], [Build unit tests [default=no]]) +) + AM_CONDITIONAL([DEBUG], [test x"$enable_debug" == x"yes"]) AM_CONDITIONAL([PROGRAMS], [test x"$enable_programs" != x"no"]) +AM_CONDITIONAL([TESTS], [test x"$enable_tests" != x"no"]) LT_INIT @@ -55,10 +60,18 @@ AC_CONFIG_MACRO_DIR([m4]) dnl Check for pthreads AX_PTHREAD(,[ - AC_CHECK_LIB(pthreadGC2,pthread_join,[ - AC_SUBST(PTHREAD_LIBS,"-lpthreadGC2") - AC_SUBST(PTHREAD_CFLAGS,"-DPTW32_STATIC_LIB"]) - ]) + AC_CHECK_LIB(pthreadGC2,pthread_join,[ + AC_SUBST(PTHREAD_LIBS,"-lpthreadGC2") + AC_SUBST(PTHREAD_CFLAGS,"-DPTW32_STATIC_LIB"]) + ]) +]) + +############################## +## Checks for cmocka test framework +AS_IF([test x"$enable_tests" = x"yes"], [ + PKG_CHECK_MODULES([CMOCKA], [cmocka]) + AC_SUBST([CMOCKA_CFLAGS]) + AC_SUBST([CMOCKA_LIBS]) ]) ############################## @@ -110,8 +123,8 @@ AC_CHECK_FUNCS([pipe _pipe getifaddrs]) AC_CHECK_HEADERS([bsd/string.h langinfo.h alloca.h sys/queue.h arpa/inet.h sys/socket.h ifaddrs.h]) ## Configure random device path -AC_ARG_WITH([urandom], - [AS_HELP_STRING([--with-urandom=PATH], +AC_ARG_WITH([urandom], + [AS_HELP_STRING([--with-urandom=PATH], [Configure the path of the random generation device used @<:@default=/dev/urandom@:>@ ])], [], [with_urandom=/dev/urandom]) AC_DEFINE_UNQUOTED([URANDOM], ["$with_urandom"], [Path of the random number generation device]) diff --git a/tests/all_tests.c b/tests/all_tests.c new file mode 100644 index 0000000..c297d5f --- /dev/null +++ b/tests/all_tests.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +#include "tests.h" + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_hmac_md5), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/tests/test_hmac.c b/tests/test_hmac.c new file mode 100644 index 0000000..3179471 --- /dev/null +++ b/tests/test_hmac.c @@ -0,0 +1,61 @@ +/***************************************************************************** + * __________________ _________ _____ _____ .__ ._. + * \______ \______ \ / _____/ / \ / _ \ |__| ____ | | + * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | | + * | | \| ` \/ / Y \ / | | \ ___/ \| + * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __ + * \/ \/ \/ \/ )/ \/ \/ \/ + * + * This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS + * + * Author: Julien 'Lta' BALLET + * + * liBDSM is released under LGPLv2.1 (or later) and is also available + * under a commercial license. + ***************************************************************************** + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include + +#include "tests.h" + +#include "src/hmac_md5.h" +#include "src/smb_ntlm.h" + +void test_hmac_md5(void **s) +{ + (void)s; + + const char key1[] = "12345678"; + const char key2[] = "aaaabbbb"; + const char msg1[] = "'Wut wut' is first message :)"; + const char msg2[] = "A second awesome message !"; + smb_ntlmh hash1, hash2; + + // Hashing the same things should give the same result + HMAC_MD5(key1, strlen(key1), msg1, strlen(msg1), hash1); + HMAC_MD5(key1, strlen(key1), msg1, strlen(msg1), hash2); + assert_memory_equal(hash1, hash2, sizeof(hash1)); + + // Hashing different stuff gives different results + HMAC_MD5(key2, strlen(key2), msg2, strlen(msg2), hash2); + assert_memory_not_equal(hash1, hash2, sizeof(hash1)); + + // Test against a precomputed hmac + smb_ntlmh expected_hash1 = { 0xc7, 0x30, 0x7e, 0x75, 0x1b, 0x42, 0xb9, 0x37, + 0xc8, 0x01, 0x22, 0xe2, 0x09, 0xda, 0x75, 0x0a }; + assert_memory_equal(hash1, expected_hash1, sizeof(hash1)); +} diff --git a/tests/tests.h b/tests/tests.h new file mode 100644 index 0000000..0134bc6 --- /dev/null +++ b/tests/tests.h @@ -0,0 +1,43 @@ +/***************************************************************************** + * __________________ _________ _____ _____ .__ ._. + * \______ \______ \ / _____/ / \ / _ \ |__| ____ | | + * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | | + * | | \| ` \/ / Y \ / | | \ ___/ \| + * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __ + * \/ \/ \/ \/ )/ \/ \/ \/ + * + * This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS + * + * Author: Julien 'Lta' BALLET + * + * liBDSM is released under LGPLv2.1 (or later) and is also available + * under a commercial license. + ***************************************************************************** + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifndef _TESTS_H_ +#define _TESTS_H_ + +#include +#include +#include +#include + +#define declare_test(name) void test_ ## name(void **state); + +declare_test(hmac_md5) + +#endif From 12a858e284090743adf91b697c5e7fc5f8ae2036 Mon Sep 17 00:00:00 2001 From: Julien 'Lta' BALLET Date: Tue, 25 Apr 2017 19:19:23 +0200 Subject: [PATCH 2/3] Document netbios name encode/decode functions Signed-off-by: Julien 'Lta' BALLET --- src/netbios_utils.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/netbios_utils.h b/src/netbios_utils.h index 0471e4f..c8009d1 100644 --- a/src/netbios_utils.h +++ b/src/netbios_utils.h @@ -33,13 +33,47 @@ #include "netbios_defs.h" +/** + * @internal + * @brief Encode the machine name part of a netbios name + * + * @param name The string to encode. Only the first 15 chars are used + * @param encoded_name This string will be filled with the encoded name. Must + * be 33 bytes long + * @param type In netbios, the name encodes the service type, + * like NETBIOS_FILESERVER + */ void netbios_name_level1_encode(const char *name, char *encoded_name, unsigned type); +/** + * @internal + * @brief Decode the machine part of a netbios name + */ void netbios_name_level1_decode(const char *encoded_name, char *name); // XXX: domain support is not implemented + +/** + * @internal + * @brief Encode a netbios machine name. + * + * @param name The name of the machine. Only the first 15 bytes are used + * @param domain Not implemented yet + * @param type The type of service we're talking to. Like NETBIOS_FILESERVER or + * NETBIOS_WORKSTATION + */ char *netbios_name_encode(const char *name, char *domain, unsigned type); + +/** + * @internal + * @brief Decode a netbios machine name. + * + * @param encoded_name The encoded name + * @param name A 16 bytes long string (including null byte). Will be filled + * with the decoded name + * @param domain Not Implemented yet + */ int netbios_name_decode(const char *encoded_name, char *name, char **domain); From 9194cb149e95c476555c1fbddf0efc878353b182 Mon Sep 17 00:00:00 2001 From: Julien 'Lta' BALLET Date: Tue, 25 Apr 2017 19:28:08 +0200 Subject: [PATCH 3/3] Testing smb_buffer, smb_utils and netbios_utils Signed-off-by: Julien 'Lta' BALLET --- Makefile.am | 7 +++- src/smb_buffer.h | 8 +++- tests/all_tests.c | 4 ++ tests/test_netbios_name_encode.c | 59 ++++++++++++++++++++++++++ tests/test_smb_buffer.c | 71 ++++++++++++++++++++++++++++++++ tests/test_smb_utils.c | 66 +++++++++++++++++++++++++++++ tests/tests.h | 7 ++++ 7 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 tests/test_netbios_name_encode.c create mode 100644 tests/test_smb_buffer.c create mode 100644 tests/test_smb_utils.c diff --git a/Makefile.am b/Makefile.am index 74b44e0..2c25a90 100644 --- a/Makefile.am +++ b/Makefile.am @@ -116,9 +116,12 @@ dsm_lookup_SOURCES = bin/lookup.c if TESTS bin_PROGRAMS += all_tests -all_tests_SOURCES = tests/all_tests.c \ +all_tests_SOURCES = $(libdsm_la_SOURCES) \ + tests/all_tests.c \ tests/test_hmac.c \ - $(libdsm_la_SOURCES) + tests/test_smb_utils.c \ + tests/test_smb_buffer.c \ + tests/test_netbios_name_encode.c all_tests_CFLAGS = @CMOCKA_CFLAGS@ all_tests_LDADD = @CMOCKA_LIBS@ libcompat.la $(TASN1_LIBS) @LTLIBICONV@ diff --git a/src/smb_buffer.h b/src/smb_buffer.h index 536380f..2f12c0a 100644 --- a/src/smb_buffer.h +++ b/src/smb_buffer.h @@ -49,14 +49,20 @@ typedef struct } smb_buffer; /** - * @brief Initialize a buffer structure. It'll contain nothing + * @brief Initialize a buffer structure with the provided data * * @param buf Pointer to a buffer to initialize + * @param data Pointer to a memory area to be assigned to the buffer. It'll be + * freed if you call smb_buffer_free + @ @param size Size in bytes of the memory pointed by data */ void smb_buffer_init(smb_buffer *buf, void *data, size_t size); /** * @brief Allocate a size long memory area and place it in the buffer structure + * + * @param buf Pointer to a buffer to initialize + * @param size Size in bytes of the memory area to allocate for this buffer. */ int smb_buffer_alloc(smb_buffer *buf, size_t size); diff --git a/tests/all_tests.c b/tests/all_tests.c index c297d5f..712d1c6 100644 --- a/tests/all_tests.c +++ b/tests/all_tests.c @@ -8,6 +8,10 @@ int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_hmac_md5), + cmocka_unit_test(test_smb_utf16), + cmocka_unit_test(test_smb_buffer_init), + cmocka_unit_test(test_smb_buffer_alloc), + cmocka_unit_test(test_nb_encode), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/tests/test_netbios_name_encode.c b/tests/test_netbios_name_encode.c new file mode 100644 index 0000000..5effbdb --- /dev/null +++ b/tests/test_netbios_name_encode.c @@ -0,0 +1,59 @@ +/***************************************************************************** + * __________________ _________ _____ _____ .__ ._. + * \______ \______ \ / _____/ / \ / _ \ |__| ____ | | + * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | | + * | | \| ` \/ / Y \ / | | \ ___/ \| + * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __ + * \/ \/ \/ \/ )/ \/ \/ \/ + * + * This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS + * + * Author: Julien 'Lta' BALLET + * + * liBDSM is released under LGPLv2.1 (or later) and is also available + * under a commercial license. + ***************************************************************************** + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include +#include + +#include "tests.h" + +#include "src/netbios_utils.h" + +void test_nb_encode(void **s) +{ + (void) s; // Unused + + const char *name1 = "Lta's Mainframe"; + const char *name1_encoded = "EMFEEBCHFDCAENEBEJEOEGFCEBENEFCA"; + const char *name1_decoded = "LTA'S MAINFRAME"; + const char *name2 = "abcdefghijklmnopqrstuvwxyz"; + const char *name2_decoded = "ABCDEFGHIJKLMNO"; + char encoded[33], decoded[16]; + + // Simple + netbios_name_level1_encode(name1, (char *)encoded, NETBIOS_FILESERVER); + assert_string_equal(name1_encoded, encoded); + netbios_name_level1_decode((char *)encoded, (char *)decoded); + assert_string_equal(name1_decoded, decoded); + + // Truncating at 15 chars + netbios_name_level1_encode(name2, (char *)encoded, NETBIOS_FILESERVER); + netbios_name_level1_decode((char *)encoded, (char *)decoded); + assert_string_equal(name2_decoded, decoded); +} diff --git a/tests/test_smb_buffer.c b/tests/test_smb_buffer.c new file mode 100644 index 0000000..128354c --- /dev/null +++ b/tests/test_smb_buffer.c @@ -0,0 +1,71 @@ +/***************************************************************************** + * __________________ _________ _____ _____ .__ ._. + * \______ \______ \ / _____/ / \ / _ \ |__| ____ | | + * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | | + * | | \| ` \/ / Y \ / | | \ ___/ \| + * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __ + * \/ \/ \/ \/ )/ \/ \/ \/ + * + * This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS + * + * Author: Julien 'Lta' BALLET + * + * liBDSM is released under LGPLv2.1 (or later) and is also available + * under a commercial license. + ***************************************************************************** + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include +#include + +#include "tests.h" + +#include "src/smb_buffer.h" + +void test_smb_buffer_init(void **s) +{ + (void) s; // Unused + + void *data; + smb_buffer buf; + + data = malloc(42); + smb_buffer_init(&buf, data, 42); + assert_ptr_equal(buf.data, data); + assert_true(buf.size == 42); + + smb_buffer_free(&buf); + assert_null(buf.data); + assert_true(buf.size == 0); +} + +void test_smb_buffer_alloc(void **s) +{ + (void) s; // Unused + + smb_buffer buf; + + smb_buffer_alloc(&buf, 42); + assert_non_null(buf.data); + assert_true(buf.size == 42); + + // Triggers a segfault is alloc is faulty + ((char *)buf.data)[0] + 42; + + smb_buffer_free(&buf); + assert_null(buf.data); + assert_true(buf.size == 0); +} diff --git a/tests/test_smb_utils.c b/tests/test_smb_utils.c new file mode 100644 index 0000000..f20777a --- /dev/null +++ b/tests/test_smb_utils.c @@ -0,0 +1,66 @@ +/***************************************************************************** + * __________________ _________ _____ _____ .__ ._. + * \______ \______ \ / _____/ / \ / _ \ |__| ____ | | + * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | | + * | | \| ` \/ / Y \ / | | \ ___/ \| + * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __ + * \/ \/ \/ \/ )/ \/ \/ \/ + * + * This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS + * + * Author: Julien 'Lta' BALLET + * + * liBDSM is released under LGPLv2.1 (or later) and is also available + * under a commercial license. + ***************************************************************************** + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include +#include + +#include "tests.h" + +#include "src/smb_utils.h" + +void test_smb_utf16(void **s) +{ + (void) s; // Unused + + const char str1[] = "only ascii chars"; + const char str2[] = "àéïóù"; + size_t result_size1, result_size2; + char *result1, *result2; + + // Only ASCII + result_size1 = smb_to_utf16(str1, strlen(str1), &result1); + assert_true(result_size1 == strlen(str1) * 2); + + result_size2 = smb_from_utf16(result1, result_size1, &result2); + assert_memory_equal(str1, result2, result_size2); + + free(result1); + free(result2); + + // With Noice accents + result_size1 = smb_to_utf16(str2, strlen(str2), &result1); + result_size2 = smb_from_utf16(result1, result_size1, &result2); + assert_memory_equal(str2, result2, result_size2); + + free(result1); + free(result2); + + +} diff --git a/tests/tests.h b/tests/tests.h index 0134bc6..208d0ae 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -40,4 +40,11 @@ declare_test(hmac_md5) +declare_test(smb_utf16) + +declare_test(smb_buffer_init) +declare_test(smb_buffer_alloc) + +declare_test(nb_encode) + #endif