Sat, 03 Jan 2015 20:18:00 +0100
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: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Test harness for XPCOM objects, providing a scoped XPCOM initializer, |
michael@0 | 8 | * nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String, |
michael@0 | 9 | * and stdio.h/stdlib.h. |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef TestHarness_h__ |
michael@0 | 13 | #define TestHarness_h__ |
michael@0 | 14 | |
michael@0 | 15 | #if defined(_MSC_VER) && defined(MOZ_STATIC_JS) |
michael@0 | 16 | /* |
michael@0 | 17 | * Including js/OldDebugAPI.h may cause build break with --disable-shared-js |
michael@0 | 18 | * This is a workaround for bug 673616. |
michael@0 | 19 | */ |
michael@0 | 20 | #define STATIC_JS_API |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | #include "mozilla/ArrayUtils.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "prenv.h" |
michael@0 | 26 | #include "nsComponentManagerUtils.h" |
michael@0 | 27 | #include "nsServiceManagerUtils.h" |
michael@0 | 28 | #include "nsCOMPtr.h" |
michael@0 | 29 | #include "nsAutoPtr.h" |
michael@0 | 30 | #include "nsStringGlue.h" |
michael@0 | 31 | #include "nsAppDirectoryServiceDefs.h" |
michael@0 | 32 | #include "nsDirectoryServiceDefs.h" |
michael@0 | 33 | #include "nsDirectoryServiceUtils.h" |
michael@0 | 34 | #include "nsIDirectoryService.h" |
michael@0 | 35 | #include "nsIFile.h" |
michael@0 | 36 | #include "nsIProperties.h" |
michael@0 | 37 | #include "nsIObserverService.h" |
michael@0 | 38 | #include "nsXULAppAPI.h" |
michael@0 | 39 | #include <stdio.h> |
michael@0 | 40 | #include <stdlib.h> |
michael@0 | 41 | #include <stdarg.h> |
michael@0 | 42 | |
michael@0 | 43 | static uint32_t gFailCount = 0; |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * Prints the given failure message and arguments using printf, prepending |
michael@0 | 47 | * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and |
michael@0 | 48 | * appending "\n" to eliminate having to type it at each call site. |
michael@0 | 49 | */ |
michael@0 | 50 | void fail(const char* msg, ...) |
michael@0 | 51 | { |
michael@0 | 52 | va_list ap; |
michael@0 | 53 | |
michael@0 | 54 | printf("TEST-UNEXPECTED-FAIL | "); |
michael@0 | 55 | |
michael@0 | 56 | va_start(ap, msg); |
michael@0 | 57 | vprintf(msg, ap); |
michael@0 | 58 | va_end(ap); |
michael@0 | 59 | |
michael@0 | 60 | putchar('\n'); |
michael@0 | 61 | ++gFailCount; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Prints the given success message and arguments using printf, prepending |
michael@0 | 66 | * "TEST-PASS " for the benefit of the test harness and |
michael@0 | 67 | * appending "\n" to eliminate having to type it at each call site. |
michael@0 | 68 | */ |
michael@0 | 69 | void passed(const char* msg, ...) |
michael@0 | 70 | { |
michael@0 | 71 | va_list ap; |
michael@0 | 72 | |
michael@0 | 73 | printf("TEST-PASS | "); |
michael@0 | 74 | |
michael@0 | 75 | va_start(ap, msg); |
michael@0 | 76 | vprintf(msg, ap); |
michael@0 | 77 | va_end(ap); |
michael@0 | 78 | |
michael@0 | 79 | putchar('\n'); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | //----------------------------------------------------------------------------- |
michael@0 | 83 | |
michael@0 | 84 | class ScopedLogging |
michael@0 | 85 | { |
michael@0 | 86 | public: |
michael@0 | 87 | ScopedLogging() |
michael@0 | 88 | { |
michael@0 | 89 | NS_LogInit(); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | ~ScopedLogging() |
michael@0 | 93 | { |
michael@0 | 94 | NS_LogTerm(); |
michael@0 | 95 | } |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | class ScopedXPCOM : public nsIDirectoryServiceProvider2 |
michael@0 | 99 | { |
michael@0 | 100 | public: |
michael@0 | 101 | NS_DECL_ISUPPORTS |
michael@0 | 102 | |
michael@0 | 103 | ScopedXPCOM(const char* testName, |
michael@0 | 104 | nsIDirectoryServiceProvider *dirSvcProvider = nullptr) |
michael@0 | 105 | : mDirSvcProvider(dirSvcProvider) |
michael@0 | 106 | { |
michael@0 | 107 | mTestName = testName; |
michael@0 | 108 | printf("Running %s tests...\n", mTestName); |
michael@0 | 109 | |
michael@0 | 110 | nsresult rv = NS_InitXPCOM2(&mServMgr, nullptr, this); |
michael@0 | 111 | if (NS_FAILED(rv)) |
michael@0 | 112 | { |
michael@0 | 113 | fail("NS_InitXPCOM2 returned failure code 0x%x", rv); |
michael@0 | 114 | mServMgr = nullptr; |
michael@0 | 115 | return; |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | ~ScopedXPCOM() |
michael@0 | 120 | { |
michael@0 | 121 | // If we created a profile directory, we need to remove it. |
michael@0 | 122 | if (mProfD) { |
michael@0 | 123 | nsCOMPtr<nsIObserverService> os = |
michael@0 | 124 | do_GetService(NS_OBSERVERSERVICE_CONTRACTID); |
michael@0 | 125 | MOZ_ASSERT(os); |
michael@0 | 126 | if (os) { |
michael@0 | 127 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-change-net-teardown", nullptr))); |
michael@0 | 128 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-change-teardown", nullptr))); |
michael@0 | 129 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change", nullptr))); |
michael@0 | 130 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change2", nullptr))); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | if (NS_FAILED(mProfD->Remove(true))) { |
michael@0 | 134 | NS_WARNING("Problem removing profile directory"); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | mProfD = nullptr; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if (mServMgr) |
michael@0 | 141 | { |
michael@0 | 142 | NS_RELEASE(mServMgr); |
michael@0 | 143 | nsresult rv = NS_ShutdownXPCOM(nullptr); |
michael@0 | 144 | if (NS_FAILED(rv)) |
michael@0 | 145 | { |
michael@0 | 146 | fail("XPCOM shutdown failed with code 0x%x", rv); |
michael@0 | 147 | exit(1); |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | printf("Finished running %s tests.\n", mTestName); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | bool failed() |
michael@0 | 155 | { |
michael@0 | 156 | return mServMgr == nullptr; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | already_AddRefed<nsIFile> GetProfileDirectory() |
michael@0 | 160 | { |
michael@0 | 161 | if (mProfD) { |
michael@0 | 162 | nsCOMPtr<nsIFile> copy = mProfD; |
michael@0 | 163 | return copy.forget(); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | // Create a unique temporary folder to use for this test. |
michael@0 | 167 | // Note that runcppunittests.py will run tests with a temp |
michael@0 | 168 | // directory as the cwd, so just put something under that. |
michael@0 | 169 | nsCOMPtr<nsIFile> profD; |
michael@0 | 170 | nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR, |
michael@0 | 171 | getter_AddRefs(profD)); |
michael@0 | 172 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 173 | |
michael@0 | 174 | rv = profD->Append(NS_LITERAL_STRING("cpp-unit-profd")); |
michael@0 | 175 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 176 | |
michael@0 | 177 | rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755); |
michael@0 | 178 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 179 | |
michael@0 | 180 | mProfD = profD; |
michael@0 | 181 | return profD.forget(); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | already_AddRefed<nsIFile> GetGREDirectory() |
michael@0 | 185 | { |
michael@0 | 186 | if (mGRED) { |
michael@0 | 187 | nsCOMPtr<nsIFile> copy = mGRED; |
michael@0 | 188 | return copy.forget(); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | char* env = PR_GetEnv("MOZ_XRE_DIR"); |
michael@0 | 192 | nsCOMPtr<nsIFile> greD; |
michael@0 | 193 | if (env) { |
michael@0 | 194 | NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), false, |
michael@0 | 195 | getter_AddRefs(greD)); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | mGRED = greD; |
michael@0 | 199 | return greD.forget(); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 203 | //// nsIDirectoryServiceProvider |
michael@0 | 204 | |
michael@0 | 205 | NS_IMETHODIMP GetFile(const char *aProperty, bool *_persistent, |
michael@0 | 206 | nsIFile **_result) |
michael@0 | 207 | { |
michael@0 | 208 | // If we were supplied a directory service provider, ask it first. |
michael@0 | 209 | if (mDirSvcProvider && |
michael@0 | 210 | NS_SUCCEEDED(mDirSvcProvider->GetFile(aProperty, _persistent, |
michael@0 | 211 | _result))) { |
michael@0 | 212 | return NS_OK; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | // Otherwise, the test harness provides some directories automatically. |
michael@0 | 216 | if (0 == strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) || |
michael@0 | 217 | 0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR) || |
michael@0 | 218 | 0 == strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) { |
michael@0 | 219 | nsCOMPtr<nsIFile> profD = GetProfileDirectory(); |
michael@0 | 220 | NS_ENSURE_TRUE(profD, NS_ERROR_FAILURE); |
michael@0 | 221 | |
michael@0 | 222 | nsCOMPtr<nsIFile> clone; |
michael@0 | 223 | nsresult rv = profD->Clone(getter_AddRefs(clone)); |
michael@0 | 224 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 225 | |
michael@0 | 226 | *_persistent = true; |
michael@0 | 227 | clone.forget(_result); |
michael@0 | 228 | return NS_OK; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | if (0 == strcmp(aProperty, NS_GRE_DIR)) { |
michael@0 | 232 | nsCOMPtr<nsIFile> greD = GetGREDirectory(); |
michael@0 | 233 | NS_ENSURE_TRUE(greD, NS_ERROR_FAILURE); |
michael@0 | 234 | |
michael@0 | 235 | *_persistent = true; |
michael@0 | 236 | greD.forget(_result); |
michael@0 | 237 | return NS_OK; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | return NS_ERROR_FAILURE; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | //////////////////////////////////////////////////////////////////////////// |
michael@0 | 244 | //// nsIDirectoryServiceProvider2 |
michael@0 | 245 | |
michael@0 | 246 | NS_IMETHODIMP GetFiles(const char *aProperty, nsISimpleEnumerator **_enum) |
michael@0 | 247 | { |
michael@0 | 248 | // If we were supplied a directory service provider, ask it first. |
michael@0 | 249 | nsCOMPtr<nsIDirectoryServiceProvider2> provider = |
michael@0 | 250 | do_QueryInterface(mDirSvcProvider); |
michael@0 | 251 | if (provider && NS_SUCCEEDED(provider->GetFiles(aProperty, _enum))) { |
michael@0 | 252 | return NS_OK; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | return NS_ERROR_FAILURE; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | private: |
michael@0 | 259 | const char* mTestName; |
michael@0 | 260 | nsIServiceManager* mServMgr; |
michael@0 | 261 | nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider; |
michael@0 | 262 | nsCOMPtr<nsIFile> mProfD; |
michael@0 | 263 | nsCOMPtr<nsIFile> mGRED; |
michael@0 | 264 | }; |
michael@0 | 265 | |
michael@0 | 266 | NS_IMPL_QUERY_INTERFACE( |
michael@0 | 267 | ScopedXPCOM, |
michael@0 | 268 | nsIDirectoryServiceProvider, |
michael@0 | 269 | nsIDirectoryServiceProvider2 |
michael@0 | 270 | ) |
michael@0 | 271 | |
michael@0 | 272 | NS_IMETHODIMP_(MozExternalRefCountType) |
michael@0 | 273 | ScopedXPCOM::AddRef() |
michael@0 | 274 | { |
michael@0 | 275 | return 2; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | NS_IMETHODIMP_(MozExternalRefCountType) |
michael@0 | 279 | ScopedXPCOM::Release() |
michael@0 | 280 | { |
michael@0 | 281 | return 1; |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | #endif // TestHarness_h__ |