PageRenderTime 238ms CodeModel.GetById 81ms app.highlight 10ms RepoModel.GetById 73ms app.codeStats 28ms

/mordor/tests/efs_stream.cpp

http://github.com/mozy/mordor
C++ | 106 lines | 88 code | 12 blank | 6 comment | 5 complexity | 1906f2c4803664db449501f2f97a4c5b MD5 | raw file
  1// Copyright (c) 2009 - Mozy, Inc.
  2
  3#ifdef WINDOWS
  4
  5#include "mordor/fiber.h"
  6#include "mordor/streams/file.h"
  7#include "mordor/streams/efs.h"
  8#include "mordor/streams/transfer.h"
  9#include "mordor/streams/random.h"
 10#include "mordor/streams/memory.h"
 11#include "mordor/test/test.h"
 12
 13using namespace Mordor;
 14using namespace Mordor::Test;
 15
 16MORDOR_UNITTEST(EFSStream, basic)
 17{
 18    {
 19        FileStream file("dummy.efs", FileStream::WRITE, FileStream::OVERWRITE_OR_CREATE);
 20        file.write("cody", 4);
 21    }
 22    if (!EncryptFileW(L"dummy.efs")) {
 23        DWORD error = GetLastError();
 24        MORDOR_LOG_INFO(Mordor::Log::root()) << "EncryptFileW failed with code " << error;
 25        DeleteFileW(L"dummy.efs");
 26        throw TestSkippedException();
 27    }
 28    Buffer b, b2;
 29    {
 30        EFSStream efs("dummy.efs", true);
 31        efs.read(b, 65536);
 32        MORDOR_TEST_ASSERT_EQUAL(efs.read(b, 65536), 0u);
 33    }
 34    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b.readAvailable(), 4u);
 35    {
 36        EFSStream efs("dummy2.efs", false);
 37        efs.write(b, b.readAvailable());
 38        efs.close();
 39    }
 40    {
 41        FileStream file("dummy2.efs");
 42        Stream &fileStream = file;
 43        MORDOR_TEST_ASSERT_EQUAL(fileStream.read(b2, 65536), 4u);
 44        MORDOR_TEST_ASSERT(b2 == "cody");
 45    }
 46    DeleteFileW(L"dummy.efs");
 47    DeleteFileW(L"dummy2.efs");
 48}
 49
 50MORDOR_UNITTEST(EFSStream, seek)
 51{
 52    {
 53        // create an EFS'd file with random content
 54        {
 55            RandomStream r;
 56            FileStream out("dummy3.efs", FileStream::WRITE, FileStream::OVERWRITE_OR_CREATE);
 57            transferStream(r, out, 100000);
 58        }
 59        if (!EncryptFileW(L"dummy3.efs")) {
 60            DWORD error = GetLastError();
 61            MORDOR_LOG_INFO(Mordor::Log::root()) << "EncryptFileW failed with code " << error;
 62            DeleteFileW(L"dummy3.efs");
 63            throw TestSkippedException();
 64        }
 65
 66        // now open for reading
 67        EFSStream efsr("dummy3.efs", true);
 68
 69        // read the file serially to a MemoryStream
 70        MemoryStream control;
 71        transferStream(efsr, control);
 72
 73        // now seek back to various offsets and ensure the data read is correct
 74        long long seek_offsets[] = {
 75            0,       // eof to bof
 76            50000,   // mid-file forward
 77            12345,   // mid-file backward
 78            0,       // mid-file to bof
 79            99999,   // forward, truncated read
 80            100000,  // eof
 81        };
 82        size_t num_tests = sizeof(seek_offsets) / sizeof(seek_offsets[0]);
 83        for(size_t i = 0; i < num_tests; ++i)
 84        {
 85            Buffer control_buf;
 86            control.seek(seek_offsets[i]);
 87            control.read(control_buf, 1000);
 88
 89            Buffer test_buf;
 90            efsr.seek(seek_offsets[i]);
 91            // EFSStream is known to return partial reads; that is by design
 92            size_t to_read = 1000;
 93            do {
 94                to_read -= efsr.read(test_buf, to_read);
 95            } while(to_read > 0);
 96
 97            MORDOR_TEST_ASSERT_EQUAL(control.tell(), efsr.tell());
 98            MORDOR_TEST_ASSERT_EQUAL(control_buf.readAvailable(), test_buf.readAvailable());
 99            MORDOR_TEST_ASSERT(control_buf == test_buf);
100        }
101    }
102
103    DeleteFileW(L"dummy3.efs");
104}
105
106#endif