summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/filetest.cpp
blob: d0f8bbfd98ba6b46e543a7a16b4f58bbf61614c4 (plain) (blame)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tests.h"
#include <vespa/fastos/file.h>
#include <memory>
#include <cassert>
#include <sys/mman.h>
#include <filesystem>

class FileTest : public BaseTest
{
public:
    const std::string srcDir = getenv("SOURCE_DIRECTORY") ? getenv("SOURCE_DIRECTORY") : ".";
    const std::string roFilename = srcDir + "/hello.txt";
    const std::string woFilename = "generated/writeonlytest.txt";
    const std::string rwFilename = "generated/readwritetest.txt";

    void GetCurrentDirTest ()
    {
        TestHeader ("Get Current Directory Test");

        std::string currentDir = FastOS_File::getCurrentDirectory();

        Progress(!currentDir.empty(),
                 "Current dir: %s", !currentDir.empty() ?
                 currentDir.c_str() : "<failed>");

        bool dirrc = FastOS_File::SetCurrentDirectory("..");

        std::string parentDir;

        if (dirrc) {
            parentDir = FastOS_File::getCurrentDirectory();
        }

        Progress(dirrc && strcmp(currentDir.c_str(), parentDir.c_str()) != 0,
                 "Parent dir: %s", !parentDir.empty() ?
                 parentDir.c_str() : "<failed>");

        dirrc = FastOS_File::SetCurrentDirectory(currentDir.c_str());

        Progress(dirrc, "Changed back to working directory.");

        PrintSeparator();
    }

    void MemoryMapTest (int mmap_flags)
    {
        TestHeader ("Memory Map Test");

        int i;
        const int bufSize = 1000;

        std::filesystem::create_directory(std::filesystem::path("generated"));
        FastOS_File file("generated/memorymaptest");

        bool rc = file.OpenReadWrite();
        Progress(rc, "Opening file 'generated/memorymaptest'");

        if (rc) {
            char *buffer = new char [bufSize];
            for (i = 0; i < bufSize; i++) {
                buffer[i] = i % 256;
            }
            ssize_t wroteB = file.Write2(buffer, bufSize);
            Progress(wroteB == bufSize, "Writing %d bytes to file", bufSize);

            bool close_ok = file.Close();
            assert(close_ok);
            file.enableMemoryMap(mmap_flags);

            rc = file.OpenReadOnly();

            Progress(rc, "Opening file 'generated/memorymaptest' read-only");
            if (rc) {
                bool mmapEnabled;
                char *mmapBuffer = nullptr;

                mmapEnabled = file.IsMemoryMapped();
                mmapBuffer = static_cast<char *>(file.MemoryMapPtr(0));

                Progress(rc, "Memory mapping %s",
                         mmapEnabled ? "enabled" : "disabled");
                Progress(rc, "Map address: 0x%p", mmapBuffer);

                if (mmapEnabled) {
                    rc = 0;
                    for (i = 0; i < bufSize; i++) {
                        rc |= (mmapBuffer[i] == i % 256);
                    }
                    Progress(rc, "Reading %d bytes from memory map", bufSize);
                }
            }
            delete [] buffer;
        }
        std::filesystem::remove_all(std::filesystem::path("generated"));
        PrintSeparator();
    }

