Skip to content

Commit

Permalink
Initial setup of unit testing, with one test
Browse files Browse the repository at this point in the history
Signed-off-by: Julien 'Lta' BALLET <[email protected]>
  • Loading branch information
elthariel committed Apr 25, 2017
1 parent 5c874e1 commit 7eab94d
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dsm
dsm_discover
dsm_lookup
dsm_inverse
all_tests
configure
.*/
build*/
Expand Down
11 changes: 11 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 19 additions & 6 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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])

This comment has been minimized.

Copy link
@chouquette

chouquette Apr 25, 2017

Contributor

Why not using CMOCKA_CFLAGS directly? PKG_CHECK_MODULE should be setting those.

This comment has been minimized.

Copy link
@elthariel

elthariel Apr 25, 2017

Author Contributor

The docs said recent versions of PKG_CHECK_MODULE were already doing the AC_SUBST, but not the older versions, hence the call

This comment has been minimized.

Copy link
@elthariel

elthariel Apr 25, 2017

Author Contributor

But I might remove it as well if we're sure that we don't care about old versions.

])

##############################
Expand Down Expand Up @@ -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])
Expand Down
14 changes: 14 additions & 0 deletions tests/all_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include "tests.h"

int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_hmac_md5),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
61 changes: 61 additions & 0 deletions tests/test_hmac.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*****************************************************************************
* __________________ _________ _____ _____ .__ ._.
* \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
* | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
* | | \| ` \/ / Y \ / | | \ ___/ \|
* |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
* \/ \/ \/ \/ )/ \/ \/ \/
*
* This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS
*
* Author: Julien 'Lta' BALLET <[email protected]>
*
* 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 <string.h>

#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));
}
43 changes: 43 additions & 0 deletions tests/tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*****************************************************************************
* __________________ _________ _____ _____ .__ ._.
* \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
* | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
* | | \| ` \/ / Y \ / | | \ ___/ \|
* |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
* \/ \/ \/ \/ )/ \/ \/ \/
*
* This file is part of liBDSM. Copyright © 2014-2017 VideoLabs SAS
*
* Author: Julien 'Lta' BALLET <[email protected]>
*
* 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 <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#define declare_test(name) void test_ ## name(void **state);

declare_test(hmac_md5)

#endif

4 comments on commit 7eab94d

@chouquette
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you don't want to use the autotools test system? It's old school and ugly, but well, it integrates nicely with autotools, while other systems might prove a bit more difficult (AFAIK you either build the tests from automake, and run them separately (though explicitely from CLI), or you run them when invoking make check, but you don't have a way of running a specific test)

@elthariel
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite surprised you're monitoring commits on random branches on this repo.

Do we really care at all about this ? Very little of this codebase is actually unit testable anyway.

@chouquette
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently we have a bot that monitors all the branches :)

It's just quick remarks (from someone not using the autotools test harness, so I'd understand if you want to use something fancier ;))

@elthariel
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite impressive anyway :)

I don't think any of those tools will make POC unit testing more pleasurable. This one had a nice website with colors and looked pretty simple.

Please sign in to comment.