TestKernelPledge.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2018-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibTest/TestCase.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. TEST_CASE(test_nonexistent_pledge)
  11. {
  12. auto res = pledge("testing123", "notthere");
  13. if (res >= 0)
  14. FAIL("Pledging on existent promises should fail.");
  15. }
  16. TEST_CASE(test_pledge_argument_validation)
  17. {
  18. const auto long_argument = String::repeated('a', 2048);
  19. auto res = pledge(long_argument.characters(), "stdio");
  20. EXPECT_EQ(res, -1);
  21. EXPECT_EQ(errno, E2BIG);
  22. res = pledge("stdio", long_argument.characters());
  23. EXPECT_EQ(res, -1);
  24. EXPECT_EQ(errno, E2BIG);
  25. res = pledge(long_argument.characters(), long_argument.characters());
  26. EXPECT_EQ(res, -1);
  27. EXPECT_EQ(errno, E2BIG);
  28. res = pledge("fake", "stdio");
  29. EXPECT_EQ(res, -1);
  30. EXPECT_EQ(errno, EINVAL);
  31. res = pledge("stdio", "fake");
  32. EXPECT_EQ(res, -1);
  33. EXPECT_EQ(errno, EINVAL);
  34. }
  35. TEST_CASE(test_pledge_failures)
  36. {
  37. auto res = pledge("stdio unix rpath", "stdio");
  38. if (res < 0)
  39. FAIL("Initial pledge is expected to work.");
  40. res = pledge("stdio unix", "stdio unix");
  41. if (res >= 0)
  42. FAIL("Additional execpromise \"unix\" should have failed");
  43. res = pledge("stdio", "stdio");
  44. if (res < 0)
  45. FAIL("Reducing promises is expected to work.");
  46. }