Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: [WIP]: CMocka unit-test suite ......................................................................
[WIP]: CMocka unit-test suite
Change-Id: I66665f56627b3d99049176bfbebbd771b080370a Signed-off-by: Edward O'Callaghan quasisec@google.com --- M meson.build A subprojects/cmocka.wrap A tests/include/test.h A tests/meson.build A tests/spi_test.c 5 files changed, 147 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/22/41622/1
diff --git a/meson.build b/meson.build index 5d8e630..3d395e8 100644 --- a/meson.build +++ b/meson.build @@ -424,3 +424,23 @@ )
subdir('util') + +# unit-test framework +cmocka_dep = dependency( + 'cmocka', + fallback: ['cmocka', 'cmocka_dep'] +) +flashrom_test_dep = declare_dependency( + include_directories : include_directories('.'), + sources : [ + srcs, + 'cli_common.c', + 'cli_output.c', + 'flashrom.c', + ], + dependencies : [ + deps, + ], +) + +subdir('tests') diff --git a/subprojects/cmocka.wrap b/subprojects/cmocka.wrap new file mode 100644 index 0000000..21e84f9 --- /dev/null +++ b/subprojects/cmocka.wrap @@ -0,0 +1,10 @@ +[wrap-file] +directory = cmocka-1.1.5 + +source_url = https://cmocka.org/files/1.1/cmocka-1.1.5.tar.xz +source_filename = cmocka-1.1.5.tar.xz +source_hash = f0ccd8242d55e2fd74b16ba518359151f6f8383ff8aef4976e48393f77bba8b6 + +patch_url = https://wrapdb.mesonbuild.com/v1/projects/cmocka/1.1.5/3/get_zip +patch_filename = cmocka-1.1.5-3-wrap.zip +patch_hash = 81ce48613680d3c3a0b396ac570c852b290adcd18202fb16aaf703a9493f4348 diff --git a/tests/include/test.h b/tests/include/test.h new file mode 100644 index 0000000..b4e0dd2 --- /dev/null +++ b/tests/include/test.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* This file is part of the coreboot project. */ + +#ifndef _TESTS_TEST_H +#define _TESTS_TEST_H + +/* + * Standard test header that should be included in all tests. For now it just encapsulates the + * include dependencies for Cmocka. Test-specific APIs that are so generic we would want them + * available everywhere could also be added here. + */ + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#endif /* _TESTS_TEST_H */ diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..6d9f8c2 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,26 @@ +root_includes = include_directories('../subprojects') + +srcs = files('spi_test.c') + +mocks = [ + '-Wl,--wrap=physunmap', + '-Wl,--wrap=physmap', + '-Wl,--gc-sections', +] + +flashrom_tests = executable('flashrom_unit_tests', + srcs, + include_directories : root_includes, + c_args : [ + cargs, + '-ffunction-sections', + '-fdata-sections', + # '-DSTANDALONE', + '-DCONFIG_DEFAULT_PROGRAMMER=PROGRAMMER_DUMMY', + '-DCONFIG_DEFAULT_PROGRAMMER_ARGS=""', + ], + export_dynamic : true, + link_args : mocks, + dependencies : [cmocka_dep, flashrom_test_dep], +) +test('cmocka test flashrom', flashrom_tests) diff --git a/tests/spi_test.c b/tests/spi_test.c new file mode 100644 index 0000000..0a0453d --- /dev/null +++ b/tests/spi_test.c @@ -0,0 +1,73 @@ +#include <include/test.h> + +#include "programmer.h" + +#include <string.h> + + +static int mock_spi_send_command(const struct flashctx *flash, + unsigned int write_count, + unsigned int read_count, + const unsigned char *write_buffer, + unsigned char *read_buffer) +{ + return 0xAA55; +} + +static void registered_masters_test_success(void **state) +{ + (void) state; /* unused */ + + static struct spi_master mst = { + .max_data_read = 16, + .max_data_write = 8, + .command = mock_spi_send_command, + .multicommand = default_spi_send_multicommand, + .read = default_spi_read, + .write_256 = default_spi_write_256, + .write_aai = default_spi_write_aai, + }; + assert_int_not_equal(register_spi_master(&mst), ERROR_FLASHROM_BUG); + + struct spi_master *master = ®istered_masters[0].spi; + assert_int_equal(master->max_data_read, 16); + assert_int_equal(master->max_data_write, 8); + assert_int_equal(master->command(NULL,0,0,NULL,NULL), 0xAA55); +} + +/* redefinitions/wrapping */ +void __wrap_physunmap(void *virt_addr, size_t len) +{ + fprintf(stderr, "%s\n", __func__); +} +void *__wrap_physmap(const char *descr, uintptr_t phys_addr, size_t len) +{ + fprintf(stderr, "%s\n", __func__); + return NULL; +} + +static void programmer_init_test_success(void **state) +{ + (void) state; /* unused */ + + assert_int_equal( + programmer_init( + CONFIG_DEFAULT_PROGRAMMER, + strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS) + ), 0 + ); + + struct flashrom_flashctx flashctx = { 0, }; + int ret = probe_flash(®istered_masters[0], 0, &flashctx, 0); + assert_int_not_equal(ret, -1); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(registered_masters_test_success), + cmocka_unit_test(programmer_init_test_success), + }; + + return cmocka_run_group_tests_name("spi_master tests", tests, NULL, NULL); +}
Edward O'Callaghan has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: [WIP]: CMocka unit-test suite ......................................................................
Patch Set 1:
RFC
Hello build bot (Jenkins), Patrick Georgi, Stefan Reinauer, David Hendricks, Angel Pons,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/flashrom/+/41622
to look at the new patch set (#2).
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
tests/: Add CMocka unit-test infrastructure
This adds the CMocka unit-testing infrastructure into the meson build system which we will latter follow up with unit-tests for flashrom's core logic.
BUG=b:157280555 BRANCH=none TEST=builds
Change-Id: I66665f56627b3d99049176bfbebbd771b080370a Signed-off-by: Edward O'Callaghan quasisec@google.com --- M meson.build A subprojects/cmocka.wrap A tests/include/test.h A tests/meson.build A tests/tests.c A tests/tests.h 6 files changed, 102 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/22/41622/2
Hello build bot (Jenkins), Furquan Shaikh, Patrick Georgi, Stefan Reinauer, David Hendricks, Angel Pons, Stuart Langley,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/flashrom/+/41622
to look at the new patch set (#3).
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
tests/: Add CMocka unit-test infrastructure
This adds the CMocka unit-testing infrastructure into the meson build system which we will latter follow up with unit-tests for flashrom's core logic.
BUG=b:157280555 BRANCH=none TEST=builds
Change-Id: I66665f56627b3d99049176bfbebbd771b080370a Signed-off-by: Edward O'Callaghan quasisec@google.com --- M meson.build A subprojects/cmocka.wrap A tests/include/test.h A tests/meson.build A tests/tests.c A tests/tests.h 6 files changed, 102 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/22/41622/3
Angel Pons has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
Patch Set 3:
(3 comments)
https://review.coreboot.org/c/flashrom/+/41622/3//COMMIT_MSG Commit Message:
https://review.coreboot.org/c/flashrom/+/41622/3//COMMIT_MSG@10 PS3, Line 10: meson Only meson? It would be nice to handle the "we have two build systems" problem someday. AFAIK, Jenkins uses the Makefile to build-test things, and it would be good to run these tests automatically.
@pgeorgi any ideas? How much hassle would be to have Jenkins run the unit tests alongside build tests?
https://review.coreboot.org/c/flashrom/+/41622/3/tests/include/test.h File tests/include/test.h:
https://review.coreboot.org/c/flashrom/+/41622/3/tests/include/test.h@2 PS3, Line 2: /* This file is part of the coreboot project. */ This was dropped in upstream coreboot
https://review.coreboot.org/c/flashrom/+/41622/3/tests/tests.c File tests/tests.c:
PS3: No license headers here?
Edward O'Callaghan has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
Patch Set 3:
(3 comments)
https://review.coreboot.org/c/flashrom/+/41622/3//COMMIT_MSG Commit Message:
https://review.coreboot.org/c/flashrom/+/41622/3//COMMIT_MSG@10 PS3, Line 10: meson
Only meson? It would be nice to handle the "we have two build systems" problem someday. […]
I can follow up with something for the Makefile once we get this initial attempt into shape by taking out any of the major issues if any?
I'll follow up with Patrick or Stefan about the buildbot as we definitely want to have it run the unit-tests once they are merged. I think we should have the buildbot build with meson as well.
https://review.coreboot.org/c/flashrom/+/41622/3/tests/include/test.h File tests/include/test.h:
https://review.coreboot.org/c/flashrom/+/41622/3/tests/include/test.h@2 PS3, Line 2: /* This file is part of the coreboot project. */
This was dropped in upstream coreboot
I think it is pretty useful here unless I am missing a good reason to get rid of it?
https://review.coreboot.org/c/flashrom/+/41622/3/tests/tests.c File tests/tests.c:
PS3:
No license headers here?
Done
Hello build bot (Jenkins), Furquan Shaikh, Patrick Georgi, Stefan Reinauer, David Hendricks, Angel Pons, Stuart Langley,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/flashrom/+/41622
to look at the new patch set (#4).
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
tests/: Add CMocka unit-test infrastructure
This adds the CMocka unit-testing infrastructure into the meson build system which we will latter follow up with unit-tests for flashrom's core logic.
BUG=b:157280555 BRANCH=none TEST=builds
Change-Id: I66665f56627b3d99049176bfbebbd771b080370a Signed-off-by: Edward O'Callaghan quasisec@google.com --- M meson.build A subprojects/cmocka.wrap A tests/include/test.h A tests/meson.build A tests/tests.c A tests/tests.h 6 files changed, 105 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/22/41622/4
Edward O'Callaghan has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
Patch Set 4:
Can we get the first few patches merged?
Stefan Reinauer has posted comments on this change. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
Patch Set 4: Code-Review+2
Edward O'Callaghan has submitted this change. ( https://review.coreboot.org/c/flashrom/+/41622 )
Change subject: tests/: Add CMocka unit-test infrastructure ......................................................................
tests/: Add CMocka unit-test infrastructure
This adds the CMocka unit-testing infrastructure into the meson build system which we will latter follow up with unit-tests for flashrom's core logic.
BUG=b:157280555 BRANCH=none TEST=builds
Change-Id: I66665f56627b3d99049176bfbebbd771b080370a Signed-off-by: Edward O'Callaghan quasisec@google.com Reviewed-on: https://review.coreboot.org/c/flashrom/+/41622 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Stefan Reinauer stefan.reinauer@coreboot.org --- M meson.build A subprojects/cmocka.wrap A tests/include/test.h A tests/meson.build A tests/tests.c A tests/tests.h 6 files changed, 105 insertions(+), 0 deletions(-)
Approvals: build bot (Jenkins): Verified Stefan Reinauer: Looks good to me, approved
diff --git a/meson.build b/meson.build index 5d8e630..3d395e8 100644 --- a/meson.build +++ b/meson.build @@ -424,3 +424,23 @@ )
subdir('util') + +# unit-test framework +cmocka_dep = dependency( + 'cmocka', + fallback: ['cmocka', 'cmocka_dep'] +) +flashrom_test_dep = declare_dependency( + include_directories : include_directories('.'), + sources : [ + srcs, + 'cli_common.c', + 'cli_output.c', + 'flashrom.c', + ], + dependencies : [ + deps, + ], +) + +subdir('tests') diff --git a/subprojects/cmocka.wrap b/subprojects/cmocka.wrap new file mode 100644 index 0000000..21e84f9 --- /dev/null +++ b/subprojects/cmocka.wrap @@ -0,0 +1,10 @@ +[wrap-file] +directory = cmocka-1.1.5 + +source_url = https://cmocka.org/files/1.1/cmocka-1.1.5.tar.xz +source_filename = cmocka-1.1.5.tar.xz +source_hash = f0ccd8242d55e2fd74b16ba518359151f6f8383ff8aef4976e48393f77bba8b6 + +patch_url = https://wrapdb.mesonbuild.com/v1/projects/cmocka/1.1.5/3/get_zip +patch_filename = cmocka-1.1.5-3-wrap.zip +patch_hash = 81ce48613680d3c3a0b396ac570c852b290adcd18202fb16aaf703a9493f4348 diff --git a/tests/include/test.h b/tests/include/test.h new file mode 100644 index 0000000..b4e0dd2 --- /dev/null +++ b/tests/include/test.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* This file is part of the coreboot project. */ + +#ifndef _TESTS_TEST_H +#define _TESTS_TEST_H + +/* + * Standard test header that should be included in all tests. For now it just encapsulates the + * include dependencies for Cmocka. Test-specific APIs that are so generic we would want them + * available everywhere could also be added here. + */ + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#endif /* _TESTS_TEST_H */ diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..0c21cb9 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,28 @@ +root_includes = include_directories('../subprojects') + +srcs = [ + 'tests.c', +] + +mocks = [ + '-Wl,--wrap=physunmap', + '-Wl,--wrap=physmap', + '-Wl,--gc-sections', +] + +flashrom_tests = executable('flashrom_unit_tests', + srcs, + include_directories : root_includes, + c_args : [ + cargs, + '-ffunction-sections', + '-fdata-sections', + # '-DSTANDALONE', + '-DCONFIG_DEFAULT_PROGRAMMER=PROGRAMMER_DUMMY', + '-DCONFIG_DEFAULT_PROGRAMMER_ARGS=""', + ], + export_dynamic : true, + link_args : mocks, + dependencies : [cmocka_dep, flashrom_test_dep], +) +test('cmocka test flashrom', flashrom_tests) diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..32353fa --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* This file is part of the coreboot project. */ + +#include <include/test.h> +#include "tests.h" + +#include <stdio.h> + +/* redefinitions/wrapping */ +void __wrap_physunmap(void *virt_addr, size_t len) +{ + fprintf(stderr, "%s\n", __func__); +} +void *__wrap_physmap(const char *descr, uintptr_t phys_addr, size_t len) +{ + fprintf(stderr, "%s\n", __func__); + return NULL; +} + +int main(void) +{ + int ret = 0; + + return ret; +} diff --git a/tests/tests.h b/tests/tests.h new file mode 100644 index 0000000..b088e24 --- /dev/null +++ b/tests/tests.h @@ -0,0 +1,4 @@ +#ifndef TESTS_H +#define TESTS_H + +#endif /* TESTS_H */