Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / tests / TestPlatformMgr.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *      This file implements a unit test suite for the Platform Manager
21  *      code functionality.
22  *
23  */
24
25 #include <inttypes.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <nlunit-test.h>
32 #include <support/CHIPMem.h>
33 #include <support/CodeUtils.h>
34 #include <support/UnitTestRegistration.h>
35
36 #include <platform/CHIPDeviceLayer.h>
37
38 using namespace chip;
39 using namespace chip::Logging;
40 using namespace chip::Inet;
41 using namespace chip::DeviceLayer;
42
43 // =================================
44 //      Unit tests
45 // =================================
46
47 static void TestPlatformMgr_Init(nlTestSuite * inSuite, void * inContext)
48 {
49     CHIP_ERROR err = PlatformMgr().InitChipStack();
50     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
51 }
52
53 static void TestPlatformMgr_StartEventLoopTask(nlTestSuite * inSuite, void * inContext)
54 {
55     CHIP_ERROR err = PlatformMgr().StartEventLoopTask();
56     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
57 }
58
59 static void TestPlatformMgr_TryLockChipStack(nlTestSuite * inSuite, void * inContext)
60 {
61     bool locked = PlatformMgr().TryLockChipStack();
62     if (locked)
63         PlatformMgr().UnlockChipStack();
64 }
65
66 static int sEventRecieved = 0;
67
68 void DeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg)
69 {
70     // NL_TEST_ASSERT(inSuite, arg == 12345);
71     sEventRecieved++;
72 }
73
74 static void TestPlatformMgr_AddEventHandler(nlTestSuite * inSuite, void * inContext)
75 {
76     CHIP_ERROR error;
77     sEventRecieved = 0;
78     error          = PlatformMgr().AddEventHandler(DeviceEventHandler, 12345);
79     NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR);
80
81 #if 0
82     while (sEventRecieved == 0)
83     {
84     }
85
86     NL_TEST_ASSERT(inSuite, sEventRecieved > 0);
87 #endif
88 }
89
90 /**
91  *   Test Suite. It lists all the test functions.
92  */
93 static const nlTest sTests[] = {
94
95     NL_TEST_DEF("Test PlatformMgr::Init", TestPlatformMgr_Init),
96     NL_TEST_DEF("Test PlatformMgr::StartEventLoopTask", TestPlatformMgr_StartEventLoopTask),
97     NL_TEST_DEF("Test PlatformMgr::TryLockChipStack", TestPlatformMgr_TryLockChipStack),
98     NL_TEST_DEF("Test PlatformMgr::AddEventHandler", TestPlatformMgr_AddEventHandler),
99
100     NL_TEST_SENTINEL()
101 };
102
103 /**
104  *  Set up the test suite.
105  */
106 int TestPlatformMgr_Setup(void * inContext)
107 {
108     CHIP_ERROR error = chip::Platform::MemoryInit();
109     if (error != CHIP_NO_ERROR)
110         return FAILURE;
111     return SUCCESS;
112 }
113
114 /**
115  *  Tear down the test suite.
116  */
117 int TestPlatformMgr_Teardown(void * inContext)
118 {
119     chip::Platform::MemoryShutdown();
120     return SUCCESS;
121 }
122
123 int TestPlatformMgr()
124 {
125     nlTestSuite theSuite = { "PlatformMgr tests", &sTests[0], TestPlatformMgr_Setup, TestPlatformMgr_Teardown };
126
127     // Run test suit againt one context.
128     nlTestRunner(&theSuite, nullptr);
129     return nlTestRunnerStats(&theSuite);
130 }
131
132 CHIP_REGISTER_TEST_SUITE(TestPlatformMgr);