xpcom/tests/TestTimers.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: 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 #include "TestHarness.h"
michael@0 7
michael@0 8 #include "nsIThread.h"
michael@0 9 #include "nsITimer.h"
michael@0 10
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "nsComponentManagerUtils.h"
michael@0 13 #include "nsServiceManagerUtils.h"
michael@0 14 #include "nsThreadUtils.h"
michael@0 15 #include "prinrval.h"
michael@0 16 #include "prmon.h"
michael@0 17 #include "prthread.h"
michael@0 18 #include "mozilla/Attributes.h"
michael@0 19
michael@0 20 #include "mozilla/ReentrantMonitor.h"
michael@0 21 using namespace mozilla;
michael@0 22
michael@0 23 typedef nsresult(*TestFuncPtr)();
michael@0 24
michael@0 25 class AutoTestThread
michael@0 26 {
michael@0 27 public:
michael@0 28 AutoTestThread() {
michael@0 29 nsCOMPtr<nsIThread> newThread;
michael@0 30 nsresult rv = NS_NewThread(getter_AddRefs(newThread));
michael@0 31 if (NS_FAILED(rv))
michael@0 32 return;
michael@0 33
michael@0 34 newThread.swap(mThread);
michael@0 35 }
michael@0 36
michael@0 37 ~AutoTestThread() {
michael@0 38 mThread->Shutdown();
michael@0 39 }
michael@0 40
michael@0 41 operator nsIThread*() const {
michael@0 42 return mThread;
michael@0 43 }
michael@0 44
michael@0 45 nsIThread* operator->() const {
michael@0 46 return mThread;
michael@0 47 }
michael@0 48
michael@0 49 private:
michael@0 50 nsCOMPtr<nsIThread> mThread;
michael@0 51 };
michael@0 52
michael@0 53 class AutoCreateAndDestroyReentrantMonitor
michael@0 54 {
michael@0 55 public:
michael@0 56 AutoCreateAndDestroyReentrantMonitor() {
michael@0 57 mReentrantMonitor = new ReentrantMonitor("TestTimers::AutoMon");
michael@0 58 NS_ASSERTION(mReentrantMonitor, "Out of memory!");
michael@0 59 }
michael@0 60
michael@0 61 ~AutoCreateAndDestroyReentrantMonitor() {
michael@0 62 delete mReentrantMonitor;
michael@0 63 }
michael@0 64
michael@0 65 operator ReentrantMonitor* () {
michael@0 66 return mReentrantMonitor;
michael@0 67 }
michael@0 68
michael@0 69 private:
michael@0 70 ReentrantMonitor* mReentrantMonitor;
michael@0 71 };
michael@0 72
michael@0 73 class TimerCallback MOZ_FINAL : public nsITimerCallback
michael@0 74 {
michael@0 75 public:
michael@0 76 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 77
michael@0 78 TimerCallback(nsIThread** aThreadPtr, ReentrantMonitor* aReentrantMonitor)
michael@0 79 : mThreadPtr(aThreadPtr), mReentrantMonitor(aReentrantMonitor) { }
michael@0 80
michael@0 81 NS_IMETHOD Notify(nsITimer* aTimer) {
michael@0 82 NS_ASSERTION(mThreadPtr, "Callback was not supposed to be called!");
michael@0 83 nsCOMPtr<nsIThread> current(do_GetCurrentThread());
michael@0 84
michael@0 85 ReentrantMonitorAutoEnter mon(*mReentrantMonitor);
michael@0 86
michael@0 87 NS_ASSERTION(!*mThreadPtr, "Timer called back more than once!");
michael@0 88 *mThreadPtr = current;
michael@0 89
michael@0 90 mon.Notify();
michael@0 91
michael@0 92 return NS_OK;
michael@0 93 }
michael@0 94 private:
michael@0 95 nsIThread** mThreadPtr;
michael@0 96 ReentrantMonitor* mReentrantMonitor;
michael@0 97 };
michael@0 98
michael@0 99 NS_IMPL_ISUPPORTS(TimerCallback, nsITimerCallback)
michael@0 100
michael@0 101 nsresult
michael@0 102 TestTargetedTimers()
michael@0 103 {
michael@0 104 AutoCreateAndDestroyReentrantMonitor newMon;
michael@0 105 NS_ENSURE_TRUE(newMon, NS_ERROR_OUT_OF_MEMORY);
michael@0 106
michael@0 107 AutoTestThread testThread;
michael@0 108 NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
michael@0 109
michael@0 110 nsresult rv;
michael@0 111 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
michael@0 112 NS_ENSURE_SUCCESS(rv, rv);
michael@0 113
michael@0 114 nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
michael@0 115
michael@0 116 rv = timer->SetTarget(target);
michael@0 117 NS_ENSURE_SUCCESS(rv, rv);
michael@0 118
michael@0 119 nsIThread* notifiedThread = nullptr;
michael@0 120
michael@0 121 nsCOMPtr<nsITimerCallback> callback =
michael@0 122 new TimerCallback(&notifiedThread, newMon);
michael@0 123 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
michael@0 124
michael@0 125 rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(2000),
michael@0 126 nsITimer::TYPE_ONE_SHOT);
michael@0 127 NS_ENSURE_SUCCESS(rv, rv);
michael@0 128
michael@0 129 ReentrantMonitorAutoEnter mon(*newMon);
michael@0 130 while (!notifiedThread) {
michael@0 131 mon.Wait();
michael@0 132 }
michael@0 133 NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE);
michael@0 134
michael@0 135 return NS_OK;
michael@0 136 }
michael@0 137
michael@0 138 nsresult
michael@0 139 TestTimerWithStoppedTarget()
michael@0 140 {
michael@0 141 AutoTestThread testThread;
michael@0 142 NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
michael@0 143
michael@0 144 nsresult rv;
michael@0 145 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
michael@0 146 NS_ENSURE_SUCCESS(rv, rv);
michael@0 147
michael@0 148 nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
michael@0 149
michael@0 150 rv = timer->SetTarget(target);
michael@0 151 NS_ENSURE_SUCCESS(rv, rv);
michael@0 152
michael@0 153 // If this is called, we'll assert
michael@0 154 nsCOMPtr<nsITimerCallback> callback =
michael@0 155 new TimerCallback(nullptr, nullptr);
michael@0 156 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
michael@0 157
michael@0 158 rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(100),
michael@0 159 nsITimer::TYPE_ONE_SHOT);
michael@0 160 NS_ENSURE_SUCCESS(rv, rv);
michael@0 161
michael@0 162 testThread->Shutdown();
michael@0 163
michael@0 164 PR_Sleep(400);
michael@0 165
michael@0 166 return NS_OK;
michael@0 167 }
michael@0 168
michael@0 169 int main(int argc, char** argv)
michael@0 170 {
michael@0 171 ScopedXPCOM xpcom("TestTimers");
michael@0 172 NS_ENSURE_FALSE(xpcom.failed(), 1);
michael@0 173
michael@0 174 static TestFuncPtr testsToRun[] = {
michael@0 175 TestTargetedTimers,
michael@0 176 TestTimerWithStoppedTarget
michael@0 177 };
michael@0 178 static uint32_t testCount = sizeof(testsToRun) / sizeof(testsToRun[0]);
michael@0 179
michael@0 180 for (uint32_t i = 0; i < testCount; i++) {
michael@0 181 nsresult rv = testsToRun[i]();
michael@0 182 NS_ENSURE_SUCCESS(rv, 1);
michael@0 183 }
michael@0 184
michael@0 185 return 0;
michael@0 186 }

mercurial