Skip to content

Commit

Permalink
tests: add a mock implementation of {Get,Set}Variable and tests for it
Browse files Browse the repository at this point in the history
Some tests will need variables, and so we need a mock implementation of
the various calls relating to them.

This patch adds implementations for the EFI Runtime Services calls
GetVariable(), SetVariable(), GetNextVariableName(), and
QueryVariableInfo().  Additionally, it enforces tunable limits on
storage for variables, and (with only a little work) the limits can be
different for SetVariable() vs what is returned by QueryVariableInfo().
That is, it can lie to you like real systems do.

Signed-off-by: Peter Jones <[email protected]>
  • Loading branch information
vathpela committed Sep 7, 2021
1 parent 5ed2730 commit 2c9eebc
Show file tree
Hide file tree
Showing 122 changed files with 2,299 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ Make.local
shim_cert.h
/test-*
!/test-*.c
!/test-data/
/test-random.h
version.c
170 changes: 170 additions & 0 deletions include/mock-variables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* mock-variables.h - a mock GetVariable/SetVariable/GNVN/etc
* implementation for testing.
* Copyright Peter Jones <[email protected]>
*/

#ifndef SHIM_MOCK_VARIABLES_H_
#define SHIM_MOCK_VARIABLES_H_

#include "test.h"

EFI_STATUS EFIAPI mock_get_variable(CHAR16 *name, EFI_GUID *guid, UINT32 *attrs,
UINTN *size, VOID *data);
EFI_STATUS EFIAPI mock_get_next_variable_name(UINTN *size, CHAR16 *name,
EFI_GUID *guid);
EFI_STATUS EFIAPI mock_set_variable(CHAR16 *name, EFI_GUID *guid, UINT32 attrs,
UINTN size, VOID *data);
EFI_STATUS EFIAPI mock_query_variable_info(UINT32 attrs,
UINT64 *max_var_storage,
UINT64 *remaining_var_storage,
UINT64 *max_var_size);

struct mock_variable_limits {
UINT32 attrs;
UINT64 *max_var_storage;
UINT64 *remaining_var_storage;
UINT64 *max_var_size;
EFI_STATUS status;

list_t list;
};

typedef enum {
MOCK_SORT_DESCENDING,
MOCK_SORT_PREPEND,
MOCK_SORT_APPEND,
MOCK_SORT_ASCENDING,
MOCK_SORT_MAX_SENTINEL
} mock_sort_policy_t;

extern mock_sort_policy_t mock_variable_sort_policy;

extern list_t mock_default_variable_limits;
extern list_t *mock_qvi_limits;
extern list_t *mock_sv_limits;

struct mock_variable {
CHAR16 *name;
EFI_GUID guid;
void *data;
size_t size;
uint32_t attrs;

list_t list;
};

extern list_t mock_variables;

static inline void
dump_mock_variables(const char * const file,
const int line,
const char * const func)
{
list_t *pos = NULL;
printf("%s:%d:%s(): dumping variables\n", file, line, func);
list_for_each(pos, &mock_variables) {
struct mock_variable *var;

var = list_entry(pos, struct mock_variable, list);
printf("%s:%d:%s(): "GUID_FMT"-%s\n", file, line, func,
GUID_ARGS(var->guid), Str2str(var->name));
}
}

static inline void
dump_mock_variables_if_wrong(const char * const file,
const int line,
const char * const func,
EFI_GUID *guid, CHAR16 *first)
{
UINTN size = 0;
CHAR16 buf[8192] = { 0, };
EFI_STATUS status;

size = sizeof(buf);
buf[0] = L'\0';
status = RT->GetNextVariableName(&size, buf, guid);
if (EFI_ERROR(status)) {
printf("%s:%d:%s() Can't dump variables: %lx\n",
__FILE__, __LINE__, __func__,
(unsigned long)status);
return;
}
buf[size] = L'\0';
if (StrCmp(buf, first) == 0)
return;
printf("%s:%d:%s():expected \"%s\" but got \"%s\". Variables:\n",
file, line, func, Str2str(first), Str2str(buf));
dump_mock_variables(file, line, func);
}

void mock_load_variables(const char *const dirname, const char *filters[],
bool filter_out);
void mock_install_query_variable_info(void);
void mock_uninstall_query_variable_info(void);
void mock_reset_variables(void);
void mock_finalize_vars(void);

typedef enum {
NONE = 0,
CREATE,
DELETE,
APPEND,
REPLACE,
} mock_variable_op_t;

typedef EFI_STATUS (mock_set_variable_pre_hook_t)(CHAR16 *name, EFI_GUID *guid,
UINT32 attrs, UINTN size,
VOID *data);
extern mock_set_variable_pre_hook_t *mock_set_variable_pre_hook;

typedef void (mock_set_variable_post_hook_t)(CHAR16 *name, EFI_GUID *guid,
UINT32 attrs, UINTN size,
VOID *data, EFI_STATUS *status,
mock_variable_op_t op,
const char * const file,
const int line,
const char * const func);
extern mock_set_variable_post_hook_t *mock_set_variable_post_hook;

typedef EFI_STATUS (mock_get_variable_pre_hook_t)(CHAR16 *name, EFI_GUID *guid,
UINT32 *attrs, UINTN *size,
VOID *data);
extern mock_get_variable_pre_hook_t *mock_get_variable_pre_hook;

typedef void (mock_get_variable_post_hook_t)(CHAR16 *name, EFI_GUID *guid,
UINT32 *attrs, UINTN *size,
VOID *data, EFI_STATUS *status,
const char * const file,
const int line,
const char * const func);
extern mock_get_variable_post_hook_t *mock_get_variable_post_hook;

typedef EFI_STATUS (mock_get_next_variable_name_pre_hook_t)(UINTN *size,
CHAR16 *name,
EFI_GUID *guid);
extern mock_get_next_variable_name_pre_hook_t
*mock_get_next_variable_name_pre_hook;

typedef void (mock_get_next_variable_name_post_hook_t)(
UINTN *size, CHAR16 *name, EFI_GUID *guid,
EFI_STATUS *status, const char * const file,
const int line, const char * const func);
extern mock_get_next_variable_name_post_hook_t
*mock_get_next_variable_name_post_hook;

typedef EFI_STATUS (mock_query_variable_info_pre_hook_t)(
UINT32 attrs, UINT64 *max_var_storage,
UINT64 *remaining_var_storage, UINT64 *max_var_size);
extern mock_query_variable_info_pre_hook_t *mock_query_variable_info_pre_hook;

typedef void (mock_query_variable_info_post_hook_t)(
UINT32 attrs, UINT64 *max_var_storage, UINT64 *remaining_var_storage,
UINT64 *max_var_size, EFI_STATUS *status, const char * const file,
const int line, const char * const func);
extern mock_query_variable_info_post_hook_t *mock_query_variable_info_post_hook;

#endif /* !SHIM_MOCK_VARIABLES_H_ */
// vim:fenc=utf-8:tw=75:noet
Loading

0 comments on commit 2c9eebc

Please sign in to comment.