Blame IlmImfFuzzTest/fuzzFile.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2007, Industrial Light & Magic, a division of Lucas
Packit 0d464f
// Digital Ltd. LLC
Packit 0d464f
// 
Packit 0d464f
// All rights reserved.
Packit 0d464f
// 
Packit 0d464f
// Redistribution and use in source and binary forms, with or without
Packit 0d464f
// modification, are permitted provided that the following conditions are
Packit 0d464f
// met:
Packit 0d464f
// *       Redistributions of source code must retain the above copyright
Packit 0d464f
// notice, this list of conditions and the following disclaimer.
Packit 0d464f
// *       Redistributions in binary form must reproduce the above
Packit 0d464f
// copyright notice, this list of conditions and the following disclaimer
Packit 0d464f
// in the documentation and/or other materials provided with the
Packit 0d464f
// distribution.
Packit 0d464f
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 0d464f
// its contributors may be used to endorse or promote products derived
Packit 0d464f
// from this software without specific prior written permission. 
Packit 0d464f
// 
Packit 0d464f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 0d464f
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 0d464f
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 0d464f
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 0d464f
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 0d464f
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 0d464f
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 0d464f
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 0d464f
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 0d464f
//
Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
Packit 0d464f
Packit 0d464f
#include <fuzzFile.h>
Packit 0d464f
Packit 0d464f
#include <ImfRgbaFile.h>
Packit 0d464f
#include <ImfArray.h>
Packit 0d464f
#include <Iex.h>
Packit 0d464f
#include <half.h>
Packit 0d464f
Packit 0d464f
#include <iostream>
Packit 0d464f
#include <fstream>
Packit 0d464f
Packit 0d464f
// Handle the case when the custom namespace is not exposed
Packit 0d464f
#include <OpenEXRConfig.h>
Packit 0d464f
using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
Packit 0d464f
using namespace std;
Packit 0d464f
using namespace IMATH_NAMESPACE;
Packit 0d464f
Packit 0d464f
Packit 0d464f
namespace {
Packit 0d464f
Packit 0d464f
Int64
Packit 0d464f
lengthOfFile (const char fileName[])
Packit 0d464f
{
Packit 0d464f
    ifstream ifs (fileName, ios_base::binary);
Packit 0d464f
Packit 0d464f
    if (!ifs)
Packit 0d464f
	return 0;
Packit 0d464f
Packit 0d464f
    ifs.seekg (0, ios_base::end);
Packit 0d464f
    return ifs.tellg();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fuzzFile (const char goodFile[],
Packit 0d464f
          const char brokenFile[],
Packit 0d464f
	  Int64 offset,
Packit 0d464f
	  Int64 windowSize,
Packit 0d464f
	  Rand48 &random,
Packit 0d464f
	  double fuzzAmount)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Read the input file.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    ifstream ifs (goodFile, ios_base::binary);
Packit 0d464f
Packit 0d464f
    if (!ifs)
Packit 0d464f
	THROW_ERRNO ("Cannot open file " << goodFile << " (%T).");
Packit 0d464f
Packit 0d464f
    ifs.seekg (0, ios_base::end);
Packit 0d464f
    Int64 fileLength = ifs.tellg();
Packit 0d464f
    ifs.seekg (0, ios_base::beg);
Packit 0d464f
Packit 0d464f
    Array<char> data (fileLength);
Packit 0d464f
    ifs.read (data, fileLength);
Packit 0d464f
Packit 0d464f
    if (!ifs)
Packit 0d464f
	THROW_ERRNO ("Cannot read file " << goodFile << " (%T)." << endl);
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Damage the contents of the file by overwriting some of the bytes
Packit 0d464f
    // in a window of size windowSize, starting at the specified offset.
Packit 0d464f
    // 
Packit 0d464f
Packit 0d464f
    for (Int64 i = offset; i < offset + windowSize; ++i)
Packit 0d464f
    {
Packit 0d464f
	if (random.nextf() < fuzzAmount)
Packit 0d464f
	    data[i] = char (random.nexti());
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Save the damaged file contents in the output file.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    ofstream ofs (brokenFile, ios_base::binary);
Packit 0d464f
Packit 0d464f
    if (!ofs)
Packit 0d464f
	THROW_ERRNO ("Cannot open file " << brokenFile << " (%T)." << endl);
Packit 0d464f
Packit 0d464f
    ofs.write (data, fileLength);
Packit 0d464f
Packit 0d464f
    if (!ofs)
Packit 0d464f
	THROW_ERRNO ("Cannot write file " << brokenFile << " (%T)." << endl);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
} // namespace
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fuzzFile (const char goodFile[],
Packit 0d464f
          const char brokenFile[],
Packit 0d464f
	  void (*readFile) (const char[]),
Packit 0d464f
	  int nSlidingWindow,
Packit 0d464f
	  int nFixedWindow,
Packit 0d464f
	  Rand48 &random)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // We want to test how resilient the IlmImf library is with respect
Packit 0d464f
    // to malformed OpenEXR input files.  In order to do this we damage
Packit 0d464f
    // a good input file by overwriting parts of it with random data.
Packit 0d464f
    // We then call function readFile() to try and read the damaged file.
Packit 0d464f
    // Provided the IlmImf library works as advertised, a try/catch(...)
Packit 0d464f
    // block in readFile() should be able to handle all errors that could
Packit 0d464f
    // possibly result from reading a broken OpenEXR file.  We repeat
Packit 0d464f
    // this damage/read cycle many times, overwriting different parts
Packit 0d464f
    // of the file:
Packit 0d464f
    //
Packit 0d464f
    // First we slide a window along the file.  The size of the window
Packit 0d464f
    // is fileSize*2/nSlidingWindow bytes.  In each damage/read cycle
Packit 0d464f
    // we overwrite up to 10% of the bytes the window, try to read the
Packit 0d464f
    // file, and advance the window by fileSize/nSlidingWindow bytes.
Packit 0d464f
    //
Packit 0d464f
    // Next we overwrite up to 10% of the file's first 2048 bytes and
Packit 0d464f
    // try to read the file.  We repeat this nFixedWindow times.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    {
Packit 0d464f
	Int64 fileSize = lengthOfFile (goodFile);
Packit 0d464f
	Int64 windowSize = fileSize * 2 / nSlidingWindow;
Packit 0d464f
	Int64 lastWindowOffset = fileSize - windowSize;
Packit 0d464f
Packit 0d464f
	cout << "sliding " << windowSize << "-byte window" << endl;
Packit 0d464f
Packit 0d464f
	for (int i = 0; i < nSlidingWindow; ++i)
Packit 0d464f
	{
Packit 0d464f
	    if (i % 100 == 0)
Packit 0d464f
		cout << i << "\r" << flush;
Packit 0d464f
Packit 0d464f
	    Int64 offset = lastWindowOffset * i / (nSlidingWindow - 1);
Packit 0d464f
	    double fuzzAmount = random.nextf (0.0, 0.1);
Packit 0d464f
Packit 0d464f
	    fuzzFile (goodFile, brokenFile,
Packit 0d464f
		      offset, windowSize,
Packit 0d464f
		      random, fuzzAmount);
Packit 0d464f
Packit 0d464f
	    readFile (brokenFile);
Packit 0d464f
	}
Packit 0d464f
Packit 0d464f
	cout << nSlidingWindow << endl;
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    {
Packit 0d464f
	Int64 windowSize = 2048;
Packit 0d464f
Packit 0d464f
	cout << windowSize << "-byte window at start of file" << endl;
Packit 0d464f
Packit 0d464f
	for (int i = 0; i < nFixedWindow; ++i)
Packit 0d464f
	{
Packit 0d464f
	    if (i % 100 == 0)
Packit 0d464f
		cout << i << "\r" << flush;
Packit 0d464f
Packit 0d464f
	    double fuzzAmount = random.nextf (0.0, 0.1);
Packit 0d464f
Packit 0d464f
	    fuzzFile (goodFile, brokenFile,
Packit 0d464f
		      0, windowSize,
Packit 0d464f
		      random, fuzzAmount);
Packit 0d464f
Packit 0d464f
	    readFile (brokenFile);
Packit 0d464f
	}
Packit 0d464f
Packit 0d464f
	cout << nFixedWindow << endl;
Packit 0d464f
    }
Packit 0d464f
}