a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/* $Id$ */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/** @file
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * IPRT Testcase - Manifest files.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/*
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2010-2012 Oracle Corporation
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync *
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * available from http://www.virtualbox.org. This file is free software;
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * General Public License (GPL) as published by the Free Software
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync *
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * The contents of this file may alternatively be used under the terms
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * of the Common Development and Distribution License Version 1.0
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * VirtualBox OSE distribution, in which case the provisions of the
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * CDDL are applicable instead of those of the GPL.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync *
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * You may elect to license modified versions of this file under the
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * terms and conditions of either the GPL or the CDDL or both.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/*******************************************************************************
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync* Header Files *
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync*******************************************************************************/
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync#include <iprt/manifest.h>
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync#include <iprt/string.h>
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync#include <iprt/err.h>
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync#include <iprt/mem.h>
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync#include <iprt/test.h>
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/**
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * Basic API checks.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsyncstatic void tst1(void)
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync{
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTestISub("Manifest creation");
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync size_t cbSize = 0;
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync size_t iFailed = 0;
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /*
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * test1.txt = "This is a test text."
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * test2.txt = "Another test text."
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync static RTMANIFESTTEST /*const*/ s_aFiles[] = /** @todo API misdesign, this should be const. */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync {
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync { "test1.txt", "794a8cc644b318ae6461aeea62915e399e441e8" },
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync { "test2.txt", "f17393902ee94c1e8bbd4bf417cdc70051feca00" }
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync };
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync static const char s_szTestPattern[] =
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync "SHA1 (test1.txt)= 794a8cc644b318ae6461aeea62915e399e441e8\n"
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync "SHA1 (test2.txt)= f17393902ee94c1e8bbd4bf417cdc70051feca00\n"
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync ;
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync void *pvBuf = NULL;
454ac5c6ef4960887035ceea6b5247789d3272davboxsync RTTESTI_CHECK_RC_RETV(RTManifestWriteFilesBuf(&pvBuf, &cbSize, RTDIGESTTYPE_SHA1, s_aFiles, 2), VINF_SUCCESS);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Check returned memory size */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK_RETV(cbSize == strlen(s_szTestPattern));
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Check for correct manifest file content */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK(memcmp(pvBuf, s_szTestPattern, cbSize) == 0);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTestISub("Manifest verify");
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, s_aFiles, 2, 0), VINF_SUCCESS);
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* To little files to check */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, s_aFiles, 1, 0), VERR_MANIFEST_FILE_MISMATCH);
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Make the digest type invalid */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[0] = 'L';
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, s_aFiles, 2, 0), VERR_MANIFEST_UNSUPPORTED_DIGEST_TYPE);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[0] = 'S'; /* Restore */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Make the file name invalid */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[8] = 'z';
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, s_aFiles, 2, 0), VERR_MANIFEST_FILE_MISMATCH);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[8] = 's'; /* Restore */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Make the second digest invalid */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[99] = '0';
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, s_aFiles, 2, &iFailed), VERR_MANIFEST_DIGEST_MISMATCH);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK(iFailed == 1);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Cleanup */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync RTMemFree(pvBuf);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync}
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsyncint main()
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync{
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTEST hTest;
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync int rc = RTTestInitAndCreate("tstRTManifest", &hTest);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync if (rc)
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync return rc;
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTestBanner(hTest);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync tst1();
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync return RTTestSummaryAndDestroy(hTest);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync}
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync