security/sandbox/win/src/threadpool_unittest.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/win/src/threadpool_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "sandbox/win/src/win2k_threadpool.h"
     1.9 +#include "testing/gtest/include/gtest/gtest.h"
    1.10 +
    1.11 +void __stdcall EmptyCallBack(void*, unsigned char) {
    1.12 +}
    1.13 +
    1.14 +void __stdcall TestCallBack(void* context, unsigned char) {
    1.15 +  HANDLE event = reinterpret_cast<HANDLE>(context);
    1.16 +  ::SetEvent(event);
    1.17 +}
    1.18 +
    1.19 +namespace sandbox {
    1.20 +
    1.21 +// Test that register and unregister work, part 1.
    1.22 +TEST(IPCTest, ThreadPoolRegisterTest1) {
    1.23 +  Win2kThreadPool thread_pool;
    1.24 +
    1.25 +  EXPECT_EQ(0, thread_pool.OutstandingWaits());
    1.26 +
    1.27 +  HANDLE event1 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
    1.28 +  HANDLE event2 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
    1.29 +
    1.30 +  uint32 context = 0;
    1.31 +  EXPECT_FALSE(thread_pool.RegisterWait(0, event1, EmptyCallBack, &context));
    1.32 +  EXPECT_EQ(0, thread_pool.OutstandingWaits());
    1.33 +
    1.34 +  EXPECT_TRUE(thread_pool.RegisterWait(this, event1, EmptyCallBack, &context));
    1.35 +  EXPECT_EQ(1, thread_pool.OutstandingWaits());
    1.36 +  EXPECT_TRUE(thread_pool.RegisterWait(this, event2, EmptyCallBack, &context));
    1.37 +  EXPECT_EQ(2, thread_pool.OutstandingWaits());
    1.38 +
    1.39 +  EXPECT_TRUE(thread_pool.UnRegisterWaits(this));
    1.40 +  EXPECT_EQ(0, thread_pool.OutstandingWaits());
    1.41 +
    1.42 +  EXPECT_EQ(TRUE, ::CloseHandle(event1));
    1.43 +  EXPECT_EQ(TRUE, ::CloseHandle(event2));
    1.44 +}
    1.45 +
    1.46 +// Test that register and unregister work, part 2.
    1.47 +TEST(IPCTest, ThreadPoolRegisterTest2) {
    1.48 +  Win2kThreadPool thread_pool;
    1.49 +
    1.50 +  HANDLE event1 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
    1.51 +  HANDLE event2 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
    1.52 +
    1.53 +  uint32 context = 0;
    1.54 +  uint32 c1 = 0;
    1.55 +  uint32 c2 = 0;
    1.56 +
    1.57 +  EXPECT_TRUE(thread_pool.RegisterWait(&c1, event1, EmptyCallBack, &context));
    1.58 +  EXPECT_EQ(1, thread_pool.OutstandingWaits());
    1.59 +  EXPECT_TRUE(thread_pool.RegisterWait(&c2, event2, EmptyCallBack, &context));
    1.60 +  EXPECT_EQ(2, thread_pool.OutstandingWaits());
    1.61 +
    1.62 +  EXPECT_TRUE(thread_pool.UnRegisterWaits(&c2));
    1.63 +  EXPECT_EQ(1, thread_pool.OutstandingWaits());
    1.64 +  EXPECT_TRUE(thread_pool.UnRegisterWaits(&c2));
    1.65 +  EXPECT_EQ(1, thread_pool.OutstandingWaits());
    1.66 +
    1.67 +  EXPECT_TRUE(thread_pool.UnRegisterWaits(&c1));
    1.68 +  EXPECT_EQ(0, thread_pool.OutstandingWaits());
    1.69 +
    1.70 +  EXPECT_EQ(TRUE, ::CloseHandle(event1));
    1.71 +  EXPECT_EQ(TRUE, ::CloseHandle(event2));
    1.72 +}
    1.73 +
    1.74 +// Test that the thread pool has at least a thread that services an event.
    1.75 +// Test that when the event is un-registered is no longer serviced.
    1.76 +TEST(IPCTest, ThreadPoolSignalAndWaitTest) {
    1.77 +  Win2kThreadPool thread_pool;
    1.78 +
    1.79 +  // The events are auto reset and start not signaled.
    1.80 +  HANDLE event1 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
    1.81 +  HANDLE event2 = ::CreateEventW(NULL, FALSE, FALSE, NULL);
    1.82 +
    1.83 +  EXPECT_TRUE(thread_pool.RegisterWait(this, event1, TestCallBack, event2));
    1.84 +
    1.85 +  EXPECT_EQ(WAIT_OBJECT_0, ::SignalObjectAndWait(event1, event2, 5000, FALSE));
    1.86 +  EXPECT_EQ(WAIT_OBJECT_0, ::SignalObjectAndWait(event1, event2, 5000, FALSE));
    1.87 +
    1.88 +  EXPECT_TRUE(thread_pool.UnRegisterWaits(this));
    1.89 +  EXPECT_EQ(0, thread_pool.OutstandingWaits());
    1.90 +
    1.91 +  EXPECT_EQ(WAIT_TIMEOUT, ::SignalObjectAndWait(event1, event2, 1000, FALSE));
    1.92 +
    1.93 +  EXPECT_EQ(TRUE, ::CloseHandle(event1));
    1.94 +  EXPECT_EQ(TRUE, ::CloseHandle(event2));
    1.95 +}
    1.96 +
    1.97 +}  // namespace sandbox

mercurial