[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 #define NAMES_COUNT 14
24 #define LOCALES_COUNT 3
25
26 PWSTR Names[NAMES_COUNT] = { 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" };
27 PWSTR ShortNames1[LOCALES_COUNT][NAMES_COUNT] =
28 {
29 { 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" },
30 { 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" },
31 { 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" },
32 };
33 PWSTR ShortNames2[LOCALES_COUNT][NAMES_COUNT] =
34 {
35 { 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" },
36 { 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" },
37 { 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" },
38 };
39 PWSTR ExShortNames1[LOCALES_COUNT][NAMES_COUNT] =
40 {
41 { 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" },
42 { 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" },
43 { L"MENUDÉ~1", L"SÉLECT~1.LNK", L"ÉCAU~1.TXT", L"UACÉ~1.TXT", L"ÉCAU~1.TXT", L"UAC~1.TXT", L"ÉCAUEE~1.TXT", L"ÉECÉCU~1.TXT", L"TEST~1.ÉXC", L"T£$T¤~1.TXT", L"LONGFI~1.TXT", L"LONGFI~1", L"LONGFI~1.TXT", L"LONGFI~1" },
44 };
45 PWSTR ExShortNames2[LOCALES_COUNT][NAMES_COUNT] =
46 {
47 { 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" },
48 { 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" },
49 { L"MENUDÉ~2", L"SÉLECT~2.LNK", L"ÉCAU~2.TXT", L"UACÉ~2.TXT", L"ÉCAU~2.TXT", L"UAC~2.TXT", L"ÉCAUEE~2.TXT", L"ÉECÉCU~2.TXT", L"TEST~2.ÉXC", L"T£$T¤~2.TXT", L"LONGFI~2.TXT", L"LONGFI~2", L"LONGFI~2.TXT", L"LONGFI~2" },
50 };
51
52 ULONG Locales[LOCALES_COUNT][2] =
53 {
54 {1252, 850}, // Most used for latin langs
55 {1252, 437}, // Used for English US (not only)
56 {1252, 775}, // Used for Estonian
57 };
58
59 PVOID LoadCodePageData(ULONG Code)
60 {
61 char filename[MAX_PATH], sysdir[MAX_PATH];
62 HANDLE hFile;
63 PVOID Data = NULL;
64 GetSystemDirectoryA(sysdir, MAX_PATH);
65
66 if (Code != -1)
67 sprintf(filename, "%s\\c_%lu.nls", sysdir, Code);
68 else
69 sprintf(filename, "%s\\l_intl.nls", sysdir);
70
71 hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
72 if (hFile != INVALID_HANDLE_VALUE)
73 {
74 DWORD dwRead;
75 DWORD dwFileSize = GetFileSize(hFile, NULL);
76 Data = malloc(dwFileSize);
77 ReadFile(hFile, Data, dwFileSize, &dwRead, NULL);
78 CloseHandle(hFile);
79 }
80 return Data;
81 }
82
83 /* https://www.microsoft.com/resources/msdn/goglobal/default.mspx */
84 void SetupLocale(ULONG AnsiCode, ULONG OemCode, ULONG Unicode)
85 {
86 NLSTABLEINFO NlsTable;
87 PVOID AnsiCodePageData;
88 PVOID OemCodePageData;
89 PVOID UnicodeCaseTableData;
90
91 AnsiCodePageData = LoadCodePageData(AnsiCode);
92 OemCodePageData = LoadCodePageData(OemCode);
93 UnicodeCaseTableData = LoadCodePageData(Unicode);
94
95 RtlInitNlsTables(AnsiCodePageData, OemCodePageData, UnicodeCaseTableData, &NlsTable);
96 RtlResetRtlTranslations(&NlsTable);
97 /* Do NOT free the buffers here, they are directly used!
98 Yes, we leak the old buffers, but this is a test anyway... */
99
100 }
101
102 START_TEST(RtlGenerate8dot3Name)
103 {
104 USHORT i, j;
105
106 for (j = 0; j < LOCALES_COUNT; ++j)
107 {
108 /* Setup locale. */
109 SetupLocale(Locales[j][0], Locales[j][1], -1);
110
111 for (i = 0; i < NAMES_COUNT; ++i)
112 {
113 WCHAR Buffer[12];
114 GENERATE_NAME_CONTEXT Context;
115 UNICODE_STRING LongName, ShortName, Expected;
116
117 RtlZeroMemory(&Context, sizeof(GENERATE_NAME_CONTEXT));
118 RtlInitUnicodeString(&LongName, Names[i]);
119 ShortName.Buffer = Buffer;
120 ShortName.Length = 0;
121 ShortName.MaximumLength = sizeof(Buffer);
122
123 RtlGenerate8dot3Name(&LongName, FALSE, &Context, &ShortName);
124 RtlInitUnicodeString(&Expected, ShortNames1[j][i]);
125 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "%u:: Generated: %.*S. Expected: %.*S\n", j, ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
126
127 ShortName.Length = 0;
128 RtlGenerate8dot3Name(&LongName, FALSE, &Context, &ShortName);
129 RtlInitUnicodeString(&Expected, ShortNames2[j][i]);
130 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "%u:: Generated: %.*S. Expected: %.*S\n", j, ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
131
132 RtlZeroMemory(&Context, sizeof(GENERATE_NAME_CONTEXT));
133 ShortName.Length = 0;
134 RtlGenerate8dot3Name(&LongName, TRUE, &Context, &ShortName);
135 RtlInitUnicodeString(&Expected, ExShortNames1[j][i]);
136 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "%u:: Generated: %.*S. Expected: %.*S\n", j, ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
137
138 ShortName.Length = 0;
139 RtlGenerate8dot3Name(&LongName, TRUE, &Context, &ShortName);
140 RtlInitUnicodeString(&Expected, ExShortNames2[j][i]);
141 ok(RtlEqualUnicodeString(&Expected, &ShortName, FALSE), "%u:: Generated: %.*S. Expected: %.*S\n", j, ShortName.Length / sizeof(WCHAR), ShortName.Buffer, Expected.Length / sizeof(WCHAR), Expected.Buffer);
142 }
143 }
144 }