CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / xpcom / tests / windows / TestWinFileAttribs.cpp
blobd2eef2595a61ddff9cd5cfe92d815b338f33d037
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2009
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Jim Mathies <jmathies@mozilla.com>.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 * Test:
43 #include "../TestHarness.h"
44 #include "nsEmbedString.h"
45 #include "nsILocalFileWin.h"
46 #include <windows.h>
48 #define BUFFSIZE 512
50 BOOL IsXPOrGreater()
52 OSVERSIONINFO osvi;
54 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
55 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
57 GetVersionEx(&osvi);
59 return ( (osvi.dwMajorVersion > 5) ||
60 ( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1) ));
63 nsresult TestWinAttribs()
65 if (!IsXPOrGreater())
66 return NS_OK;
68 printf("Is XP or greater, running tests...\n");
70 nsresult rv;
72 // File variables
73 HANDLE hIndexed;
74 nsCOMPtr<nsILocalFile> localFile;
75 WCHAR filePath[MAX_PATH];
77 // Create and open temporary file
78 hIndexed = CreateFileW(L".\\indexbit.txt",
79 GENERIC_READ | GENERIC_WRITE,
80 0,
81 NULL,
82 CREATE_ALWAYS,
83 FILE_ATTRIBUTE_NORMAL, //FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, not supported by cf
84 NULL);
86 if(hIndexed == INVALID_HANDLE_VALUE)
88 fail("Test Win Attribs: Creating Test File");
89 return NS_ERROR_FAILURE;
92 CloseHandle(hIndexed);
94 GetFullPathNameW((LPCWSTR)L".\\indexbit.txt",
95 MAX_PATH, filePath, NULL);
97 //wprintf(filePath);
98 //wprintf(L"\n");
100 rv = NS_NewLocalFile(nsEmbedString(filePath), PR_FALSE,
101 getter_AddRefs(localFile));
102 if (NS_FAILED(rv))
104 fail("Test Win Attribs: Opening Test File");
105 DeleteFileW(filePath);
106 return rv;
109 nsCOMPtr<nsILocalFileWin> localFileWin(do_QueryInterface(localFile));
111 DWORD dwAttrs = GetFileAttributesW(filePath);
112 if (dwAttrs == INVALID_FILE_ATTRIBUTES)
114 fail("Test Win Attribs: GetFileAttributesW - couldn't find our temp file.");
115 DeleteFileW(filePath);
116 return NS_ERROR_FAILURE;
119 dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
120 SetFileAttributesW(filePath, dwAttrs);
122 PRUint32 attribs = 0;
123 rv = localFileWin->GetFileAttributesWin(&attribs);
125 if (NS_FAILED(rv))
127 fail("Test Win Attribs: GetFileAttributesWin failed to GET attributes. (1)");
128 DeleteFileW(filePath);
129 return NS_ERROR_FAILURE;
132 if (attribs & nsILocalFileWin::WFA_SEARCH_INDEXED)
134 fail("Test Win Attribs: GetFileAttributesWin attributed did not match. (2)");
135 DeleteFileW(filePath);
136 return NS_ERROR_FAILURE;
139 dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
140 SetFileAttributesW(filePath, dwAttrs);
142 rv = localFileWin->GetFileAttributesWin(&attribs);
144 if (NS_FAILED(rv))
146 fail("Test Win Attribs: GetFileAttributesWin failed to GET attributes. (3)");
147 DeleteFileW(filePath);
148 return NS_ERROR_FAILURE;
151 if (!(attribs & nsILocalFileWin::WFA_SEARCH_INDEXED))
153 fail("Test Win Attribs: GetFileAttributesWin attributed did not match. (4)");
154 DeleteFileW(filePath);
155 return NS_ERROR_FAILURE;
158 dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
159 SetFileAttributesW(filePath, dwAttrs);
161 attribs = nsILocalFileWin::WFA_SEARCH_INDEXED;
162 rv = localFileWin->SetFileAttributesWin(attribs);
164 dwAttrs = GetFileAttributesW(filePath);
166 if (NS_FAILED(rv))
168 fail("Test Win Attribs: GetFileAttributesWin failed to SET attributes. (5)");
169 DeleteFileW(filePath);
170 return NS_ERROR_FAILURE;
173 if (dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
175 fail("Test Win Attribs: SetFileAttributesWin attributed did not match. (6)");
176 DeleteFileW(filePath);
177 return NS_ERROR_FAILURE;
180 dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
181 SetFileAttributesW(filePath, dwAttrs);
183 attribs = 0;
184 rv = localFileWin->SetFileAttributesWin(attribs);
186 dwAttrs = GetFileAttributesW(filePath);
188 if (NS_FAILED(rv))
190 fail("Test Win Attribs: GetFileAttributesWin failed to SET attributes. (7)");
191 DeleteFileW(filePath);
192 return NS_ERROR_FAILURE;
195 if (!(dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
197 fail("Test Win Attribs: SetFileAttributesWin attributed did not match. (8)");
198 DeleteFileW(filePath);
199 return NS_ERROR_FAILURE;
202 DeleteFileW(filePath);
204 passed("Test Win Attribs: passed tests.");
206 return NS_OK;
209 int main(int argc, char** argv)
211 ScopedXPCOM xpcom("WinFileAttributes");
212 if (xpcom.failed())
213 return 1;
215 int rv = 0;
217 if(NS_FAILED(TestWinAttribs()))
218 rv = 1;
220 return rv;