Skip to content

Commit

Permalink
Add some test cases, and make "make test" actually work.
Browse files Browse the repository at this point in the history
Note the one test case I'm not 100% sure about.  Someone let me know.

Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela committed Mar 10, 2021
1 parent 9beca88 commit bbdfa72
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 26 deletions.
6 changes: 6 additions & 0 deletions include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct _EFI_CONSOLE_CONTROL_PROTOCOL {
extern VOID console_fini(VOID);
extern VOID setup_verbosity(VOID);
extern UINT32 verbose;
#ifndef SHIM_UNIT_TEST
#define dprint_(fmt, ...) ({ \
UINTN __dprint_ret = 0; \
if (verbose) \
Expand All @@ -101,6 +102,11 @@ extern UINT32 verbose;
#define dprint(fmt, ...) \
dprint_(L"%a:%d:%a() " fmt, __FILE__, __LINE__ - 1, __func__, \
##__VA_ARGS__)
#else
#define dprint_(...)
#define dprint(fmt, ...)
#endif

extern EFI_STATUS EFIAPI vdprint_(const CHAR16 *fmt, const char *file, int line,
const char *func, va_list args);
#define vdprint(fmt, ...) \
Expand Down
17 changes: 17 additions & 0 deletions include/system/string.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
// SPDX-License-Identifier: BSD-2-Clause-Patent
#ifdef SHIM_UNIT_TEST
#include_next <string.h>

__typeof__(strlen) shim_strlen;
__typeof__(strcmp) shim_strcmp;
__typeof__(strncmp) shim_strncmp;
__typeof__(strncasecmp) shim_strncasecmp;
__typeof__(strcasecmp) shim_strcasecmp;
__typeof__(strrchr) shim_strrchr;
__typeof__(strrchr) shim_strrchr;
__typeof__(strnlen) shim_strnlen;
__typeof__(strcpy) shim_strcpy;
__typeof__(strncpy) shim_strncpy;
__typeof__(strdup) shim_strdup;
__typeof__(strndup) shim_strndup;
__typeof__(stpcpy) shim_stpcpy;
__typeof__(strchrnul) shim_strchrnul;
__typeof__(strchr) shim_strchr;

#else
#ifndef _STRING_H
#define _STRING_H
Expand Down
47 changes: 47 additions & 0 deletions include/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,53 @@ extern int debug;
assert(cond); \
})

