xref: /aosp_15_r20/system/chre/test/simulation/test_base.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2021 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 "test_base.h"
18 
19 #include <gtest/gtest.h>
20 #include <pw_containers/vector.h>
21 
22 #include "chre/core/event_loop_manager.h"
23 #include "chre/core/init.h"
24 #include "chre/platform/linux/platform_log.h"
25 #include "chre/platform/linux/task_util/task_manager.h"
26 #include "chre/util/system/message_router.h"
27 #include "chre/util/time.h"
28 #include "chre_api/chre/version.h"
29 #include "inc/test_util.h"
30 #include "test_util.h"
31 
32 using ::chre::message::MessageRouter;
33 using ::chre::message::MessageRouterSingleton;
34 using ::chre::message::Session;
35 
36 namespace chre {
37 
38 namespace {
39 
40 constexpr size_t kMaxMessageHubs = 2;
41 constexpr size_t kMaxSessions = 25;
42 pw::Vector<MessageRouter::MessageHubRecord, kMaxMessageHubs> gMessageHubs;
43 pw::Vector<Session, kMaxSessions> gSessions;
44 
45 }  // anonymous namespace
46 
47 /**
48  * This base class initializes and runs the event loop.
49  *
50  * This test framework makes use of the TestEventQueue as a primary method
51  * of a test execution barrier (see its documentation for details). To simplify
52  * the test execution flow, it is encouraged that any communication between
53  * threads (e.g. a nanoapp and the main test thread) through this
54  * TestEventQueue. In this way, we can design simulation tests in a way that
55  * validates an expected sequence of events in a well-defined manner.
56  *
57  * To avoid the test from potentially stalling, we also push a timeout event
58  * to the TestEventQueue once a fixed timeout has elapsed since the start of
59  * this test.
60  */
SetUp()61 void TestBase::SetUp() {
62   MessageRouterSingleton::init(gMessageHubs, gSessions);
63   chre::PlatformLogSingleton::init();
64   TaskManagerSingleton::init();
65   TestEventQueueSingleton::init();
66   chre::init();
67   EventLoopManagerSingleton::get()->lateInit();
68 
69   mChreThread = std::thread(
70       []() { EventLoopManagerSingleton::get()->getEventLoop().run(); });
71 
72   auto callback = [](void *) {
73     LOGE("Test timed out ...");
74     TestEventQueueSingleton::get()->pushEvent(
75         CHRE_EVENT_SIMULATION_TEST_TIMEOUT);
76   };
77 
78   ASSERT_TRUE(mSystemTimer.init());
79   ASSERT_TRUE(mSystemTimer.set(callback, nullptr /*data*/,
80                                Nanoseconds(getTimeoutNs())));
81 }
82 
TearDown()83 void TestBase::TearDown() {
84   mSystemTimer.cancel();
85   // Free memory allocated for event on the test queue.
86   TestEventQueueSingleton::get()->flush();
87   EventLoopManagerSingleton::get()->getEventLoop().stop();
88   mChreThread.join();
89 
90   chre::deinit();
91   TestEventQueueSingleton::deinit();
92   TaskManagerSingleton::deinit();
93   deleteNanoappInfos();
94   unregisterAllTestNanoapps();
95   chre::PlatformLogSingleton::deinit();
96   MessageRouterSingleton::deinit();
97 }
98 
TEST_F(TestBase,CanLoadAndStartSingleNanoapp)99 TEST_F(TestBase, CanLoadAndStartSingleNanoapp) {
100   constexpr uint64_t kAppId = 0x0123456789abcdef;
101   constexpr uint32_t kAppVersion = 0;
102   constexpr uint32_t kAppPerms = 0;
103 
104   UniquePtr<Nanoapp> nanoapp = createStaticNanoapp(
105       "Test nanoapp", kAppId, kAppVersion, kAppPerms, defaultNanoappStart,
106       defaultNanoappHandleEvent, defaultNanoappEnd);
107 
108   EventLoopManagerSingleton::get()->deferCallback(
109       SystemCallbackType::FinishLoadingNanoapp, std::move(nanoapp),
110       testFinishLoadingNanoappCallback);
111   waitForEvent(CHRE_EVENT_SIMULATION_TEST_NANOAPP_LOADED);
112 }
113 
TEST_F(TestBase,CanLoadAndStartMultipleNanoapps)114 TEST_F(TestBase, CanLoadAndStartMultipleNanoapps) {
115   constexpr uint64_t kAppId1 = 0x123;
116   constexpr uint64_t kAppId2 = 0x456;
117   constexpr uint32_t kAppVersion = 0;
118   constexpr uint32_t kAppPerms = 0;
119   loadNanoapp("Test nanoapp", kAppId1, kAppVersion, kAppPerms,
120               defaultNanoappStart, defaultNanoappHandleEvent,
121               defaultNanoappEnd);
122 
123   loadNanoapp("Test nanoapp", kAppId2, kAppVersion, kAppPerms,
124               defaultNanoappStart, defaultNanoappHandleEvent,
125               defaultNanoappEnd);
126 
127   uint16_t id1;
128   EXPECT_TRUE(EventLoopManagerSingleton::get()
129                   ->getEventLoop()
130                   .findNanoappInstanceIdByAppId(kAppId1, &id1));
131   uint16_t id2;
132   EXPECT_TRUE(EventLoopManagerSingleton::get()
133                   ->getEventLoop()
134                   .findNanoappInstanceIdByAppId(kAppId2, &id2));
135 
136   EXPECT_NE(id1, id2);
137 }
138 
TEST_F(TestBase,methods)139 TEST_F(TestBase, methods) {
140   CREATE_CHRE_TEST_EVENT(SOME_EVENT, 0);
141 
142   class App : public TestNanoapp {
143    public:
144     explicit App(TestNanoappInfo info) : TestNanoapp(info) {}
145     bool start() override {
146       LOGE("start");
147       mTest = 0xc0ffee;
148       return true;
149     }
150 
151     void handleEvent(uint32_t /*senderInstanceId*/, uint16_t /*eventType*/,
152                      const void * /**eventData*/) override {
153       LOGE("handleEvent %" PRIx16, mTest);
154     }
155 
156     void end() override {
157       LOGE("end");
158     }
159 
160    protected:
161     uint32_t mTest = 0;
162   };
163 
164   uint64_t appId = loadNanoapp(MakeUnique<App>(TestNanoappInfo{.id = 0x456}));
165 
166   sendEventToNanoapp(appId, SOME_EVENT);
167 }
168 
169 // Explicitly instantiate the TestEventQueueSingleton to reduce codesize.
170 template class Singleton<TestEventQueue>;
171 
172 }  // namespace chre
173