Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186/* * PROJECT: ReactOS kernel-mode tests * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later) * PURPOSE: Test for ntstrsafe.h functions * COPYRIGHT: Copyright 2018 Hern�n Di Pietro <hernan.di.pietro@gmail.com> * Copyright 2019 Colin Finck <colin@reactos.org> */
#define KMT_EMULATE_KERNEL#include <kmt_test.h>#include <ntstrsafe.h>#include <ntdef.h>#include <ndk/rtlfuncs.h>
#define TESTAPI static void
static const WCHAR FormatStringInts[] = L"%d %d %d";static const WCHAR FormatStringIntsResult[] = L"1 2 3";static const WCHAR FormatStringStrs[] = L"%s %s %s";
TESTAPITest_RtlUnicodeStringPrintf(){ NTSTATUS Status; PWSTR pBuffer = NULL; USHORT BufferSize; size_t EqualBytes; UNICODE_STRING UsString;
KmtStartSeh();
/* STATUS_SUCCESS test */ BufferSize = 6 * sizeof(WCHAR); pBuffer = KmtAllocateGuarded(BufferSize); if (!pBuffer) goto Cleanup;
RtlFillMemory(pBuffer, BufferSize, 0xAA); RtlInitEmptyUnicodeString(&UsString, pBuffer, BufferSize);
Status = RtlUnicodeStringPrintf(&UsString, FormatStringInts, 1, 2, 3); EqualBytes = RtlCompareMemory(UsString.Buffer, FormatStringIntsResult, sizeof(FormatStringIntsResult)); ok_eq_hex(Status, STATUS_SUCCESS); ok_eq_size(EqualBytes, sizeof(FormatStringIntsResult)); ok_eq_uint(UsString.Length, sizeof(FormatStringIntsResult) - sizeof(WCHAR)); ok_eq_uint(UsString.MaximumLength, BufferSize);
KmtFreeGuarded(pBuffer); pBuffer = NULL;
/* STATUS_BUFFER_OVERFLOW tests */ BufferSize = 2 * sizeof(WCHAR); pBuffer = KmtAllocateGuarded(BufferSize); if (!pBuffer) goto Cleanup;
RtlInitEmptyUnicodeString(&UsString, pBuffer, BufferSize);
Status = RtlUnicodeStringPrintf(&UsString, FormatStringStrs, L"AAA", L"BBB", L"CCC"); EqualBytes = RtlCompareMemory(UsString.Buffer, L"AA", BufferSize); ok_eq_hex(Status, STATUS_BUFFER_OVERFLOW); ok_eq_size(EqualBytes, BufferSize); ok_eq_uint(UsString.Length, UsString.MaximumLength);
KmtFreeGuarded(pBuffer); pBuffer = NULL;
BufferSize = 7 * sizeof(WCHAR); pBuffer = KmtAllocateGuarded(BufferSize); if (!pBuffer) goto Cleanup;
RtlInitEmptyUnicodeString(&UsString, pBuffer, BufferSize);
Status = RtlUnicodeStringPrintf(&UsString, FormatStringStrs, L"0123", L"4567", L"89AB"); EqualBytes = RtlCompareMemory(UsString.Buffer, L"0123 45", BufferSize); ok_eq_hex(Status, STATUS_BUFFER_OVERFLOW); ok_eq_size(EqualBytes, BufferSize); ok_eq_uint(UsString.Length, UsString.MaximumLength);
KmtFreeGuarded(pBuffer); pBuffer = NULL;
// Note: RtlUnicodeStringPrintf returns STATUS_BUFFER_OVERFLOW here while RtlUnicodeStringPrintfEx returns STATUS_INVALID_PARAMETER! // Documented on MSDN and verified with the Win10 version of ntstrsafe.h RtlInitEmptyUnicodeString(&UsString, NULL, 0); Status = RtlUnicodeStringPrintf(&UsString, FormatStringStrs, L"AAA", L"BBB", L"CCC"); ok_eq_hex(Status, STATUS_BUFFER_OVERFLOW);
Cleanup: if (pBuffer) KmtFreeGuarded(pBuffer);
// None of these functions should have crashed. KmtEndSeh(STATUS_SUCCESS);}
TESTAPITest_RtlUnicodeStringPrintfEx(){ NTSTATUS Status; PWSTR pBuffer = NULL; USHORT BufferSize; size_t EqualBytes; UNICODE_STRING RemString; UNICODE_STRING UsString; WCHAR FillResult[10];
RtlFillMemory(FillResult, sizeof(FillResult), 0xAA);
KmtStartSeh();
/* STATUS_SUCCESS test, fill behind flag: low-byte as fill character */ BufferSize = sizeof(FormatStringIntsResult) - sizeof(UNICODE_NULL) + sizeof(FillResult); pBuffer = KmtAllocateGuarded(BufferSize); if (!pBuffer) goto Cleanup;
RtlInitEmptyUnicodeString(&UsString, pBuffer, BufferSize); RtlInitEmptyUnicodeString(&RemString, NULL, 0);
Status = RtlUnicodeStringPrintfEx(&UsString, &RemString, STRSAFE_FILL_BEHIND | 0xAA, FormatStringInts, 1, 2, 3); EqualBytes = RtlCompareMemory(UsString.Buffer, FormatStringIntsResult, sizeof(FormatStringIntsResult) - sizeof(WCHAR)); ok_eq_hex(Status, STATUS_SUCCESS); ok_eq_size(EqualBytes, sizeof(FormatStringIntsResult) - sizeof(WCHAR)); ok_eq_uint(UsString.Length, sizeof(FormatStringIntsResult) - sizeof(WCHAR)); ok_eq_uint(UsString.MaximumLength, BufferSize);
ok_eq_pointer(RemString.Buffer, &UsString.Buffer[UsString.Length / sizeof(WCHAR)]); ok_eq_uint(RemString.Length, 0); ok_eq_uint(RemString.MaximumLength, UsString.MaximumLength - UsString.Length);
EqualBytes = RtlCompareMemory(RemString.Buffer, FillResult, RemString.MaximumLength); ok_eq_size(EqualBytes, sizeof(FillResult));
KmtFreeGuarded(pBuffer); pBuffer = NULL;
/* STATUS_BUFFER_OVERFLOW test */ BufferSize = 8 * sizeof(WCHAR); pBuffer = KmtAllocateGuarded(BufferSize); if (!pBuffer) goto Cleanup;
RtlInitEmptyUnicodeString(&UsString, pBuffer, BufferSize); RtlInitEmptyUnicodeString(&RemString, NULL, 0);
Status = RtlUnicodeStringPrintfEx(&UsString, &RemString, 0, FormatStringStrs, L"AAA", L"BBB", L"CCC"); EqualBytes = RtlCompareMemory(UsString.Buffer, L"AAA BBB ", UsString.Length); ok_eq_hex(Status, STATUS_BUFFER_OVERFLOW); ok_eq_size(EqualBytes, UsString.Length); ok_eq_uint(UsString.Length, UsString.MaximumLength);
ok_eq_pointer(RemString.Buffer, &UsString.Buffer[UsString.Length / sizeof(WCHAR)]); ok_eq_uint(RemString.Length, 0); ok_eq_uint(RemString.MaximumLength, 0);
KmtFreeGuarded(pBuffer); pBuffer = NULL;
// Note: RtlUnicodeStringPrintf returns STATUS_BUFFER_OVERFLOW here while RtlUnicodeStringPrintfEx returns STATUS_INVALID_PARAMETER! // Documented on MSDN and verified with the Win10 version of ntstrsafe.h RtlInitEmptyUnicodeString(&UsString, NULL, 0); Status = RtlUnicodeStringPrintfEx(&UsString, NULL, 0, FormatStringStrs, L"AAA", L"BBB", L"CCC"); ok_eq_hex(Status, STATUS_INVALID_PARAMETER);
Cleanup: if (pBuffer) KmtFreeGuarded(pBuffer);
// None of these functions should have crashed. KmtEndSeh(STATUS_SUCCESS);}
START_TEST(RtlStrSafe){ Test_RtlUnicodeStringPrintf(); Test_RtlUnicodeStringPrintfEx();}