#define assert_true_return(a, status, fmt, ...) \
({ \
if (!(a)) { \
printf("%s:%d:got %lld, expected nonzero " fmt, \
__func__, __LINE__, (long long)(a), \
##__VA_ARGS__); \
printf("%s:%d:Assertion `%s' failed.\n", __func__, \
__LINE__, __stringify(!(a))); \
return status; \
} \
})
#define assert_nonzero_return(a, ...) assert_true_return(a, ##__VA_ARGS__)

#define assert_false_return(a, status, fmt, ...) \
({ \
if (a) { \
printf("%s:%d:got %lld, expected zero " fmt, __func__, \
__LINE__, (long long)(a), ##__VA_ARGS__); \
printf("%s:%d:Assertion `%s' failed.\n", __func__, \
__LINE__, __stringify(a)); \
return status; \
} \
})
#define assert_zero_return(a, ...) assert_false_return(a, ##__VA_ARGS__)

#define assert_positive_return(a, status, fmt, ...) \
({ \
if ((a) <= 0) { \
printf("%s:%d:got %lld, expected > 0 " fmt, __func__, \
__LINE__, (long long)(a), ##__VA_ARGS__); \
printf("%s:%d:Assertion `%s' failed.\n", __func__, \
__LINE__, __stringify((a) <= 0)); \
return status; \
} \
})

#define assert_negative_return(a, status, fmt, ...) \
({ \
if ((a) >= 0) { \
printf("%s:%d:got %lld, expected < 0 " fmt, __func__, \
__LINE__, (long long)(a), ##__VA_ARGS__); \
printf("%s:%d:Assertion `%s' failed.\n", __func__, \
__LINE__, __stringify((a) >= 0)); \
return status; \
} \
})

#define assert_equal_return(a, b, status, fmt, ...) \
({ \
if (!((a) == (b))) { \
Expand Down
1 change: 1 addition & 0 deletions include/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test-random.h:
xxd -i random.bin test-random.h

test-sbat_FILES = csv.c
test-str_FILES = lib/string.c

tests := $(patsubst %.c,%,$(wildcard test-*.c))

Expand Down
17 changes: 17 additions & 0 deletions lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
#define SHIM_STRING_C_
#include "shim.h"

#ifdef SHIM_UNIT_TEST
#define strlen shim_strlen
#define strcmp shim_strcmp
#define strncmp shim_strncmp
#define strncasecmp shim_strncasecmp
#define strcasecmp shim_strcasecmp
#define strrchr shim_strrchr
#define strlen shim_strlen
#define strcpy shim_strcpy
#define strncpy shim_strncpy
#define strdup shim_strdup
#define strndup shim_strndup
#define stpcpy shim_stpcpy
#define strchrnul shim_strchrnul
#define strchr shim_strchr
#endif

size_t
strlen(const char *s1)
{
Expand Down
4 changes: 4 additions & 0 deletions shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#ifndef SHIM_H_
#define SHIM_H_

#ifdef SHIM_UNIT_TEST
#define _GNU_SOURCE
#endif

#if defined __GNUC__ && defined __GNUC_MINOR__
# define GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
Expand Down
47 changes: 25 additions & 22 deletions test-sbat.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,26 +427,29 @@ test_parse_sbat_var_data(void)
return 0;
}

#if 0
/*
* verify_sbat() tests
* Note: verify_sbat also frees the underlying "sbat_entries" memory.
*/
int
test_verify_sbat_null_sbat(void)
test_verify_sbat_null_sbat_section(void)
{
list_t *test_sbat_entries;
test_sbat_entries = create_mock_sbat_entries_one_entry("test1", "1");
if (!test_sbat_entries)
return -1;
char sbat_var_data[] = "test1,1";
EFI_STATUS status;
list_t test_sbat_var;
size_t n = 0;
struct sbat_section_entry **entries = NULL;

status = verify_sbat(NULL, test_sbat_entries);
INIT_LIST_HEAD(&test_sbat_var);
status = parse_sbat_var_data(&test_sbat_var, sbat_var_data, sizeof(sbat_var_data));
assert_equal_return(status, EFI_SUCCESS, -1, "got %#x expected %#x\n");

assert(status == EFI_INVALID_PARAMETER);
status = verify_sbat_helper(&sbat_var, n, entries);
assert_equal_return(status, EFI_SUCCESS, -1, "got %#x expected %#x\n");
return 0;
}

#if 0
int
test_verify_sbat_null_sbat_entries(void)
{
Expand Down Expand Up @@ -969,22 +972,22 @@ main(void)
test(test_parse_sbat_var_data_null_data);
test(test_parse_sbat_var_data_zero_size);

#if 0
// verify_sbat tests
//test_verify_sbat_null_sbat();
test_verify_sbat_null_sbat_entries();
test_verify_sbat_match_one_exact();
test_verify_sbat_match_one_higher();
test_verify_sbat_reject_one();
test_verify_sbat_reject_many();
test_verify_sbat_match_many_higher();
test_verify_sbat_match_many_exact();
test_verify_sbat_reject_many_all();
test_verify_sbat_match_diff_name();
test_verify_sbat_match_diff_name_mixed();
test_verify_sbat_reject_diff_name_mixed();
test(test_verify_sbat_null_sbat_section);
#if 0
test(test_verify_sbat_null_sbat_entries);
test(test_verify_sbat_match_one_exact);
test(test_verify_sbat_match_one_higher);
test(test_verify_sbat_reject_one);
test(test_verify_sbat_reject_many);
test(test_verify_sbat_match_many_higher);
test(test_verify_sbat_match_many_exact);
test(test_verify_sbat_reject_many_all);
test(test_verify_sbat_match_diff_name);
test(test_verify_sbat_match_diff_name_mixed);
test(test_verify_sbat_reject_diff_name_mixed);
#endif
test_parse_and_verify();
test(test_parse_and_verify);

return 0;
}
Expand Down
Loading

0 comments on commit bbdfa72

Please sign in to comment.