security/sandbox/win/src/threadpool_unittest.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "sandbox/win/src/win2k_threadpool.h"
michael@0 6 #include "testing/gtest/include/gtest/gtest.h"
michael@0 7
michael@0 8 void __stdcall EmptyCallBack(void*, unsigned char) {
michael@0 9 }
michael@0 10
michael@0 11 void __stdcall TestCallBack(void* context, unsigned char) {
michael@0 12 HANDLE event = reinterpret_cast<HANDLE>(context);
michael@0 13 ::SetEvent(event);
michael@0 14 }
michael@0 15
michael@0 16 namespace sandbox {
michael@0 17
michael@0 18 // Test that register and unregister work, part 1.
michael@0 19 TEST(IPCTest, ThreadPoolRegisterTest1) {
michael@0 20 Win2kThreadPool thread_pool;
michael@0 21
michael@0 22 EXPECT_EQ(0, thread_pool.OutstandingWaits());
michael@0 23
michael@0 24 HANDLE event1 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
michael@0 25 HANDLE event2 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
michael@0 26
michael@0 27 uint32 context = 0;
michael@0 28 EXPECT_FALSE(thread_pool.RegisterWait(0, event1, EmptyCallBack, &context));
michael@0 29 EXPECT_EQ(0, thread_pool.OutstandingWaits());
michael@0 30
michael@0 31 EXPECT_TRUE(thread_pool.RegisterWait(this, event1, EmptyCallBack, &context));
michael@0 32 EXPECT_EQ(1, thread_pool.OutstandingWaits());
michael@0 33 EXPECT_TRUE(thread_pool.RegisterWait(this, event2, EmptyCallBack, &context));
michael@0 34 EXPECT_EQ(2, thread_pool.OutstandingWaits());
michael@0 35
michael@0 36 EXPECT_TRUE(thread_pool.UnRegisterWaits(this));
michael@0 37 EXPECT_EQ(0, thread_pool.OutstandingWaits());
michael@0 38
michael@0 39 EXPECT_EQ(TRUE, ::CloseHandle(event1));
michael@0 40 EXPECT_EQ(TRUE, ::CloseHandle(event2));
michael@0 41 }
michael@0 42
michael@0 43 // Test that register and unregister work, part 2.
michael@0 44 TEST(IPCTest, ThreadPoolRegisterTest2) {
michael@0 45 Win2kThreadPool thread_pool;
michael@0 46
michael@0 47 HANDLE event1 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
michael@0 48 HANDLE event2 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
michael@0 49
michael@0 50 uint32 context = 0;
michael@0 51 uint32 c1 = 0;
michael@0 52 uint32 c2 = 0;
michael@0 53
michael@0 54 EXPECT_TRUE(thread_pool.RegisterWait(&c1, event1, EmptyCallBack, &context));
michael@0 55 EXPECT_EQ(1, thread_pool.OutstandingWaits());
michael@0 56 EXPECT_TRUE(thread_pool.RegisterWait(&c2, event2, EmptyCallBack, &context));
michael@0 57 EXPECT_EQ(2, thread_pool.OutstandingWaits());
michael@0 58
michael@0 59 EXPECT_TRUE(thread_pool.UnRegisterWaits(&c2));
michael@0 60 EXPECT_EQ(1, thread_pool.OutstandingWaits());
michael@0 61 EXPECT_TRUE(thread_pool.UnRegisterWaits(&c2));
michael@0 62 EXPECT_EQ(1, thread_pool.OutstandingWaits());
michael@0 63
michael@0 64 EXPECT_TRUE(thread_pool.UnRegisterWaits(&c1));
michael@0 65 EXPECT_EQ(0, thread_pool.OutstandingWaits());
michael@0 66
michael@0 67 EXPECT_EQ(TRUE, ::CloseHandle(event1));
michael@0 68 EXPECT_EQ(TRUE, ::CloseHandle(event2));
michael@0 69 }
michael@0 70
michael@0 71 // Test that the thread pool has at least a thread that services an event.
michael@0 72 // Test that when the event is un-registered is no longer serviced.
michael@0 73 TEST(IPCTest, ThreadPoolSignalAndWaitTest) {
michael@0 74 Win2kThreadPool thread_pool;
michael@0 75
michael@0 76 // The events are auto reset and start not signaled.
michael@0 77 HANDLE event1 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
michael@0 78 HANDLE event2 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
michael@0 79
michael@0 80 EXPECT_TRUE(thread_pool.RegisterWait(this, event1, TestCallBack, event2));
michael@0 81
michael@0 82 EXPECT_EQ(WAIT_OBJECT_0, ::SignalObjectAndWait(event1, event2, 5000, FALSE));
michael@0 83 EXPECT_EQ(WAIT_OBJECT_0, ::SignalObjectAndWait(event1, event2, 5000, FALSE));
michael@0 84
michael@0 85 EXPECT_TRUE(thread_pool.UnRegisterWaits(this));
michael@0 86 EXPECT_EQ(0, thread_pool.OutstandingWaits());
michael@0 87
michael@0 88 EXPECT_EQ(WAIT_TIMEOUT, ::SignalObjectAndWait(event1, event2, 1000, FALSE));
michael@0 89
michael@0 90 EXPECT_EQ(TRUE, ::CloseHandle(event1));
michael@0 91 EXPECT_EQ(TRUE, ::CloseHandle(event2));
michael@0 92 }
michael@0 93
michael@0 94 } // namespace sandbox

mercurial