PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/mordor/tests/file_stream.cpp

http://github.com/mozy/mordor
C++ | 136 lines | 124 code | 10 blank | 2 comment | 9 complexity | 9f550dbb997054eb929654b2e2fd6f4f MD5 | raw file
  1// Copyright (c) 2010 - Mozy, Inc.
  2
  3#include "mordor/pch.h"
  4
  5#include "mordor/streams/file.h"
  6#include "mordor/test/test.h"
  7
  8using namespace Mordor;
  9
 10#if 0 // WINDOWS
 11MORDOR_UNITTEST(FileStream, openSymlinkToDirectory)
 12{
 13    if (!CreateDirectoryW(L"symlinkdir", NULL))
 14        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("CreateDirectoryW");
 15    if (!CreateSymbolicLinkW(L"symlink", L"symlinkdir", SYMBOLIC_LINK_FLAG_DIRECTORY)) {
 16        RemoveDirectoryW(L"symlinkdir");
 17        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("CreateSymbolicLinkW");
 18    }
 19    try {
 20        FileStream fstream(L"symlinktodir", FileStream::READ);
 21        Stream &stream(fstream);
 22        char buffer[6];
 23        stream.read(buffer, 6);
 24    } catch (...) {
 25        RemoveDirectoryW(L"symlinkdir");
 26        DeleteFileW(L"symlink");
 27        throw;
 28    }
 29    RemoveDirectoryW(L"symlinkdir");
 30    DeleteFileW(L"symlink");
 31}
 32
 33MORDOR_UNITTEST(FileStream, openSymlinkToSelf)
 34{
 35    if (!CreateSymbolicLinkW(L"symlink", L"symlink", 0))
 36        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("CreateSymbolicLinkW");
 37    try {
 38        MORDOR_TEST_ASSERT_EXCEPTION(FileStream stream(L"symlinktoself",
 39            FileStream::READ), UnresolvablePathException);
 40    } catch (...) {
 41        DeleteFileW(L"symlink");
 42        throw;
 43    }
 44    DeleteFileW(L"symlink");
 45}
 46
 47MORDOR_UNITTEST(FileStream, openSymlinkToNothing)
 48{
 49    if (!CreateSymbolicLinkW(L"symlinkt", L"nothing", 0))
 50        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("CreateSymbolicLinkW");
 51    try {
 52        MORDOR_TEST_ASSERT_EXCEPTION(FileStream stream(L"symlinktonothing",
 53            FileStream::READ), FileNotFoundException);
 54    } catch (...) {
 55        DeleteFileW(L"symlink");
 56        throw;
 57    }
 58    DeleteFileW(L"symlink");
 59}
 60#else
 61
 62static std::string tempfilename()
 63{
 64    // Silly glibc, *forcing* me to not use mktemp, tempnam, or tmpnam
 65    std::string result("/tmp/mordorXXXXXX");
 66    int fd = mkstemp(&result[0]);
 67    if (fd < 0)
 68        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("mkstemp");
 69    unlink(result.c_str());
 70    close(fd);
 71    return result;
 72}
 73
 74MORDOR_UNITTEST(FileStream, openSymlinkToDirectory)
 75{
 76    std::string dir("/tmp/mordorXXXXXX");
 77    if (!mkdtemp(&dir[0]))
 78        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("mkdtemp");
 79    std::string sym;
 80    try {
 81        sym = tempfilename();
 82    } catch(...) {
 83        rmdir(dir.c_str());
 84        throw;
 85    }
 86    if (symlink(dir.c_str(), sym.c_str())) {
 87        rmdir(dir.c_str());
 88        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("symlink");
 89    }
 90    try {
 91        FileStream fstream(sym, FileStream::READ);
 92        Stream &stream(fstream);
 93        char buffer[6];
 94        stream.read(buffer, 6);
 95        MORDOR_NOTREACHED();
 96    } catch (IsDirectoryException &) {
 97    } catch (...) {
 98        rmdir(dir.c_str());
 99        unlink(sym.c_str());
100        throw;
101    }
102    rmdir(dir.c_str());
103    unlink(sym.c_str());
104}
105
106MORDOR_UNITTEST(FileStream, openSymlinkToSelf)
107{
108    std::string sym = tempfilename();
109    if (symlink(sym.c_str(), sym.c_str()))
110        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("symlink");
111    try {
112        MORDOR_TEST_ASSERT_EXCEPTION(FileStream stream(sym, FileStream::READ),
113            TooManySymbolicLinksException);
114    } catch (...) {
115        unlink(sym.c_str());
116        throw;
117    }
118    unlink(sym.c_str());
119}
120
121MORDOR_UNITTEST(FileStream, openSymlinkToNothing)
122{
123    std::string sym = tempfilename();
124    std::string nothing = tempfilename();
125    if (symlink(nothing.c_str(), sym.c_str()))
126        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("symlink");
127    try {
128        MORDOR_TEST_ASSERT_EXCEPTION(FileStream stream(sym, FileStream::READ),
129            FileNotFoundException);
130    } catch (...) {
131        unlink(sym.c_str());
132        throw;
133    }
134    unlink(sym.c_str());
135}
136#endif