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.

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

mercurial