GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/files.cc Lines: 124 124 100.0 %
Date: 2017-11-29 Branches: 339 790 42.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "unittests/unittests.h"
22
23
24
#include "fs/files.h"
25
26
#include "fs/virtfs/fs.h"
27
#include "fs/virtfs/tools.h"
28
29
#include "utils/stringutils.h"
30
31
#include "resources/resourcemanager/resourcemanager.h"
32
33
#include "debug.h"
34
35
6
TEST_CASE("Files renameFile", "")
36
{
37

8
    VirtFs::mountDirSilent("data", Append_false);
38

8
    VirtFs::mountDirSilent("../data", Append_false);
39
40
2
    const int sz = 1234567;
41
2
    char *buf = new char[sz];
42
4938270
    for (int f = 0; f < sz; f ++)
43
2469134
        buf[f] = f;
44
45
8
    const std::string name1 = "file1.test";
46
8
    const std::string name2 = "file2.test";
47
2
    FILE *file = fopen(name1.c_str(), "w+b");
48
2
    fwrite(buf, 1, sz, file);
49
2
    fclose(file);
50
51




8
    REQUIRE(0 == Files::renameFile(name1, name2));
52
2
    char *buf2 = new char[sz];
53
2
    FILE *file2 = fopen(name2.c_str(), "rb");
54



8
    REQUIRE_FALSE(nullptr == file2);
55
4
    fread(buf2, 1, sz, file2);
56
2
    fclose(file2);
57
2
    ::remove(name1.c_str());
58
2
    ::remove(name2.c_str());
59
60
2469136
    for (int f = 0; f < sz; f ++)
61



9876536
        REQUIRE(buf[f] == buf2[f]);
62
63
2
    delete [] buf;
64
2
    delete [] buf2;
65
2
    ResourceManager::deleteInstance();
66

8
    VirtFs::unmountDirSilent("data");
67

8
    VirtFs::unmountDirSilent("../data");
68
2
}
69
70
6
TEST_CASE("Files existsLocal", "")
71
{
72

8
    VirtFs::mountDirSilent("data", Append_false);
73

8
    VirtFs::mountDirSilent("../data", Append_false);
74




16
    REQUIRE(Files::existsLocal(VirtFs::getPath("help/about.txt")) == true);
75




14
    REQUIRE_FALSE(Files::existsLocal(VirtFs::getPath("help/about1.txt")));
76




14
    REQUIRE_FALSE(Files::existsLocal(VirtFs::getPath("help1/about.txt")));
77
2
    ResourceManager::deleteInstance();
78

8
    VirtFs::unmountDirSilent("data");
79

8
    VirtFs::unmountDirSilent("../data");
80
2
}
81
82
6
TEST_CASE("Files loadTextFileString", "")
83
{
84

8
    VirtFs::mountDirSilent("data", Append_false);
85

8
    VirtFs::mountDirSilent("../data", Append_false);
86




16
    REQUIRE(VirtFs::loadTextFileString("test/simplefile.txt") ==
87
        "this is test \nfile.");
88
2
    ResourceManager::deleteInstance();
89

8
    VirtFs::unmountDirSilent("data");
90

8
    VirtFs::unmountDirSilent("../data");
91
2
}
92
93
6
TEST_CASE("Files loadTextFile", "")
94
{
95

8
    VirtFs::mountDirSilent("data", Append_false);
96

8
    VirtFs::mountDirSilent("../data", Append_false);
97
98
4
    StringVect lines;
99

8
    VirtFs::loadTextFile("test/simplefile.txt", lines);
100



10
    REQUIRE(lines.size() == 2);
101



8
    REQUIRE(lines[0] == "this is test ");
102



10
    REQUIRE(lines[1] == "file.");
103
2
    ResourceManager::deleteInstance();
104

8
    VirtFs::unmountDirSilent("data");
105

8
    VirtFs::unmountDirSilent("../data");
106
2
}
107
108
6
TEST_CASE("Files saveTextFile", "")
109
{
110

8
    VirtFs::mountDirSilent("data", Append_false);
111

8
    VirtFs::mountDirSilent("../data", Append_false);
112
113

10
    const std::string dir = VirtFs::getPath("test");
114



8
    REQUIRE(!dir.empty());
115

14
    Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2");
116

10
    std::string data = VirtFs::loadTextFileString("test/tempfile.txt");
117
6
    ::remove((dir + "/tempfile.txt").c_str());
118
#ifdef WIN32
119
    REQUIRE(data == "test line\r\ntext line2\r\n");
120
#else  // WIN32
121
122



8
    REQUIRE(data == "test line\ntext line2\n");
123
#endif  // WIN32
124
125
2
    ResourceManager::deleteInstance();
126

8
    VirtFs::unmountDirSilent("data");
127

8
    VirtFs::unmountDirSilent("../data");
128
2
}
129
130
8
TEST_CASE("Files copyFile1", "")
131
{
132

16
    VirtFs::mountDirSilent("data", Append_false);
133

16
    VirtFs::mountDirSilent("../data", Append_false);
134
135

20
    const std::string dir = VirtFs::getPath("test");
136



16
    REQUIRE(!dir.empty());
137



28
    SECTION("copy")
138
    {
139





28
        REQUIRE(Files::copyFile(pathJoin(dir, "test.txt"),
140
            pathJoin(dir, "tempfile.txt")) == 0);
141

10
        std::string data = VirtFs::loadTextFileString("test/tempfile.txt");
142
6
        ::remove((dir + "/tempfile.txt").c_str());
143



8
        REQUIRE(data == "test line 1\ntest line 2");
144
    }
145
146



28
    SECTION("errors")
147
    {
148






32
        REQUIRE(Files::copyFile(pathJoin(dir, "test_not_exists.txt"),
149
            pathJoin(dir, "tempfile.txt")) == -1);
150





26
        REQUIRE(Files::copyFile(pathJoin(dir, "test.txt"),
151
            "/nonexist/root/dir123") == -1);
152
    }
153
154
4
    ResourceManager::deleteInstance();
155

16
    VirtFs::unmountDirSilent("data");
156

16
    VirtFs::unmountDirSilent("../data");
157
4
}
158
159
6
TEST_CASE("Files loadTextFileLocal", "")
160
{
161

8
    VirtFs::mountDirSilent("data", Append_false);
162

8
    VirtFs::mountDirSilent("../data", Append_false);
163
164

10
    const std::string dir = VirtFs::getPath("test");
165



8
    REQUIRE(!dir.empty());
166

14
    Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2");
167
2
    StringVect lines;
168





18
    REQUIRE(Files::loadTextFileLocal(pathJoin(dir, "tempfile.txt"),
169
        lines));
170
6
    ::remove((dir + "/tempfile.txt").c_str());
171



10
    REQUIRE(lines.size() == 2);
172



8
    REQUIRE(lines[0] == "test line");
173



10
    REQUIRE(lines[1] == "text line2");
174
175
2
    ResourceManager::deleteInstance();
176

8
    VirtFs::unmountDirSilent("data");
177

8
    VirtFs::unmountDirSilent("../data");
178
2
}
179
180
6
TEST_CASE("Files getFilesInDir", "")
181
{
182

8
    VirtFs::mountDirSilent("data", Append_false);
183

8
    VirtFs::mountDirSilent("../data", Append_false);
184
185
4
    StringVect list;
186

14
    VirtFs::getFilesInDir("test",
187
        list,
188
        ".gpl");
189



10
    REQUIRE(list.size() == 1);
190





22
    REQUIRE(list[0] == pathJoin("test", "palette.gpl"));
191
192
2
    list.clear();
193

14
    VirtFs::getFilesInDir("perserver/default",
194
        list,
195
        ".xml");
196



10
    REQUIRE(list.size() == 6);
197





28
    REQUIRE(list[0] == pathJoin("perserver", "default", "charcreation.xml"));
198





30
    REQUIRE(list[1] == pathJoin("perserver", "default", "deadmessages.xml"));
199





30
    REQUIRE(list[2] ==
200
        pathJoin("perserver", "default", "defaultcommands.xml"));
201





30
    REQUIRE(list[3] == pathJoin("perserver", "default", "features.xml"));
202





30
    REQUIRE(list[4] == pathJoin("perserver", "default", "groups.xml"));
203





30
    REQUIRE(list[5] == pathJoin("perserver", "default", "weapons.xml"));
204
2
    ResourceManager::deleteInstance();
205

8
    VirtFs::unmountDirSilent("data");
206

8
    VirtFs::unmountDirSilent("../data");
207

8
}