xref: /freebsd/lib/libkvm/tests/kvm_open2_test.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
110f81a9bSEnji Cooper /*-
2*148a8da8SEnji Cooper  * Copyright (c) 2017 Enji Cooper <ngie@freebsd.org>
310f81a9bSEnji Cooper  *
410f81a9bSEnji Cooper  * Redistribution and use in source and binary forms, with or without
510f81a9bSEnji Cooper  * modification, are permitted provided that the following conditions
610f81a9bSEnji Cooper  * are met:
710f81a9bSEnji Cooper  * 1. Redistributions of source code must retain the above copyright
810f81a9bSEnji Cooper  *    notice, this list of conditions and the following disclaimer.
910f81a9bSEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1010f81a9bSEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1110f81a9bSEnji Cooper  *    documentation and/or other materials provided with the distribution.
1210f81a9bSEnji Cooper  *
1310f81a9bSEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1410f81a9bSEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1510f81a9bSEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1610f81a9bSEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1710f81a9bSEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1810f81a9bSEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1910f81a9bSEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2010f81a9bSEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2110f81a9bSEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2210f81a9bSEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2310f81a9bSEnji Cooper  * SUCH DAMAGE.
2410f81a9bSEnji Cooper  */
2510f81a9bSEnji Cooper 
2610f81a9bSEnji Cooper #include <sys/param.h>
2710f81a9bSEnji Cooper #include <errno.h>
2810f81a9bSEnji Cooper #include <fcntl.h>
2910f81a9bSEnji Cooper #include <kvm.h>
3010f81a9bSEnji Cooper #include <limits.h>
3110f81a9bSEnji Cooper #include <paths.h>
3210f81a9bSEnji Cooper #include <signal.h>
3310f81a9bSEnji Cooper #include <stdio.h>
3410f81a9bSEnji Cooper #include <stdlib.h>
3510f81a9bSEnji Cooper #include <string.h>
3610f81a9bSEnji Cooper 
3710f81a9bSEnji Cooper #include <atf-c.h>
3810f81a9bSEnji Cooper 
3910f81a9bSEnji Cooper #include "kvm_test_common.h"
4010f81a9bSEnji Cooper 
4110f81a9bSEnji Cooper ATF_TC_WITHOUT_HEAD(kvm_open2_negative_test_nonexistent_corefile);
ATF_TC_BODY(kvm_open2_negative_test_nonexistent_corefile,tc)4210f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_nonexistent_corefile, tc)
4310f81a9bSEnji Cooper {
4410f81a9bSEnji Cooper 
4510f81a9bSEnji Cooper 	errbuf_clear();
4610f81a9bSEnji Cooper 	ATF_CHECK(kvm_open2(NULL, "/nonexistent", O_RDONLY, NULL, NULL) == NULL);
4710f81a9bSEnji Cooper 	ATF_CHECK(!errbuf_has_error(errbuf));
4810f81a9bSEnji Cooper 	errbuf_clear();
4910f81a9bSEnji Cooper 	ATF_CHECK(kvm_open2(NULL, "/nonexistent", O_RDONLY,
5010f81a9bSEnji Cooper 	    errbuf, NULL) == NULL);
5110f81a9bSEnji Cooper 	ATF_CHECK(errbuf_has_error(errbuf));
5210f81a9bSEnji Cooper }
5310f81a9bSEnji Cooper 
5410f81a9bSEnji Cooper ATF_TC_WITHOUT_HEAD(kvm_open2_negative_test_nonexistent_execfile);
ATF_TC_BODY(kvm_open2_negative_test_nonexistent_execfile,tc)5510f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_nonexistent_execfile, tc)
5610f81a9bSEnji Cooper {
5710f81a9bSEnji Cooper 
5810f81a9bSEnji Cooper 	errbuf_clear();
5910f81a9bSEnji Cooper 	ATF_CHECK(kvm_open2("/nonexistent", _PATH_DEVZERO, O_RDONLY,
6010f81a9bSEnji Cooper 	    NULL, NULL) == NULL);
6110f81a9bSEnji Cooper 	ATF_CHECK(strlen(errbuf) == 0);
6210f81a9bSEnji Cooper 	errbuf_clear();
6310f81a9bSEnji Cooper 	ATF_CHECK(kvm_open2("/nonexistent", _PATH_DEVZERO, O_RDONLY,
6410f81a9bSEnji Cooper 	    errbuf, NULL) == NULL);
6510f81a9bSEnji Cooper 	ATF_CHECK(errbuf_has_error(errbuf));
6610f81a9bSEnji Cooper }
6710f81a9bSEnji Cooper 
6810f81a9bSEnji Cooper ATF_TC(kvm_open2_negative_test_invalid_corefile);
ATF_TC_HEAD(kvm_open2_negative_test_invalid_corefile,tc)6910f81a9bSEnji Cooper ATF_TC_HEAD(kvm_open2_negative_test_invalid_corefile, tc)
7010f81a9bSEnji Cooper {
7110f81a9bSEnji Cooper 
7210f81a9bSEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
7310f81a9bSEnji Cooper }
7410f81a9bSEnji Cooper 
ATF_TC_BODY(kvm_open2_negative_test_invalid_corefile,tc)7510f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_invalid_corefile, tc)
7610f81a9bSEnji Cooper {
7710f81a9bSEnji Cooper 	kvm_t *kd;
7810f81a9bSEnji Cooper 
7910f81a9bSEnji Cooper 	errbuf_clear();
8010f81a9bSEnji Cooper 	atf_utils_create_file("some-file", "this is a text file");
8110f81a9bSEnji Cooper 	kd = kvm_open2(NULL, "some-file", O_RDONLY, errbuf, NULL);
8210f81a9bSEnji Cooper 	ATF_CHECK(errbuf_has_error(errbuf));
8310f81a9bSEnji Cooper 	ATF_REQUIRE_MSG(kd == NULL, "kvm_open2 succeeded");
8410f81a9bSEnji Cooper }
8510f81a9bSEnji Cooper 
8610f81a9bSEnji Cooper ATF_TC(kvm_open2_negative_test_invalid_execfile);
ATF_TC_HEAD(kvm_open2_negative_test_invalid_execfile,tc)8710f81a9bSEnji Cooper ATF_TC_HEAD(kvm_open2_negative_test_invalid_execfile, tc)
8810f81a9bSEnji Cooper {
8910f81a9bSEnji Cooper 
9010f81a9bSEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
9110f81a9bSEnji Cooper }
9210f81a9bSEnji Cooper 
ATF_TC_BODY(kvm_open2_negative_test_invalid_execfile,tc)9310f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_invalid_execfile, tc)
9410f81a9bSEnji Cooper {
9510f81a9bSEnji Cooper 	kvm_t *kd;
9610f81a9bSEnji Cooper 
9710f81a9bSEnji Cooper 	errbuf_clear();
9810f81a9bSEnji Cooper 	atf_utils_create_file("some-file", "this is a text file");
9910f81a9bSEnji Cooper 	kd = kvm_open2("some-file", "/bin/sh", O_RDONLY, errbuf, NULL);
10010f81a9bSEnji Cooper 	ATF_CHECK(errbuf_has_error(errbuf));
10110f81a9bSEnji Cooper 	ATF_REQUIRE_MSG(kd == NULL, "kvm_open2 succeeded unexpectedly");
10210f81a9bSEnji Cooper }
10310f81a9bSEnji Cooper 
ATF_TP_ADD_TCS(tp)10410f81a9bSEnji Cooper ATF_TP_ADD_TCS(tp)
10510f81a9bSEnji Cooper {
10610f81a9bSEnji Cooper 
10710f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open2_negative_test_invalid_corefile);
10810f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open2_negative_test_invalid_execfile);
10910f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open2_negative_test_nonexistent_corefile);
11010f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open2_negative_test_nonexistent_execfile);
11110f81a9bSEnji Cooper 
11210f81a9bSEnji Cooper 	return (atf_no_error());
11310f81a9bSEnji Cooper }
114