xpcom/tests/windows/TestNtPathToDosPath.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.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "TestHarness.h"
michael@0 8
michael@0 9 #include <windows.h>
michael@0 10 #include <winnetwk.h>
michael@0 11
michael@0 12 #include "mozilla/FileUtilsWin.h"
michael@0 13 #include "mozilla/DebugOnly.h"
michael@0 14 #include "nsCRTGlue.h"
michael@0 15
michael@0 16 class DriveMapping
michael@0 17 {
michael@0 18 public:
michael@0 19 DriveMapping(const nsAString& aRemoteUNCPath);
michael@0 20 ~DriveMapping();
michael@0 21
michael@0 22 bool
michael@0 23 Init();
michael@0 24 bool
michael@0 25 ChangeDriveLetter();
michael@0 26 wchar_t
michael@0 27 GetDriveLetter() { return mDriveLetter; }
michael@0 28
michael@0 29 private:
michael@0 30 bool
michael@0 31 DoMapping();
michael@0 32 void
michael@0 33 Disconnect(wchar_t aDriveLetter);
michael@0 34
michael@0 35 wchar_t mDriveLetter;
michael@0 36 nsString mRemoteUNCPath;
michael@0 37 };
michael@0 38
michael@0 39 DriveMapping::DriveMapping(const nsAString& aRemoteUNCPath)
michael@0 40 : mDriveLetter(0)
michael@0 41 , mRemoteUNCPath(aRemoteUNCPath)
michael@0 42 {
michael@0 43 }
michael@0 44
michael@0 45 bool
michael@0 46 DriveMapping::Init()
michael@0 47 {
michael@0 48 if (mDriveLetter) {
michael@0 49 return false;
michael@0 50 }
michael@0 51 return DoMapping();
michael@0 52 }
michael@0 53
michael@0 54 bool
michael@0 55 DriveMapping::DoMapping()
michael@0 56 {
michael@0 57 wchar_t drvTemplate[] = L" :";
michael@0 58 NETRESOURCEW netRes = {0};
michael@0 59 netRes.dwType = RESOURCETYPE_DISK;
michael@0 60 netRes.lpLocalName = drvTemplate;
michael@0 61 netRes.lpRemoteName = reinterpret_cast<wchar_t*>(mRemoteUNCPath.BeginWriting());
michael@0 62 wchar_t driveLetter = L'D';
michael@0 63 DWORD result = NO_ERROR;
michael@0 64 do {
michael@0 65 drvTemplate[0] = driveLetter;
michael@0 66 result = WNetAddConnection2W(&netRes, nullptr, nullptr, CONNECT_TEMPORARY);
michael@0 67 } while (result == ERROR_ALREADY_ASSIGNED && ++driveLetter <= L'Z');
michael@0 68 if (result != NO_ERROR) {
michael@0 69 return false;
michael@0 70 }
michael@0 71 mDriveLetter = driveLetter;
michael@0 72 return true;
michael@0 73 }
michael@0 74
michael@0 75 bool
michael@0 76 DriveMapping::ChangeDriveLetter()
michael@0 77 {
michael@0 78 wchar_t prevDriveLetter = mDriveLetter;
michael@0 79 bool result = DoMapping();
michael@0 80 MOZ_ASSERT(mDriveLetter != prevDriveLetter);
michael@0 81 if (result && prevDriveLetter) {
michael@0 82 Disconnect(prevDriveLetter);
michael@0 83 }
michael@0 84 return result;
michael@0 85 }
michael@0 86
michael@0 87 void
michael@0 88 DriveMapping::Disconnect(wchar_t aDriveLetter)
michael@0 89 {
michael@0 90 wchar_t drvTemplate[] = {aDriveLetter, L':', L'\0'};
michael@0 91 mozilla::DebugOnly<DWORD> result = WNetCancelConnection2W(drvTemplate, 0, TRUE);
michael@0 92 MOZ_ASSERT(result == NO_ERROR);
michael@0 93 }
michael@0 94
michael@0 95 DriveMapping::~DriveMapping()
michael@0 96 {
michael@0 97 if (mDriveLetter) {
michael@0 98 Disconnect(mDriveLetter);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 bool
michael@0 103 DriveToNtPath(const wchar_t aDriveLetter, nsAString& aNtPath)
michael@0 104 {
michael@0 105 const wchar_t drvTpl[] = {aDriveLetter, L':', L'\0'};
michael@0 106 aNtPath.SetLength(MAX_PATH);
michael@0 107 DWORD pathLen;
michael@0 108 while (true) {
michael@0 109 pathLen = QueryDosDeviceW(drvTpl, reinterpret_cast<wchar_t*>(aNtPath.BeginWriting()), aNtPath.Length());
michael@0 110 if (pathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
michael@0 111 break;
michael@0 112 }
michael@0 113 aNtPath.SetLength(aNtPath.Length() * 2);
michael@0 114 }
michael@0 115 if (!pathLen) {
michael@0 116 return false;
michael@0 117 }
michael@0 118 // aNtPath contains embedded NULLs, so we need to figure out the real length
michael@0 119 // via wcslen.
michael@0 120 aNtPath.SetLength(NS_strlen(aNtPath.BeginReading()));
michael@0 121 return true;
michael@0 122 }
michael@0 123
michael@0 124 bool
michael@0 125 TestNtPathToDosPath(const wchar_t* aNtPath,
michael@0 126 const wchar_t* aExpectedDosPath)
michael@0 127 {
michael@0 128 nsAutoString output;
michael@0 129 bool result = mozilla::NtPathToDosPath(nsDependentString(aNtPath), output);
michael@0 130 return result && output == aExpectedDosPath;
michael@0 131 }
michael@0 132
michael@0 133 int main(int argc, char* argv[])
michael@0 134 {
michael@0 135 ScopedXPCOM xpcom("NtPathToDosPath");
michael@0 136 if (xpcom.failed()) {
michael@0 137 fail("XPCOM Startup");
michael@0 138 return 1;
michael@0 139 }
michael@0 140 nsAutoString cDrive;
michael@0 141 if (!DriveToNtPath(L'C', cDrive)) {
michael@0 142 fail("Querying for this machine's C:");
michael@0 143 return 1;
michael@0 144 }
michael@0 145
michael@0 146 int result = 0;
michael@0 147
michael@0 148 // empty string
michael@0 149 if (!TestNtPathToDosPath(L"", L"")) {
michael@0 150 fail("Empty string");
michael@0 151 result = 1;
michael@0 152 }
michael@0 153 // non-existent device, must fail
michael@0 154 if (TestNtPathToDosPath(L"\\Device\\ThisDeviceDoesNotExist\\Foo", nullptr)) {
michael@0 155 fail("Non-existent device");
michael@0 156 result = 1;
michael@0 157 }
michael@0 158 // base case
michael@0 159 nsAutoString testPath(cDrive);
michael@0 160 testPath.Append(L"\\Foo");
michael@0 161 if (!TestNtPathToDosPath(testPath.get(), L"C:\\Foo")) {
michael@0 162 fail("Base case");
michael@0 163 result = 1;
michael@0 164 }
michael@0 165 // drive letters as symbolic links (NtCreateFile uses these)
michael@0 166 if (!TestNtPathToDosPath(L"\\??\\C:\\Foo", L"C:\\Foo")) {
michael@0 167 fail("Path specified as symbolic link");
michael@0 168 result = 1;
michael@0 169 }
michael@0 170 // other symbolic links (should fail)
michael@0 171 if (TestNtPathToDosPath(L"\\??\\MountPointManager", nullptr)) {
michael@0 172 fail("Other symbolic link");
michael@0 173 result = 1;
michael@0 174 }
michael@0 175 // socket (should fail)
michael@0 176 if (TestNtPathToDosPath(L"\\Device\\Afd\\Endpoint", nullptr)) {
michael@0 177 fail("Socket");
michael@0 178 result = 1;
michael@0 179 }
michael@0 180 // currently UNC paths that are not mapped to drive letters are unsupported,
michael@0 181 // so this should fail
michael@0 182 if (TestNtPathToDosPath(L"\\Device\\Mup\\127.0.0.1\\C$", nullptr)) {
michael@0 183 fail("Unmapped UNC path");
michael@0 184 result = 1;
michael@0 185 }
michael@0 186 DriveMapping drvMapping(NS_LITERAL_STRING("\\\\127.0.0.1\\C$"));
michael@0 187 // Only run these tests if we were able to map; some machines don't have perms
michael@0 188 if (drvMapping.Init()) {
michael@0 189 wchar_t expected[] = L" :\\";
michael@0 190 expected[0] = drvMapping.GetDriveLetter();
michael@0 191 nsAutoString networkPath;
michael@0 192 if (!DriveToNtPath(drvMapping.GetDriveLetter(), networkPath)) {
michael@0 193 fail("Querying network drive");
michael@0 194 return 1;
michael@0 195 }
michael@0 196 networkPath += MOZ_UTF16("\\");
michael@0 197 if (!TestNtPathToDosPath(networkPath.get(), expected)) {
michael@0 198 fail("Mapped UNC path");
michael@0 199 result = 1;
michael@0 200 }
michael@0 201 // NtPathToDosPath must correctly handle paths whose drive letter mapping has
michael@0 202 // changed. We need to test this because the APIs called by NtPathToDosPath
michael@0 203 // return different info if this has happened.
michael@0 204 if (!drvMapping.ChangeDriveLetter()) {
michael@0 205 fail("Change drive letter");
michael@0 206 return 1;
michael@0 207 }
michael@0 208 expected[0] = drvMapping.GetDriveLetter();
michael@0 209 if (!DriveToNtPath(drvMapping.GetDriveLetter(), networkPath)) {
michael@0 210 fail("Querying second network drive");
michael@0 211 return 1;
michael@0 212 }
michael@0 213 networkPath += MOZ_UTF16("\\");
michael@0 214 if (!TestNtPathToDosPath(networkPath.get(), expected)) {
michael@0 215 fail("Re-mapped UNC path");
michael@0 216 result = 1;
michael@0 217 }
michael@0 218 }
michael@0 219
michael@0 220 return result;
michael@0 221 }
michael@0 222

mercurial