File: test_rsync.cc

package info (click to toggle)
eckit 1.32.4-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 600,644 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 41
file content (357 lines) | stat: -rw-r--r-- 10,893 bytes parent folder | download | duplicates (7)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * (C) Copyright 2020- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <fstream>
#include <memory>
#include <string>

#include <librsync.h>

#include "eckit/config/LibEcKit.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/filesystem/LocalPathName.h"
#include "eckit/filesystem/PathName.h"
#include "eckit/filesystem/TmpFile.h"
#include "eckit/io/DataHandle.h"
#include "eckit/io/MemoryHandle.h"
#include "eckit/io/StdFile.h"
#include "eckit/log/Log.h"
#include "eckit/utils/Rsync.h"

#include "eckit/testing/Test.h"

using namespace eckit;
using namespace eckit::testing;


namespace eckit {
namespace test {

static void remove_dir_recursive(const PathName& dir) {
    if (not dir.exists())
        return;

    ASSERT(dir.isDir());

    std::vector<PathName> files;
    std::vector<PathName> dirs;
    dir.childrenRecursive(files, dirs);

    for (const auto& file : files) {
        file.unlink();
    }

    for (auto d = dirs.rbegin(); d != dirs.rend(); ++d) {
        d->rmdir();
    }

    dir.rmdir();
}

static bool same_contents(const PathName& left, const PathName& right) {
    std::unique_ptr<DataHandle> lhs(left.fileHandle());
    std::unique_ptr<DataHandle> rhs(right.fileHandle());
    return rhs->compare(*lhs);
}

static bool same_dir(const PathName& left, const PathName& right) {
    std::vector<PathName> lfiles;
    std::vector<PathName> ldirs;
    left.childrenRecursive(lfiles, ldirs);

    Log::debug<LibEcKit>() << "left files " << lfiles << std::endl;
    Log::debug<LibEcKit>() << "left dirs " << ldirs << std::endl;

    std::vector<PathName> rfiles;
    std::vector<PathName> rdirs;
    right.childrenRecursive(rfiles, rdirs);

    Log::debug<LibEcKit>() << "right files " << rfiles << std::endl;
    Log::debug<LibEcKit>() << "right dirs " << rdirs << std::endl;

    if (rfiles.size() != lfiles.size() or rdirs.size() != ldirs.size())
        return false;

    // compare files
    {
        std::vector<PathName>::const_iterator riter = rfiles.begin();
        std::vector<PathName>::const_iterator liter = lfiles.begin();

        for (; riter != rfiles.end(); ++riter, ++liter) {
            LocalPathName rhs = LocalPathName(*riter).relativePath(right.localPath());
            LocalPathName lhs = LocalPathName(*liter).relativePath(left.localPath());

            Log::debug<LibEcKit>() << "comparing files " << lhs << " to " << rhs << std::endl;

            if (rhs != lhs)
                return false;

            if (not same_contents(*liter, *riter))
                return false;
        }
    }

    // compare dirs
    {
        std::vector<PathName>::const_iterator riter = rdirs.begin();
        std::vector<PathName>::const_iterator liter = ldirs.begin();

        for (; riter != rdirs.end(); ++riter, ++liter) {
            LocalPathName rhs = LocalPathName(*riter).relativePath(right.localPath());
            LocalPathName lhs = LocalPathName(*liter).relativePath(left.localPath());

            Log::debug<LibEcKit>() << "comparing dirs " << lhs << " to " << rhs << std::endl;

            if (rhs != lhs)
                return false;
        }
    }

    return true;
}

static void fill(const PathName& path, const std::string& msg) {
    std::ofstream ofs(path.localPath());
    ofs << msg << std::endl;
}

CASE("File sync") {

    Rsync rsync;

    PathName source = PathName::unique(PathName(LocalPathName::cwd()) / "test");
    PathName target = PathName::unique(PathName(LocalPathName::cwd()) / "test");

    SECTION("File sync to inexistent target") {
        fill(source, "The quick brown fox jumps over the lazy dog");

        EXPECT_NO_THROW(rsync.syncData(source, target));
        EXPECT(same_contents(source, target));
    }

    SECTION("File sync from empty file ") {
        source.touch();
        fill(target, "The quick brown fox jumps over the lazy dog");

        EXPECT_NO_THROW(rsync.syncData(source, target));
        EXPECT(same_contents(source, target));
    }

    SECTION("File sync to existing empty file") {
        fill(source, "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.");
        target.touch();

        EXPECT_NO_THROW(rsync.syncData(source, target));
        EXPECT(same_contents(source, target));
    }

    SECTION("Delta in the begining") {
        fill(source,
             "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was "
             "born and I will give you a complete account of the system...");

        fill(target,
             "He should complain to you how all this mistaken idea of denouncing pleasure and praising pain was "
             "born and I will give you a complete account of the system...");

        EXPECT_NO_THROW(rsync.syncData(source, target));
        EXPECT(same_contents(source, target));
    }

    SECTION("Delta in the middle") {
        fill(source,
             "One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed "
             "into a horrible vermin.");
        fill(target, "One morning transformed in his bed into a horrible vermin.");

        EXPECT_NO_THROW(rsync.syncData(source, target));
        EXPECT(same_contents(source, target));
    }

    SECTION("Delta at the end") {
        fill(source, "The quick brown fox jumps over the lazy dog.");
        fill(target, "The quick brown fox jumps over the curious duck.");

        EXPECT_NO_THROW(rsync.syncData(source, target));
        EXPECT(same_contents(source, target));
    }

    SECTION("Source file doesnt exist") {
        // no source
        fill(target, "The quick brown fox jumps over the curious duck.");
        EXPECT_THROWS_AS(rsync.syncData(source, target), eckit::CantOpenFile);
    }

    if (source.exists())
        source.unlink();

    if (target.exists())
        target.unlink();
}

CASE("Directory sync") {

    Rsync rsync;

    // fill in source dirctory
    PathName source = PathName::unique(PathName(LocalPathName::cwd()) / "rsync" / "source");
    source.mkdir();

    PathName d1 = source / "dir1";
    d1.mkdir();
    PathName d2 = source / "dir2";
    d2.mkdir();
    PathName d23 = d2 / "dir23";
    d23.mkdir();

    PathName target = PathName::unique(PathName(LocalPathName::cwd()) / "rsync" / "target");

    SECTION("Sync empty dirs") {
        EXPECT_NO_THROW(rsync.syncRecursive(source, target));
        EXPECT(same_dir(source, target));
    }

    SECTION("Sync full dirs") {

        PathName f1d1 = d1 / "f1";
        fill(f1d1, "F1D1");
        PathName f2d1 = d1 / "f2";
        fill(f2d1, "F2D1");
        PathName f3d1 = d1 / "f3";
        fill(f3d1, "F3D1");
        PathName f1d2 = d2 / "f1";
        fill(f1d2, "F1D2");
        PathName f2d2 = d2 / "f2";
        fill(f2d2, "F2D2");
        PathName f1d23 = d23 / "f1";
        fill(f1d23, "F1D23");

        PathName d234 = d23 / "dir234";
        d234.mkdir();
        PathName d12 = d1 / "dir12";
        d12.mkdir();

        EXPECT_NO_THROW(rsync.syncRecursive(source, target));
        EXPECT(same_dir(source, target));
    }

    SECTION("Sync dirs with update") {
        PathName f1d1 = d1 / "f1";
        fill(f1d1, "F1D1bis");
        EXPECT_NO_THROW(rsync.syncRecursive(source, target));
        EXPECT(same_dir(source, target));
    }

    remove_dir_recursive(target);
    remove_dir_recursive(source);
}

CASE("DataHandle operations") {

    Rsync rsync;

    PathName source    = PathName::unique(PathName(LocalPathName::cwd()) / "test");
    PathName target    = PathName::unique(PathName(LocalPathName::cwd()) / "test");
    PathName signature = PathName::unique(PathName(LocalPathName::cwd()) / "test");
    PathName delta     = PathName::unique(PathName(LocalPathName::cwd()) / "test");
    PathName patched   = PathName::unique(PathName(LocalPathName::cwd()) / "test");

    fill(source,
         "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was "
         "born and I will give you a complete account of the system...");

    fill(target,
         "He should complain to you how all this mistaken idea of denouncing pleasure and praising pain was "
         "born and I will give you a complete account of the system...");

    {
        AutoStdFile tgt(target);
        AutoStdFile sig(signature, "w");
        ASSERT(rs_sig_file(tgt, sig, RS_DEFAULT_BLOCK_LEN, 0, RS_RK_BLAKE2_SIG_MAGIC, nullptr) == RS_DONE);
    }

    {
        AutoStdFile sigf(signature);
        rs_signature_t* sig;
        ASSERT(rs_loadsig_file(sigf, &sig, nullptr) == RS_DONE);
        ASSERT(rs_build_hash_table(sig) == RS_DONE);

        AutoStdFile src(source);
        AutoStdFile dlt(delta, "w");
        ASSERT(rs_delta_file(sig, src, dlt, nullptr) == RS_DONE);
    }

    {
        AutoStdFile tgt(target);
        AutoStdFile dlt(delta);
        AutoStdFile patch(patched, "w");
        ASSERT(rs_patch_file(tgt, dlt, patch, nullptr) == RS_DONE);
    }

    SECTION("Compute signature") {
        std::unique_ptr<DataHandle> in(target.fileHandle());
        in->openForRead();
        MemoryHandle out;
        out.openForWrite(0);
        EXPECT_NO_THROW(rsync.computeSignature(*in, out));

        std::unique_ptr<DataHandle> ref(signature.fileHandle());
        EXPECT(ref->compare(out));
    }

    SECTION("Compute delta") {
        std::unique_ptr<DataHandle> in(source.fileHandle());
        in->openForRead();
        std::unique_ptr<DataHandle> sig(signature.fileHandle());
        sig->openForRead();
        MemoryHandle out;
        out.openForWrite(0);
        EXPECT_NO_THROW(rsync.computeDelta(*sig, *in, out));

        std::unique_ptr<DataHandle> ref(delta.fileHandle());
        EXPECT(ref->compare(out));
    }

    SECTION("Update data") {
        std::unique_ptr<DataHandle> in(target.fileHandle());
        in->openForRead();
        std::unique_ptr<DataHandle> dlt(delta.fileHandle());
        dlt->openForRead();
        MemoryHandle out;
        out.openForWrite(0);
        EXPECT_NO_THROW(rsync.updateData(*in, *dlt, out));

        std::unique_ptr<DataHandle> ref(patched.fileHandle());
        EXPECT(ref->compare(out));
    }

    if (source.exists())
        source.unlink();

    if (target.exists())
        target.unlink();

    if (signature.exists())
        signature.unlink();

    if (delta.exists())
        delta.unlink();

    if (patched.exists())
        patched.unlink();
}


}  // end namespace test
}  // end namespace eckit

int main(int argc, char* argv[]) {
    return run_tests(argc, argv);
}