ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt */
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#include <config.h>
8f0cf84bb1ff9a86af69cfe13c2b2c952890c3cbMark Andrews
8f0cf84bb1ff9a86af69cfe13c2b2c952890c3cbMark Andrews#include <fcntl.h>
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#include <stdlib.h>
8f0cf84bb1ff9a86af69cfe13c2b2c952890c3cbMark Andrews#include <string.h>
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#include <unistd.h>
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#include <atf-c.h>
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#include <isc/file.h>
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#include <isc/result.h>
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan HuntATF_TC(isc_file_sanitize);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan HuntATF_TC_HEAD(isc_file_sanitize, tc) {
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt atf_tc_set_md_var(tc, "descr", "sanitized filenames");
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt}
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define NAME "internal"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define SHA "3bed2cb3a3acf7b6a8ef408420cc682d5520e26976d354254f528c965612054f"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define TRUNC_SHA "3bed2cb3a3acf7b6"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define BAD1 "in/internal"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define BADHASH1 "8bbb97a888791399"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define BAD2 "Internal"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define BADHASH2 "2ea1842b445b0c81"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt#define F(x) "testdata/file/" x ".test"
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
c325ff9c799403a1ef13c6e11ad1299011401999Evan Huntstatic void
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunttouch(const char *filename) {
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt int fd;
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt unlink(filename);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt fd = creat(filename, 0644);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt if (fd != -1)
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt close(fd);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt}
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan HuntATF_TC_BODY(isc_file_sanitize, tc) {
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt isc_result_t result;
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt char buf[1024];
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
8f0cf84bb1ff9a86af69cfe13c2b2c952890c3cbMark Andrews ATF_CHECK(chdir(TESTS) != -1);
8f0cf84bb1ff9a86af69cfe13c2b2c952890c3cbMark Andrews
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_CHECK(strcmp(buf, F(NAME)) == 0);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt touch(F(TRUNC_SHA));
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt ATF_CHECK_EQ(result, ISC_R_SUCCESS);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt ATF_CHECK(strcmp(buf, F(TRUNC_SHA)) == 0);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt touch(F(SHA));
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt ATF_CHECK_EQ(result, ISC_R_SUCCESS);
c325ff9c799403a1ef13c6e11ad1299011401999Evan Hunt ATF_CHECK(strcmp(buf, F(SHA)) == 0);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt result = isc_file_sanitize("testdata/file", BAD1, "test", buf, 1024);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_CHECK(strcmp(buf, F(BADHASH1)) == 0);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt result = isc_file_sanitize("testdata/file", BAD2, "test", buf, 1024);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_CHECK(strcmp(buf, F(BADHASH2)) == 0);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt unlink(F(TRUNC_SHA));
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt unlink(F(SHA));
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt}
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt/*
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt * Main
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt */
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan HuntATF_TP_ADD_TCS(tp) {
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt ATF_TP_ADD_TC(tp, isc_file_sanitize);
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt return (atf_no_error());
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt}
ce96d4326c872c8165b5e3a81ac5b49950c782c6Evan Hunt