1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/test/common/fixtures/memory_management_fixture.h"
9 #include "shared/test/common/test_macros/test.h"
10 
11 #include "opencl/source/context/context.h"
12 #include "opencl/source/platform/platform.h"
13 #include "opencl/test/unit_test/fixtures/cl_device_fixture.h"
14 #include "opencl/test/unit_test/mocks/mock_platform.h"
15 
16 #include "CL/cl_gl.h"
17 #include "gtest/gtest.h"
18 
19 #include <memory>
20 
21 using namespace NEO;
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 typedef Test<MemoryManagementFixture> ContextFailureInjection;
25 
TEST_F(ContextFailureInjection,GivenFailedAllocationInjectionWhenCreatingContextThenOutOfHostMemoryErrorIsReturned)26 TEST_F(ContextFailureInjection, GivenFailedAllocationInjectionWhenCreatingContextThenOutOfHostMemoryErrorIsReturned) {
27     auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
28     cl_device_id deviceID = device.get();
29 
30     InjectedFunction method = [deviceID](size_t failureIndex) {
31         auto retVal = CL_INVALID_VALUE;
32         auto context = Context::create<Context>(nullptr, ClDeviceVector(&deviceID, 1), nullptr,
33                                                 nullptr, retVal);
34 
35         if (MemoryManagement::nonfailingAllocation == failureIndex) {
36             EXPECT_EQ(CL_SUCCESS, retVal);
37             EXPECT_NE(nullptr, context);
38         } else {
39             EXPECT_EQ(CL_OUT_OF_HOST_MEMORY, retVal) << "for allocation " << failureIndex;
40             EXPECT_EQ(nullptr, context);
41         }
42 
43         delete context;
44         context = nullptr;
45     };
46     injectFailures(method);
47 }
48 
TEST(InvalidPropertyContextTest,GivenInvalidPropertiesWhenContextIsCreatedThenErrorIsReturned)49 TEST(InvalidPropertyContextTest, GivenInvalidPropertiesWhenContextIsCreatedThenErrorIsReturned) {
50     auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
51     cl_device_id deviceID = device.get();
52     auto pPlatform = NEO::platform();
53     cl_platform_id pid[1];
54     pid[0] = pPlatform;
55     cl_context_properties invalidProperties[5] = {CL_CONTEXT_PLATFORM, (cl_context_properties)pid[0], CL_CGL_SHAREGROUP_KHR, 0x10000, 0};
56     cl_context_properties invalidProperties2[5] = {CL_CONTEXT_PLATFORM, (cl_context_properties)pid[0], (cl_context_properties)0xdeadbeef, 0x10000, 0};
57 
58     cl_int retVal = 0;
59     auto context = Context::create<Context>(invalidProperties, ClDeviceVector(&deviceID, 1), nullptr,
60                                             nullptr, retVal);
61 
62     EXPECT_EQ(CL_INVALID_PROPERTY, retVal);
63     EXPECT_EQ(nullptr, context);
64     delete context;
65 
66     context = Context::create<Context>(invalidProperties2, ClDeviceVector(&deviceID, 1), nullptr,
67                                        nullptr, retVal);
68 
69     EXPECT_EQ(CL_INVALID_PROPERTY, retVal);
70     EXPECT_EQ(nullptr, context);
71     delete context;
72 }
73