michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "sandbox/win/src/policy_engine_params.h" michael@0: #include "sandbox/win/src/policy_engine_processor.h" michael@0: #include "testing/gtest/include/gtest/gtest.h" michael@0: michael@0: #define POLPARAMS_BEGIN(x) sandbox::ParameterSet x[] = { michael@0: #define POLPARAM(p) sandbox::ParamPickerMake(p), michael@0: #define POLPARAMS_END } michael@0: michael@0: namespace sandbox { michael@0: michael@0: bool SetupNtdllImports(); michael@0: michael@0: TEST(PolicyEngineTest, Rules1) { michael@0: SetupNtdllImports(); michael@0: michael@0: // Construct two policy rules that say: michael@0: // michael@0: // #1 michael@0: // If the path is c:\\documents and settings\\* AND michael@0: // If the creation mode is 'open existing' AND michael@0: // If the security descriptor is null THEN michael@0: // Ask the broker. michael@0: // michael@0: // #2 michael@0: // If the security descriptor is null AND michael@0: // If the path ends with *.txt AND michael@0: // If the creation mode is not 'create new' THEN michael@0: // return Access Denied. michael@0: michael@0: enum FileCreateArgs { michael@0: FileNameArg, michael@0: CreationDispositionArg, michael@0: FlagsAndAttributesArg, michael@0: SecurityAttributes michael@0: }; michael@0: michael@0: const size_t policy_sz = 1024; michael@0: PolicyBuffer* policy = reinterpret_cast(new char[policy_sz]); michael@0: OpcodeFactory opcode_maker(policy, policy_sz - 0x40); michael@0: michael@0: // Add rule set #1 michael@0: opcode_maker.MakeOpWStringMatch(FileNameArg, michael@0: L"c:\\documents and settings\\", michael@0: 0, CASE_INSENSITIVE, kPolNone); michael@0: opcode_maker.MakeOpNumberMatch(CreationDispositionArg, OPEN_EXISTING, michael@0: kPolNone); michael@0: opcode_maker.MakeOpVoidPtrMatch(SecurityAttributes, (void*)NULL, michael@0: kPolNone); michael@0: opcode_maker.MakeOpAction(ASK_BROKER, kPolNone); michael@0: michael@0: // Add rule set #2 michael@0: opcode_maker.MakeOpWStringMatch(FileNameArg, L".TXT", michael@0: kSeekToEnd, CASE_INSENSITIVE, kPolNone); michael@0: opcode_maker.MakeOpNumberMatch(CreationDispositionArg, CREATE_NEW, michael@0: kPolNegateEval); michael@0: opcode_maker.MakeOpAction(FAKE_ACCESS_DENIED, kPolNone); michael@0: policy->opcode_count = 7; michael@0: michael@0: wchar_t* filename = L"c:\\Documents and Settings\\Microsoft\\BLAH.txt"; michael@0: unsigned long creation_mode = OPEN_EXISTING; michael@0: unsigned long flags = FILE_ATTRIBUTE_NORMAL; michael@0: void* security_descriptor = NULL; michael@0: michael@0: POLPARAMS_BEGIN(eval_params) michael@0: POLPARAM(filename) michael@0: POLPARAM(creation_mode) michael@0: POLPARAM(flags) michael@0: POLPARAM(security_descriptor) michael@0: POLPARAMS_END; michael@0: michael@0: PolicyResult pr; michael@0: PolicyProcessor pol_ev(policy); michael@0: michael@0: // Test should match the first rule set. michael@0: pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params)); michael@0: EXPECT_EQ(POLICY_MATCH, pr); michael@0: EXPECT_EQ(ASK_BROKER, pol_ev.GetAction()); michael@0: michael@0: // Test should still match the first rule set. michael@0: pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params)); michael@0: EXPECT_EQ(POLICY_MATCH, pr); michael@0: EXPECT_EQ(ASK_BROKER, pol_ev.GetAction()); michael@0: michael@0: // Changing creation_mode such that evaluation should not match any rule. michael@0: creation_mode = CREATE_NEW; michael@0: pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params)); michael@0: EXPECT_EQ(NO_POLICY_MATCH, pr); michael@0: michael@0: // Changing creation_mode such that evaluation should match rule #2. michael@0: creation_mode = OPEN_ALWAYS; michael@0: pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params)); michael@0: EXPECT_EQ(POLICY_MATCH, pr); michael@0: EXPECT_EQ(FAKE_ACCESS_DENIED, pol_ev.GetAction()); michael@0: michael@0: delete [] reinterpret_cast(policy); michael@0: } michael@0: michael@0: } // namespace sandbox