    void DirectIOTest ()
    {
        TestHeader ("Direct Disk IO Test");

        int i;
        const int bufSize = 40000;

        std::filesystem::create_directory(std::filesystem::path("generated"));
        FastOS_File file("generated/diotest");

        bool rc = file.OpenWriteOnly();
        Progress(rc, "Opening file 'generated/diotest' write-only");

        if (rc) {
            char *buffer = new char [bufSize];

            for (i=0; i<bufSize; i++) {
                buffer[i] = 'A' + (i % 17);
            }
            ssize_t wroteB = file.Write2(buffer, bufSize);
            Progress(wroteB == bufSize, "Writing %d bytes to file", bufSize);

            bool close_ok = file.Close();
            assert(close_ok);

            if (rc) {
                file.EnableDirectIO();

                rc = file.OpenReadOnly();
                Progress(rc, "Opening file 'generated/diotest' read-only");
                if (rc) {
                    bool dioEnabled;
                    size_t memoryAlignment=0;
                    size_t transferGranularity=0;
                    size_t transferMaximum=0;

                    dioEnabled = file.GetDirectIORestrictions(memoryAlignment,
                                                              transferGranularity,
                                                              transferMaximum);

                    Progress(rc, "DirectIO %s", dioEnabled ? "enabled" : "disabled");
                    Progress(rc, "Memory alignment: %u bytes", memoryAlignment);
                    Progress(rc, "Transfer granularity: %u bytes", transferGranularity);
                    Progress(rc, "Transfer maximum: %u bytes", transferMaximum);

                    if (dioEnabled) {
                        int eachRead = (8192 + transferGranularity - 1) / transferGranularity;

                        char *buffer2 = new char [(eachRead * transferGranularity +
                                                   memoryAlignment - 1)];
                        char *alignPtr = buffer2;
                        unsigned int align =
                            static_cast<unsigned int>
                            (reinterpret_cast<unsigned long>(alignPtr) &
                             (memoryAlignment - 1));
                        if (align != 0) {
                            alignPtr = &alignPtr[memoryAlignment - align];
                        }
                        int residue = bufSize;
                        int pos=0;
                        while (residue > 0) {
                            int readThisTime = eachRead * transferGranularity;
                            if (readThisTime > residue) {
                                readThisTime = residue;
                            }
                            file.ReadBuf(alignPtr, readThisTime, pos);

                            for (i=0; i<readThisTime; i++) {
                                rc = (alignPtr[i] == 'A' + ((i+pos) % 17));
                                if (!rc) {
                                    Progress(false, "Read error at offset %d", i);
                                    break;
                                }
                            }
                            residue -= readThisTime;
                            pos += readThisTime;

                            if (!rc) break;
                        }
                        if (rc) {
                            Progress(true, "Read success");

                            rc = file.SetPosition(1);
                            Progress(rc, "SetPosition(1)");
                            if (rc) {
                                try {
                                    const int attemptReadBytes = 173;
                                    ssize_t readB = file.Read(buffer, attemptReadBytes);
                                    Progress(false, "Expected to get an exception for unaligned read");
                                    ProgressI64(readB == attemptReadBytes, "Got %ld bytes from attempted 173", readB);
                                } catch(const DirectIOException &e) {
                                    Progress(true, "Got exception as expected");
                                }
                            }
                            if (rc) {
                                rc = file.SetPosition(1);
                                Progress(rc, "SetPosition(1)");
                                if (rc) {
                                    try {
                                        const int attemptReadBytes = 4096;
                                        ssize_t readB = file.Read(buffer, attemptReadBytes);
                                        Progress(false, "Expected to get an exception for unaligned read");
                                        ProgressI64(readB == attemptReadBytes, "Got %ld bytes from attempted 4096", readB);
                                    } catch(const DirectIOException &e) {
                                        Progress(true, "Got exception as expected");
                                    }
                                }
                            }
                        }
                        delete [] buffer2;
                    } else {
                        memset(buffer, 0, bufSize);

                        ssize_t readBytes = file.Read(buffer, bufSize);
                        Progress(readBytes == bufSize,
                                 "Reading %d bytes from file", bufSize);

                        for (i=0; i<bufSize; i++) {
                            rc = (buffer[i] == 'A' + (i % 17));
                            if (!rc) {
                                Progress(false, "Read error at offset %d", i);
                                break;
                            }
                        }
                        if (rc) Progress(true, "Read success");
                    }
                }
            }
            delete [] buffer;
        }

        std::filesystem::remove_all(std::filesystem::path("generated"));
        PrintSeparator();
    }

    void ReadOnlyTest ()
    {
        TestHeader("Read-Only Test");

        FastOS_File *myFile = new FastOS_File(roFilename.c_str());

        if (myFile->OpenReadOnly()) {
            int64_t filesize;
            filesize = myFile->GetSize();
            ProgressI64((filesize == 27), "File size: %ld", filesize);

            char dummyData[6] = "Dummy";
            bool writeResult = myFile->CheckedWrite(dummyData, 6);

            if (writeResult) {
                Progress(false, "Should not be able to write a file opened for read-only access.");
            } else {
                char dummyData2[28];
                Progress(true, "Write failed with read-only access.");

                bool rc = myFile->SetPosition(1);
                Progress(rc, "Setting position to 1");

                if (rc) {
                    ssize_t readBytes;
                    int64_t filePosition;
                    readBytes = myFile->Read(dummyData2, 28);

                    Progress(readBytes == 26, "Attempting to read 28 bytes, should get 26. Got: %d", readBytes);

                    filePosition = myFile->GetPosition();
                    Progress(filePosition == 27, "File position should now be 27. Was: %d", int(filePosition));

                    readBytes = myFile->Read(dummyData2, 6);
                    Progress(readBytes == 0, "We should now get 0 bytes. Read: %d bytes", readBytes);

                    filePosition = myFile->GetPosition();
                    Progress(filePosition == 27, "File position should now be 27. Was: %d", int(filePosition));
                }
            }
        } else {
            Progress(false, "Unable to open file '%s'.", roFilename.c_str());
        }
        delete(myFile);
        PrintSeparator();
    }

