Skip to content

Commit

Permalink
bootloader: fix the cmocka-based tests
Browse files Browse the repository at this point in the history
The functions that were renamed and made static in ac87b6b are
in fact used by cmocka-based tests in the bootloader. Update the
names in the test code, and remove "static" keyword from functions
themselves in order to make them visible to tests during linking.
  • Loading branch information
rokm committed Jan 20, 2023
1 parent f2c5a13 commit 604559c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
6 changes: 4 additions & 2 deletions bootloader/src/pyi_launch.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
*/

/* Constructs the file path from given components and checks that the path exists. */
static int
/* NOTE: must be visible outside of this unit (i.e., non-static) due to tests! */
int
_format_and_check_path(char *buf, const char *fmt, ...)
{
va_list args;
Expand All @@ -69,7 +70,8 @@ _format_and_check_path(char *buf, const char *fmt, ...)
}

/* Splits the item in the form path:filename */
static int
/* NOTE: must be visible outside of this unit (i.e., non-static) due to tests! */
int
_split_dependency_name(char *path, char *filename, const char *item)
{
char *p;
Expand Down
44 changes: 22 additions & 22 deletions bootloader/tests/test_launch.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
#include <setjmp.h> // required fo cmocka :-(
#include <cmocka.h>

int checkFile(char *buf, const char *fmt, ...);
int _format_and_check_path(char *buf, const char *fmt, ...);

static void test_checkFile(void **state) {
static void test__format_and_check_path(void **state) {
char result[PATH_MAX];

// TODO: use some mocks to determine stat() output

errno = 0;
assert_int_equal(-1, checkFile(result, "%s%s%s.pkg", "a1", "bb", "cc", "dd"));
assert_int_equal(-1, _format_and_check_path(result, "%s%s%s.pkg", "a1", "bb", "cc", "dd"));
assert_int_not_equal(errno, 0); // formatting passed, stat failed
assert_string_equal(result, "a1bbcc.pkg");

errno = 0;
assert_int_equal(-1, checkFile(result, "%s", ""));
assert_int_equal(-1, _format_and_check_path(result, "%s", ""));
assert_int_not_equal(errno, 0); // formatting passed, stat failed
assert_string_equal(result, "");

Expand All @@ -42,55 +42,55 @@ static void test_checkFile(void **state) {
// a few bytes more
errno = 0;
path2[PATH_MAX+8] = '\0';
assert_int_equal(-1, checkFile(result, "%s%s%s.pkg", "a1", path2, "ccc"));
assert_int_equal(-1, _format_and_check_path(result, "%s%s%s.pkg", "a1", path2, "ccc"));
assert_int_equal(errno, 0); // formatting formatting failed
// exact length
errno = 0;
path2[PATH_MAX] = '\0';
assert_int_equal(-1, checkFile(result, "%s", path2));
assert_int_equal(-1, _format_and_check_path(result, "%s", path2));
assert_int_equal(errno, 0); // formatting formatting failed
// one byte less
errno = 0;
path2[PATH_MAX-1] = '\0';
assert_int_equal(-1, checkFile(result, "%s", path2));
assert_int_equal(-1, _format_and_check_path(result, "%s", path2));
assert_int_not_equal(errno, 0); // formatting passed, stat failed
}

int splitName(char *path, char *filename, const char *item);
int _split_dependency_name(char *path, char *filename, const char *item);

static void test_splitName(void **state) {
static void test_split_dependency_name(void **state) {
char path[PATH_MAX];
char filename[PATH_MAX];

// TODO: use some mocks to determine

assert_int_equal(0, splitName(path, filename, "aaa:bbb"));
assert_int_equal(0, _split_dependency_name(path, filename, "aaa:bbb"));
assert_string_equal(path, "aaa");
assert_string_equal(filename, "bbb");

assert_int_equal(-1, splitName(path, filename, ""));
assert_int_equal(-1, splitName(path, filename, ":"));
assert_int_equal(-1, splitName(path, filename, "aaa"));
assert_int_equal(-1, splitName(path, filename, "aaa:"));
assert_int_equal(-1, splitName(path, filename, ":bbb"));
assert_int_equal(-1, _split_dependency_name(path, filename, ""));
assert_int_equal(-1, _split_dependency_name(path, filename, ":"));
assert_int_equal(-1, _split_dependency_name(path, filename, "aaa"));
assert_int_equal(-1, _split_dependency_name(path, filename, "aaa:"));
assert_int_equal(-1, _split_dependency_name(path, filename, ":bbb"));

// these cases are not expected to occur in real life
assert_int_equal(0, splitName(path, filename, "aaa:::"));
assert_int_equal(0, _split_dependency_name(path, filename, "aaa:::"));
assert_string_equal(filename, "::");
assert_int_equal(-1, splitName(path, filename, ":::bbb"));
assert_int_equal(-1, _split_dependency_name(path, filename, ":::bbb"));

char *path2 = (char *) malloc(PATH_MAX+10);
memset(path2, 'a', PATH_MAX+8);
path2[10] = ':';
// a few bytes more
path2[PATH_MAX+8] = '\0';
assert_int_equal(-1, splitName(path, filename, path2));
assert_int_equal(-1, _split_dependency_name(path, filename, path2));
// exact length
path2[PATH_MAX] = '\0';
assert_int_equal(-1, splitName(path, filename, path2));
assert_int_equal(-1, _split_dependency_name(path, filename, path2));
// one byte less
path2[PATH_MAX-1] = '\0';
assert_int_equal(0, splitName(path, filename, path2));
assert_int_equal(0, _split_dependency_name(path, filename, path2));
assert_string_equal(path, "aaaaaaaaaa");
}

Expand All @@ -101,8 +101,8 @@ int main(void)
#endif
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_checkFile),
cmocka_unit_test(test_splitName),
cmocka_unit_test(test__format_and_check_path),
cmocka_unit_test(test_split_dependency_name),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit 604559c

Please sign in to comment.