diff options
Diffstat (limited to 'core/tests')
-rw-r--r-- | core/tests/Android.mk | 2 | ||||
-rw-r--r-- | core/tests/gtest_stubs.cpp | 54 | ||||
-rw-r--r-- | core/tests/gtest_stubs.h | 129 | ||||
-rw-r--r-- | core/tests/manifest.c | 25 | ||||
-rw-r--r-- | core/tests/nvram_manager_test.cpp | 4 | ||||
-rw-r--r-- | core/tests/rules.mk | 35 |
6 files changed, 248 insertions, 1 deletions
diff --git a/core/tests/Android.mk b/core/tests/Android.mk index f3d3b36..f3b19d8 100644 --- a/core/tests/Android.mk +++ b/core/tests/Android.mk @@ -22,7 +22,7 @@ LOCAL_MODULE_TAGS := debug LOCAL_SRC_FILES := \ fake_storage.cpp \ nvram_manager_test.cpp -LOCAL_CFLAGS := -Wall -Werror -Wextra +LOCAL_CFLAGS := -Wall -Werror -Wextra -DHAS_GTEST LOCAL_CLANG := true LOCAL_STATIC_LIBRARIES := libnvram-core-host libmincrypt LOCAL_SHARED_LIBRARIES := libnvram-messages-host diff --git a/core/tests/gtest_stubs.cpp b/core/tests/gtest_stubs.cpp new file mode 100644 index 0000000..bb8351d --- /dev/null +++ b/core/tests/gtest_stubs.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest_stubs.h" + +namespace testing { + +bool g_test_status = false; + +namespace detail { + +void TestRegistry::RunAllTests() { + int test_count = 0; + int test_failures = 0; + for (TestDeclarationBase* decl = tests_; decl; decl = decl->next) { + TestInstanceBase* instance = decl->create_function(); + fprintf(stderr, "[ %s ] Starting...\n", decl->name); + g_test_status = true; + instance->Run(); + test_failures += g_test_status ? 0 : 1; + ++test_count; + fprintf(stderr, "[ %s ] %s\n", decl->name, g_test_status ? "PASS" : "FAIL"); + delete instance; + } + + fprintf(stderr, "Ran %d tests, %d failures.\n", test_count, test_failures); +} + +void TestRegistry::Register(TestDeclarationBase* test_declaration) { + test_declaration->next = tests_; + tests_ = test_declaration; +} + +TestRegistry TestRegistry::g_instance; + +} // namespace detail +} // namespace testing + +int main() { + testing::detail::TestRegistry::instance()->RunAllTests(); +} diff --git a/core/tests/gtest_stubs.h b/core/tests/gtest_stubs.h new file mode 100644 index 0000000..e2dc33c --- /dev/null +++ b/core/tests/gtest_stubs.h @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * This is a minimal set of stubs to compile nvram_manager_test.cpp in + * environments where googletest is not available. The test status output isn't + * as pretty, but good enough to follow test progress and pinpoint test + * failures. + */ + +extern "C" { +#include <stdio.h> +} // extern "C" + +namespace testing { + +// Global test status. +extern bool g_test_status; + +#define ASSERT_MSG(cond) \ + if (!(cond)) { \ + testing::g_test_status = false; \ + fprintf(stderr, "Assertion failed: " #cond "\n"); \ + return; \ + } +#define ASSERT_TRUE(cond) ASSERT_MSG(cond) +#define ASSERT_EQ(expected, actual) ASSERT_MSG(expected == actual) + +#define EXPECT_MSG(cond) \ + if (!(cond)) { \ + testing::g_test_status = false; \ + fprintf(stderr, "Expectation failed: " #cond "\n"); \ + } +#define EXPECT_TRUE(cond) EXPECTED_MSG(cond) +#define EXPECT_EQ(expected, actual) EXPECT_MSG((expected) == (actual)) +#define EXPECT_NE(expected, actual) EXPECT_MSG((expected) != (actual)) + +// Test fixture base class. +class Test {}; + +namespace detail { + +// A polymorphic wrapper around test instances. This is the base class that +// defines the common interface. +class TestInstanceBase { + public: + virtual ~TestInstanceBase() = default; + virtual void Run() = 0; +}; + +// Test-specific subclass that holds an instance of the test. +template<typename TestCase> +class TestInstance : public TestInstanceBase { + public: + ~TestInstance() override = default; + + static TestInstanceBase* Create() { + return new TestInstance<TestCase>; + } + + private: + void Run() override { + test_.Run(); + } + + TestCase test_; +}; + +struct TestDeclarationBase; +using CreateTestInstanceFunction = TestInstanceBase*(void); + +// |TestRegistry| keeps track of all registered tests. +class TestRegistry { + public: + static TestRegistry* instance() { return &g_instance; } + + void RunAllTests(); + void Register(TestDeclarationBase* test_declaration); + + private: + TestDeclarationBase* tests_ = nullptr; + + static TestRegistry g_instance; +}; + +struct TestDeclarationBase { + TestDeclarationBase(const char* name, + CreateTestInstanceFunction* create_function) + : name(name), create_function(create_function) { + TestRegistry::instance()->Register(this); + } + + const char* name; + CreateTestInstanceFunction* create_function; + TestDeclarationBase* next; +}; + +} // namespace detail + +// Registers |TestCase| with |TestRegistry|. +template <typename TestCase> +struct TestDeclaration : public detail::TestDeclarationBase { + TestDeclaration(const char* name) + : TestDeclarationBase(name, &detail::TestInstance<TestCase>::Create) {} +}; + +#define TEST_F(fixture, name) \ + class fixture##_##name : public fixture { \ + public: \ + void Run(); \ + }; \ + static testing::TestDeclaration<fixture##_##name> \ + g_##fixture##_##name##_declaration(#name); \ + void fixture##_##name::Run() + +} // namespace testing diff --git a/core/tests/manifest.c b/core/tests/manifest.c new file mode 100644 index 0000000..a45db92 --- /dev/null +++ b/core/tests/manifest.c @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <trusty_app_manifest.h> + +trusty_app_manifest_t TRUSTY_APP_MANIFEST_ATTRS trusty_app_manifest = { + // UUID: {879419fa-37a8-44a1-b719-504d03b7502a} + {0x879419fa, + 0x37a8, + 0x44a1, + {0xb7, 0x19, 0x50, 0x4d, 0x03, 0xb7, 0x50, 0x2a}}, +}; diff --git a/core/tests/nvram_manager_test.cpp b/core/tests/nvram_manager_test.cpp index ef4a118..b42f26a 100644 --- a/core/tests/nvram_manager_test.cpp +++ b/core/tests/nvram_manager_test.cpp @@ -14,7 +14,11 @@ * limitations under the License. */ +#if defined(HAS_GTEST) #include <gtest/gtest.h> +#else +#include "gtest_stubs.h" +#endif #include <string.h> diff --git a/core/tests/rules.mk b/core/tests/rules.mk new file mode 100644 index 0000000..5dae25a --- /dev/null +++ b/core/tests/rules.mk @@ -0,0 +1,35 @@ +# +# Copyright (C) 2016 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_DIR := $(GET_LOCAL_DIR) + +MODULE := $(LOCAL_DIR) + +MODULE_SRCS += \ + $(LOCAL_DIR)/nvram_manager_test.cpp \ + $(LOCAL_DIR)/fake_storage.cpp \ + $(LOCAL_DIR)/gtest_stubs.cpp \ + $(LOCAL_DIR)/manifest.c + +MODULE_CPPFLAGS := -Wall -Werror -Wextra -std=c++11 + +MODULE_DEPS += \ + app/trusty \ + lib/libc-trusty \ + system/nvram/core + +include make/module.mk + |