KmtestsHowto

From ReactOS Wiki
Revision as of 08:21, 29 June 2011 by ThFabba (talk | contribs) (Skipping tests: another example)
Jump to: navigation, search

Test syntax

Testing a condition

Size = 1024 * 1024 * 1024 * 2;
Pointer = ExAllocatePoolWithTag(Size, NonPagedPool, 'xxxx');
ok(Pointer == NULL, "Allocating %lu bytes of non-paged pool should fail\n", Size);

Convenience macros

Several ok_* convenience macros for the most common conditions are also available.

ok_irql(PASSIVE_LEVEL);
ok_bool_false(KeAreAllApcsDisabled(), "KeAreAllApcsDisabled returned");
ok_eq_pointer(Something->ListEntry.Flink, &Something->ListEntry);
ok_eq_ulong(KeGetCurrentProcessorNumber(), 0);
ok_eq_int(Irp->RequestorMode, UserMode);
ok_eq_hex(Status, STATUS_ACCESS_VIOLATION);
ok_eq_str(AnsiName, "Ansi Name");
ok_eq_wstr(UnicodeName, L"Unicode Name");

Warning: Most of these macros will evaluate the expression passed to them twice! Do not call functions that have side-effects inside these macros!

ok_eq_long(InterlockedIncrement(&Variable), 5);

Instead, add a variable for the return value:

Ret = InterlockedIncrement(&Variable);
ok_eq_long(Ret, 5);

The ok_bool_true and ok_bool_false macros are notable exceptions that are safe to use.

Adding informational output

trace("Registry Path: %wZ\n", RegistryPath);

Skipping tests

If some condition prevents specific tests from running, these tests can be skipped using the skip() macro. This is preferred over executing tests that might otherwise crash.

Pointer = ExAllocatePoolWithTag(PAGE_SIZE, PagedPool, 'xxxx');
ok(Pointer != NULL, "Out of memory\n");

if (!skip(Pointer != NULL, "Allocation failed\n"))
{
    /* do stuff that uses the memory */
    ExFreePoolWithTag(Pointer, 'xxxx');
}

skip() should be passed a condition which must be TRUE in order for the following test(s) to succeed. It will then return whether to skip the test(s).

An approach in line with the Winetest version can also be taken to prevent nesting:

Pointer = ExAllocatePoolWithTag(PAGE_SIZE, PagedPool, 'xxxx');

if (skip(Pointer != NULL, "Allocation failed\n"))
    goto done;

/* do stuff that uses the memory */

done:
if (Pointer)
    ExFreePoolWithTag(Pointer, 'xxxx');