mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 20:50:44 +00:00
120 lines
5.1 KiB
C++
120 lines
5.1 KiB
C++
#include "synthetic_storage.h"
|
|||
|
|
|
||
|
|
#include <array>
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstring>
|
||
|
|
|
||
|
|
using namespace bx::storage;
|
||
|
|
|
||
|
|
#define CHECK(condition) \
|
||
|
|
do { \
|
||
|
|
if (!(condition)) { \
|
||
|
|
std::fprintf(stderr, "xcrdapi:%d: %s (Win32 error %lu)\n", \
|
||
|
|
__LINE__, #condition, static_cast<unsigned long>(GetLastError())); \
|
||
|
|
return 1; \
|
||
|
|
} \
|
||
|
|
} while (false)
|
||
|
|
|
||
|
|
template<class Interface>
|
||
|
|
struct Com {
|
||
|
|
Interface* value = nullptr;
|
||
|
|
|
||
|
|
~Com() { if (value) value->Release(); }
|
||
|
|
Interface* operator->() const { return value; }
|
||
|
|
void** out() { return reinterpret_cast<void**>(&value); }
|
||
|
|
};
|
||
|
|
|
||
|
|
struct TemporaryFile {
|
||
|
|
WCHAR name[MAX_PATH]{};
|
||
|
|
~TemporaryFile() { if (*name) DeleteFileW(name); }
|
||
|
|
};
|
||
|
|
|
||
|
|
struct Event {
|
||
|
|
HANDLE handle = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||
|
|
~Event() { if (handle) CloseHandle(handle); }
|
||
|
|
};
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
void* unsupported = nullptr;
|
||
|
|
CHECK(DStorageGetFactory(file_iid, &unsupported) == E_NOINTERFACE && !unsupported);
|
||
|
|
CHECK(DStorageGetFactory(factory_iid, nullptr) == E_POINTER);
|
||
|
|
Com<Factory> factory;
|
||
|
|
CHECK(DStorageGetFactory(factory_iid, factory.out()) == S_OK);
|
||
|
|
|
||
|
|
Com<IUnknown> identity;
|
||
|
|
CHECK(factory->QueryInterface(IID_IUnknown, identity.out()) == S_OK);
|
||
|
|
CHECK(identity.value == factory.value);
|
||
|
|
CHECK(factory->SetStagingBufferSize(0) == S_OK);
|
||
|
|
|
||
|
|
TemporaryFile temporary;
|
||
|
|
WCHAR directory[MAX_PATH];
|
||
|
|
const DWORD directory_len = GetTempPathW(MAX_PATH, directory);
|
||
|
|
CHECK(directory_len && directory_len < MAX_PATH);
|
||
|
|
CHECK(GetTempFileNameW(directory, L"bxs", 0, temporary.name));
|
||
|
|
const HANDLE writer = CreateFileW(temporary.name, GENERIC_WRITE, 0, nullptr,
|
||
|
|
TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||
|
|
CHECK(writer != INVALID_HANDLE_VALUE);
|
||
|
|
constexpr char contents[] = "ABCDEF0123456789";
|
||
|
|
DWORD written = 0;
|
||
|
|
const BOOL wrote = WriteFile(writer, contents, sizeof(contents), &written, nullptr);
|
||
|
|
CloseHandle(writer);
|
||
|
|
CHECK(wrote && written == sizeof(contents));
|
||
|
|
|
||
|
|
Com<File> file;
|
||
|
|
CHECK(factory->OpenFile(temporary.name, file_iid, file.out()) == S_OK);
|
||
|
|
BY_HANDLE_FILE_INFORMATION file_info{};
|
||
|
|
CHECK(file->GetFileInformation(&file_info) == S_OK);
|
||
|
|
CHECK(file_info.nFileSizeHigh == 0 && file_info.nFileSizeLow == sizeof(contents));
|
||
|
|
|
||
|
|
Com<Status> status;
|
||
|
|
CHECK(factory->CreateStatusArray(2, status_iid, status.out()) == S_OK);
|
||
|
|
Event event;
|
||
|
|
CHECK(event.handle);
|
||
|
|
std::array<char, 5> first{}, second{}, copied{};
|
||
|
|
Com<Queue> queue;
|
||
|
|
CHECK(factory->CreateQueue(5, queue_iid, queue.out()) == S_OK);
|
||
|
|
|
||
|
|
Request read1{file.value, 2, nullptr, first.data(), 4, 0};
|
||
|
|
Request read2{file.value, 10, nullptr, second.data(), 4, 0};
|
||
|
|
Request copy{nullptr, 0, first.data(), copied.data(), 4, 0};
|
||
|
|
CHECK(queue->EnqueueRequest(&read1) == S_OK);
|
||
|
|
CHECK(queue->EnqueueRequest(&read2) == S_OK);
|
||
|
|
CHECK(queue->EnqueueRequest(©) == S_OK);
|
||
|
|
CHECK(queue->EnqueueStatus(status.value, 0) == S_OK);
|
||
|
|
CHECK(queue->EnqueueSetEvent(event.handle) == S_OK);
|
||
|
|
CHECK(queue->EnqueueRequest(©) == HRESULT_FROM_WIN32(ERROR_BUSY));
|
||
|
|
CHECK(status->GetResult(0) == E_PENDING);
|
||
|
|
CHECK(WaitForSingleObject(event.handle, 0) == WAIT_TIMEOUT);
|
||
|
|
CHECK(first[0] == 0 && second[0] == 0 && copied[0] == 0);
|
||
|
|
|
||
|
|
CHECK(queue->Submit() == S_OK);
|
||
|
|
CHECK(WaitForSingleObject(event.handle, 5000) == WAIT_OBJECT_0);
|
||
|
|
CHECK(status->GetResult(0) == S_OK);
|
||
|
|
CHECK(std::strcmp(first.data(), "CDEF") == 0);
|
||
|
|
CHECK(std::strcmp(second.data(), "4567") == 0);
|
||
|
|
CHECK(std::strcmp(copied.data(), "CDEF") == 0);
|
||
|
|
|
||
|
|
ErrorRecord error{};
|
||
|
|
CHECK(queue->RetrieveErrorRecord(&error) == S_OK && error.failure_count == 0);
|
||
|
|
Request past_end{file.value, sizeof(contents), nullptr, first.data(), 1, 0};
|
||
|
|
Request compressed{nullptr, 0, first.data(), copied.data(), 4, 1};
|
||
|
|
CHECK(queue->EnqueueRequest(&past_end) == S_OK);
|
||
|
|
CHECK(queue->EnqueueRequest(&compressed) == S_OK);
|
||
|
|
CHECK(queue->EnqueueStatus(status.value, 0) == S_OK);
|
||
|
|
CHECK(queue->EnqueueStatus(status.value, 1) == S_OK);
|
||
|
|
CHECK(queue->Submit() == S_OK);
|
||
|
|
CHECK(file->Close() == S_OK);
|
||
|
|
CHECK(file->GetFileInformation(&file_info) == HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE));
|
||
|
|
CHECK(queue->Close() == S_OK);
|
||
|
|
|
||
|
|
CHECK(status->GetResult(0) == HRESULT_FROM_WIN32(ERROR_HANDLE_EOF));
|
||
|
|
CHECK(status->GetResult(1) == S_OK);
|
||
|
|
CHECK(queue->RetrieveErrorRecord(&error) == S_OK);
|
||
|
|
CHECK(error.failure_count == 2 && error.first_error == HRESULT_FROM_WIN32(ERROR_HANDLE_EOF));
|
||
|
|
CHECK(queue->RetrieveErrorRecord(&error) == S_OK && error.failure_count == 0);
|
||
|
|
CHECK(queue->EnqueueRequest(©) == HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE));
|
||
|
|
QueueInfo info{};
|
||
|
|
CHECK(queue->Query(&info) == S_OK && info.capacity == 5 && info.available == 5);
|
||
|
|
|
||
|
|
std::puts("test_xcrdapi: passed");
|
||
|
|
}
|