1 /*
2  * PROJECT:     ReactOS api tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Test for SfcIsFileProtected
5  * COPYRIGHT:   Copyright 2018 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include <apitest.h>
9 #include <strsafe.h>
10 #include <ndk/umtypes.h>
11 #include <ndk/rtlfuncs.h>
12 
13 BOOL (WINAPI *SfcIsFileProtected)(HANDLE  RpcHandle,LPCWSTR ProtFileName);
14 static DWORD g_WinVersion;
15 
16 typedef struct _osrange
17 {
18     DWORD Min;
19     DWORD Max;
20 } osrange;
21 
22 typedef struct _testdata
23 {
24     PCWSTR Path;
25     BOOL Expand;
26     osrange Success;
27 } testdata;
28 
29 
30 #define _WIN32_WINNT_MINVER 0x0001
31 
32 #define MAKERANGE(from, to) \
33     { _WIN32_WINNT_ ## from, _WIN32_WINNT_ ## to }
34 
35 #define PASS(from, to) \
36     MAKERANGE(from, to)
37 
38 static testdata tests[] =
39 {
40     { L"%systemroot%\\system32\\kernel32.dll", TRUE,                PASS(MINVER, WIN10) },
41     { L"%SYSTEMROOT%\\SYSTEM32\\KERNEL32.DLL", TRUE,                PASS(MINVER, WIN10) },
42     /* Paths are normalized on newer windows */
43     { L"%systemroot%//system32\\kernel32.dll", TRUE,                PASS(VISTA, WIN10) },
44     { L"%systemroot%\\system32\\..\\system32\\kernel32.dll", TRUE,  PASS(VISTA, WIN10) },
45 
46     /* These are rejected as-is */
47     { L"kernel32.dll", FALSE,                                       PASS(MINVER, MINVER) },
48     { L"%systemroot%//system32\\kernel32.dll", FALSE,               PASS(MINVER, MINVER) },
49     /* Environment strings are expanded on older windows */
50     { L"%systemroot%\\system32\\kernel32.dll", FALSE,               PASS(MINVER, WS03) },
51     { L"%SYSTEMROOT%\\SYSTEM32\\KERNEL32.DLL", FALSE,               PASS(MINVER, WS03) },
52 
53     /* Show some files under SFC protection */
54     { L"%systemroot%\\system32\\user32.dll", TRUE,                  PASS(MINVER, WIN10) },
55     { L"%systemroot%\\system32\\shell32.dll", TRUE,                 PASS(MINVER, WIN10) },
56     { L"%systemroot%\\system32\\browseui.dll", TRUE,                PASS(MINVER, WIN10) },
57     { L"%systemroot%\\system32\\apphelp.dll", TRUE,                 PASS(MINVER, WIN10) },
58     { L"%systemroot%\\system32\\sfc.dll", TRUE,                     PASS(MINVER, WIN10) },
59     { L"%systemroot%\\system32\\sfc_os.dll", TRUE,                  PASS(MINVER, WIN10) },
60     { L"%systemroot%\\system32\\sdbinst.exe", TRUE,                 PASS(MINVER, WIN10) },
61     { L"%systemroot%\\AppPatch\\sysmain.sdb", TRUE,                 PASS(MINVER, WIN10) },
62     { L"%systemroot%\\fonts\\tahoma.ttf", TRUE,                     PASS(MINVER, WIN10) },
63     { L"%systemroot%\\fonts\\tahomabd.ttf", TRUE,                   PASS(MINVER, WIN10) },
64     { L"%systemroot%\\system32\\ntoskrnl.exe", TRUE,                PASS(MINVER, WIN10) },
65     { L"%systemroot%\\system32\\c_1252.nls", TRUE,                  PASS(MINVER, WIN10) },
66     { L"%systemroot%\\NOTEPAD.EXE", TRUE,                           PASS(MINVER, WIN10) },
67 };
68 
69 
70 static void Test_ProtectedFiles()
71 {
72     ULONG n;
73     BOOL Protected;
74     WCHAR Buffer[MAX_PATH * 3];
75 
76     for (n = 0; n < _countof(tests); ++n)
77     {
78         if (tests[n].Expand)
79             ExpandEnvironmentStringsW(tests[n].Path, Buffer, _countof(Buffer));
80         else
81             StringCchCopyW(Buffer, _countof(Buffer), tests[n].Path);
82 
83         Protected = SfcIsFileProtected(NULL, Buffer);
84 
85         if (g_WinVersion >= tests[n].Success.Min && g_WinVersion <= tests[n].Success.Max)
86         {
87             ok(Protected, "[%lu,0x%04lx]: Failed: %S\n", n, g_WinVersion, Buffer);
88         }
89         else
90         {
91             ok(!Protected, "[%lu,0x%04lx]: Succeeded: %S\n", n, g_WinVersion, Buffer);
92         }
93     }
94 }
95 
96 
97 START_TEST(SfcIsFileProtected)
98 {
99     RTL_OSVERSIONINFOW rtlinfo = { sizeof(rtlinfo) };
100     HMODULE mod;
101 
102     mod = LoadLibraryA("sfc_os.dll");
103     ok(mod != NULL, "Unable to load sfc_os: 0x%lu\n", GetLastError());
104     if (!mod)
105         return;
106 
107     SfcIsFileProtected = (void*)GetProcAddress(mod, "SfcIsFileProtected");
108     ok(SfcIsFileProtected != NULL, "Unable to resolve SfcIsFileProtected: 0x%lu\n", GetLastError());
109     if (!SfcIsFileProtected)
110         return;
111 
112     RtlGetVersion(&rtlinfo);
113     g_WinVersion = (rtlinfo.dwMajorVersion << 8) | rtlinfo.dwMinorVersion;
114 
115     Test_ProtectedFiles();
116 }
117