xpcom/tests/TestThreadUtils.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http:mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "TestHarness.h"
michael@0 6 #include "nsThreadUtils.h"
michael@0 7
michael@0 8 enum {
michael@0 9 TEST_CALL_VOID_ARG_VOID_RETURN,
michael@0 10 TEST_CALL_VOID_ARG_NONVOID_RETURN,
michael@0 11 TEST_CALL_NONVOID_ARG_VOID_RETURN,
michael@0 12 TEST_CALL_NONVOID_ARG_NONVOID_RETURN,
michael@0 13 TEST_CALL_NONVOID_ARG_VOID_RETURN_EXPLICIT,
michael@0 14 TEST_CALL_NONVOID_ARG_NONVOID_RETURN_EXPLICIT,
michael@0 15 #ifdef HAVE_STDCALL
michael@0 16 TEST_STDCALL_VOID_ARG_VOID_RETURN,
michael@0 17 TEST_STDCALL_VOID_ARG_NONVOID_RETURN,
michael@0 18 TEST_STDCALL_NONVOID_ARG_VOID_RETURN,
michael@0 19 TEST_STDCALL_NONVOID_ARG_NONVOID_RETURN,
michael@0 20 TEST_STDCALL_NONVOID_ARG_NONVOID_RETURN_EXPLICIT,
michael@0 21 #endif
michael@0 22 TEST_CALL_NEWTHREAD_SUICIDAL,
michael@0 23 MAX_TESTS
michael@0 24 };
michael@0 25
michael@0 26 bool gRunnableExecuted[MAX_TESTS];
michael@0 27
michael@0 28 class nsFoo : public nsISupports {
michael@0 29 NS_DECL_ISUPPORTS
michael@0 30 nsresult DoFoo(bool* aBool) {
michael@0 31 *aBool = true;
michael@0 32 return NS_OK;
michael@0 33 }
michael@0 34 virtual ~nsFoo() {}
michael@0 35 };
michael@0 36
michael@0 37 NS_IMPL_ISUPPORTS0(nsFoo)
michael@0 38
michael@0 39 class TestSuicide : public nsRunnable {
michael@0 40 NS_IMETHOD Run() {
michael@0 41 // Runs first time on thread "Suicide", then dies on MainThread
michael@0 42 if (!NS_IsMainThread()) {
michael@0 43 mThread = do_GetCurrentThread();
michael@0 44 NS_DispatchToMainThread(this);
michael@0 45 return NS_OK;
michael@0 46 }
michael@0 47 MOZ_ASSERT(mThread);
michael@0 48 mThread->Shutdown();
michael@0 49 gRunnableExecuted[TEST_CALL_NEWTHREAD_SUICIDAL] = true;
michael@0 50 return NS_OK;
michael@0 51 }
michael@0 52
michael@0 53 private:
michael@0 54 nsCOMPtr<nsIThread> mThread;
michael@0 55 };
michael@0 56
michael@0 57 class nsBar : public nsISupports {
michael@0 58 NS_DECL_ISUPPORTS
michael@0 59 virtual ~nsBar() {}
michael@0 60 void DoBar1(void) {
michael@0 61 gRunnableExecuted[TEST_CALL_VOID_ARG_VOID_RETURN] = true;
michael@0 62 }
michael@0 63 nsresult DoBar2(void) {
michael@0 64 gRunnableExecuted[TEST_CALL_VOID_ARG_NONVOID_RETURN] = true;
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67 void DoBar3(nsFoo* aFoo) {
michael@0 68 aFoo->DoFoo(&gRunnableExecuted[TEST_CALL_NONVOID_ARG_VOID_RETURN]);
michael@0 69 }
michael@0 70 nsresult DoBar4(nsFoo* aFoo) {
michael@0 71 return aFoo->DoFoo(&gRunnableExecuted[TEST_CALL_NONVOID_ARG_NONVOID_RETURN]);
michael@0 72 }
michael@0 73 void DoBar5(nsFoo* aFoo) {
michael@0 74 if (aFoo)
michael@0 75 gRunnableExecuted[TEST_CALL_NONVOID_ARG_VOID_RETURN_EXPLICIT] = true;
michael@0 76 }
michael@0 77 nsresult DoBar6(char* aFoo) {
michael@0 78 if (strlen(aFoo))
michael@0 79 gRunnableExecuted[TEST_CALL_NONVOID_ARG_NONVOID_RETURN_EXPLICIT] = true;
michael@0 80 return NS_OK;
michael@0 81 }
michael@0 82 #ifdef HAVE_STDCALL
michael@0 83 void __stdcall DoBar1std(void) {
michael@0 84 gRunnableExecuted[TEST_STDCALL_VOID_ARG_VOID_RETURN] = true;
michael@0 85 }
michael@0 86 nsresult __stdcall DoBar2std(void) {
michael@0 87 gRunnableExecuted[TEST_STDCALL_VOID_ARG_NONVOID_RETURN] = true;
michael@0 88 return NS_OK;
michael@0 89 }
michael@0 90 void __stdcall DoBar3std(nsFoo* aFoo) {
michael@0 91 aFoo->DoFoo(&gRunnableExecuted[TEST_STDCALL_NONVOID_ARG_VOID_RETURN]);
michael@0 92 }
michael@0 93 nsresult __stdcall DoBar4std(nsFoo* aFoo) {
michael@0 94 return aFoo->DoFoo(&gRunnableExecuted[TEST_STDCALL_NONVOID_ARG_NONVOID_RETURN]);
michael@0 95 }
michael@0 96 void __stdcall DoBar5std(nsFoo* aFoo) {
michael@0 97 if (aFoo)
michael@0 98 gRunnableExecuted[TEST_STDCALL_NONVOID_ARG_VOID_RETURN_EXPLICIT] = true;
michael@0 99 }
michael@0 100 nsresult __stdcall DoBar6std(char* aFoo) {
michael@0 101 if (strlen(aFoo))
michael@0 102 gRunnableExecuted[TEST_CALL_NONVOID_ARG_VOID_RETURN_EXPLICIT] = true;
michael@0 103 return NS_OK;
michael@0 104 }
michael@0 105 #endif
michael@0 106 };
michael@0 107
michael@0 108 NS_IMPL_ISUPPORTS0(nsBar)
michael@0 109
michael@0 110 int main(int argc, char** argv)
michael@0 111 {
michael@0 112 ScopedXPCOM xpcom("ThreadUtils");
michael@0 113 NS_ENSURE_FALSE(xpcom.failed(), 1);
michael@0 114
michael@0 115 memset(gRunnableExecuted, false, MAX_TESTS * sizeof(bool));
michael@0 116 // Scope the smart ptrs so that the runnables need to hold on to whatever they need
michael@0 117 {
michael@0 118 nsRefPtr<nsFoo> foo = new nsFoo();
michael@0 119 nsRefPtr<nsBar> bar = new nsBar();
michael@0 120
michael@0 121 // This pointer will be freed at the end of the block
michael@0 122 // Do not dereference this pointer in the runnable method!
michael@0 123 nsFoo * rawFoo = new nsFoo();
michael@0 124
michael@0 125 // Read only string. Dereferencing in runnable method to check this works.
michael@0 126 char* message = (char*)"Test message";
michael@0 127
michael@0 128 NS_DispatchToMainThread(NS_NewRunnableMethod(bar, &nsBar::DoBar1));
michael@0 129 NS_DispatchToMainThread(NS_NewRunnableMethod(bar, &nsBar::DoBar2));
michael@0 130 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg< nsRefPtr<nsFoo> >
michael@0 131 (bar, &nsBar::DoBar3, foo));
michael@0 132 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg< nsRefPtr<nsFoo> >
michael@0 133 (bar, &nsBar::DoBar4, foo));
michael@0 134 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg<nsFoo*>(bar, &nsBar::DoBar5, rawFoo));
michael@0 135 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg<char*>(bar, &nsBar::DoBar6, message));
michael@0 136 #ifdef HAVE_STDCALL
michael@0 137 NS_DispatchToMainThread(NS_NewRunnableMethod(bar, &nsBar::DoBar1std));
michael@0 138 NS_DispatchToMainThread(NS_NewRunnableMethod(bar, &nsBar::DoBar2std));
michael@0 139 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg< nsRefPtr<nsFoo> >
michael@0 140 (bar, &nsBar::DoBar3std, foo));
michael@0 141 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg< nsRefPtr<nsFoo> >
michael@0 142 (bar, &nsBar::DoBar4std, foo));
michael@0 143 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg<nsFoo*>(bar, &nsBar::DoBar5std, rawFoo));
michael@0 144 NS_DispatchToMainThread(NS_NewRunnableMethodWithArg<char*>(bar, &nsBar::DoBar6std, message));
michael@0 145 #endif
michael@0 146
michael@0 147 delete rawFoo;
michael@0 148 }
michael@0 149
michael@0 150 // Spin the event loop
michael@0 151 NS_ProcessPendingEvents(nullptr);
michael@0 152
michael@0 153 // Now test a suicidal event in NS_New(Named)Thread
michael@0 154 nsCOMPtr<nsIThread> thread;
michael@0 155 NS_NewNamedThread("SuicideThread", getter_AddRefs(thread), new TestSuicide());
michael@0 156 MOZ_ASSERT(thread);
michael@0 157
michael@0 158 while (!gRunnableExecuted[TEST_CALL_NEWTHREAD_SUICIDAL]) {
michael@0 159 NS_ProcessPendingEvents(nullptr);
michael@0 160 }
michael@0 161
michael@0 162 int result = 0;
michael@0 163
michael@0 164 for (uint32_t i = 0; i < MAX_TESTS; i++) {
michael@0 165 if (gRunnableExecuted[i]) {
michael@0 166 passed("Test %d passed",i);
michael@0 167 } else {
michael@0 168 fail("Error in test %d", i);
michael@0 169 result = 1;
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 return result;
michael@0 174 }

mercurial