file_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 #include <config.h>
00018 
00019 #include <fcntl.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <unistd.h>
00023 
00024 #include <atf-c.h>
00025 
00026 #include <isc/file.h>
00027 #include <isc/result.h>
00028 
00029 ATF_TC(isc_file_sanitize);
00030 ATF_TC_HEAD(isc_file_sanitize, tc) {
00031         atf_tc_set_md_var(tc, "descr", "sanitized filenames");
00032 }
00033 
00034 #define NAME "internal"
00035 #define SHA "3bed2cb3a3acf7b6a8ef408420cc682d5520e26976d354254f528c965612054f"
00036 #define TRUNC_SHA "3bed2cb3a3acf7b6"
00037 
00038 #define BAD1 "in/internal"
00039 #define BADHASH1 "8bbb97a888791399"
00040 
00041 #define BAD2 "Internal"
00042 #define BADHASH2 "2ea1842b445b0c81"
00043 
00044 #define F(x) "testdata/file/" x ".test"
00045 
00046 static void
00047 touch(const char *filename) {
00048         int fd;
00049 
00050         unlink(filename);
00051         fd = creat(filename, 0644);
00052         if (fd != -1)
00053                 close(fd);
00054 }
00055 
00056 ATF_TC_BODY(isc_file_sanitize, tc) {
00057         isc_result_t result;
00058         char buf[1024];
00059 
00060         ATF_CHECK(chdir(TESTS) != -1);
00061 
00062         result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
00063         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00064         ATF_CHECK(strcmp(buf, F(NAME)) == 0);
00065 
00066         touch(F(TRUNC_SHA));
00067         result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
00068         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00069         ATF_CHECK(strcmp(buf, F(TRUNC_SHA)) == 0);
00070 
00071         touch(F(SHA));
00072         result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
00073         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00074         ATF_CHECK(strcmp(buf, F(SHA)) == 0);
00075 
00076         result = isc_file_sanitize("testdata/file", BAD1, "test", buf, 1024);
00077         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00078         ATF_CHECK(strcmp(buf, F(BADHASH1)) == 0);
00079 
00080         result = isc_file_sanitize("testdata/file", BAD2, "test", buf, 1024);
00081         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00082         ATF_CHECK(strcmp(buf, F(BADHASH2)) == 0);
00083 
00084         unlink(F(TRUNC_SHA));
00085         unlink(F(SHA));
00086 }
00087 
00088 /*
00089  * Main
00090  */
00091 ATF_TP_ADD_TCS(tp) {
00092         ATF_TP_ADD_TC(tp, isc_file_sanitize);
00093         return (atf_no_error());
00094 }
00095 

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha