• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (C) 2020 The Android Open Source Project
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  #include <android-base/stringprintf.h>
18  #include <gtest/gtest.h>
19  #include <trusty/coverage/coverage.h>
20  #include <trusty/tipc.h>
21  #include <array>
22  #include <memory>
23  
24  using android::base::unique_fd;
25  using std::array;
26  using std::make_unique;
27  using std::unique_ptr;
28  
29  #define TIPC_DEV "/dev/trusty-ipc-dev0"
30  #define TEST_SRV_PORT "com.android.trusty.sancov.test.srv"
31  #define TEST_SRV_MODULE "srv.syms.elf"
32  
33  namespace android {
34  namespace trusty {
35  namespace coverage {
36  
37  /* Test server's UUID is 77f68803-c514-43ba-bdce-3254531c3d24 */
38  static struct uuid test_srv_uuid = {
39          0x77f68803,
40          0xc514,
41          0x43ba,
42          {0xbd, 0xce, 0x32, 0x54, 0x53, 0x1c, 0x3d, 0x24},
43  };
44  
45  class CoverageTest : public ::testing::Test {
46    public:
SetUp()47      void SetUp() override {
48          record_ = make_unique<CoverageRecord>(TIPC_DEV, &test_srv_uuid);
49          auto ret = record_->Open();
50          ASSERT_TRUE(ret.ok()) << ret.error();
51      }
52  
TearDown()53      void TearDown() override { record_.reset(); }
54  
55      unique_ptr<CoverageRecord> record_;
56  };
57  
TEST_F(CoverageTest,CoverageReset)58  TEST_F(CoverageTest, CoverageReset) {
59      record_->ResetFullRecord();
60      auto counter = record_->TotalEdgeCounts();
61      ASSERT_EQ(counter, 0);
62  }
63  
TEST_F(CoverageTest,TestServerCoverage)64  TEST_F(CoverageTest, TestServerCoverage) {
65      unique_fd test_srv(tipc_connect(TIPC_DEV, TEST_SRV_PORT));
66      ASSERT_GE(test_srv, 0);
67  
68      uint32_t mask = (uint32_t)-1;
69      uint32_t magic = 0xdeadbeef;
70      uint64_t high_watermark = 0;
71  
72      for (size_t i = 1; i < sizeof(magic) * 8; i++) {
73          /* Reset coverage */
74          record_->ResetCounts();
75  
76          /* Send message to test server */
77          uint32_t msg = magic & ~(mask << i);
78          int rc = write(test_srv, &msg, sizeof(msg));
79          ASSERT_EQ(rc, sizeof(msg));
80  
81          /* Read message from test server */
82          rc = read(test_srv, &msg, sizeof(msg));
83          ASSERT_EQ(rc, sizeof(msg));
84  
85          /* Count number of non-unique blocks executed */
86          auto counter = record_->TotalEdgeCounts();
87          /* Each consecutive input should exercise more or same blocks */
88          ASSERT_GE(counter, high_watermark);
89          high_watermark = counter;
90  
91          auto sancov_filename = android::base::StringPrintf(
92                  "/data/local/tmp/" TEST_SRV_MODULE ".%d.sancov", getpid());
93          auto res = record_->SaveSancovFile(sancov_filename);
94          ASSERT_TRUE(res.ok());
95      }
96  
97      ASSERT_GT(high_watermark, 0);
98  }
99  
100  }  // namespace coverage
101  }  // namespace trusty
102  }  // namespace android
103