1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/windows/TestWinFileAttribs.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Test: 1.11 + */ 1.12 + 1.13 +#include "../TestHarness.h" 1.14 +#include "nsEmbedString.h" 1.15 +#include "nsILocalFileWin.h" 1.16 +#include <windows.h> 1.17 + 1.18 +#define BUFFSIZE 512 1.19 + 1.20 +nsresult TestWinAttribs() 1.21 +{ 1.22 + 1.23 + nsresult rv; 1.24 + 1.25 + // File variables 1.26 + HANDLE hIndexed; 1.27 + nsCOMPtr<nsIFile> localFile; 1.28 + WCHAR filePath[MAX_PATH]; 1.29 + 1.30 + // Create and open temporary file 1.31 + hIndexed = CreateFileW(L".\\indexbit.txt", 1.32 + GENERIC_READ | GENERIC_WRITE, 1.33 + 0, 1.34 + nullptr, 1.35 + CREATE_ALWAYS, 1.36 + FILE_ATTRIBUTE_NORMAL, //FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, not supported by cf 1.37 + nullptr); 1.38 + 1.39 + if(hIndexed == INVALID_HANDLE_VALUE) 1.40 + { 1.41 + fail("Test Win Attribs: Creating Test File"); 1.42 + return NS_ERROR_FAILURE; 1.43 + } 1.44 + 1.45 + CloseHandle(hIndexed); 1.46 + 1.47 + GetFullPathNameW((LPCWSTR)L".\\indexbit.txt", 1.48 + MAX_PATH, filePath, nullptr); 1.49 + 1.50 + //wprintf(filePath); 1.51 + //wprintf(L"\n"); 1.52 + 1.53 + rv = NS_NewLocalFile(nsEmbedString(filePath), false, 1.54 + getter_AddRefs(localFile)); 1.55 + if (NS_FAILED(rv)) 1.56 + { 1.57 + fail("Test Win Attribs: Opening Test File"); 1.58 + DeleteFileW(filePath); 1.59 + return rv; 1.60 + } 1.61 + 1.62 + nsCOMPtr<nsILocalFileWin> localFileWin(do_QueryInterface(localFile)); 1.63 + 1.64 + DWORD dwAttrs = GetFileAttributesW(filePath); 1.65 + if (dwAttrs == INVALID_FILE_ATTRIBUTES) 1.66 + { 1.67 + fail("Test Win Attribs: GetFileAttributesW - couldn't find our temp file."); 1.68 + DeleteFileW(filePath); 1.69 + return NS_ERROR_FAILURE; 1.70 + } 1.71 + 1.72 + dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; 1.73 + SetFileAttributesW(filePath, dwAttrs); 1.74 + 1.75 + uint32_t attribs = 0; 1.76 + rv = localFileWin->GetFileAttributesWin(&attribs); 1.77 + 1.78 + if (NS_FAILED(rv)) 1.79 + { 1.80 + fail("Test Win Attribs: GetFileAttributesWin failed to GET attributes. (1)"); 1.81 + DeleteFileW(filePath); 1.82 + return NS_ERROR_FAILURE; 1.83 + } 1.84 + 1.85 + if (attribs & nsILocalFileWin::WFA_SEARCH_INDEXED) 1.86 + { 1.87 + fail("Test Win Attribs: GetFileAttributesWin attributed did not match. (2)"); 1.88 + DeleteFileW(filePath); 1.89 + return NS_ERROR_FAILURE; 1.90 + } 1.91 + 1.92 + dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; 1.93 + SetFileAttributesW(filePath, dwAttrs); 1.94 + 1.95 + rv = localFileWin->GetFileAttributesWin(&attribs); 1.96 + 1.97 + if (NS_FAILED(rv)) 1.98 + { 1.99 + fail("Test Win Attribs: GetFileAttributesWin failed to GET attributes. (3)"); 1.100 + DeleteFileW(filePath); 1.101 + return NS_ERROR_FAILURE; 1.102 + } 1.103 + 1.104 + if (!(attribs & nsILocalFileWin::WFA_SEARCH_INDEXED)) 1.105 + { 1.106 + fail("Test Win Attribs: GetFileAttributesWin attributed did not match. (4)"); 1.107 + DeleteFileW(filePath); 1.108 + return NS_ERROR_FAILURE; 1.109 + } 1.110 + 1.111 + dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; 1.112 + SetFileAttributesW(filePath, dwAttrs); 1.113 + 1.114 + attribs = nsILocalFileWin::WFA_SEARCH_INDEXED; 1.115 + rv = localFileWin->SetFileAttributesWin(attribs); 1.116 + 1.117 + dwAttrs = GetFileAttributesW(filePath); 1.118 + 1.119 + if (NS_FAILED(rv)) 1.120 + { 1.121 + fail("Test Win Attribs: GetFileAttributesWin failed to SET attributes. (5)"); 1.122 + DeleteFileW(filePath); 1.123 + return NS_ERROR_FAILURE; 1.124 + } 1.125 + 1.126 + if (dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) 1.127 + { 1.128 + fail("Test Win Attribs: SetFileAttributesWin attributed did not match. (6)"); 1.129 + DeleteFileW(filePath); 1.130 + return NS_ERROR_FAILURE; 1.131 + } 1.132 + 1.133 + dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; 1.134 + SetFileAttributesW(filePath, dwAttrs); 1.135 + 1.136 + attribs = 0; 1.137 + rv = localFileWin->SetFileAttributesWin(attribs); 1.138 + 1.139 + dwAttrs = GetFileAttributesW(filePath); 1.140 + 1.141 + if (NS_FAILED(rv)) 1.142 + { 1.143 + fail("Test Win Attribs: GetFileAttributesWin failed to SET attributes. (7)"); 1.144 + DeleteFileW(filePath); 1.145 + return NS_ERROR_FAILURE; 1.146 + } 1.147 + 1.148 + if (!(dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)) 1.149 + { 1.150 + fail("Test Win Attribs: SetFileAttributesWin attributed did not match. (8)"); 1.151 + DeleteFileW(filePath); 1.152 + return NS_ERROR_FAILURE; 1.153 + } 1.154 + 1.155 + DeleteFileW(filePath); 1.156 + 1.157 + passed("Test Win Attribs: passed tests."); 1.158 + 1.159 + return NS_OK; 1.160 +} 1.161 + 1.162 +int main(int argc, char** argv) 1.163 +{ 1.164 + ScopedXPCOM xpcom("WinFileAttributes"); 1.165 + if (xpcom.failed()) 1.166 + return 1; 1.167 + 1.168 + int rv = 0; 1.169 + 1.170 + if(NS_FAILED(TestWinAttribs())) 1.171 + rv = 1; 1.172 + 1.173 + return rv; 1.174 + 1.175 +} 1.176 +