[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / RtlGenerate8dot3Name.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for RtlGenerate8dot3Name
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <ndk/rtlfuncs.h>
12 #include <stdio.h>
13
14 NTSYSAPI
15 VOID
16 NTAPI
17 RtlGenerate8dot3Name(
18 _In_ PCUNICODE_STRING Name,
19 _In_ BOOLEAN AllowExtendedCharacters,
20 _Inout_ PGENERATE_NAME_CONTEXT Context,
21 _Inout_ PUNICODE_STRING Name8dot3);
22
23 PWSTR Names[] = { L"Menu Démarrer", L"Sélecteur de configuration clavier.lnk", L"éèàùç.txt", L"çùàèé.txt", L"éèàù.txt", L"çùàè.txt", L"éèàùçeeauc.txt", L"éeèéçcùu.txt", L"test.éxè", L"t£$t¤.txt", L"Long file name.txt", L"Long file name", L"Longfilename.txt", L"Longfilename" };
24 PWSTR ShortNames1[] = { L"MENUDM~1", L"SLECTE~1.LNK", L"5C2D~1.TXT", L"423C~1.TXT", L"925E~1.TXT", L"7E4C~1.TXT", L"EEAUC~1.TXT", L"ECU~1.TXT", L"TEST~1.X", L"T$T~1.TXT", L"LONGFI~1.TXT", L"LONGFI~1", L"LONGFI~1.TXT", L"LONGFI~1" };
25 PWSTR ShortNames2[] = { L"MENUDM~2", L"SLECTE~2.LNK", L"5C2D~2.TXT", L"423C~2.TXT", L"925E~2.TXT", L"7E4C~2.TXT", L"EEAUC~2.TXT", L"ECU~2.TXT", L"TEST~2.X", L"T$T~2.TXT", L"LONGFI~2.TXT", L"LONGFI~2", L"LONGFI~2.TXT", L"LONGFI~2" };
26 PWSTR ExShortNames1[] = { L"MENUDÉ~1", L"SÉLECT~1.LNK", L"ÉÈÀÙÇ~1.TXT", L"ÇÙÀÈÉ~1.TXT", L"ÉÈÀÙ~1.TXT", L"ÇÙÀÈ~1.TXT", L"ÉÈÀÙÇE~1.TXT", L"ÉEÈÉÇC~1.TXT", L"TEST~1.ÉXÈ", L"T£$T¤~1.TXT", L"LONGFI~1.TXT", L"LONGFI~1", L"LONGFI~1.TXT", L"LONGFI~1" };
27 PWSTR ExShortNames2[] = { L"MENUDÉ~2", L"SÉLECT~2.LNK", L"ÉÈÀÙÇ~2.TXT", L"ÇÙÀÈÉ~2.TXT", L"ÉÈÀÙ~2.TXT", L"ÇÙÀÈ~2.TXT", L"ÉÈÀÙÇE~2.TXT", L"ÉEÈÉÇC~2.TXT", L"TEST~2.ÉXÈ", L"T£$T¤~2.TXT", L"LONGFI~2.TXT", L"LONGFI~2", L"LONGFI~2.TXT", L"LONGFI~2" };
28
29 PVOID LoadCodePageData(ULONG Code)
30 {
31 char filename[MAX_PATH], sysdir[MAX_PATH];
32 HANDLE hFile;
33 PVOID Data = NULL;
34 GetSystemDirectoryA(sysdir, MAX_PATH);
35
36 if (Code != -1)
37 sprintf(filename, "%s\\c_%lu.nls", sysdir, Code);
38 else
39 sprintf(filename, "%s\\l_intl.nls", sysdir);
40
41 hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
42 if (hFile != INVALID_HANDLE_VALUE)
43 {
44 DWORD dwRead;
45 DWORD dwFileSize = GetFileSize(hFile, NULL);
46 Data = malloc(dwFileSize);
47 ReadFile(hFile, Data, dwFileSize, &dwRead, NULL);
48 CloseHandle(hFile);
49 }
50 return Data;
51 }
52
53 /* https://www.microsoft.com/resources/msdn/goglobal/default.mspx */
54 void SetupLocale(ULONG AnsiCode, ULONG OemCode, ULONG Unicode)
55 {
56 NLSTABLEINFO NlsTable;
57 PVOID AnsiCodePageData;
58 PVOID OemCodePageData;
59 PVOID UnicodeCaseTableData;
60
61 AnsiCodePageData = LoadCodePageData(AnsiCode);
62 OemCodePageData = LoadCodePageData(OemCode);
63 UnicodeCaseTableData = LoadCodePageData(Unicode);
64
65 RtlInitNlsTables(AnsiCodePageData, OemCodePageData, UnicodeCaseTableData, &NlsTable);
66 RtlResetRtlTranslations(&NlsTable);
67 /* Do NOT free the buffers here, they are directly used!
68 Yes, we leak the old buffers, but this is a test anyway... */
69
70 }
71
72 START_TEST(RtlGenerate8dot3Name)
73 {
74 USHORT i;
75
76 /* Set a french locale. */
77 SetupLocale(1252, 850, -1);
78
79 for (i = 0; i < 14; ++i)
80 {
81 WCHAR Buffer[12];
82 GENERATE_NAME_CONTEXT Context;
83 UNICODE_STRING LongName, ShortName, Expected;
84
85 RtlZeroMemory(&Context, sizeof(GENERATE_NAME_CONTEXT));
86 RtlInitUnicodeString(&LongName, Names[i]);
87 ShortName.Buffer = Buffer;
88 ShortName.Length = 0;
89 ShortName.MaximumLength = sizeof(Buffer);
90
91 RtlGenerate8dot3Name(&LongName, FALSE, &Context, &ShortName);
92 RtlInitUnicodeString(&Expected, ShortNames1[i]);
93 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "Generated: %.*S. Expected: %.*S\n", ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
94
95 ShortName.Length = 0;
96 RtlGenerate8dot3Name(&LongName, FALSE, &Context, &ShortName);
97 RtlInitUnicodeString(&Expected, ShortNames2[i]);
98 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "Generated: %.*S. Expected: %.*S\n", ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
99
100 RtlZeroMemory(&Context, sizeof(GENERATE_NAME_CONTEXT));
101 ShortName.Length = 0;
102 RtlGenerate8dot3Name(&LongName, TRUE, &Context, &ShortName);
103 RtlInitUnicodeString(&Expected, ExShortNames1[i]);
104 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "Generated: %.*S. Expected: %.*S\n", ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
105
106 ShortName.Length = 0;
107 RtlGenerate8dot3Name(&LongName, TRUE, &Context, &ShortName);
108 RtlInitUnicodeString(&Expected, ExShortNames2[i]);
109 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "Generated: %.*S. Expected: %.*S\n", ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
110 }
111 }