    void WriteOnlyTest ()
    {
        TestHeader("Write-Only Test");
        std::filesystem::create_directory(std::filesystem::path("generated"));

        FastOS_File *myFile = new FastOS_File(woFilename.c_str());

        if (myFile->OpenWriteOnly()) {
            int64_t filesize;
            filesize = myFile->GetSize();

            ProgressI64((filesize == 0), "File size: %ld", filesize);

            char dummyData[6] = "Dummy";
            bool writeResult = myFile->CheckedWrite(dummyData, 6);

            if (!writeResult) {
                Progress(false, "Should be able to write to file opened for write-only access.");
            } else {
                Progress(true, "Write 6 bytes ok.");

                int64_t filePosition = myFile->GetPosition();
                if (filePosition == 6) {
                    Progress(true, "Fileposition is now 6.");

                    if (myFile->SetPosition(0)) {
                        Progress(true, "SetPosition(0) success.");
                        filePosition = myFile->GetPosition();

                        if (filePosition == 0) {
                            Progress(true, "Fileposition is now 0.");

                            int readBytes = myFile->Read(dummyData, 6);

                            if (readBytes != 6) {
                                Progress(true, "Trying to read a write-only file should fail and it did.");
                                Progress(true, "Return code was: %d.", readBytes);
                            } else {
                                Progress(false, "Read on a file with write-only access should fail, but it didn't.");
                            }
                        } else {
                            ProgressI64(false, "Fileposition should be 6, but was %ld.", filePosition);
                        }
                    } else {
                        Progress(false, "SetPosition(0) failed");
                    }
                } else {
                    ProgressI64(false, "Fileposition should be 6, but was %ld.", filePosition);
                }
            }
            bool closeResult = myFile->Close();
            Progress(closeResult, "Close file.");
        } else {
            Progress(false, "Unable to open file '%s'.", woFilename.c_str());
        }


        bool deleteResult = myFile->Delete();
        Progress(deleteResult, "Delete file '%s'.", woFilename.c_str());

        delete(myFile);
        std::filesystem::remove_all(std::filesystem::path("generated"));
        PrintSeparator();
    }

    void ReadWriteTest ()
    {
        TestHeader("Read/Write Test");
        std::filesystem::create_directory(std::filesystem::path("generated"));

        FastOS_File *myFile = new FastOS_File(rwFilename.c_str());

        if (myFile->OpenExisting()) {
            Progress(false, "OpenExisting() should not work when '%s' does not exist.", rwFilename.c_str());
            bool close_ok = myFile->Close();
            assert(close_ok);
        } else {
            Progress(true, "OpenExisting() should fail when '%s' does not exist, and it did.", rwFilename.c_str());
        }

        if (myFile->OpenReadWrite()) {
            int64_t filesize;

            filesize = myFile->GetSize();

            ProgressI64((filesize == 0), "File size: %ld", filesize);

            char dummyData[6] = "Dummy";

            bool writeResult = myFile->CheckedWrite(dummyData, 6);

            if (!writeResult) {
                Progress(false, "Should be able to write to file opened for read/write access.");
            } else {
                Progress(true, "Write 6 bytes ok.");

                int64_t filePosition = myFile->GetPosition();

                if (filePosition == 6) {
                    Progress(true, "Fileposition is now 6.");

                    if (myFile->SetPosition(0)) {
                        Progress(true, "SetPosition(0) success.");
                        filePosition = myFile->GetPosition();

                        if (filePosition == 0) {
                            Progress(true, "Fileposition is now 0.");

                            char dummyData2[7];
                            int readBytes = myFile->Read(dummyData2, 6);

                            if (readBytes == 6) {
                                Progress(true, "Reading 6 bytes worked.");

                                int cmpResult = memcmp(dummyData, dummyData2, 6);
                                Progress((cmpResult == 0), "Comparing the written and read result.\n");

                                bool rc = myFile->SetPosition(1);
                                Progress(rc, "Setting position to 1");

                                if (rc) {
                                    readBytes = myFile->Read(dummyData2, 7);

                                    Progress(readBytes == 5, "Attempting to read 7 bytes, should get 5. Got: %d", readBytes);

                                    filePosition = myFile->GetPosition();
                                    Progress(filePosition == 6, "File position should now be 6. Was: %d", int(filePosition));

                                    readBytes = myFile->Read(dummyData2, 6);
                                    Progress(readBytes == 0, "We should not be able to read any more. Read: %d bytes", readBytes);

                                    filePosition = myFile->GetPosition();
                                    Progress(filePosition == 6, "File position should now be 6. Was: %d", int(filePosition));
                                }
                            } else {
                                Progress(false, "Reading 6 bytes failed.");
                            }
                        } else {
                            ProgressI64(false, "Fileposition should be 6, but was %ld.", filePosition);
                        }
                    } else {
                        Progress(false, "SetPosition(0) failed");
                    }
                } else {
                    ProgressI64(false, "Fileposition should be 6, but was %ld.", filePosition);
                }
            }

            bool closeResult = myFile->Close();
            Progress(closeResult, "Close file.");
        } else {
            Progress(false, "Unable to open file '%s'.", rwFilename.c_str());
        }
        bool deleteResult = myFile->Delete();
        Progress(deleteResult, "Delete file '%s'.", rwFilename.c_str());

        delete(myFile);
        std::filesystem::remove_all(std::filesystem::path("generated"));
        PrintSeparator();
    }

