126f5ecb7SAlan Somers /*-
226f5ecb7SAlan Somers * Copyright 2018 Aniket Pandey
326f5ecb7SAlan Somers *
426f5ecb7SAlan Somers * Redistribution and use in source and binary forms, with or without
526f5ecb7SAlan Somers * modification, are permitted provided that the following conditions
626f5ecb7SAlan Somers * are met:
726f5ecb7SAlan Somers * 1. Redistributions of source code must retain the above copyright
826f5ecb7SAlan Somers * notice, this list of conditions and the following disclaimer.
926f5ecb7SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright
1026f5ecb7SAlan Somers * notice, this list of conditions and the following disclaimer in the
1126f5ecb7SAlan Somers * documentation and/or other materials provided with the distribution.
1226f5ecb7SAlan Somers *
1326f5ecb7SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1426f5ecb7SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1526f5ecb7SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1626f5ecb7SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1726f5ecb7SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1826f5ecb7SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1926f5ecb7SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2026f5ecb7SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2126f5ecb7SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2226f5ecb7SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2326f5ecb7SAlan Somers * SUCH DAMAGE.
2426f5ecb7SAlan Somers */
2526f5ecb7SAlan Somers
2626f5ecb7SAlan Somers #include <atf-c.h>
2726f5ecb7SAlan Somers #include <fcntl.h>
2826f5ecb7SAlan Somers
2926f5ecb7SAlan Somers #include "utils.h"
3026f5ecb7SAlan Somers
3126f5ecb7SAlan Somers static struct pollfd fds[1];
3226f5ecb7SAlan Somers static mode_t mode = 0777;
33*b13a70d5SAlan Somers static int filedesc;
3426f5ecb7SAlan Somers static off_t offlen = 0;
3526f5ecb7SAlan Somers static const char *path = "fileforaudit";
3626f5ecb7SAlan Somers static const char *errpath = "dirdoesnotexist/fileforaudit";
3726f5ecb7SAlan Somers static const char *successreg = "fileforaudit.*return,success";
3826f5ecb7SAlan Somers static const char *failurereg = "fileforaudit.*return,failure";
3926f5ecb7SAlan Somers
4026f5ecb7SAlan Somers
4126f5ecb7SAlan Somers ATF_TC_WITH_CLEANUP(truncate_success);
ATF_TC_HEAD(truncate_success,tc)4226f5ecb7SAlan Somers ATF_TC_HEAD(truncate_success, tc)
4326f5ecb7SAlan Somers {
4426f5ecb7SAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
4526f5ecb7SAlan Somers "truncate(2) call");
4626f5ecb7SAlan Somers }
4726f5ecb7SAlan Somers
ATF_TC_BODY(truncate_success,tc)4826f5ecb7SAlan Somers ATF_TC_BODY(truncate_success, tc)
4926f5ecb7SAlan Somers {
5026f5ecb7SAlan Somers /* File needs to exist to call truncate(2) */
51*b13a70d5SAlan Somers ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
5226f5ecb7SAlan Somers FILE *pipefd = setup(fds, "fw");
5326f5ecb7SAlan Somers ATF_REQUIRE_EQ(0, truncate(path, offlen));
5426f5ecb7SAlan Somers check_audit(fds, successreg, pipefd);
55*b13a70d5SAlan Somers close(filedesc);
5626f5ecb7SAlan Somers }
5726f5ecb7SAlan Somers
ATF_TC_CLEANUP(truncate_success,tc)5826f5ecb7SAlan Somers ATF_TC_CLEANUP(truncate_success, tc)
5926f5ecb7SAlan Somers {
6026f5ecb7SAlan Somers cleanup();
6126f5ecb7SAlan Somers }
6226f5ecb7SAlan Somers
6326f5ecb7SAlan Somers
6426f5ecb7SAlan Somers ATF_TC_WITH_CLEANUP(truncate_failure);
ATF_TC_HEAD(truncate_failure,tc)6526f5ecb7SAlan Somers ATF_TC_HEAD(truncate_failure, tc)
6626f5ecb7SAlan Somers {
6726f5ecb7SAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
6826f5ecb7SAlan Somers "truncate(2) call");
6926f5ecb7SAlan Somers }
7026f5ecb7SAlan Somers
ATF_TC_BODY(truncate_failure,tc)7126f5ecb7SAlan Somers ATF_TC_BODY(truncate_failure, tc)
7226f5ecb7SAlan Somers {
7326f5ecb7SAlan Somers FILE *pipefd = setup(fds, "fw");
7426f5ecb7SAlan Somers /* Failure reason: file does not exist */
7526f5ecb7SAlan Somers ATF_REQUIRE_EQ(-1, truncate(errpath, offlen));
7626f5ecb7SAlan Somers check_audit(fds, failurereg, pipefd);
7726f5ecb7SAlan Somers }
7826f5ecb7SAlan Somers
ATF_TC_CLEANUP(truncate_failure,tc)7926f5ecb7SAlan Somers ATF_TC_CLEANUP(truncate_failure, tc)
8026f5ecb7SAlan Somers {
8126f5ecb7SAlan Somers cleanup();
8226f5ecb7SAlan Somers }
8326f5ecb7SAlan Somers
8426f5ecb7SAlan Somers
8526f5ecb7SAlan Somers ATF_TC_WITH_CLEANUP(ftruncate_success);
ATF_TC_HEAD(ftruncate_success,tc)8626f5ecb7SAlan Somers ATF_TC_HEAD(ftruncate_success, tc)
8726f5ecb7SAlan Somers {
8826f5ecb7SAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
8926f5ecb7SAlan Somers "ftruncate(2) call");
9026f5ecb7SAlan Somers }
9126f5ecb7SAlan Somers
ATF_TC_BODY(ftruncate_success,tc)9226f5ecb7SAlan Somers ATF_TC_BODY(ftruncate_success, tc)
9326f5ecb7SAlan Somers {
9426f5ecb7SAlan Somers const char *regex = "ftruncate.*return,success";
9526f5ecb7SAlan Somers /* Valid file descriptor needs to exist to call ftruncate(2) */
9626f5ecb7SAlan Somers ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR)) != -1);
9726f5ecb7SAlan Somers FILE *pipefd = setup(fds, "fw");
9826f5ecb7SAlan Somers ATF_REQUIRE_EQ(0, ftruncate(filedesc, offlen));
9926f5ecb7SAlan Somers check_audit(fds, regex, pipefd);
100*b13a70d5SAlan Somers close(filedesc);
10126f5ecb7SAlan Somers }
10226f5ecb7SAlan Somers
ATF_TC_CLEANUP(ftruncate_success,tc)10326f5ecb7SAlan Somers ATF_TC_CLEANUP(ftruncate_success, tc)
10426f5ecb7SAlan Somers {
10526f5ecb7SAlan Somers cleanup();
10626f5ecb7SAlan Somers }
10726f5ecb7SAlan Somers
10826f5ecb7SAlan Somers
10926f5ecb7SAlan Somers ATF_TC_WITH_CLEANUP(ftruncate_failure);
ATF_TC_HEAD(ftruncate_failure,tc)11026f5ecb7SAlan Somers ATF_TC_HEAD(ftruncate_failure, tc)
11126f5ecb7SAlan Somers {
11226f5ecb7SAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
11326f5ecb7SAlan Somers "ftruncate(2) call");
11426f5ecb7SAlan Somers }
11526f5ecb7SAlan Somers
ATF_TC_BODY(ftruncate_failure,tc)11626f5ecb7SAlan Somers ATF_TC_BODY(ftruncate_failure, tc)
11726f5ecb7SAlan Somers {
11826f5ecb7SAlan Somers const char *regex = "ftruncate.*return,failure";
11926f5ecb7SAlan Somers FILE *pipefd = setup(fds, "fw");
12026f5ecb7SAlan Somers /* Failure reason: bad file descriptor */
12126f5ecb7SAlan Somers ATF_REQUIRE_EQ(-1, ftruncate(-1, offlen));
12226f5ecb7SAlan Somers check_audit(fds, regex, pipefd);
12326f5ecb7SAlan Somers }
12426f5ecb7SAlan Somers
ATF_TC_CLEANUP(ftruncate_failure,tc)12526f5ecb7SAlan Somers ATF_TC_CLEANUP(ftruncate_failure, tc)
12626f5ecb7SAlan Somers {
12726f5ecb7SAlan Somers cleanup();
12826f5ecb7SAlan Somers }
12926f5ecb7SAlan Somers
13026f5ecb7SAlan Somers
ATF_TP_ADD_TCS(tp)13126f5ecb7SAlan Somers ATF_TP_ADD_TCS(tp)
13226f5ecb7SAlan Somers {
13326f5ecb7SAlan Somers ATF_TP_ADD_TC(tp, truncate_success);
13426f5ecb7SAlan Somers ATF_TP_ADD_TC(tp, truncate_failure);
13526f5ecb7SAlan Somers ATF_TP_ADD_TC(tp, ftruncate_success);
13626f5ecb7SAlan Somers ATF_TP_ADD_TC(tp, ftruncate_failure);
13726f5ecb7SAlan Somers
13826f5ecb7SAlan Somers return (atf_no_error());
13926f5ecb7SAlan Somers }
140