1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/*
* Copyright (C) 2018-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/os_library.h"
#include "shared/test/common/fixtures/memory_management_fixture.h"
#include "shared/test/common/test_macros/test.h"
#include "gtest/gtest.h"
#include <memory>
namespace Os {
extern const char *testDllName;
} // namespace Os
const std::string fakeLibName = "_fake_library_name_";
const std::string fnName = "testDynamicLibraryFunc";
using namespace NEO;
TEST(OSLibraryTest, whenLibraryNameIsEmptyThenCurrentProcesIsUsedAsLibrary) {
std::unique_ptr<OsLibrary> library{OsLibrary::loadFunc({""})};
EXPECT_NE(nullptr, library);
void *ptr = library->getProcAddress("selfDynamicLibraryFunc");
EXPECT_NE(nullptr, ptr);
}
TEST(OSLibraryTest, GivenFakeLibNameWhenLoadingLibraryThenNullIsReturned) {
OsLibrary *library = OsLibrary::loadFunc(fakeLibName);
EXPECT_EQ(nullptr, library);
}
TEST(OSLibraryTest, GivenFakeLibNameWhenLoadingLibraryThenNullIsReturnedAndErrorString) {
std::string errorValue;
OsLibraryCreateProperties properties(fakeLibName);
properties.errorValue = &errorValue;
OsLibrary *library = OsLibrary::loadFunc(properties);
EXPECT_FALSE(errorValue.empty());
EXPECT_EQ(nullptr, library);
}
TEST(OSLibraryTest, GivenValidLibNameWhenLoadingLibraryThenLibraryIsLoaded) {
std::unique_ptr<OsLibrary> library(OsLibrary::loadFunc({Os::testDllName}));
EXPECT_NE(nullptr, library);
}
TEST(OSLibraryTest, GivenValidLibNameWhenLoadingLibraryThenLibraryIsLoadedWithNoErrorString) {
std::string errorValue;
OsLibraryCreateProperties properties(Os::testDllName);
properties.errorValue = &errorValue;
std::unique_ptr<OsLibrary> library(OsLibrary::loadFunc(properties));
EXPECT_TRUE(errorValue.empty());
EXPECT_NE(nullptr, library);
}
TEST(OSLibraryTest, whenSymbolNameIsValidThenGetProcAddressReturnsNonNullPointer) {
std::unique_ptr<OsLibrary> library(OsLibrary::loadFunc({Os::testDllName}));
EXPECT_NE(nullptr, library);
void *ptr = library->getProcAddress(fnName);
EXPECT_NE(nullptr, ptr);
}
TEST(OSLibraryTest, whenSymbolNameIsInvalidThenGetProcAddressReturnsNullPointer) {
std::unique_ptr<OsLibrary> library(OsLibrary::loadFunc({Os::testDllName}));
EXPECT_NE(nullptr, library);
void *ptr = library->getProcAddress(fnName + "invalid");
EXPECT_EQ(nullptr, ptr);
}
using OsLibraryTestWithFailureInjection = Test<MemoryManagementFixture>;
TEST_F(OsLibraryTestWithFailureInjection, GivenFailureInjectionWhenLibraryIsLoadedThenOnlyFailedAllocationIsNull) {
InjectedFunction method = [](size_t failureIndex) {
std::string libName(Os::testDllName);
// System under test
OsLibrary *library = OsLibrary::loadFunc(libName);
if (MemoryManagement::nonfailingAllocation == failureIndex) {
EXPECT_NE(nullptr, library);
} else {
EXPECT_EQ(nullptr, library);
}
// Make sure that we only have 1 buffer allocated at a time
delete library;
};
injectFailures(method);
}
TEST(OsLibrary, whenCallingIndexOperatorThenObjectConvertibleToFunctionOrVoidPointerIsReturned) {
struct MockOsLibrary : OsLibrary {
void *getProcAddress(const std::string &procName) override {
lastRequestedProcName = procName;
return ptrToReturn;
}
bool isLoaded() override { return true; }
std::string getFullPath() override { return std::string(); }
void *ptrToReturn = nullptr;
std::string lastRequestedProcName;
};
MockOsLibrary lib;
int varA;
int varB;
int varC;
using FunctionTypeA = void (*)(int *, float);
using FunctionTypeB = int (*)();
lib.ptrToReturn = &varA;
FunctionTypeA functionA = lib["funcA"];
EXPECT_STREQ("funcA", lib.lastRequestedProcName.c_str());
EXPECT_EQ(&varA, reinterpret_cast<void *>(functionA));
lib.ptrToReturn = &varB;
FunctionTypeB functionB = lib["funcB"];
EXPECT_STREQ("funcB", lib.lastRequestedProcName.c_str());
EXPECT_EQ(&varB, reinterpret_cast<void *>(functionB));
lib.ptrToReturn = &varC;
void *rawPtr = lib["funcC"];
EXPECT_STREQ("funcC", lib.lastRequestedProcName.c_str());
EXPECT_EQ(&varC, rawPtr);
}
|