summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile23
-rw-r--r--common/build.mk3
-rw-r--r--common/mock/README.md60
-rw-r--r--common/mock/build.mk8
-rw-r--r--include/config.h14
-rw-r--r--include/mock_filter.h22
6 files changed, 130 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 5c3a0360cc..0a8177e68c 100644
--- a/Makefile
+++ b/Makefile
@@ -188,6 +188,29 @@ $(foreach c,$(_tsk_cfg_rw) $(_flag_cfg_rw),$(eval $(c)=rw))
$(foreach c,$(_tsk_cfg_ro) $(_flag_cfg_ro),$(eval $(c)=ro))
$(foreach c,$(_tsk_cfg) $(_flag_cfg),$(eval $(c)=y))
+# Fetch list of mocks from .mocklist files for tests and fuzzers.
+# The following will transform the the list of mocks into
+# HAS_MOCK_<NAME> for use in the build systems and CPP,
+# similar to task definitions.
+_mock_lst_flags := $(if $(TEST_FUZZ),-Ifuzz,-Itest) -DTEST_BUILD=$(EMPTY) \
+ -imacros $(PROJECT).mocklist \
+ -I$(BDIR) -DBOARD_$(UC_BOARD)=$(EMPTY) -I$(BASEDIR) \
+ -DBASEBOARD_$(UC_BASEBOARD)=$(EMPTY) \
+ -D_MAKEFILE=$(EMPTY)
+_mock_file := $(if $(TEST_FUZZ),fuzz,test)/$(PROJECT).mocklist
+
+# If test/fuzz build and mockfile exists, source the list of
+# mocks from mockfile.
+_mock_lst :=
+ifneq ($(and $(TEST_BUILD),$(wildcard $(_mock_file))),)
+ _mock_lst += $(shell $(CPP) -P $(_mock_lst_flags) \
+ include/mock_filter.h)
+endif
+
+_mock_cfg := $(foreach t,$(_mock_lst) ,HAS_MOCK_$(t))
+CPPFLAGS += $(foreach t,$(_mock_cfg),-D$(t)=$(EMPTY))
+$(foreach c,$(_mock_cfg),$(eval $(c)=y))
+
ifneq "$(CONFIG_COMMON_RUNTIME)" "y"
_irq_list:=$(shell $(CPP) $(CPPFLAGS) -P -Ichip/$(CHIP) -I$(BASEDIR) \
-I$(BDIR) -D"ENABLE_IRQ(x)=EN_IRQ x" \
diff --git a/common/build.mk b/common/build.mk
index 77732b18f2..d443c85f6a 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -241,3 +241,6 @@ endif
include $(_common_dir)fpsensor/build.mk
include $(_common_dir)usbc/build.mk
+
+include $(_common_dir)mock/build.mk
+common-y+=$(foreach m,$(mock-y),mock/$(m)) \ No newline at end of file
diff --git a/common/mock/README.md b/common/mock/README.md
new file mode 100644
index 0000000000..e1962651a6
--- /dev/null
+++ b/common/mock/README.md
@@ -0,0 +1,60 @@
+# Common Mocks
+This directory holds mock implementations for use in fuzzers and tests.
+
+Each mock is given some friendly build name, like ROLLBACK or FP_SENSOR.
+This name is defined in [common/mock/build.mk](build.mk) and referenced
+from unit tests and fuzzers' `.mocklist` file.
+
+## Creating a new mock
+
+* Add the mock source to [common/mock](/common/mock) and the
+ optional header file to [include/mock](/include/mock).
+ Header files are only necessary if you want to expose additional
+ mock control functions/variables.
+* Add an new entry in [common/mock/build.mk](build.mk)
+ that is conditioned on your mock's name.
+
+If a unit test or fuzzer requests this mock, the build system will
+set the variable `HAS_MOCK_<BUILD_NAME>` to `y` at build time.
+This variable is used to conditionally include the the mock source
+in [common/mock/build.mk](build.mk).
+
+Example line from [common/mock/build.mk](build.mk):
+
+```make
+# Mocks
+mock-$(HAS_MOCK_ROLLBACK) += mock/rollback_mock.o
+```
+
+## Using a mock
+Unit tests and fuzzers can request a particular mock by adding an entry to
+their `.mocklist` file. The mocklist file is similar to a `.tasklist`
+file, where it is named according to the test/fuzz's name followed by
+`.mocklist`, like `fpsensor.mocklist`.
+The mocklist file is optional, so you may need to create one.
+
+Example `.mocklist`:
+
+```c
+/* Copyright 2019 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+ #define CONFIG_TEST_MOCK_LIST \
+ MOCK(ROLLBACK) \
+ MOCK(FP_SENSOR)
+```
+
+If you need additional mock control functionality, you may need to include
+the mock's header file, which is prepended with `mock/`.
+
+For example, to control the return values of the rollback mock:
+
+```c
+#include "mock/rollback_mock.h"
+
+void somefunc() {
+ mock_ctrl_rollback.get_secret_fail = true;
+}
+``` \ No newline at end of file
diff --git a/common/mock/build.mk b/common/mock/build.mk
new file mode 100644
index 0000000000..190dde9be8
--- /dev/null
+++ b/common/mock/build.mk
@@ -0,0 +1,8 @@
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# See common/mock/README.md for more information.
+
+# Example:
+# mock-$(HAS_MOCK_ROLLBACK) += rollback_mock.o \ No newline at end of file
diff --git a/include/config.h b/include/config.h
index 5b7c6661ce..ba18c558a5 100644
--- a/include/config.h
+++ b/include/config.h
@@ -3334,6 +3334,20 @@
#define CONFIG_TASK_PROFILING
/*****************************************************************************/
+/* Mock config */
+
+/*
+ * List of mock implementations to pull into the build.
+ *
+ * This should contain a flat list of MOCK(the-mock-name) elements.
+ *
+ * This is defined in the following two files:
+ * test/{testname}.mocklist
+ * fuzz/{fuzzname}.mocklist
+ */
+#undef CONFIG_TEST_MOCK_LIST
+
+/*****************************************************************************/
/* Temperature sensor config */
/* Compile common code for temperature sensor support */
diff --git a/include/mock_filter.h b/include/mock_filter.h
new file mode 100644
index 0000000000..113c227a3b
--- /dev/null
+++ b/include/mock_filter.h
@@ -0,0 +1,22 @@
+/* Copyright 2019 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/*
+ * Filter mocklists for makefile relevant items.
+ * A mocklist is the .mocklist file in test/ and fuzz/ directories.
+ * See common/mock/README.md for more information.
+ */
+
+#ifndef __CROS_EC_MOCK_FILTER_H
+#define __CROS_EC_MOCK_FILTER_H
+
+/* If included directly from Makefile, dump mock list. */
+#ifdef _MAKEFILE
+#define MOCK(n) n
+CONFIG_TEST_MOCK_LIST
+#endif
+
+
+#endif /* __CROS_EC_MOCK_FILTER_H */