From 5b40e898947e33968f75b4e6e57a43038ab181e5 Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Sun, 1 Nov 2020 13:01:29 -0800 Subject: trusty: coverage: Coverage client library Bug: 169776499 Test: /data/nativetest64/libtrusty_coverage_test/libtrusty_coverage_test Change-Id: I5f432a3df04fe7b0e2940a12f8d28b3d0655791f --- trusty/coverage/coverage_test.cpp | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 trusty/coverage/coverage_test.cpp (limited to 'trusty/coverage/coverage_test.cpp') diff --git a/trusty/coverage/coverage_test.cpp b/trusty/coverage/coverage_test.cpp new file mode 100644 index 000000000..d8df7a46f --- /dev/null +++ b/trusty/coverage/coverage_test.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2020 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 +#include +#include +#include +#include + +using android::base::unique_fd; +using std::array; +using std::make_unique; +using std::unique_ptr; + +#define TIPC_DEV "/dev/trusty-ipc-dev0" +#define TEST_SRV_PORT "com.android.trusty.sancov.test.srv" + +namespace android { +namespace trusty { +namespace coverage { + +/* Test server's UUID is 77f68803-c514-43ba-bdce-3254531c3d24 */ +static struct uuid test_srv_uuid = { + 0x77f68803, + 0xc514, + 0x43ba, + {0xbd, 0xce, 0x32, 0x54, 0x53, 0x1c, 0x3d, 0x24}, +}; + +class CoverageTest : public ::testing::Test { + public: + void SetUp() override { + record_ = make_unique(TIPC_DEV, &test_srv_uuid); + auto ret = record_->Open(); + ASSERT_TRUE(ret.ok()) << ret.error(); + } + + void TearDown() override { record_.reset(); } + + unique_ptr record_; +}; + +TEST_F(CoverageTest, CoverageReset) { + record_->Reset(); + auto counter = record_->CountEdges(); + ASSERT_EQ(counter, 0); +} + +TEST_F(CoverageTest, TestServerCoverage) { + unique_fd test_srv(tipc_connect(TIPC_DEV, TEST_SRV_PORT)); + ASSERT_GE(test_srv, 0); + + uint32_t mask = (uint32_t)-1; + uint32_t magic = 0xdeadbeef; + uint64_t high_watermark = 0; + + for (size_t i = 1; i < sizeof(magic) * 8; i++) { + /* Reset coverage */ + record_->Reset(); + + /* Send message to test server */ + uint32_t msg = magic & ~(mask << i); + int rc = write(test_srv, &msg, sizeof(msg)); + ASSERT_EQ(rc, sizeof(msg)); + + /* Read message from test server */ + rc = read(test_srv, &msg, sizeof(msg)); + ASSERT_EQ(rc, sizeof(msg)); + + /* Count number of non-unique blocks executed */ + auto counter = record_->CountEdges(); + /* Each consecutive input should exercise more or same blocks */ + ASSERT_GE(counter, high_watermark); + high_watermark = counter; + } + + ASSERT_GT(high_watermark, 0); +} + +} // namespace coverage +} // namespace trusty +} // namespace android -- cgit v1.2.3