⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtl.c

📁 winNT技术操作系统,国外开放的原代码和LIUX一样
💻 C
📖 第 1 页 / 共 3 页
字号:
 * call RtlRandom aggain with seed set to 0:
 */
    seed = 0;
    result_expected = 0x7fffffc3;
    seed_expected =0x44b;
    result = pRtlRandom(&seed);
    ok(result == result_expected,
        "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
        result, result_expected);
    ok(seed == seed_expected,
        "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
        seed, seed_expected);
/*
 * Seed is set to the same value as before but the result is different.
 * To see more we call RtlRandom aggain with seed set to 0:
 */
    seed = 0;
    result_expected = 0x7fffffc3;
    seed_expected =0x44b;
    result = pRtlRandom(&seed);
    ok(result == result_expected,
        "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
        result, result_expected);
    ok(seed == seed_expected,
        "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
        seed, seed_expected);
/*
 * Seed is aggain set to the same value as before. This time we also
 * have the same result as before. Interestingly the value of the
 * result is 0x7fffffc3 which is the same value used in RtlUniform
 * as const_2. If we do
 *
 * seed = 0;
 * result = RtlUniform(&seed);
 *
 * we get the same result (0x7fffffc3) as with
 *
 * seed = 0;
 * RtlRandom(&seed);
 * seed = 0;
 * result = RtlRandom(&seed);
 *
 * And there is another interesting thing. If we do
 *
 * seed = 0;
 * RtlUniform(&seed);
 * RtlUniform(&seed);
 *
 * seed is set to the value 0x44b which ist the same value that
 *
 * seed = 0;
 * RtlRandom(&seed);
 *
 * assigns to seed. Putting these two findings together leads to
 * the concluson that RtlRandom saves the value in some variable,
 * like in the following algorithm:
 *
 * result = saved_value;
 * saved_value = RtlUniform(&seed);
 * RtlUniform(&seed);
 * return(result);
 *
 * Now we do further tests with seed set to 1:
 */
    seed = 1;
    result_expected = 0x7a50bbc6;
    seed_expected =0x5a1;
    result = pRtlRandom(&seed);
    ok(result == result_expected,
        "RtlRandom(&seed (seed == 1)) returns %lx, expected %lx\n",
        result, result_expected);
    ok(seed == seed_expected,
        "RtlRandom(&seed (seed == 1)) sets seed to %lx, expected %lx\n",
        seed, seed_expected);
/*
 * If there is just one saved_value the result now would be
 * 0x7fffffc3. From this test we can see that there is more than
 * one saved_value, like with this algorithm:
 *
 * result = saved_value[pos];
 * saved_value[pos] = RtlUniform(&seed);
 * RtlUniform(&seed);
 * return(result);
 *
 * But how is the value of pos determined? The calls to RtlUniform
 * create a sequence of random numbers. Every second random number
 * is put into the saved_value array and is used in some later call
 * of RtlRandom as result. The only reasonable source to determine
 * pos are the random numbers generated by RtlUniform which are not
 * put into the saved_value array. This are the values of seed
 * between the two calls of RtlUniform as in this algorithm:
 *
 * rand = RtlUniform(&seed);
 * RtlUniform(&seed);
 * pos = position(seed);
 * result = saved_value[pos];
 * saved_value[pos] = rand;
 * return(result);
 *
 * What remains to be determined is: The size of the saved_value array,
 * the initial values of the saved_value array and the function
 * position(seed). These tests are not shown here. 
 * The result of these tests is: The size of the saved_value array
 * is 128, the initial values can be seen in the my_RtlRandom
 * function and the position(seed) function is (seed & 0x7f).
 *
 * For a full test of RtlRandom use one of the following loop heads:
 *
 *  for (num = 0; num <= 0xffffffff; num++) {
 *      seed = num;
 *      ...
 *
 *  seed = 0;
 *  for (num = 0; num <= 0xffffffff; num++) {
 *      ...
 */
    seed = 0;
    for (num = 0; num <= 100000; num++) {
        seed_bak = seed;
	seed_expected = seed;
        result_expected = my_RtlRandom(&seed_expected);
	/* The following corrections are necessary because the */
	/* previous tests changed the saved_value array */
	if (num == 0) {
	    result_expected = 0x7fffffc3;
        } else if (num == 81) {
	    result_expected = 0x7fffffb1;
	} /* if */
        result = pRtlRandom(&seed);
        ok(result == result_expected,
                "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
                num, seed_bak, result, result_expected);
        ok(seed == seed_expected,
                "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
                num, seed_bak, seed, seed_expected);
    } /* for */
}


typedef struct {
    ACCESS_MASK GrantedAccess;
    ACCESS_MASK DesiredAccess;
    BOOLEAN result;
} all_accesses_t;

