[USP10_WINETEST] Sync with Wine Staging 2.9. CORE-13362
[reactos.git] / rostests / kmtests / ntos_fsrtl / FsRtlTunnel.c
index 1097807..206b3c8 100644 (file)
@@ -33,9 +33,16 @@ PUNICODE_STRING CopyUS(PUNICODE_STRING a)
     ok(b != NULL, "US is NULL after allocated memory\n");
     b->Length = 0;
     b->MaximumLength =a->MaximumLength;
-    b->Buffer = (PWSTR)ExAllocatePoolWithTag(PagedPool, b->MaximumLength, 1633);
-    ok(b->Buffer != NULL, "US->Buffer is NULL after allocated memory\n");
-    RtlCopyUnicodeString(b, a);
+    if (b->MaximumLength)
+    {
+        b->Buffer = (PWSTR)ExAllocatePoolWithTag(PagedPool, b->MaximumLength, 1633);
+        ok(b->Buffer != NULL, "US->Buffer is NULL after allocated memory\n");
+        RtlCopyUnicodeString(b, a);
+    }
+    else
+    {
+        b->Buffer = NULL;
+    }
     return b;
 }
 
@@ -90,6 +97,13 @@ void TestFsRtlAddToTunnelCache(ULONGLONG DirectoryKey, PUNICODE_STRING s_name, P
     ok (b == 0, "long name after call FsRtlAddToTunnelCache != long name befo call FsRtlAddToTunnelCache\n\n");
     b = RtlCompareUnicodeString(s_name, bs_name, TRUE);
     ok (b == 0, "short name after call FsRtlAddToTunnelCache != short name befo call FsRtlAddToTunnelCache\n\n");
+
+    if (bs_name->Buffer) ExFreePool(bs_name->Buffer);
+    ExFreePool(bs_name);
+    if (bl_name->Buffer) ExFreePool(bl_name->Buffer);
+    ExFreePool(bl_name);
+    ExFreePool(Bufb);
+    ExFreePool(Buf);
 }
 
 BOOLEAN TestFsRtlFindInTunnelCache(ULONG DirectoryKey, PUNICODE_STRING name, PUNICODE_STRING s_name, PUNICODE_STRING l_name)
@@ -107,6 +121,116 @@ void TestFsRtlDeleteKeyFromTunnelCache(ULONGLONG a)
     FsRtlDeleteKeyFromTunnelCache(T, a);
 }
 
+static
+void DuplicatesTest()
+{
+    UNICODE_STRING ShortName, LongName, OutShort, OutLong, ShortName2, LongName2;
+    ULONG First, Second, OutLength, OutData;
+    PTUNNEL Tunnel;
+    PVOID Buffer;
+
+    First = 1;
+    Second = 2;
+    RtlInitUnicodeString(&ShortName, L"LONGFI~1.TXT");
+    RtlInitUnicodeString(&LongName, L"Longfilename.txt");
+    RtlInitUnicodeString(&ShortName2, L"LONGFI~2.TXT");
+    RtlInitUnicodeString(&LongName2, L"Longfilenamr.txt");
+    Tunnel = ExAllocatePool(NonPagedPool, sizeof(TUNNEL));
+    RtlZeroMemory(Tunnel, sizeof(TUNNEL));
+    OutShort.MaximumLength = 13 * sizeof(WCHAR);
+    OutShort.Buffer = ExAllocatePool(PagedPool, OutShort.MaximumLength);
+    OutLong.MaximumLength = 17 * sizeof(WCHAR);
+    OutLong.Buffer = Buffer = ExAllocatePool(PagedPool, OutLong.MaximumLength);
+
+    FsRtlInitializeTunnelCache(Tunnel);
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName, &LongName, TRUE, sizeof(ULONG), &First);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "First call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+    ok_eq_pointer(OutLong.Buffer, Buffer);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName, &LongName, TRUE, sizeof(ULONG), &Second);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Second call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+    ok_eq_pointer(OutLong.Buffer, Buffer);
+
+    OutLong.MaximumLength = 13 * sizeof(WCHAR);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Third call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+    ok(OutLong.Buffer != Buffer, "Buffer didn't get reallocated!\n");
+    ok_eq_uint(OutLong.MaximumLength, 16 * sizeof(WCHAR));
+
+    FsRtlDeleteKeyFromTunnelCache(Tunnel, 1);
+    ok_bool_false(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Fourth call");
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName, &LongName, TRUE, sizeof(ULONG), &First);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Fifth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName2, &LongName2, TRUE, sizeof(ULONG), &First);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Sixth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName2, &OutShort, &OutLong, &OutLength, &OutData), "Seventh call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName, &LongName, TRUE, sizeof(ULONG), &Second);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Eighth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName2, &OutShort, &OutLong, &OutLength, &OutData), "Ninth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName2, &LongName2, TRUE, sizeof(ULONG), &Second);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Tenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName2, &OutShort, &OutLong, &OutLength, &OutData), "Eleventh call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+
+    FsRtlDeleteKeyFromTunnelCache(Tunnel, 1);
+    ok_bool_false(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Twelfth call");
+    ok_bool_false(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName2, &OutShort, &OutLong, &OutLength, &OutData), "Thirteenth call");
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName, &LongName, TRUE, sizeof(ULONG), &First);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Fourteenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName, &LongName, TRUE, sizeof(ULONG), &Second);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Fifteenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName2, &LongName2, TRUE, sizeof(ULONG), &First);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Sixteenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName2, &OutShort, &OutLong, &OutLength, &OutData), "Seventeenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 1);
+
+    FsRtlAddToTunnelCache(Tunnel, 1, &ShortName2, &LongName2, TRUE, sizeof(ULONG), &Second);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName, &OutShort, &OutLong, &OutLength, &OutData), "Eighteenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+    ok_bool_true(FsRtlFindInTunnelCache(Tunnel, 1, &ShortName2, &OutShort, &OutLong, &OutLength, &OutData), "Nineteenth call");
+    ok_eq_ulong(OutLength, sizeof(ULONG));
+    ok_eq_ulong(OutData, 2);
+
+    FsRtlDeleteTunnelCache(Tunnel);
+    ExFreePool(OutShort.Buffer);
+    ExFreePool(OutLong.Buffer);
+    ExFreePool(Buffer);
+    ExFreePool(Tunnel);
+}
+
 START_TEST(FsRtlTunnel)
 {
     PUNICODE_STRING s_name;
@@ -147,6 +271,7 @@ START_TEST(FsRtlTunnel)
     TestFsRtlAddToTunnelCache(12345, s_name, l_name, TRUE);
     TestFsRtlAddToTunnelCache(12347, s_name, l_name, TRUE);
     a = (PUNICODE_STRING)ExAllocatePool(PagedPool,sizeof(UNICODE_STRING));
+    RtlInitUnicodeString(a, NULL);
     TestFsRtlAddToTunnelCache(12346, a, l_name, FALSE);
 
     //Clear all
@@ -160,4 +285,14 @@ START_TEST(FsRtlTunnel)
 
     is = TestFsRtlFindInTunnelCache(12347, name, s_name, l_name);
     ok(is == FALSE, "FsRtlDeleteTunnelCache dosn't clear cash\n");
+
+    ExFreePool(a);
+    ExFreePool(name);
+    ExFreePool(l_name);
+    ExFreePool(s_name);
+
+    ExFreePool(Tb);
+    ExFreePool(T);
+
+    DuplicatesTest();
 }