tpm2-software / tpm2-tss

OSS implementation of the TCG TPM2 Software Stack (TSS2)
https://tpm2-software.github.io
BSD 2-Clause "Simplified" License
730 stars 359 forks source link

test unit: Failed to build with libcmocka-dev<=0.4.1 on Debian 8.0(Jessie) #608

Closed liuqun closed 6 years ago

liuqun commented 6 years ago

https://github.com/intel/tpm2-tss/blob/fb93568596419c4ac20031db898963abb0aca0ec/configure.ac#L21-L25

Failed to build tpm2-tss with libcmocka-dev<=0.4.1 on Debian 8.0 or older. Old release of libcmocka-dev<=0.4.1 on Debian 8.0 do not support "CMUnitTest" or "cmocka_unit_test". In the old release of /usr/inclue/cmocka.h, "unittest" was defined without prefix "cmocka" and "UnitTest" was defined without prefix "CM":

typedef struct UnitTest {
    const char* name;
    UnitTestFunction function;
    UnitTestFunctionType function_type;
} UnitTest;

/** Initializes a UnitTest structure. */
#define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
struct CMUnitTest {
    const char *name;
    CMUnitTestFunction test_func;
    CMFixtureFunction setup_func;
    CMFixtureFunction teardown_func;
    void *initial_state;
};

/** Initializes a CMUnitTest structure. */
#define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
$ cat /etc/debian_version 
8.0
$ apt show libcmocka-dev
Package: libcmocka-dev
Source: cmocka
Version: 0.4.1-2
Maintainer: David Prévot <taffit@debian.org>
Installed-Size: 62.5 kB
Depends: libcmocka0 (= 0.4.1-2)
Recommends: cmocka-doc
Homepage: http://cmocka.org/
Priority: extra
Section: libdevel
Download-Size: 13.0 kB
APT-Manual-Installed: yes
APT-Sources: http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages
Description: development files for the CMocka unit testing framework
 This package contains the library development files for CMocka.
 .
 The idea of CMocka is that a test application only requires the
 standard C library and the CMocka library itself to minimize conflicts
 with standard C headers especially on a lot of different platforms.
 .
 Features:
  * Easy to use framework to write unit tests
  * Support for mock objects
  * Testing of memory leaks, buffer overflows and underflows
  * Platform and compiler independent

https://github.com/intel/tpm2-tss/blob/77305c06c39ca165f915657522b3bfd8e21e9b71/test/unit/tcti-device.c#L239-L256

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.9/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 4.9.2-10' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.9.2 (Raspbian 4.9.2-10) 

$ git clone --depth=1 --branch mock-built-in-cast --single-branch https://github.com/flihp/tpm2-tss
$ cd tpm2-tss
$ git branch -v
* mock-built-in-cast 0eb6065 test: Use built-in mock with cast functions instead of manual casting.
$ ./bootstrap && ./configure --enable-unit
...

$ make check
...
test/unit/tcti-device.c: In function ‘main’:
test/unit/tcti-device.c:242:29: error: array type has incomplete element type
     const struct CMUnitTest tests[] = {
                             ^
test/unit/tcti-device.c:243:9: error: implicit declaration of function ‘cmocka_unit_test’ [-Werror=implicit-function-declaration]
         cmocka_unit_test (tcti_device_init_all_null_test),
         ^
test/unit/tcti-device.c:248:9: error: implicit declaration of function ‘cmocka_unit_test_setup_teardown’ [-Werror=implicit-function-declaration]
         cmocka_unit_test_setup_teardown (tcti_device_receive_success,
         ^
test/unit/tcti-device.c:255:5: error: implicit declaration of function ‘cmocka_run_group_tests’ [-Werror=implicit-function-declaration]
     return cmocka_run_group_tests (tests, NULL, NULL);
     ^
test/unit/tcti-device.c:242:29: error: unused variable ‘tests’ [-Werror=unused-variable]
     const struct CMUnitTest tests[] = {
                             ^
cc1: all warnings being treated as errors
flihp commented 6 years ago

We updated cmocka to the 1.x releases some time back. This was a major version bump (API changes). Since this doesn't effect compatibility with the libraries (just the unit tests) and building a newer version of cmocka from source is pretty trivial on Debian (this is what I do) I don't think this is a bug, it's a feature.

liuqun commented 6 years ago

in tpm2-tss/configure.ac, checking cmocka >= 1.x.y will simply help other developers understand why gcc report those errors above, for example:

PKG_CHECK_MODULES(
  [CMOCKA], 
  [cmocka >= 1.1.1],
  [
    AC_DEFINE([HAVE_CMOCKA], [1])
  ],
  [
    AC_MSG_ERROR([Please update cmocka to the newest version! https://cmocka.org/])
  ]
)
flihp commented 6 years ago

Sorry I missed your point. I didn't realize that we weren't checking for the version in the configure script. This is definitely the right thing to do. Since the cmocka API we're using is compatible with all 1.0 releases this check should probably be for any cmocka version >= 1.0. Any chance I can get you to turn this into a PR?

flihp commented 6 years ago

I'm getting our repos confused. We have this check in tpm2-abrmd configure.ac, just not here in the TSS. It's a minor change so I'll just make that happen. Thanks for pointing this out @liuqun.