xpcom/tests/windows/TestWinFileAttribs.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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/. */
     6 /*
     7  * Test: 
     8  */
    10 #include "../TestHarness.h"
    11 #include "nsEmbedString.h"
    12 #include "nsILocalFileWin.h"
    13 #include <windows.h>
    15 #define BUFFSIZE 512
    17 nsresult TestWinAttribs()
    18 {
    20     nsresult rv;
    22     // File variables
    23     HANDLE hIndexed;
    24     nsCOMPtr<nsIFile> localFile;
    25     WCHAR filePath[MAX_PATH];
    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);  
    36     if(hIndexed == INVALID_HANDLE_VALUE)
    37     {
    38         fail("Test Win Attribs: Creating Test File");
    39         return NS_ERROR_FAILURE;
    40     }
    42     CloseHandle(hIndexed);
    44     GetFullPathNameW((LPCWSTR)L".\\indexbit.txt", 
    45                         MAX_PATH, filePath, nullptr);
    47     //wprintf(filePath);
    48     //wprintf(L"\n");
    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     }
    59     nsCOMPtr<nsILocalFileWin> localFileWin(do_QueryInterface(localFile));
    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     }
    69     dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
    70     SetFileAttributesW(filePath, dwAttrs);
    72     uint32_t attribs = 0;
    73     rv = localFileWin->GetFileAttributesWin(&attribs);
    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     }
    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     }
    89     dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
    90     SetFileAttributesW(filePath, dwAttrs);
    92     rv = localFileWin->GetFileAttributesWin(&attribs);
    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     }
   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     }
   108     dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
   109     SetFileAttributesW(filePath, dwAttrs);
   111     attribs = nsILocalFileWin::WFA_SEARCH_INDEXED;
   112     rv = localFileWin->SetFileAttributesWin(attribs);
   114     dwAttrs = GetFileAttributesW(filePath);
   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     }
   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     }
   130     dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
   131     SetFileAttributesW(filePath, dwAttrs);
   133     attribs = 0;
   134     rv = localFileWin->SetFileAttributesWin(attribs);
   136     dwAttrs = GetFileAttributesW(filePath);
   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     }
   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     }
   152     DeleteFileW(filePath);
   154     passed("Test Win Attribs: passed tests.");
   156     return NS_OK;
   157 }
   159 int main(int argc, char** argv)
   160 {
   161     ScopedXPCOM xpcom("WinFileAttributes");
   162     if (xpcom.failed())
   163         return 1;
   165     int rv = 0;
   167     if(NS_FAILED(TestWinAttribs()))
   168         rv = 1;
   170     return rv;
   172 }

mercurial