1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h"
9 
10 #include "mock_fs_ras.h"
11 
12 extern bool sysmanUltsEnable;
13 
14 using ::testing::_;
15 using ::testing::Matcher;
16 using ::testing::NiceMock;
17 
18 namespace L0 {
19 namespace ult {
20 
21 constexpr uint32_t mockHandleCount = 0;
22 struct SysmanRasFixture : public SysmanDeviceFixture {
23   protected:
24     std::unique_ptr<Mock<RasFsAccess>> pFsAccess;
25     std::vector<ze_device_handle_t> deviceHandles;
26     FsAccess *pFsAccessOriginal = nullptr;
SetUpL0::ult::SysmanRasFixture27     void SetUp() override {
28         if (!sysmanUltsEnable) {
29             GTEST_SKIP();
30         }
31         SysmanDeviceFixture::SetUp();
32         pFsAccess = std::make_unique<NiceMock<Mock<RasFsAccess>>>();
33         pFsAccessOriginal = pLinuxSysmanImp->pFsAccess;
34         pLinuxSysmanImp->pFsAccess = pFsAccess.get();
35         ON_CALL(*pFsAccess.get(), isRootUser())
36             .WillByDefault(::testing::Invoke(pFsAccess.get(), &Mock<RasFsAccess>::userIsRoot));
37         pSysmanDeviceImp->pRasHandleContext->handleList.clear();
38         uint32_t subDeviceCount = 0;
39         Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr);
40         if (subDeviceCount == 0) {
41             deviceHandles.resize(1, device->toHandle());
42         } else {
43             deviceHandles.resize(subDeviceCount, nullptr);
44             Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data());
45         }
46         pSysmanDeviceImp->pRasHandleContext->init(deviceHandles);
47     }
TearDownL0::ult::SysmanRasFixture48     void TearDown() override {
49         if (!sysmanUltsEnable) {
50             GTEST_SKIP();
51         }
52         SysmanDeviceFixture::TearDown();
53         pLinuxSysmanImp->pFsAccess = pFsAccessOriginal;
54     }
55 
get_ras_handlesL0::ult::SysmanRasFixture56     std::vector<zes_ras_handle_t> get_ras_handles(uint32_t count) {
57         std::vector<zes_ras_handle_t> handles(count, nullptr);
58         EXPECT_EQ(zesDeviceEnumRasErrorSets(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
59         return handles;
60     }
61 };
62 
TEST_F(SysmanRasFixture,GivenValidSysmanHandleWhenRasErrorSetsThenCorrectCountIsReported)63 TEST_F(SysmanRasFixture, GivenValidSysmanHandleWhenRasErrorSetsThenCorrectCountIsReported) {
64     uint32_t count = 0;
65     ze_result_t result = zesDeviceEnumRasErrorSets(device->toHandle(), &count, NULL);
66     EXPECT_EQ(ZE_RESULT_SUCCESS, result);
67     EXPECT_EQ(count, mockHandleCount);
68 
69     uint32_t testcount = count + 1;
70     result = zesDeviceEnumRasErrorSets(device->toHandle(), &testcount, NULL);
71     EXPECT_EQ(ZE_RESULT_SUCCESS, result);
72     EXPECT_EQ(testcount, mockHandleCount);
73 
74     count = 0;
75     std::vector<zes_ras_handle_t> handles(count, nullptr);
76     EXPECT_EQ(zesDeviceEnumRasErrorSets(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
77     EXPECT_EQ(count, mockHandleCount);
78 
79     RasImp *pTestRasImp = new RasImp(pSysmanDeviceImp->pRasHandleContext->pOsSysman, ZES_RAS_ERROR_TYPE_CORRECTABLE, device->toHandle());
80     pSysmanDeviceImp->pRasHandleContext->handleList.push_back(pTestRasImp);
81     EXPECT_EQ(zesDeviceEnumRasErrorSets(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
82     EXPECT_EQ(count, mockHandleCount + 1);
83 
84     testcount = count;
85 
86     handles.resize(testcount);
87     EXPECT_EQ(zesDeviceEnumRasErrorSets(device->toHandle(), &testcount, handles.data()), ZE_RESULT_SUCCESS);
88     EXPECT_EQ(testcount, mockHandleCount + 1);
89     EXPECT_NE(nullptr, handles.data());
90 
91     pSysmanDeviceImp->pRasHandleContext->handleList.pop_back();
92     delete pTestRasImp;
93 }
94 
TEST_F(SysmanRasFixture,GivenValidRasHandleWhenGettingRasPropertiesThenSuccessIsReturned)95 TEST_F(SysmanRasFixture, GivenValidRasHandleWhenGettingRasPropertiesThenSuccessIsReturned) {
96     RasImp *pTestRasImp = new RasImp(pSysmanDeviceImp->pRasHandleContext->pOsSysman, ZES_RAS_ERROR_TYPE_CORRECTABLE, device->toHandle());
97     pSysmanDeviceImp->pRasHandleContext->handleList.push_back(pTestRasImp);
98 
99     auto handles = get_ras_handles(mockHandleCount + 1);
100 
101     for (auto handle : handles) {
102         zes_ras_properties_t properties = {};
103         EXPECT_EQ(ZE_RESULT_SUCCESS, zesRasGetProperties(handle, &properties));
104         EXPECT_EQ(properties.pNext, nullptr);
105         EXPECT_EQ(properties.onSubdevice, false);
106         EXPECT_EQ(properties.subdeviceId, 0u);
107         EXPECT_EQ(properties.type, ZES_RAS_ERROR_TYPE_CORRECTABLE);
108     }
109     pSysmanDeviceImp->pRasHandleContext->handleList.pop_back();
110     delete pTestRasImp;
111 }
112 
TEST_F(SysmanRasFixture,GivenValidRasHandleWhileCallingZesRasGetStateThenFailureIsReturned)113 TEST_F(SysmanRasFixture, GivenValidRasHandleWhileCallingZesRasGetStateThenFailureIsReturned) {
114     RasImp *pTestRasImp = new RasImp(pSysmanDeviceImp->pRasHandleContext->pOsSysman, ZES_RAS_ERROR_TYPE_CORRECTABLE, device->toHandle());
115     pSysmanDeviceImp->pRasHandleContext->handleList.push_back(pTestRasImp);
116 
117     auto handles = get_ras_handles(mockHandleCount + 1);
118 
119     for (auto handle : handles) {
120         zes_ras_state_t state = {};
121         EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesRasGetState(handle, 0, &state));
122     }
123     pSysmanDeviceImp->pRasHandleContext->handleList.pop_back();
124     delete pTestRasImp;
125 }
126 
TEST_F(SysmanRasFixture,GivenValidRasHandleWhenCallingzesRasGetConfigAfterzesRasSetConfigThenSuccessIsReturned)127 TEST_F(SysmanRasFixture, GivenValidRasHandleWhenCallingzesRasGetConfigAfterzesRasSetConfigThenSuccessIsReturned) {
128     RasImp *pTestRasImp = new RasImp(pSysmanDeviceImp->pRasHandleContext->pOsSysman, ZES_RAS_ERROR_TYPE_CORRECTABLE, device->toHandle());
129     pSysmanDeviceImp->pRasHandleContext->handleList.push_back(pTestRasImp);
130 
131     auto handles = get_ras_handles(mockHandleCount + 1);
132 
133     for (auto handle : handles) {
134         zes_ras_config_t setConfig = {};
135         zes_ras_config_t getConfig = {};
136         setConfig.totalThreshold = 50;
137         memset(setConfig.detailedThresholds.category, 1, sizeof(setConfig.detailedThresholds.category));
138         EXPECT_EQ(ZE_RESULT_SUCCESS, zesRasSetConfig(handle, &setConfig));
139         EXPECT_EQ(ZE_RESULT_SUCCESS, zesRasGetConfig(handle, &getConfig));
140         EXPECT_EQ(setConfig.totalThreshold, getConfig.totalThreshold);
141         int compare = std::memcmp(setConfig.detailedThresholds.category, getConfig.detailedThresholds.category, sizeof(setConfig.detailedThresholds.category));
142         EXPECT_EQ(0, compare);
143     }
144     pSysmanDeviceImp->pRasHandleContext->handleList.pop_back();
145     delete pTestRasImp;
146 }
147 
TEST_F(SysmanRasFixture,GivenValidRasHandleWhenCallingzesRasSetConfigWithoutPermissionThenFailureIsReturned)148 TEST_F(SysmanRasFixture, GivenValidRasHandleWhenCallingzesRasSetConfigWithoutPermissionThenFailureIsReturned) {
149     ON_CALL(*pFsAccess.get(), isRootUser())
150         .WillByDefault(::testing::Invoke(pFsAccess.get(), &Mock<RasFsAccess>::userIsNotRoot));
151     RasImp *pTestRasImp = new RasImp(pSysmanDeviceImp->pRasHandleContext->pOsSysman, ZES_RAS_ERROR_TYPE_CORRECTABLE, device->toHandle());
152     pSysmanDeviceImp->pRasHandleContext->handleList.push_back(pTestRasImp);
153 
154     auto handles = get_ras_handles(mockHandleCount + 1);
155 
156     for (auto handle : handles) {
157         zes_ras_config_t setConfig = {};
158         setConfig.totalThreshold = 50;
159         memset(setConfig.detailedThresholds.category, 1, sizeof(setConfig.detailedThresholds.category));
160         EXPECT_EQ(ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS, zesRasSetConfig(handle, &setConfig));
161     }
162     pSysmanDeviceImp->pRasHandleContext->releaseRasHandles();
163 }
164 
TEST_F(SysmanRasFixture,GivenValidInstanceWhenOsRasImplementationIsNullThenDestructorIsCalledWithoutException)165 TEST_F(SysmanRasFixture, GivenValidInstanceWhenOsRasImplementationIsNullThenDestructorIsCalledWithoutException) {
166 
167     RasImp *pTestRasImp = new RasImp();
168     pTestRasImp->pOsRas = nullptr;
169     EXPECT_NO_THROW(delete pTestRasImp;);
170 }
171 
172 } // namespace ult
173 } // namespace L0
174