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