    void ScanDirectoryTest()
    {
        TestHeader("DirectoryScan Test");

        FastOS_DirectoryScan *scanDir = new FastOS_DirectoryScan(".");

        while (scanDir->ReadNext()) {
            const char *name = scanDir->GetName();
            bool isDirectory = scanDir->IsDirectory();
            bool isRegular   = scanDir->IsRegular();

            printf("%-30s %s\n", name,
                   isDirectory ? "DIR" : (isRegular ? "FILE" : "UNKN"));
        }

        delete(scanDir);
        PrintSeparator();
    }

    void ReadBufTest ()
    {
        TestHeader("ReadBuf Test");

        FastOS_File file(roFilename.c_str());

        char buffer[20];

        if (file.OpenReadOnly()) {
            int64_t position = file.GetPosition();
            Progress(position == 0, "File pointer should be 0 after opening file");

            ssize_t has_read = file.Read(buffer, 4);
            Progress(has_read == 4, "Must read 4 bytes");
            buffer[4] = '\0';
            position = file.GetPosition();
            Progress(position == 4, "File pointer should be 4 after reading 4 bytes");
            Progress(strcmp(buffer, "This") == 0, "[This]=[%s]", buffer);

            file.ReadBuf(buffer, 6, 8);
            buffer[6] = '\0';
            position = file.GetPosition();
            Progress(position == 4, "File pointer should still be 4 after ReadBuf");
            Progress(strcmp(buffer, "a test") == 0, "[a test]=[%s]", buffer);
        }

        PrintSeparator();
    }

    void DiskFreeSpaceTest ()
    {
        TestHeader("DiskFreeSpace Test");

        int64_t freeSpace = FastOS_File::GetFreeDiskSpace(roFilename.c_str());
        ProgressI64(freeSpace != -1, "DiskFreeSpace using file ('hello.txt'): %ld MB.", freeSpace == -1 ? -1 : freeSpace/(1024*1024));
        freeSpace = FastOS_File::GetFreeDiskSpace(".");
        ProgressI64(freeSpace != -1, "DiskFreeSpace using dir (.): %ld MB.", freeSpace == -1 ? -1 : freeSpace/(1024*1024));
        PrintSeparator();
    }

    void MaxLengthTest ()
    {
        TestHeader ("Max Lengths Test");

        int maxval = FastOS_File::GetMaximumFilenameLength(".");
        Progress(maxval > 5 && maxval < (512*1024),
                 "Maximum filename length = %d", maxval);

        maxval = FastOS_File::GetMaximumPathLength(".");
        Progress(maxval > 5 && maxval < (512*1024),
                 "Maximum path length = %d", maxval);

        PrintSeparator();
    }

    int Main () override
    {
        printf("This test should be run in the 'tests' directory.\n\n");
        printf("grep for the string '%s' to detect failures.\n\n", failString);

        GetCurrentDirTest();
        DirectIOTest();
        MaxLengthTest();
        DiskFreeSpaceTest();
        ReadOnlyTest();
        WriteOnlyTest();
        ReadWriteTest();
        ScanDirectoryTest();
        ReadBufTest();
        MemoryMapTest(0);
#ifdef __linux__
        MemoryMapTest(MAP_HUGETLB);
#endif

        PrintSeparator();
        printf("END OF TEST (%s)\n", _argv[0]);

        return allWasOk() ? 0 : 1;
    }
    FileTest();
    ~FileTest();
};

FileTest::FileTest() { }
FileTest::~FileTest() { }


int main (int argc, char **argv)
{
    FileTest app;

    setvbuf(stdout, nullptr, _IOLBF, 8192);
    return app.Entry(argc, argv);
}