tstRTManifest.cpp revision a5fb10feb5ad0a875c114c6f117b9718784c9650
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/* $Id$ */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/** @file
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * IPRT Testcase - Manifest files.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/*
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * Copyright (C) 2010 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
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync/**
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync * Basic API checks.
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsyncstatic void tst1(void)
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync{
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync void *pvBuf = NULL;
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 */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTMANIFESTTEST paFiles[] = { { "test1.txt", "794a8cc644b318ae6461aeea62915e399e441e8" }, { "test2.txt", "f17393902ee94c1e8bbd4bf417cdc70051feca00" } };
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync const char pcszTestPattern[] = "SHA1 (test1.txt)= 794a8cc644b318ae6461aeea62915e399e441e8\nSHA1 (test2.txt)= f17393902ee94c1e8bbd4bf417cdc70051feca00\n";
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTestISub("Manifest creation");
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RC_RETV(RTManifestWriteFilesBuf(&pvBuf, &cbSize, paFiles, 2), VINF_SUCCESS);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Check returned memory size */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RETV(cbSize == strlen(pcszTestPattern));
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Check for correct manifest file content */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK(memcmp(pvBuf, pcszTestPattern, cbSize) == 0);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTestISub("Manifest verify");
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, paFiles, 2, 0), VINF_SUCCESS);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* To little files to check */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, paFiles, 1, 0), VERR_MANIFEST_FILE_MISMATCH);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Make the digest type invalid */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[0] = 'L';
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, paFiles, 2, 0), VERR_MANIFEST_UNSUPPORTED_DIGEST_TYPE);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[0] = 'S'; /* Restore */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Make the file name invalid */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[8] = 'z';
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, paFiles, 2, 0), VERR_MANIFEST_FILE_MISMATCH);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[8] = 's'; /* Restore */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Make the second digest invalid */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync ((char*)pvBuf)[99] = '0';
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK_RC(RTManifestVerifyFilesBuf(pvBuf, cbSize, paFiles, 2, &iFailed), VERR_MANIFEST_DIGEST_MISMATCH);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync RTTESTI_CHECK(iFailed == 1);
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync /* Cleanup */
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync if (pvBuf)
a5fb10feb5ad0a875c114c6f117b9718784c9650vboxsync 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