diff options
| author | Peter Jones <pjones@redhat.com> | 2021-08-02 14:55:02 -0400 |
|---|---|---|
| committer | Peter Jones <pjones@redhat.com> | 2021-09-07 17:05:04 -0400 |
| commit | 1f434aa623420f9768c327229053841d4fc904c0 (patch) | |
| tree | 3916a26f966aee0f16ef13ad0ae3964add9fc22b /include | |
| parent | 284f30607aaaab3a8ad502c557e31f7f5dbc0b64 (diff) | |
| download | efi-boot-shim-1f434aa623420f9768c327229053841d4fc904c0.tar.gz efi-boot-shim-1f434aa623420f9768c327229053841d4fc904c0.zip | |
tests: make the CompareGuid() comparison size be less stupid
5f08e671e4eb introduced a CompareGuid() call in the unit test harness,
but unfortunately it has a typo and thus only ever compares the first
pointer-sized word of the guid. With 4-GUIDs, this will usually produce
the correct results; with 1-GUIDs it often won't.
A second issue is that the memcmp() implementation of CompareGuid()
produces a different sort order than comparing field-by-field, and also
a different sort order than comparing the string representation. This
is often not a problem (edk2, for example, never compares anything
except equality of two GUIDs), but when writing test cases it is
extremely helpful to be able to look at a list that is sorted in an
intuitive order.
This patch introduces a guidcmp() function in the test suite, which
compares the binary data in the same order that comparing the two GUIDs'
string representations would.
Signed-off-by: Peter Jones <pjones@redhat.com>
Diffstat (limited to 'include')
| -rw-r--r-- | include/test.h | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/include/test.h b/include/test.h index fedeb782..6d90ef18 100644 --- a/include/test.h +++ b/include/test.h @@ -8,7 +8,13 @@ #ifndef TEST_H_ #define TEST_H_ +#define _GNU_SOURCE + #include <stdarg.h> +#include <stdbool.h> +#include <stddef.h> +#include <string.h> +#include <inttypes.h> #if defined(__aarch64__) #include <aarch64/efibind.h> @@ -61,7 +67,118 @@ extern EFI_SYSTEM_TABLE *ST; extern EFI_BOOT_SERVICES *BS; extern EFI_RUNTIME_SERVICES *RT; -#define CompareGuid(a, b) memcmp(a, b, sizeof(a)) +#define GUID_FMT "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" +#define GUID_ARGS(guid) \ + ((EFI_GUID)guid).Data1, ((EFI_GUID)guid).Data2, ((EFI_GUID)guid).Data3, \ + ((EFI_GUID)guid).Data4[1], ((EFI_GUID)guid).Data4[0], \ + ((EFI_GUID)guid).Data4[2], ((EFI_GUID)guid).Data4[3], \ + ((EFI_GUID)guid).Data4[4], ((EFI_GUID)guid).Data4[5], \ + ((EFI_GUID)guid).Data4[6], ((EFI_GUID)guid).Data4[7] + +static inline INT64 +guidcmp_helper(const EFI_GUID * const guid0, const EFI_GUID * const guid1) +{ +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): Comparing "GUID_FMT" to "GUID_FMT"\n", + __FILE__, __LINE__-1, __func__, + GUID_ARGS(*guid0), GUID_ARGS(*guid1)); +#endif + + if (guid0->Data1 != guid1->Data1) { +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x%"PRIx64"-0x%"PRIx64"->0x%"PRIx64"\n", + __FILE__, __LINE__-1, __func__, + (INT64)guid0->Data1, (INT64)guid1->Data1, + (INT64)guid0->Data1 - (INT64)guid1->Data1); +#endif + return (INT64)guid0->Data1 - (INT64)guid1->Data1; + } + + if (guid0->Data2 != guid1->Data2) { +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x%"PRIx64"-0x%"PRIx64"->0x%"PRIx64"\n", + __FILE__, __LINE__-1, __func__, + (INT64)guid0->Data2, (INT64)guid1->Data2, + (INT64)guid0->Data2 - (INT64)guid1->Data2); +#endif + return (INT64)guid0->Data2 - (INT64)guid1->Data2; + } + + if (guid0->Data3 != guid1->Data3) { +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x%"PRIx64"-0x%"PRIx64"->0x%"PRIx64"\n", + __FILE__, __LINE__-1, __func__, + (INT64)guid0->Data3, (INT64)guid1->Data3, + (INT64)guid0->Data3 - (INT64)guid1->Data3); +#endif + return (INT64)guid0->Data3 - (INT64)guid1->Data3; + } + + /* + * This is out of order because that's also true in the string + * representation of it. + */ + if (guid0->Data4[1] != guid1->Data4[1]) { +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x%"PRIx64"-0x%"PRIx64"->0x%"PRIx64"\n", + __FILE__, __LINE__-1, __func__, + (INT64)guid0->Data4[1], (INT64)guid1->Data4[1], + (INT64)guid0->Data4[1] - (INT64)guid1->Data4[1]); +#endif + return (INT64)guid0->Data4[1] - (INT64)guid1->Data4[1]; + } + + if (guid0->Data4[0] != guid1->Data4[0]) { +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x%"PRIx64"-0x%"PRIx64"->0x%"PRIx64"\n", + __FILE__, __LINE__-1, __func__, + (INT64)guid0->Data4[0], (INT64)guid1->Data4[0], + (INT64)guid0->Data4[0] - (INT64)guid1->Data4[0]); +#endif + return (INT64)guid0->Data4[0] - (INT64)guid1->Data4[0]; + } + + for (UINTN i = 2; i < 8; i++) { + if (guid0->Data4[i] != guid1->Data4[i]) { +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x%"PRIx64"-0x%"PRIx64"->0x%"PRIx64"\n", + __FILE__, __LINE__-1, __func__, + (INT64)guid0->Data4[i], (INT64)guid1->Data4[i], + (INT64)guid0->Data4[i] - (INT64)guid1->Data4[i]); +#endif + return (INT64)guid0->Data4[i] - (INT64)guid1->Data4[i]; + } + } + +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s(): returning 0x0\n", + __FILE__, __LINE__-1, __func__); +#endif + return 0; +} + +static inline int +guidcmp(const EFI_GUID * const guid0, const EFI_GUID * const guid1) +{ + INT64 cmp; + int ret; + EFI_GUID empty; + const EFI_GUID * const guida = guid0 ? guid0 : ∅ + const EFI_GUID * const guidb = guid1 ? guid1 : ∅ + + SetMem(&empty, sizeof(EFI_GUID), 0); + + cmp = guidcmp_helper(guida, guidb); + ret = cmp < 0 ? -1 : (cmp > 0 ? 1 : 0); +#if (defined(SHIM_DEBUG) && SHIM_DEBUG != 0) + printf("%s:%d:%s():CompareGuid("GUID_FMT","GUID_FMT")->%lld (%d)\n", + __FILE__, __LINE__-1, __func__, + GUID_ARGS(*guida), GUID_ARGS(*guidb), cmp, ret); +#endif + return ret; +} + +#define CompareGuid(a, b) guidcmp(a, b) extern int debug; #ifdef dprint |
