Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "sandbox/win/src/sync_policy_test.h"
      6 
      7 #include "base/win/scoped_handle.h"
      8 #include "sandbox/win/src/sandbox.h"
      9 #include "sandbox/win/src/sandbox_policy.h"
     10 #include "sandbox/win/src/sandbox_factory.h"
     11 #include "sandbox/win/src/nt_internals.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace sandbox {
     15 
     16 SBOX_TESTS_COMMAND int Event_Open(int argc, wchar_t **argv) {
     17   if (argc != 2)
     18     return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
     19 
     20   DWORD desired_access = SYNCHRONIZE;
     21   if (L'f' == argv[0][0])
     22     desired_access = EVENT_ALL_ACCESS;
     23 
     24   base::win::ScopedHandle event_open(::OpenEvent(
     25       desired_access, FALSE, argv[1]));
     26   DWORD error_open = ::GetLastError();
     27 
     28   if (event_open.Get())
     29     return SBOX_TEST_SUCCEEDED;
     30 
     31   if (ERROR_ACCESS_DENIED == error_open ||
     32       ERROR_BAD_PATHNAME == error_open)
     33     return SBOX_TEST_DENIED;
     34 
     35   return SBOX_TEST_FAILED;
     36 }
     37 
     38 SBOX_TESTS_COMMAND int Event_CreateOpen(int argc, wchar_t **argv) {
     39   if (argc < 2 || argc > 3)
     40     return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
     41 
     42   wchar_t *event_name = NULL;
     43   if (3 == argc)
     44     event_name = argv[2];
     45 
     46   BOOL manual_reset = FALSE;
     47   BOOL initial_state = FALSE;
     48   if (L't' == argv[0][0])
     49     manual_reset = TRUE;
     50   if (L't' == argv[1][0])
     51     initial_state = TRUE;
     52 
     53   base::win::ScopedHandle event_create(::CreateEvent(
     54       NULL, manual_reset, initial_state, event_name));
     55   DWORD error_create = ::GetLastError();
     56   base::win::ScopedHandle event_open;
     57   if (event_name)
     58     event_open.Set(::OpenEvent(EVENT_ALL_ACCESS, FALSE, event_name));
     59 
     60   if (event_create.Get()) {
     61     DWORD wait = ::WaitForSingleObject(event_create.Get(), 0);
     62     if (initial_state && WAIT_OBJECT_0 != wait)
     63       return SBOX_TEST_FAILED;
     64 
     65     if (!initial_state && WAIT_TIMEOUT != wait)
     66       return SBOX_TEST_FAILED;
     67   }
     68 
     69   if (event_name) {
     70     // Both event_open and event_create have to be valid.
     71     if (event_open.Get() && event_create)
     72       return SBOX_TEST_SUCCEEDED;
     73 
     74     if (event_open.Get() && !event_create || !event_open.Get() && event_create)
     75       return SBOX_TEST_FAILED;
     76   } else {
     77     // Only event_create has to be valid.
     78     if (event_create.Get())
     79       return SBOX_TEST_SUCCEEDED;
     80   }
     81 
     82   if (ERROR_ACCESS_DENIED == error_create ||
     83       ERROR_BAD_PATHNAME == error_create)
     84     return SBOX_TEST_DENIED;
     85 
     86   return SBOX_TEST_FAILED;
     87 }
     88 
     89 // Tests the creation of events using all the possible combinations.
     90 TEST(SyncPolicyTest, DISABLED_TestEvent) {
     91   TestRunner runner;
     92   EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_SYNC,
     93                              TargetPolicy::EVENTS_ALLOW_ANY,
     94                              L"test1"));
     95   EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_SYNC,
     96                              TargetPolicy::EVENTS_ALLOW_ANY,
     97                              L"test2"));
     98 
     99   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen f f"));
    100   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen t f"));
    101   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen f t"));
    102   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen t t"));
    103   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen f f test1"));
    104   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen t f test2"));
    105   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen f t test1"));
    106   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen t t test2"));
    107   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen f f test3"));
    108   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen t f test4"));
    109   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen f t test3"));
    110   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen t t test4"));
    111 }
    112 
    113 // Tests opening events with read only access.
    114 TEST(SyncPolicyTest, DISABLED_TestEventReadOnly) {
    115   TestRunner runner;
    116   EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_SYNC,
    117                              TargetPolicy::EVENTS_ALLOW_READONLY,
    118                              L"test1"));
    119   EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_SYNC,
    120                              TargetPolicy::EVENTS_ALLOW_READONLY,
    121                              L"test2"));
    122   EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_SYNC,
    123                              TargetPolicy::EVENTS_ALLOW_READONLY,
    124                              L"test5"));
    125   EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_SYNC,
    126                              TargetPolicy::EVENTS_ALLOW_READONLY,
    127                              L"test6"));
    128 
    129   base::win::ScopedHandle handle1(::CreateEvent(NULL, FALSE, FALSE, L"test1"));
    130   base::win::ScopedHandle handle2(::CreateEvent(NULL, FALSE, FALSE, L"test2"));
    131   base::win::ScopedHandle handle3(::CreateEvent(NULL, FALSE, FALSE, L"test3"));
    132   base::win::ScopedHandle handle4(::CreateEvent(NULL, FALSE, FALSE, L"test4"));
    133 
    134   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen f f"));
    135   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_CreateOpen t f"));
    136   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_Open f test1"));
    137   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_Open s test2"));
    138   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_Open f test3"));
    139   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_Open s test4"));
    140   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen f f test5"));
    141   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen t f test6"));
    142   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen f t test5"));
    143   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_CreateOpen t t test6"));
    144 }
    145 
    146 }  // namespace sandbox
    147