caller_validation_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2022 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 "chrome/elevation_service/caller_validation.h"
  5. #include "base/process/launch.h"
  6. #include "base/process/process.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace elevation_service {
  9. using CallerValidationTest = ::testing::Test;
  10. TEST_F(CallerValidationTest, NoneValidationTest) {
  11. const auto my_process = base::Process::Current();
  12. const std::string data =
  13. GenerateValidationData(ProtectionLevel::NONE, my_process);
  14. ASSERT_FALSE(data.empty());
  15. ASSERT_TRUE(ValidateData(my_process, data));
  16. }
  17. TEST_F(CallerValidationTest, PathValidationTest) {
  18. const auto my_process = base::Process::Current();
  19. const std::string data =
  20. GenerateValidationData(ProtectionLevel::PATH_VALIDATION, my_process);
  21. ASSERT_FALSE(data.empty());
  22. ASSERT_TRUE(ValidateData(my_process, data));
  23. }
  24. TEST_F(CallerValidationTest, PathValidationTestFail) {
  25. const auto my_process = base::Process::Current();
  26. const std::string data =
  27. GenerateValidationData(ProtectionLevel::PATH_VALIDATION, my_process);
  28. ASSERT_FALSE(data.empty());
  29. auto notepad_process =
  30. base::LaunchProcess(L"notepad.exe", base::LaunchOptions());
  31. ASSERT_TRUE(notepad_process.IsRunning());
  32. ASSERT_FALSE(ValidateData(notepad_process, data));
  33. ASSERT_TRUE(notepad_process.Terminate(0, true));
  34. }
  35. TEST_F(CallerValidationTest, PathValidationTestOtherProcess) {
  36. std::string data;
  37. // Start two separate notepad processes to validate that path validation only
  38. // cares about the process path and not the process itself.
  39. {
  40. auto notepad_process =
  41. base::LaunchProcess(L"notepad.exe", base::LaunchOptions());
  42. ASSERT_TRUE(notepad_process.IsRunning());
  43. data = GenerateValidationData(ProtectionLevel::PATH_VALIDATION,
  44. notepad_process);
  45. ASSERT_TRUE(notepad_process.Terminate(0, true));
  46. }
  47. ASSERT_FALSE(data.empty());
  48. {
  49. auto notepad_process =
  50. base::LaunchProcess(L"notepad.exe", base::LaunchOptions());
  51. ASSERT_TRUE(notepad_process.IsRunning());
  52. ASSERT_TRUE(ValidateData(notepad_process, data));
  53. ASSERT_TRUE(notepad_process.Terminate(0, true));
  54. }
  55. }
  56. TEST_F(CallerValidationTest, NoneValidationTestOtherProcess) {
  57. const auto my_process = base::Process::Current();
  58. const std::string data =
  59. GenerateValidationData(ProtectionLevel::NONE, my_process);
  60. ASSERT_FALSE(data.empty());
  61. auto notepad_process =
  62. base::LaunchProcess(L"notepad.exe", base::LaunchOptions());
  63. ASSERT_TRUE(notepad_process.IsRunning());
  64. // None validation should not care if the process is different.
  65. ASSERT_TRUE(ValidateData(notepad_process, data));
  66. ASSERT_TRUE(notepad_process.Terminate(0, true));
  67. }
  68. } // namespace elevation_service