GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/files.cc Lines: 126 126 100.0 %
Date: 2021-03-17 Branches: 318 752 42.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
25
#include "fs/files.h"
26
27
#include "fs/virtfs/fs.h"
28
#include "fs/virtfs/tools.h"
29
30
#include "utils/stringutils.h"
31
32
#include "resources/resourcemanager/resourcemanager.h"
33
34
#include "debug.h"
35
36
3
TEST_CASE("Files renameFile", "")
37
{
38
4
    VirtFs::mountDirSilent("data", Append_false);
39
4
    VirtFs::mountDirSilent("../data", Append_false);
40
41
1
    const int sz = 1234567;
42
1
    char *buf = new char[sz];
43
2469135
    for (int f = 0; f < sz; f ++)
44
1234567
        buf[f] = f;
45
46
4
    const std::string name1 = "file1.test";
47
4
    const std::string name2 = "file2.test";
48
1
    FILE *file = fopen(name1.c_str(), "w+b");
49
1
    fwrite(buf, 1, sz, file);
50
1
    fclose(file);
51
52




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



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



4938268
        REQUIRE(buf[f] == buf2[f]);
63
64
1
    delete [] buf;
65
1
    delete [] buf2;
66
1
    ResourceManager::deleteInstance();
67

4
    VirtFs::unmountDirSilent("data");
68

4
    VirtFs::unmountDirSilent("../data");
69
1
}
70
71
3
TEST_CASE("Files existsLocal", "")
72
{
73
4
    VirtFs::mountDirSilent("data", Append_false);
74
4
    VirtFs::mountDirSilent("../data", Append_false);
75




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




7
    REQUIRE_FALSE(Files::existsLocal(VirtFs::getPath("help/about1.txt")));
77




7
    REQUIRE_FALSE(Files::existsLocal(VirtFs::getPath("help1/about.txt")));
78
1
    ResourceManager::deleteInstance();
79
4
    VirtFs::unmountDirSilent("data");
80
4
    VirtFs::unmountDirSilent("../data");
81
1
}
82
83
3
TEST_CASE("Files loadTextFileString", "")
84
{
85
4
    VirtFs::mountDirSilent("data", Append_false);
86
4
    VirtFs::mountDirSilent("../data", Append_false);
87




8
    REQUIRE(VirtFs::loadTextFileString("test/simplefile.txt") ==
88
        "this is test \nfile.");
89
1
    ResourceManager::deleteInstance();
90
4
    VirtFs::unmountDirSilent("data");
91
4
    VirtFs::unmountDirSilent("../data");
92
1
}
93
94
3
TEST_CASE("Files loadTextFile", "")
95
{
96
4
    VirtFs::mountDirSilent("data", Append_false);
97
4
    VirtFs::mountDirSilent("../data", Append_false);
98
99
2
    StringVect lines;
100

4
    VirtFs::loadTextFile("test/simplefile.txt", lines);
101



5
    REQUIRE(lines.size() == 2);
102



4
    REQUIRE(lines[0] == "this is test ");
103



5
    REQUIRE(lines[1] == "file.");
104
1
    ResourceManager::deleteInstance();
105

4
    VirtFs::unmountDirSilent("data");
106

4
    VirtFs::unmountDirSilent("../data");
107
1
}
108
109
3
TEST_CASE("Files saveTextFile", "")
110
{
111
4
    VirtFs::mountDirSilent("data", Append_false);
112
4
    VirtFs::mountDirSilent("../data", Append_false);
113
114
5
    const std::string dir = VirtFs::getPath("test");
115



4
    REQUIRE(!dir.empty());
116

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

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



4
    REQUIRE(data == "test line\ntext line2\n");
124
#endif  // WIN32
125
126
1
    ResourceManager::deleteInstance();
127

4
    VirtFs::unmountDirSilent("data");
128

4
    VirtFs::unmountDirSilent("../data");
129
1
}
130
131
4
TEST_CASE("Files copyFile1", "")
132
{
133
8
    VirtFs::mountDirSilent("data", Append_false);
134
8
    VirtFs::mountDirSilent("../data", Append_false);
135
136
10
    const std::string dir = VirtFs::getPath("test");
137



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



14
    SECTION("copy")
139
    {
140






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

5
        std::string data = VirtFs::loadTextFileString("test/tempfile.txt");
143
3
        ::remove((dir + "/tempfile.txt").c_str());
144



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



14
    SECTION("errors")
148
    {
149






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





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

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

8
    VirtFs::unmountDirSilent("../data");
158
2
}
159
160
3
TEST_CASE("Files loadTextFileLocal", "")
161
{
162
4
    VirtFs::mountDirSilent("data", Append_false);
163
4
    VirtFs::mountDirSilent("../data", Append_false);
164
165
5
    const std::string dir = VirtFs::getPath("test");
166



4
    REQUIRE(!dir.empty());
167

7
    Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2");
168
1
    StringVect lines;
169





9
    REQUIRE(Files::loadTextFileLocal(pathJoin(dir, "tempfile.txt"),
170
        lines));
171
3
    ::remove((dir + "/tempfile.txt").c_str());
172



5
    REQUIRE(lines.size() == 2);
173



4
    REQUIRE(lines[0] == "test line");
174



5
    REQUIRE(lines[1] == "text line2");
175
176
1
    ResourceManager::deleteInstance();
177

4
    VirtFs::unmountDirSilent("data");
178

4
    VirtFs::unmountDirSilent("../data");
179
1
}
180
181
3
TEST_CASE("Files getFilesInDir", "")
182
{
183
4
    VirtFs::mountDirSilent("data", Append_false);
184
4
    VirtFs::mountDirSilent("../data", Append_false);
185
186
2
    StringVect list;
187

7
    VirtFs::getFilesInDir("test",
188
        list,
189
1
        ".gpl");
190



5
    REQUIRE(list.size() == 1);
191





11
    REQUIRE(list[0] == pathJoin("test", "palette.gpl"));
192
193
1
    list.clear();
194

7
    VirtFs::getFilesInDir("perserver/default",
195
        list,
196
1
        ".xml");
197



5
    REQUIRE(list.size() == 6);
198





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





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





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





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





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





15
    REQUIRE(list[5] == pathJoin("perserver", "default", "weapons.xml"));
205
1
    ResourceManager::deleteInstance();
206

4
    VirtFs::unmountDirSilent("data");
207

4
    VirtFs::unmountDirSilent("../data");
208

4
}