Skip to content

Commit 4765e48

Browse files
test: ensure no stdout output when calling zeInitDrivers (#320)
Signed-off-by: Alicja Lukaszewicz <alicja.lukaszewicz@intel.com>
1 parent 74997ed commit 4765e48

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

test/loader_api.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@
1212
#include "ze_api.h"
1313
#include "zes_api.h"
1414

15+
#include <fstream>
16+
1517
#if defined(_WIN32)
18+
#include <io.h>
19+
#include <cstdio>
20+
#include <fcntl.h>
1621
#define putenv_safe _putenv
1722
#else
23+
#include <cstdlib>
24+
#include <sys/types.h>
25+
#include <unistd.h>
26+
#define _dup dup
27+
#define _dup2 dup2
28+
#define _close close
1829
#define putenv_safe putenv
1930
#endif
2031

@@ -365,4 +376,68 @@ TEST(
365376
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
366377
}
367378

379+
class CaptureOutput {
380+
private:
381+
int original_fd;
382+
int fd;
383+
int stream;
384+
char filename[50] = "/tmp/capture_output_XXXXXX";
385+
386+
public:
387+
enum { Stdout = 1, Stderr = 2 };
388+
389+
CaptureOutput(int stream_) : stream(stream_) {
390+
original_fd = _dup(stream);
391+
#if defined(__linux__)
392+
fd = mkstemp(filename);
393+
#elif defined(_WIN32)
394+
tmpnam_s(filename, sizeof(filename));
395+
_sopen_s(&fd, filename, _O_CREAT | _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
396+
#endif
397+
fflush(nullptr);
398+
_dup2(fd, stream);
399+
_close(fd);
400+
}
401+
402+
~CaptureOutput() {
403+
if (original_fd != -1) {
404+
fflush(nullptr);
405+
_dup2(original_fd, stream);
406+
_close(original_fd);
407+
original_fd = -1;
408+
}
409+
if (remove(filename) != 0) {
410+
std::cerr << "Deleting file " << filename << " failed.";
411+
}
412+
}
413+
414+
std::string GetOutput() {
415+
if (original_fd != -1) {
416+
fflush(nullptr);
417+
_dup2(original_fd, stream);
418+
_close(original_fd);
419+
original_fd = -1;
420+
}
421+
std::ifstream stream(filename);
422+
std::string output = std::string((std::istreambuf_iterator<char>(stream)),
423+
std::istreambuf_iterator<char>());
424+
return output;
425+
}
426+
};
427+
428+
TEST(
429+
LoaderInitDrivers,
430+
GivenZeInitDriverWhenCalledThenNoOutputIsPrintedToStdout) {
431+
uint32_t pInitDriversCount = 0;
432+
ze_init_driver_type_desc_t desc = { ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC };
433+
desc.flags = UINT32_MAX;
434+
desc.pNext = nullptr;
435+
436+
CaptureOutput capture(CaptureOutput::Stdout);
437+
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
438+
439+
std::string output = capture.GetOutput();
440+
EXPECT_TRUE(output.empty());
441+
}
442+
368443
} // namespace

0 commit comments

Comments
 (0)