static const all_accesses_t all_accesses[] = {
    {0xFEDCBA76, 0xFEDCBA76, 1},
    {0x00000000, 0xFEDCBA76, 0},
    {0xFEDCBA76, 0x00000000, 1},
    {0x00000000, 0x00000000, 1},
    {0xFEDCBA76, 0xFEDCBA70, 1},
    {0xFEDCBA70, 0xFEDCBA76, 0},
    {0xFEDCBA76, 0xFEDC8A76, 1},
    {0xFEDC8A76, 0xFEDCBA76, 0},
    {0xFEDCBA76, 0xC8C4B242, 1},
    {0xC8C4B242, 0xFEDCBA76, 0},
};
#define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))


static void test_RtlAreAllAccessesGranted(void)
{
    size_t test_num;
    BOOLEAN result;

    for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
	result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
					   all_accesses[test_num].DesiredAccess);
	ok(all_accesses[test_num].result == result,
	   "(test %d): RtlAreAllAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
	   test_num, all_accesses[test_num].GrantedAccess,
	   all_accesses[test_num].DesiredAccess,
	   result, all_accesses[test_num].result);
    } /* for */
}


typedef struct {
    ACCESS_MASK GrantedAccess;
    ACCESS_MASK DesiredAccess;
    BOOLEAN result;
} any_accesses_t;

static const any_accesses_t any_accesses[] = {
    {0xFEDCBA76, 0xFEDCBA76, 1},
    {0x00000000, 0xFEDCBA76, 0},
    {0xFEDCBA76, 0x00000000, 0},
    {0x00000000, 0x00000000, 0},
    {0xFEDCBA76, 0x01234589, 0},
    {0x00040000, 0xFEDCBA76, 1},
    {0x00040000, 0xFED8BA76, 0},
    {0xFEDCBA76, 0x00040000, 1},
    {0xFED8BA76, 0x00040000, 0},
};
#define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))


static void test_RtlAreAnyAccessesGranted(void)
{
    size_t test_num;
    BOOLEAN result;

    for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
	result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
					   any_accesses[test_num].DesiredAccess);
	ok(any_accesses[test_num].result == result,
	   "(test %d): RtlAreAnyAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
	   test_num, any_accesses[test_num].GrantedAccess,
	   any_accesses[test_num].DesiredAccess,
	   result, any_accesses[test_num].result);
    } /* for */
}

static void test_RtlComputeCrc32(void)
{
  DWORD crc = 0;

  if (!pRtlComputeCrc32)
    return;

  crc = pRtlComputeCrc32(crc, (LPBYTE)src, LEN);
  ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8lx\n", crc);
}


typedef struct MY_HANDLE
{
    RTL_HANDLE RtlHandle;
    void * MyValue;
} MY_HANDLE;

static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
{
    ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
    *AllocatedBit = *AllocatedBit | 1;
}

static void test_HandleTables(void)
{
    BOOLEAN result;
    NTSTATUS status;
    ULONG Index;
    MY_HANDLE * MyHandle;
    RTL_HANDLE_TABLE HandleTable;

    pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
    MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
    ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
    RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
    MyHandle = NULL;
    result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
    ok(result, "Handle %p wasn't valid\n", MyHandle);
    result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
    ok(result, "Couldn't free handle %p\n", MyHandle);
    status = pRtlDestroyHandleTable(&HandleTable);
    ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08lx\n", status);
}

static void test_RtlAllocateAndInitializeSid(void)
{
    NTSTATUS ret;
    SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
    PSID psid;

    ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
    ok(!ret, "RtlAllocateAndInitializeSid error %08lx\n", ret);
    ret = pRtlFreeSid(psid);
    ok(!ret, "RtlFreeSid error %08lx\n", ret);

    /* these tests crash on XP
    ret = pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
    ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);*/

    ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
    ok(ret == STATUS_INVALID_SID, "wrong error %08lx\n", ret);
}

START_TEST(rtl)
{
    InitFunctionPtrs();

    if (pRtlCompareMemory)
        test_RtlCompareMemory();
    if (pRtlCompareMemoryUlong)
        test_RtlCompareMemoryUlong();
    if (pRtlMoveMemory)
        test_RtlMoveMemory();
    if (pRtlFillMemory)
        test_RtlFillMemory();
    if (pRtlFillMemoryUlong)
        test_RtlFillMemoryUlong();
    if (pRtlZeroMemory)
        test_RtlZeroMemory();
    if (pRtlUlonglongByteSwap)
        test_RtlUlonglongByteSwap();
    if (pRtlUniform)
        test_RtlUniform();
    if (pRtlRandom)
        test_RtlRandom();
    if (pRtlAreAllAccessesGranted)
        test_RtlAreAllAccessesGranted();
    if (pRtlAreAnyAccessesGranted)
        test_RtlAreAnyAccessesGranted();
    if (pRtlComputeCrc32)
        test_RtlComputeCrc32();
    if (pRtlInitializeHandleTable)
        test_HandleTables();
    if (pRtlAllocateAndInitializeSid)
        test_RtlAllocateAndInitializeSid();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -