policy_engine_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2006-2008 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. #include <stddef.h>
  5. #include <stdint.h>
  6. #include "sandbox/win/src/policy_engine_params.h"
  7. #include "sandbox/win/src/policy_engine_processor.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #define POLPARAMS_BEGIN(x) sandbox::ParameterSet x[] = {
  10. #define POLPARAM(p) sandbox::ParamPickerMake(p),
  11. #define POLPARAMS_END }
  12. namespace sandbox {
  13. TEST(PolicyEngineTest, Rules1) {
  14. // Construct two policy rules that say:
  15. //
  16. // #1
  17. // If the path is c:\\documents and settings\\* AND
  18. // If the creation mode is 'open existing' AND
  19. // If the security descriptor is null THEN
  20. // Ask the broker.
  21. //
  22. // #2
  23. // If the security descriptor is null AND
  24. // If the path ends with *.txt AND
  25. // If the creation mode is not 'create new' THEN
  26. // return Access Denied.
  27. enum FileCreateArgs {
  28. FileNameArg,
  29. CreationDispositionArg,
  30. FlagsAndAttributesArg,
  31. SecurityAttributes
  32. };
  33. const size_t policy_sz = 1024;
  34. PolicyBuffer* policy = reinterpret_cast<PolicyBuffer*>(new char[policy_sz]);
  35. OpcodeFactory opcode_maker(policy, policy_sz - 0x40);
  36. // Add rule set #1
  37. opcode_maker.MakeOpWStringMatch(FileNameArg, L"c:\\documents and settings\\",
  38. 0, CASE_INSENSITIVE, kPolNone);
  39. opcode_maker.MakeOpNumberMatch(CreationDispositionArg, OPEN_EXISTING,
  40. kPolNone);
  41. opcode_maker.MakeOpVoidPtrMatch(SecurityAttributes, nullptr, kPolNone);
  42. opcode_maker.MakeOpAction(ASK_BROKER, kPolNone);
  43. // Add rule set #2
  44. opcode_maker.MakeOpWStringMatch(FileNameArg, L".TXT", kSeekToEnd,
  45. CASE_INSENSITIVE, kPolNone);
  46. opcode_maker.MakeOpNumberMatch(CreationDispositionArg, CREATE_NEW,
  47. kPolNegateEval);
  48. opcode_maker.MakeOpAction(FAKE_ACCESS_DENIED, kPolNone);
  49. policy->opcode_count = 7;
  50. const wchar_t* filename = L"c:\\Documents and Settings\\Microsoft\\BLAH.txt";
  51. uint32_t creation_mode = OPEN_EXISTING;
  52. uint32_t flags = FILE_ATTRIBUTE_NORMAL;
  53. void* security_descriptor = nullptr;
  54. POLPARAMS_BEGIN(eval_params)
  55. POLPARAM(filename)
  56. POLPARAM(creation_mode)
  57. POLPARAM(flags)
  58. POLPARAM(security_descriptor)
  59. POLPARAMS_END;
  60. PolicyResult pr;
  61. PolicyProcessor pol_ev(policy);
  62. // Test should match the first rule set.
  63. pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params));
  64. EXPECT_EQ(POLICY_MATCH, pr);
  65. EXPECT_EQ(ASK_BROKER, pol_ev.GetAction());
  66. // Test should still match the first rule set.
  67. pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params));
  68. EXPECT_EQ(POLICY_MATCH, pr);
  69. EXPECT_EQ(ASK_BROKER, pol_ev.GetAction());
  70. // Changing creation_mode such that evaluation should not match any rule.
  71. creation_mode = CREATE_NEW;
  72. pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params));
  73. EXPECT_EQ(NO_POLICY_MATCH, pr);
  74. // Changing creation_mode such that evaluation should match rule #2.
  75. creation_mode = OPEN_ALWAYS;
  76. pr = pol_ev.Evaluate(kShortEval, eval_params, _countof(eval_params));
  77. EXPECT_EQ(POLICY_MATCH, pr);
  78. EXPECT_EQ(FAKE_ACCESS_DENIED, pol_ev.GetAction());
  79. delete[] reinterpret_cast<char*>(policy);
  80. }
  81. } // namespace sandbox