xpcom/tests/TestExpirationTracker.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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 <stdlib.h>
michael@0 8 #include <stdio.h>
michael@0 9 #include <prthread.h>
michael@0 10 #include "nsExpirationTracker.h"
michael@0 11 #include "nsMemory.h"
michael@0 12 #include "nsAutoPtr.h"
michael@0 13 #include "nsString.h"
michael@0 14 #include "nsDirectoryServiceDefs.h"
michael@0 15 #include "nsDirectoryServiceUtils.h"
michael@0 16 #include "nsComponentManagerUtils.h"
michael@0 17 #include "nsXPCOM.h"
michael@0 18 #include "nsIFile.h"
michael@0 19 #include "prinrval.h"
michael@0 20 #include "nsThreadUtils.h"
michael@0 21
michael@0 22 namespace TestExpirationTracker {
michael@0 23
michael@0 24 struct Object {
michael@0 25 Object() : mExpired(false) { Touch(); }
michael@0 26 void Touch() { mLastUsed = PR_IntervalNow(); mExpired = false; }
michael@0 27
michael@0 28 nsExpirationState mExpiration;
michael@0 29 nsExpirationState* GetExpirationState() { return &mExpiration; }
michael@0 30
michael@0 31 PRIntervalTime mLastUsed;
michael@0 32 bool mExpired;
michael@0 33 };
michael@0 34
michael@0 35 static bool error;
michael@0 36 static uint32_t periodMS = 100;
michael@0 37 static uint32_t ops = 1000;
michael@0 38 static uint32_t iterations = 2;
michael@0 39 static bool logging = 0;
michael@0 40 static uint32_t sleepPeriodMS = 50;
michael@0 41 static uint32_t slackMS = 20; // allow this much error
michael@0 42
michael@0 43 static void SignalError() {
michael@0 44 printf("ERROR!\n");
michael@0 45 error = true;
michael@0 46 }
michael@0 47
michael@0 48 template <uint32_t K> class Tracker : public nsExpirationTracker<Object,K> {
michael@0 49 public:
michael@0 50 Tracker() : nsExpirationTracker<Object,K>(periodMS) {
michael@0 51 Object* obj = new Object();
michael@0 52 mUniverse.AppendElement(obj);
michael@0 53 LogAction(obj, "Created");
michael@0 54 }
michael@0 55
michael@0 56 nsTArray<nsAutoArrayPtr<Object> > mUniverse;
michael@0 57
michael@0 58 void LogAction(Object* aObj, const char* aAction) {
michael@0 59 if (logging) {
michael@0 60 printf("%d %p(%d): %s\n", PR_IntervalNow(),
michael@0 61 static_cast<void*>(aObj), aObj->mLastUsed, aAction);
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 void DoRandomOperation() {
michael@0 66 Object* obj;
michael@0 67 switch (rand() & 0x7) {
michael@0 68 case 0: {
michael@0 69 if (mUniverse.Length() < 50) {
michael@0 70 obj = new Object();
michael@0 71 mUniverse.AppendElement(obj);
michael@0 72 nsExpirationTracker<Object,K>::AddObject(obj);
michael@0 73 LogAction(obj, "Created and added");
michael@0 74 }
michael@0 75 break;
michael@0 76 }
michael@0 77 case 4: {
michael@0 78 if (mUniverse.Length() < 50) {
michael@0 79 obj = new Object();
michael@0 80 mUniverse.AppendElement(obj);
michael@0 81 LogAction(obj, "Created");
michael@0 82 }
michael@0 83 break;
michael@0 84 }
michael@0 85 case 1: {
michael@0 86 obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
michael@0 87 if (obj->mExpiration.IsTracked()) {
michael@0 88 nsExpirationTracker<Object,K>::RemoveObject(obj);
michael@0 89 LogAction(obj, "Removed");
michael@0 90 }
michael@0 91 break;
michael@0 92 }
michael@0 93 case 2: {
michael@0 94 obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
michael@0 95 if (!obj->mExpiration.IsTracked()) {
michael@0 96 obj->Touch();
michael@0 97 nsExpirationTracker<Object,K>::AddObject(obj);
michael@0 98 LogAction(obj, "Added");
michael@0 99 }
michael@0 100 break;
michael@0 101 }
michael@0 102 case 3: {
michael@0 103 obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
michael@0 104 if (obj->mExpiration.IsTracked()) {
michael@0 105 obj->Touch();
michael@0 106 nsExpirationTracker<Object,K>::MarkUsed(obj);
michael@0 107 LogAction(obj, "Marked used");
michael@0 108 }
michael@0 109 break;
michael@0 110 }
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 protected:
michael@0 115 void NotifyExpired(Object* aObj) {
michael@0 116 LogAction(aObj, "Expired");
michael@0 117 PRIntervalTime now = PR_IntervalNow();
michael@0 118 uint32_t timeDiffMS = (now - aObj->mLastUsed)*1000/PR_TicksPerSecond();
michael@0 119 // See the comment for NotifyExpired in nsExpirationTracker.h for these
michael@0 120 // bounds
michael@0 121 uint32_t lowerBoundMS = (K-1)*periodMS - slackMS;
michael@0 122 uint32_t upperBoundMS = K*(periodMS + sleepPeriodMS) + slackMS;
michael@0 123 if (logging) {
michael@0 124 printf("Checking: %d-%d = %d [%d,%d]\n",
michael@0 125 now, aObj->mLastUsed, timeDiffMS, lowerBoundMS, upperBoundMS);
michael@0 126 }
michael@0 127 if (timeDiffMS < lowerBoundMS || timeDiffMS > upperBoundMS) {
michael@0 128 if (timeDiffMS < periodMS && aObj->mExpired) {
michael@0 129 // This is probably OK, it probably just expired twice
michael@0 130 } else {
michael@0 131 SignalError();
michael@0 132 }
michael@0 133 }
michael@0 134 aObj->Touch();
michael@0 135 aObj->mExpired = true;
michael@0 136 DoRandomOperation();
michael@0 137 DoRandomOperation();
michael@0 138 DoRandomOperation();
michael@0 139 }
michael@0 140 };
michael@0 141
michael@0 142 template <uint32_t K> static bool test_random() {
michael@0 143 srand(K);
michael@0 144 error = false;
michael@0 145
michael@0 146 for (uint32_t j = 0; j < iterations; ++j) {
michael@0 147 Tracker<K> tracker;
michael@0 148
michael@0 149 uint32_t i = 0;
michael@0 150 for (i = 0; i < ops; ++i) {
michael@0 151 if ((rand() & 0xF) == 0) {
michael@0 152 // Simulate work that takes time
michael@0 153 if (logging) {
michael@0 154 printf("SLEEPING for %dms (%d)\n", sleepPeriodMS, PR_IntervalNow());
michael@0 155 }
michael@0 156 PR_Sleep(PR_MillisecondsToInterval(sleepPeriodMS));
michael@0 157 // Process pending timer events
michael@0 158 NS_ProcessPendingEvents(nullptr);
michael@0 159 }
michael@0 160 tracker.DoRandomOperation();
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 return !error;
michael@0 165 }
michael@0 166
michael@0 167 static bool test_random3() { return test_random<3>(); }
michael@0 168 static bool test_random4() { return test_random<4>(); }
michael@0 169 static bool test_random8() { return test_random<8>(); }
michael@0 170
michael@0 171 typedef bool (*TestFunc)();
michael@0 172 #define DECL_TEST(name) { #name, name }
michael@0 173
michael@0 174 static const struct Test {
michael@0 175 const char* name;
michael@0 176 TestFunc func;
michael@0 177 } tests[] = {
michael@0 178 DECL_TEST(test_random3),
michael@0 179 DECL_TEST(test_random4),
michael@0 180 DECL_TEST(test_random8),
michael@0 181 { nullptr, nullptr }
michael@0 182 };
michael@0 183
michael@0 184 }
michael@0 185
michael@0 186 using namespace TestExpirationTracker;
michael@0 187
michael@0 188 int main(int argc, char **argv) {
michael@0 189 int count = 1;
michael@0 190 if (argc > 1)
michael@0 191 count = atoi(argv[1]);
michael@0 192
michael@0 193 if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
michael@0 194 return -1;
michael@0 195
michael@0 196 while (count--) {
michael@0 197 for (const Test* t = tests; t->name != nullptr; ++t) {
michael@0 198 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 NS_ShutdownXPCOM(nullptr);
michael@0 203 return 0;
michael@0 204 }

mercurial