?? rtl.c
字號:
/* Unit test suite for Rtl* API functions
*
* Copyright 2003 Thomas Mertes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* NOTES
* We use function pointers here as there is no import library for NTDLL on
* windows.
*/
#include <stdlib.h>
#include "ntdll_test.h"
/* Function ptrs for ntdll calls */
static HMODULE hntdll = 0;
static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
static ULONG (WINAPI *pRtlUniform)(PULONG);
static ULONG (WINAPI *pRtlRandom)(PULONG);
static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
static void (WINAPI * pRtlInitializeHandleTable)(ULONG, ULONG, RTL_HANDLE_TABLE *);
static BOOLEAN (WINAPI * pRtlIsValidIndexHandle)(const RTL_HANDLE_TABLE *, ULONG, RTL_HANDLE **);
static NTSTATUS (WINAPI * pRtlDestroyHandleTable)(RTL_HANDLE_TABLE *);
static RTL_HANDLE * (WINAPI * pRtlAllocateHandle)(RTL_HANDLE_TABLE *, ULONG *);
static BOOLEAN (WINAPI * pRtlFreeHandle)(RTL_HANDLE_TABLE *, RTL_HANDLE *);
#define LEN 16
static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
static ULONG src_aligned_block[4];
static ULONG dest_aligned_block[32];
static const char *src = (const char*)src_aligned_block;
static char* dest = (char*)dest_aligned_block;
static void InitFunctionPtrs(void)
{
hntdll = LoadLibraryA("ntdll.dll");
ok(hntdll != 0, "LoadLibrary failed\n");
if (hntdll) {
pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
}
strcpy((char*)src_aligned_block, src_src);
ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
}
#define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
static void test_RtlCompareMemory(void)
{
SIZE_T size;
if (!pRtlCompareMemory)
return;
strcpy(dest, src);
COMP(src,src,0,0);
COMP(src,src,LEN,LEN);
dest[0] = 'x';
COMP(src,dest,LEN,0);
}
static void test_RtlCompareMemoryUlong(void)
{
ULONG a[10];
ULONG result;
a[0]= 0x0123;
a[1]= 0x4567;
a[2]= 0x89ab;
a[3]= 0xcdef;
result = pRtlCompareMemoryUlong(a, 0, 0x0123);
ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);
result = pRtlCompareMemoryUlong(a, 3, 0x0123);
ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
result = pRtlCompareMemoryUlong(a, 4, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 5, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 7, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 8, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 9, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 4, 0x0127);
ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);
result = pRtlCompareMemoryUlong(a, 4, 0x7123);
ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);
result = pRtlCompareMemoryUlong(a, 16, 0x4567);
ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);
a[1]= 0x0123;
result = pRtlCompareMemoryUlong(a, 3, 0x0123);
ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
result = pRtlCompareMemoryUlong(a, 4, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 5, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 7, 0x0123);
ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
result = pRtlCompareMemoryUlong(a, 8, 0x0123);
ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);
result = pRtlCompareMemoryUlong(a, 9, 0x0123);
ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);
}
#define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
#define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
static void test_RtlMoveMemory(void)
{
if (!pRtlMoveMemory)
return;
/* Length should be in bytes and not rounded. Use strcmp to ensure we
* didn't write past the end (it checks for the final NUL left by memset)
*/
COPY(0); CMP("");
COPY(1); CMP("T");
COPY(2); CMP("Th");
COPY(3); CMP("Thi");
COPY(4); CMP("This");
COPY(5); CMP("This ");
COPY(6); CMP("This i");
COPY(7); CMP("This is");
COPY(8); CMP("This is ");
COPY(9); CMP("This is a");
/* Overlapping */
strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
CMP("his is a test!!");
strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
CMP("TThis is a test!");
}
#define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
static void test_RtlFillMemory(void)
{
if (!pRtlFillMemory)
return;
/* Length should be in bytes and not rounded. Use strcmp to ensure we
* didn't write past the end (the remainder of the string should match)
*/
FILL(0); CMP("This is a test!");
FILL(1); CMP("xhis is a test!");
FILL(2); CMP("xxis is a test!");
FILL(3); CMP("xxxs is a test!");
FILL(4); CMP("xxxx is a test!");
FILL(5); CMP("xxxxxis a test!");
FILL(6); CMP("xxxxxxs a test!");
FILL(7); CMP("xxxxxxx a test!");
FILL(8); CMP("xxxxxxxxa test!");
FILL(9); CMP("xxxxxxxxx test!");
}
#define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
static void test_RtlFillMemoryUlong(void)
{
ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
if (!pRtlFillMemoryUlong)
return;
/* Length should be in bytes and not rounded. Use strcmp to ensure we
* didn't write past the end (the remainder of the string should match)
*/
LFILL(0); CMP("This is a test!");
LFILL(1); CMP("This is a test!");
LFILL(2); CMP("This is a test!");
LFILL(3); CMP("This is a test!");
LFILL(4); CMP("xxxx is a test!");
LFILL(5); CMP("xxxx is a test!");
LFILL(6); CMP("xxxx is a test!");
LFILL(7); CMP("xxxx is a test!");
LFILL(8); CMP("xxxxxxxxa test!");
LFILL(9); CMP("xxxxxxxxa test!");
}
#define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
#define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
static void test_RtlZeroMemory(void)
{
if (!pRtlZeroMemory)
return;
/* Length should be in bytes and not rounded. */
ZERO(0); MCMP("This is a test!");
ZERO(1); MCMP("\0his is a test!");
ZERO(2); MCMP("\0\0is is a test!");
ZERO(3); MCMP("\0\0\0s is a test!");
ZERO(4); MCMP("\0\0\0\0 is a test!");
ZERO(5); MCMP("\0\0\0\0\0is a test!");
ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
}
static void test_RtlUlonglongByteSwap(void)
{
ULONGLONG result;
result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
"RtlUlonglongByteSwap(0x7654321087654321) returns 0x%llx, expected 0x2143658710325476\n",
result);
}
static void test_RtlUniform(void)
{
ULONGLONG num;
ULONG seed;
ULONG seed_bak;
ULONG expected;
ULONG result;
/*
* According to the documentation RtlUniform is using D.H. Lehmer's 1948
* algorithm. This algorithm is:
*
* seed = (seed * const_1 + const_2) % const_3;
*
* According to the documentation the random number is distributed over
* [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
*
* seed = (seed * const_1 + const_2) % (MAXLONG + 1);
*
* Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
* algorithm can be expressed without division as:
*
* seed = (seed * const_1 + const_2) & MAXLONG;
*
* To find out const_2 we just call RtlUniform with seed set to 0:
*/
seed = 0;
expected = 0x7fffffc3;
result = pRtlUniform(&seed);
ok(result == expected,
"RtlUniform(&seed (seed == 0)) returns %lx, expected %lx\n",
result, expected);
/*
* The algorithm is now:
*
* seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
*
* To find out const_1 we can use:
*
* const_1 = RtlUniform(1) - 0x7fffffc3;
*
* If that does not work a search loop can try all possible values of
* const_1 and compare to the result to RtlUniform(1).
* This way we find out that const_1 is 0xffffffed.
*
* For seed = 1 the const_2 is 0x7fffffc4:
*/
seed = 1;
expected = seed * 0xffffffed + 0x7fffffc3 + 1;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -