Home | History | Annotate | Download | only in Support
      1 //===- llvm/unittest/Support/FileOutputBuffer.cpp - unit tests ------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/Support/FileOutputBuffer.h"
     11 #include "llvm/Support/ErrorHandling.h"
     12 #include "llvm/Support/FileSystem.h"
     13 #include "llvm/Support/Path.h"
     14 #include "llvm/Support/raw_ostream.h"
     15 #include "gtest/gtest.h"
     16 
     17 using namespace llvm;
     18 using namespace llvm::sys;
     19 
     20 #define ASSERT_NO_ERROR(x)                                                     \
     21   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
     22     errs() << #x ": did not return errc::success.\n"                           \
     23            << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"           \
     24            << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";       \
     25   } else {                                                                     \
     26   }
     27 
     28 namespace {
     29 TEST(FileOutputBuffer, Test) {
     30   // Create unique temporary directory for these tests
     31   SmallString<128> TestDirectory;
     32   {
     33     ASSERT_NO_ERROR(
     34         fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
     35   }
     36 
     37   // TEST 1: Verify commit case.
     38   SmallString<128> File1(TestDirectory);
     39 	File1.append("/file1");
     40   {
     41     std::unique_ptr<FileOutputBuffer> Buffer;
     42     ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
     43     // Start buffer with special header.
     44     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     45     // Write to end of buffer to verify it is writable.
     46     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
     47     // Commit buffer.
     48     ASSERT_NO_ERROR(Buffer->commit());
     49   }
     50 
     51   // Verify file is correct size.
     52   uint64_t File1Size;
     53   ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
     54   ASSERT_EQ(File1Size, 8192ULL);
     55   ASSERT_NO_ERROR(fs::remove(File1.str()));
     56 
     57  	// TEST 2: Verify abort case.
     58   SmallString<128> File2(TestDirectory);
     59 	File2.append("/file2");
     60   {
     61     std::unique_ptr<FileOutputBuffer> Buffer2;
     62     ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
     63     // Fill buffer with special header.
     64     memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     65     // Do *not* commit buffer.
     66   }
     67   // Verify file does not exist (because buffer not committed).
     68   bool Exists = false;
     69   ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
     70   EXPECT_FALSE(Exists);
     71   ASSERT_NO_ERROR(fs::remove(File2.str()));
     72 
     73   // TEST 3: Verify sizing down case.
     74   SmallString<128> File3(TestDirectory);
     75 	File3.append("/file3");
     76   {
     77     std::unique_ptr<FileOutputBuffer> Buffer;
     78     ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
     79     // Start buffer with special header.
     80     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     81     // Write to end of buffer to verify it is writable.
     82     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
     83     // Commit buffer, but size down to smaller size
     84     ASSERT_NO_ERROR(Buffer->commit(5000));
     85   }
     86 
     87   // Verify file is correct size.
     88   uint64_t File3Size;
     89   ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
     90   ASSERT_EQ(File3Size, 5000ULL);
     91   ASSERT_NO_ERROR(fs::remove(File3.str()));
     92 
     93   // TEST 4: Verify file can be made executable.
     94   SmallString<128> File4(TestDirectory);
     95 	File4.append("/file4");
     96   {
     97     std::unique_ptr<FileOutputBuffer> Buffer;
     98     ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
     99                                               FileOutputBuffer::F_executable));
    100     // Start buffer with special header.
    101     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
    102     // Commit buffer.
    103     ASSERT_NO_ERROR(Buffer->commit());
    104   }
    105   // Verify file exists and is executable.
    106   fs::file_status Status;
    107   ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
    108   bool IsExecutable = (Status.permissions() & fs::owner_exe);
    109   EXPECT_TRUE(IsExecutable);
    110   ASSERT_NO_ERROR(fs::remove(File4.str()));
    111 
    112   // Clean up.
    113   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
    114 }
    115 } // anonymous namespace
    116