1 /*
2  * PROJECT:     ReactOS kernel-mode tests
3  * LICENSE:     LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4  * PURPOSE:     Test for GetFileAttributes/SetFileAttributes
5  * COPYRIGHT:   Copyright 2017 Thomas Faber <thomas.faber@reactos.org>
6  *              Copyright 2017 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
7  */
8 
9 #include <kmt_test.h>
10 
11 #include "kernel32_test.h"
12 
13 START_TEST(FileAttributes)
14 {
15     PCWSTR FileName = L"\\\\.\\Global\\GLOBALROOT\\Device\\Kmtest-kernel32\\Somefile";
16     BOOL Ret;
17     DWORD Attributes;
18     DWORD Error;
19 
20     Error = KmtLoadAndOpenDriver(L"kernel32", FALSE);
21     ok_eq_int(Error, ERROR_SUCCESS);
22     if (Error)
23         return;
24 
25     /* Set read-only attribute */
26     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL);
27     Ret = SetFileAttributesW(FileName, FILE_ATTRIBUTE_READONLY);
28     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
29 
30     /* Set normal attribute */
31     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL);
32     Ret = SetFileAttributesW(FileName, FILE_ATTRIBUTE_NORMAL);
33     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
34 
35     /* Set 0 attribute (driver should receive normal) */
36     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL);
37     Ret = SetFileAttributesW(FileName, 0);
38     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
39 
40     /* Set invalid attributes */
41     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_VALID_SET_FLAGS);
42     Ret = SetFileAttributesW(FileName, INVALID_FILE_ATTRIBUTES);
43     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
44 
45     /* Query read-only attribute */
46     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_READONLY);
47     Attributes = GetFileAttributesW(FileName);
48     ok_eq_hex(Attributes, FILE_ATTRIBUTE_READONLY);
49 
50     /* Query read-only + normal attribute */
51     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL);
52     Attributes = GetFileAttributesW(FileName);
53     ok_eq_hex(Attributes, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL);
54 
55     /* Query normal attribute */
56     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL);
57     Attributes = GetFileAttributesW(FileName);
58     ok_eq_hex(Attributes, FILE_ATTRIBUTE_NORMAL);
59 
60     /* Query 0 attribute */
61     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, 0);
62     Attributes = GetFileAttributesW(FileName);
63     ok_eq_hex(Attributes, 0);
64 
65     /* Query invalid attributes */
66     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, INVALID_FILE_ATTRIBUTES);
67     Attributes = GetFileAttributesW(FileName);
68     ok_eq_hex(Attributes, INVALID_FILE_ATTRIBUTES);
69 
70     /** Directory attribute **/
71     /* Set read-only and directory attribute */
72     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY);
73     Ret = SetFileAttributesW(FileName, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY);
74     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
75 
76     /* Set normal and directory attribute */
77     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL);
78     Ret = SetFileAttributesW(FileName, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
79     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
80 
81     /* Set directory attribute */
82     KmtSendUlongToDriver(IOCTL_EXPECT_SET_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL);
83     Ret = SetFileAttributesW(FileName, FILE_ATTRIBUTE_DIRECTORY);
84     ok(Ret == TRUE, "SetFileAttributesW returned %d, error %lu\n", Ret, GetLastError());
85 
86     /* Query read-only and directory attribute */
87     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY);
88     Attributes = GetFileAttributesW(FileName);
89     ok_eq_hex(Attributes, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY);
90 
91     /* Query read-only + normal + directory attribute */
92     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
93     Attributes = GetFileAttributesW(FileName);
94     ok_eq_hex(Attributes, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
95 
96     /* Query normal and directory attribute */
97     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
98     Attributes = GetFileAttributesW(FileName);
99     ok_eq_hex(Attributes, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
100 
101     /* Query directory attribute */
102     KmtSendUlongToDriver(IOCTL_RETURN_QUERY_ATTRIBUTES, FILE_ATTRIBUTE_DIRECTORY);
103     Attributes = GetFileAttributesW(FileName);
104     ok_eq_hex(Attributes, FILE_ATTRIBUTE_DIRECTORY);
105 
106     KmtCloseDriver();
107     KmtUnloadDriver();
108 }
109