File: file_access_tests.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (123 lines) | stat: -rw-r--r-- 3,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <gtest/gtest.h>

#include <megafs.h>

namespace mega
{
namespace testing
{

struct FileAccessTests: ::testing::Test
{
    FileAccessTests():
        Test(),
        mFilesystem(),
        mFilePath(LocalPath::fromAbsolutePath("file")),
        mFileAccess(mFilesystem.newfileaccess(false))
    {}

    // Called before each test in our fixture executes.
    void SetUp() override
    {
        // Make sure we have no state from a prior test run.
        auto unlinked = mFilesystem.unlinklocal(mFilePath);

        // Unlinking was successful or the file didn't exist.
        ASSERT_TRUE(unlinked || !mFilesystem.target_exists);

        // Convenience.
        auto logOnError = FSLogging::logOnError;

        // Make sure our test file is open for IO.
        ASSERT_TRUE(mFileAccess->fopen(mFilePath, true, true, logOnError));
    }

    // How we interface with the local filesystem.
    FSACCESS_CLASS mFilesystem;

    // The path to our test file.
    LocalPath mFilePath;

    // How we read and write our test file.
    FileAccessPtr mFileAccess;
}; // FileAccessTests

TEST_F(FileAccessTests, frawread_fwrite)
{
    // Data for us to write to disk.
    static const std::string expected = "AAAABBBBCCCCDDDD";

    // Write data in reverse order, in groups of four characters.
    for (std::size_t i = 0, j = expected.size(); i != j; i += 4)
    {
        // Compute offset.
        auto offset = static_cast<m_off_t>(j - i - 4);

        // Tracks how much data we wrote to disk.
        auto count = 0ul;

        // Try and write four characters to disk.
        ASSERT_TRUE(mFileAccess->fwrite(expected.data() + offset, 4, offset, &count));

        // Make sure we actually wrote four characters to disk.
        ASSERT_EQ(count, 4ul);
    }

    // Where we'll store the data we've read.
    std::string computed(4, '\0');

    // Read data in order, in groups of four characters.
    for (std::size_t i = 0ul, j = expected.size() - 4; i != j; i += 4)
    {
        // Convenience.
        auto logOnError = FSLogging::logOnError;

        // Convenience.
        auto offset = static_cast<m_off_t>(i);

        // Try and read four characters from disk.
        ASSERT_TRUE(mFileAccess->frawread(computed.data(), 4, offset, true, logOnError));

        // Make sure we've read what we expected.
        ASSERT_FALSE(expected.compare(i, 4, computed));
    }

    // Convenience.
    auto noLogging = FSLogging::noLogging;

    // Make sure frawread(...) fails if it can't read everything.
    ASSERT_FALSE(mFileAccess->frawread(computed.data(), 4, 14, true, noLogging));
}

TEST_F(FileAccessTests, fread)
{
    // Data we want to write to disk.
    static const std::string expected = "ABCD";

    // Convenience.
    auto logOnError = FSLogging::logOnError;

    // Try and write the data to disk.
    ASSERT_TRUE(mFileAccess->fwrite(expected.data(), 4, 0));

    // Where we'll store the data we've read from disk.
    std::string computed;

    // Read without padding.
    ASSERT_TRUE(mFileAccess->fread(&computed, 4, 0, 0, logOnError));

    // Make sure we've read what we expected.
    EXPECT_EQ(computed, expected);

    // Clear the string.
    computed = std::string(6, '!');

    // Read with padding.
    ASSERT_TRUE(mFileAccess->fread(&computed, 2, 6, 2, logOnError));

    // Make sure the string was correctly padded.
    EXPECT_EQ(computed, std::string("CD\0\0\0\0\0\0", 8));
}

} // testing
} // mega