xpcom/tests/windows/TestWinFileAttribs.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:5a956f155701
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * Test:
8 */
9
10 #include "../TestHarness.h"
11 #include "nsEmbedString.h"
12 #include "nsILocalFileWin.h"
13 #include <windows.h>
14
15 #define BUFFSIZE 512
16
17 nsresult TestWinAttribs()
18 {
19
20 nsresult rv;
21
22 // File variables
23 HANDLE hIndexed;
24 nsCOMPtr<nsIFile> localFile;
25 WCHAR filePath[MAX_PATH];
26
27 // Create and open temporary file
28 hIndexed = CreateFileW(L".\\indexbit.txt",
29 GENERIC_READ | GENERIC_WRITE,
30 0,
31 nullptr,
32 CREATE_ALWAYS,
33 FILE_ATTRIBUTE_NORMAL, //FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, not supported by cf
34 nullptr);
35
36 if(hIndexed == INVALID_HANDLE_VALUE)
37 {
38 fail("Test Win Attribs: Creating Test File");
39 return NS_ERROR_FAILURE;
40 }
41
42 CloseHandle(hIndexed);
43
44 GetFullPathNameW((LPCWSTR)L".\\indexbit.txt",
45 MAX_PATH, filePath, nullptr);
46
47 //wprintf(filePath);
48 //wprintf(L"\n");
49
50 rv = NS_NewLocalFile(nsEmbedString(filePath), false,
51 getter_AddRefs(localFile));
52 if (NS_FAILED(rv))
53 {
54 fail("Test Win Attribs: Opening Test File");
55 DeleteFileW(filePath);
56 return rv;
57 }
58
59 nsCOMPtr<nsILocalFileWin> localFileWin(do_QueryInterface(localFile));
60
61 DWORD dwAttrs = GetFileAttributesW(filePath);
62 if (dwAttrs == INVALID_FILE_ATTRIBUTES)
63 {
64 fail("Test Win Attribs: GetFileAttributesW - couldn't find our temp file.");
65 DeleteFileW(filePath);
66 return NS_ERROR_FAILURE;
67 }
68
69 dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
70 SetFileAttributesW(filePath, dwAttrs);
71
72 uint32_t attribs = 0;
73 rv = localFileWin->GetFileAttributesWin(&attribs);
74
75 if (NS_FAILED(rv))
76 {
77 fail("Test Win Attribs: GetFileAttributesWin failed to GET attributes. (1)");
78 DeleteFileW(filePath);
79 return NS_ERROR_FAILURE;
80 }
81
82 if (attribs & nsILocalFileWin::WFA_SEARCH_INDEXED)
83 {
84 fail("Test Win Attribs: GetFileAttributesWin attributed did not match. (2)");
85 DeleteFileW(filePath);
86 return NS_ERROR_FAILURE;
87 }
88
89 dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
90 SetFileAttributesW(filePath, dwAttrs);
91
92 rv = localFileWin->GetFileAttributesWin(&attribs);
93
94 if (NS_FAILED(rv))
95 {
96 fail("Test Win Attribs: GetFileAttributesWin failed to GET attributes. (3)");
97 DeleteFileW(filePath);
98 return NS_ERROR_FAILURE;
99 }
100
101 if (!(attribs & nsILocalFileWin::WFA_SEARCH_INDEXED))
102 {
103 fail("Test Win Attribs: GetFileAttributesWin attributed did not match. (4)");
104 DeleteFileW(filePath);
105 return NS_ERROR_FAILURE;
106 }
107
108 dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
109 SetFileAttributesW(filePath, dwAttrs);
110
111 attribs = nsILocalFileWin::WFA_SEARCH_INDEXED;
112 rv = localFileWin->SetFileAttributesWin(attribs);
113
114 dwAttrs = GetFileAttributesW(filePath);
115
116 if (NS_FAILED(rv))
117 {
118 fail("Test Win Attribs: GetFileAttributesWin failed to SET attributes. (5)");
119 DeleteFileW(filePath);
120 return NS_ERROR_FAILURE;
121 }
122
123 if (dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
124 {
125 fail("Test Win Attribs: SetFileAttributesWin attributed did not match. (6)");
126 DeleteFileW(filePath);
127 return NS_ERROR_FAILURE;
128 }
129
130 dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
131 SetFileAttributesW(filePath, dwAttrs);
132
133 attribs = 0;
134 rv = localFileWin->SetFileAttributesWin(attribs);
135
136 dwAttrs = GetFileAttributesW(filePath);
137
138 if (NS_FAILED(rv))
139 {
140 fail("Test Win Attribs: GetFileAttributesWin failed to SET attributes. (7)");
141 DeleteFileW(filePath);
142 return NS_ERROR_FAILURE;
143 }
144
145 if (!(dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
146 {
147 fail("Test Win Attribs: SetFileAttributesWin attributed did not match. (8)");
148 DeleteFileW(filePath);
149 return NS_ERROR_FAILURE;
150 }
151
152 DeleteFileW(filePath);
153
154 passed("Test Win Attribs: passed tests.");
155
156 return NS_OK;
157 }
158
159 int main(int argc, char** argv)
160 {
161 ScopedXPCOM xpcom("WinFileAttributes");
162 if (xpcom.failed())
163 return 1;
164
165 int rv = 0;
166
167 if(NS_FAILED(TestWinAttribs()))
168 rv = 1;
169
170 return rv;
171
172 }
173

mercurial