libcap/tests/sanity-tests/test-libcap.c
Susant Sahani 931be7aaf4 Adds tests according to the CI
justification
Adds tests according to the CI wiki specifically the standard test interface in the spec.
The playbook includes Tier1 level test cases that have been tested in the following contexts and
is passing reliably: Classic. Test logs are stored in the artifacts directory.
The following steps are used to execute the tests using the standard test interface:

Test enveronment
Make sure you have installed packages from the spec
```
ansible-2.4.1.0-2.fc28.noarch
python2-dnf-2.7.5-1.fc28.noarch
libselinux-python-2.7-2.fc28.x86_64
standard-test-roles-2.5-1.fc28.noarch
```

Run tests for Classic
Snip of the example test run for Classic tests:

```

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 2 good, 0 bad
::   Assertions: 2 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Test
::   Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 15:49:17 ] :: [   LOG    ] :: Starting libcap tests ...
:: [ 15:49:17 ] :: [   LOG    ] :: Starting libcap tests ...
:: [ 15:49:17 ] :: [  BEGIN   ] :: Running '/usr/bin/test-libcap'
[==========] Running 1 test(s).
[ RUN      ] test_drop_cap_net_raw
[       OK ] test_drop_cap_net_raw
[==========] 1 test(s) run.
[  PASSED  ] 1 test(s).
:: [ 15:49:17 ] :: [   PASS   ] :: Command '/usr/bin/test-libcap' (Expected 0, got 0)
:: [ 15:49:17 ] :: [   PASS   ] :: Command '/usr/bin/test-libcap' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 1 good, 0 bad
::   Assertions: 1 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS
```
2018-06-26 15:50:59 +05:30

53 lines
1.3 KiB
C

/*
# SPDX-License-Identifier: LGPL-2.1+
# ~~~
# Description: libcap tests
#
# Author: Susant Sahani <susant@redhat.com>
# Copyright (c) 2018 Red Hat, Inc.
# ~~~
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <setjmp.h>
#include <inttypes.h>
#include <cmocka.h>
#include <sys/capability.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
void drop_cap(cap_value_t capflag) {
cap_t d;
d = cap_get_proc();
assert_non_null(d);
assert_return_code(cap_set_flag(d, CAP_EFFECTIVE, 1, &capflag, CAP_CLEAR), 0);
assert_return_code(cap_set_flag(d, CAP_PERMITTED, 1, &capflag, CAP_CLEAR), 0);
assert_return_code(cap_set_proc(d), 0);
}
void test_drop_cap_net_raw(void **state) {
int s;
assert_true((s = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) >= 0);
close(s);
drop_cap(CAP_NET_RAW);
assert_false((s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP)) >= 0);
}
int main(int argc, char *argv[]) {
const struct CMUnitTest libcap_tests[] = {
cmocka_unit_test(test_drop_cap_net_raw),
};
return cmocka_run_group_tests(libcap_tests, NULL, NULL);
}