summaryrefslogtreecommitdiffstats
path: root/src/test/dokan/dokan.cc
blob: 1a1d39580e6dd0b195d94700a78fc492e0fe6696 (plain)
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2022 Cloudbase Solutions
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include <windows.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sys/socket.h>
#include <direct.h>

#include "gtest/gtest.h"

#include "common/SubProcess.h"
#include "common/run_cmd.h"
#include "include/uuid.h"

#define DEFAULT_MOUNTPOINT "X:\\"
#define MOUNT_POLL_ATTEMPT 10
#define MOUNT_POLL_INTERVAL_MS 1000
#define TEST_VOL_SERIAL "1234567890"
#define MByte 1048576

namespace fs = std::filesystem;
using namespace std::chrono_literals;

std::string get_uuid() {
    uuid_d suffix;
    suffix.generate_random();

    return suffix.to_string();
}

bool move_eof(HANDLE handle, LARGE_INTEGER offset) {

    // Move file pointer to FILE_BEGIN + offset
    if (!SetFilePointerEx(handle, offset, NULL, FILE_BEGIN)) {
        std::cerr << "Setting file pointer failed. err: "
                  << GetLastError() << std::endl;
        return false;
    }

    if (!SetEndOfFile(handle)) {
        std::cerr << "Setting EOF failed. err: " << GetLastError() << std::endl;
        return false;
    }

    return true;
}

void write_file(std::string file_path, std::string data) {
    std::ofstream file;
    file.open(file_path);

    ASSERT_TRUE(file.is_open())
        << "Failed to open file: " << file_path;
    file << data;
    file.flush();

    file.close();
}

void expect_write_failure(std::string file_path) {
    std::ofstream file;
    file.open(file_path);

    ASSERT_FALSE(file.is_open());
}

std::string read_file(std::string file_path) {
    std::ifstream file;
    file.open(file_path);
    std::string content((std::istreambuf_iterator<char>(file)),
                         std::istreambuf_iterator<char>());
    file.close();

    return content;
}

void check_write_file(std::string file_path, std::string data) {
    write_file(file_path, data);
    ASSERT_EQ(read_file(file_path), data);
}

int wait_for_mount(std::string mount_path) {
    std::cerr << "Waiting for mount: " << mount_path << std::endl;

    int attempts = 0;
    do {
        attempts++;
        if (attempts < MOUNT_POLL_ATTEMPT)
            Sleep(MOUNT_POLL_INTERVAL_MS);
    } while (!fs::exists(mount_path)
             && attempts < MOUNT_POLL_ATTEMPT);

    if (!fs::exists(mount_path)) {
        std::cerr << "Timed out waiting for ceph-dokan mount: " 
                  << mount_path << std::endl;
        return -ETIMEDOUT;
    }

    std::cerr << "Successfully mounted: " << mount_path << std::endl;

    return 0;
}

void map_dokan(SubProcess** mount, const char* mountpoint) {
    SubProcess* new_mount = new SubProcess("ceph-dokan");

    new_mount->add_cmd_args("map", "--win-vol-name", "TestCeph",
                            "--win-vol-serial", TEST_VOL_SERIAL,
                            "-l", mountpoint, NULL);

    *mount = new_mount;
    ASSERT_EQ(new_mount->spawn(), 0);
    ASSERT_EQ(wait_for_mount(mountpoint), 0);
}

void map_dokan_read_only(
    SubProcess** mount,
    const char* mountpoint
) {
    SubProcess* new_mount = new SubProcess("ceph-dokan");
    new_mount->add_cmd_args("map", "--win-vol-name", "TestCeph",
                            "--win-vol-serial", TEST_VOL_SERIAL,
                            "--read-only", "-l", mountpoint, NULL);

    *mount = new_mount;
    ASSERT_EQ(new_mount->spawn(), 0);
    ASSERT_EQ(wait_for_mount(mountpoint), 0);
    std::cerr << mountpoint << " mounted in read-only mode"
              << std::endl;
}

void map_dokan_with_maxpath(
    SubProcess** mount,
    const char* mountpoint,
    uint64_t max_path_len)
{
    SubProcess* new_mount = new SubProcess("ceph-dokan");
    new_mount->add_cmd_args("map", "--debug", "--dokan-stderr",
                            "--win-vol-name", "TestCeph",
                            "--win-vol-serial", TEST_VOL_SERIAL,
                            "--max-path-len",
                            (std::to_string(max_path_len)).c_str(),
                            "-l", mountpoint, NULL);

    *mount = new_mount;
    ASSERT_EQ(new_mount->spawn(), 0);
    if (256 <= max_path_len && max_path_len <= 4096) {
        ASSERT_EQ(wait_for_mount(mountpoint), 0);
    } else {
        ASSERT_NE(wait_for_mount(mountpoint), 0);
    }
}

void unmap_dokan(SubProcess* mount, const char* mountpoint) {
    std::string ret = run_cmd("ceph-dokan", "unmap", "-l",
                              mountpoint, (char*)NULL);
                              
    ASSERT_EQ(ret, "") << "Failed unmapping: " << mountpoint;
    std::cerr<< "Unmounted: " << mountpoint << std::endl;

    ASSERT_EQ(mount->join(), 0);
}

int get_volume_max_path(std::string mountpoint){
    char volume_name[MAX_PATH + 1] = { 0 };
    char file_system_name[MAX_PATH + 1] = { 0 };
    DWORD serial_number = 0;
    DWORD max_component_len = 0;
    DWORD file_system_flags = 0;
    if (GetVolumeInformation(
            mountpoint.c_str(),
            volume_name,
            sizeof(volume_name),
            &serial_number,
            &max_component_len,
            &file_system_flags,
            file_system_name,
            sizeof(file_system_name)) != TRUE) {
        std::cerr << "GetVolumeInformation() failed, error: "
                  << GetLastError() << std::endl;
    }

    return max_component_len;
}

static SubProcess* shared_mount = nullptr;

class DokanTests : public testing::Test
{
protected:

    static void SetUpTestSuite() {
        map_dokan(&shared_mount, DEFAULT_MOUNTPOINT);
    }

    static void TearDownTestSuite() {
        if (shared_mount) {
            unmap_dokan(shared_mount, DEFAULT_MOUNTPOINT);
        }
        shared_mount = nullptr;
    }
};

TEST_F(DokanTests, test_mount) {
    std::string mountpoint = "Y:\\";
    SubProcess* mount = nullptr;
    map_dokan(&mount, mountpoint.c_str());
    unmap_dokan(mount, mountpoint.c_str());
}

TEST_F(DokanTests, test_mount_read_only) {
    std::string mountpoint = "Z:\\";
    std::string data = "abc123";
    std::string success_file_path = "ro_success_" + get_uuid();
    std::string failed_file_path = "ro_fail_" + get_uuid();

    SubProcess* mount = nullptr;
    map_dokan(&mount, mountpoint.c_str());

    check_write_file(mountpoint + success_file_path, data);
    ASSERT_TRUE(fs::exists(mountpoint + success_file_path));

    unmap_dokan(mount, mountpoint.c_str());

    mount = nullptr;
    map_dokan_read_only(&mount, mountpoint.c_str());

    expect_write_failure(mountpoint + failed_file_path);
    ASSERT_FALSE(fs::exists(mountpoint + failed_file_path));

    ASSERT_TRUE(fs::exists(mountpoint + success_file_path));
    ASSERT_EQ(read_file(mountpoint + success_file_path), data);

    std::string exception_msg(
        "filesystem error: cannot remove: No such device ["
        + mountpoint + success_file_path + "]");
    EXPECT_THROW({
        try {
            fs::remove(mountpoint + success_file_path);
        } catch(const fs::filesystem_error &e) {
            EXPECT_STREQ(e.what(), exception_msg.c_str());
            throw;
        }
    }, fs::filesystem_error);
    unmap_dokan(mount, mountpoint.c_str());

    map_dokan(&mount, mountpoint.c_str());
    ASSERT_TRUE(fs::exists(mountpoint + success_file_path));
    ASSERT_TRUE(fs::remove(mountpoint + success_file_path));
    unmap_dokan(mount, mountpoint.c_str());
}

TEST_F(DokanTests, test_delete_on_close) {
    std::string file_path = DEFAULT_MOUNTPOINT"file_" + get_uuid();
    HANDLE hFile = CreateFile(
        file_path.c_str(),
        GENERIC_WRITE, // open for writing
        0,             // sharing mode, none in this case
        0,             // use default security descriptor
        CREATE_NEW,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
        0);

    ASSERT_NE(hFile, INVALID_HANDLE_VALUE)
        << "Could not open file: "
        << DEFAULT_MOUNTPOINT"test_create.txt "
        << "err: " << GetLastError() << std::endl;
        
    ASSERT_NE(CloseHandle(hFile), 0);
    
    // FILE_FLAG_DELETE_ON_CLOSE is used
    ASSERT_FALSE(fs::exists(file_path));
}

TEST_F(DokanTests, test_io) {
    std::string data = "abcdef";
    std::string file_path = "test_io_" + get_uuid();

    std::string mountpoint = "I:\\";
    SubProcess* mount = nullptr;
    map_dokan(&mount, mountpoint.c_str());

    check_write_file(mountpoint + file_path, data);
    ASSERT_TRUE(fs::exists(mountpoint + file_path));
    unmap_dokan(mount, mountpoint.c_str());

    mountpoint = "O:\\";
    mount = nullptr;
    map_dokan(&mount, mountpoint.c_str());

    ASSERT_TRUE(fs::exists(mountpoint + file_path));
    EXPECT_EQ(data, read_file(mountpoint + file_path));
    ASSERT_TRUE(fs::remove((mountpoint + file_path).c_str()));
    ASSERT_FALSE(fs::exists(mountpoint + file_path));
    unmap_dokan(mount, mountpoint.c_str());
}

TEST_F(DokanTests, test_subfolders) {
    std::string base_dir_path = DEFAULT_MOUNTPOINT"base_dir_"
                                + get_uuid() + "\\";
    std::string sub_dir_path = base_dir_path
                               + "test_sub_dir" + get_uuid();
    std::string base_dir_file = base_dir_path 
                                + "file_" + get_uuid();
    std::string sub_dir_file = sub_dir_path 
                                + "file_" + get_uuid();

    std::string data = "abc";

    ASSERT_EQ(fs::create_directory(base_dir_path), true);
    ASSERT_TRUE(fs::exists(base_dir_path));

    ASSERT_EQ(fs::create_directory(sub_dir_path), true);
    ASSERT_TRUE(fs::exists(sub_dir_path));

    check_write_file(base_dir_file, data);
    ASSERT_TRUE(fs::exists(base_dir_file));

    check_write_file(sub_dir_file, data);
    ASSERT_TRUE(fs::exists(sub_dir_file));

    ASSERT_TRUE(fs::remove((sub_dir_file).c_str()))
        << "Failed to remove file: " << sub_dir_file;
    ASSERT_FALSE(fs::exists(sub_dir_file));

    // Remove empty dir
    ASSERT_TRUE(fs::remove((sub_dir_path).c_str()))
        << "Failed to remove directory: " << sub_dir_path;
    ASSERT_FALSE(fs::exists(sub_dir_file));

    ASSERT_NE(fs::remove_all((base_dir_path).c_str()), 0)
        << "Failed to remove directory: " << base_dir_path;
    ASSERT_FALSE(fs::exists(sub_dir_file));
}

TEST_F(DokanTests, test_find_files) {
    std::string basedir_path = "X:/find_" + get_uuid();
    std::string subdir_path = basedir_path + "/dir_" + get_uuid();
    std::string file1_path = basedir_path + "/file1_" + get_uuid();
    std::string file2_path = subdir_path + "/file2_" + get_uuid();

    ASSERT_TRUE(
        fs::create_directories(subdir_path)
    );

    std::ofstream{file1_path};
    std::ofstream{file2_path};

    std::vector<std::string> paths;

    for (const auto & entry : 
         fs::recursive_directory_iterator(basedir_path)
    ) {
        paths.push_back(entry.path().generic_string());
    }

    ASSERT_NE(std::find(begin(paths), end(paths), subdir_path), end(paths));
    ASSERT_NE(std::find(begin(paths), end(paths), file1_path), end(paths));
    ASSERT_NE(std::find(begin(paths), end(paths), file2_path), end(paths));

    // clean-up
    ASSERT_NE(fs::remove_all(basedir_path), 0);
}

TEST_F(DokanTests, test_move_file) {
    std::string dir1_path = DEFAULT_MOUNTPOINT
                            "test_mv_1_" + get_uuid() + "\\";
    std::string dir2_path = DEFAULT_MOUNTPOINT
                            "test_mv_2_" + get_uuid() + "\\";
    std::string file_name = "mv_file_" + get_uuid();
    std::string data = "abcd";

    ASSERT_TRUE(fs::create_directory(dir1_path));
    ASSERT_TRUE(fs::create_directory(dir2_path));

    check_write_file(dir1_path + file_name, data);

    fs::rename(dir1_path + file_name, dir2_path + file_name);

    ASSERT_TRUE(fs::exists(dir2_path + file_name));
    ASSERT_FALSE(fs::exists(dir1_path + file_name));

    ASSERT_EQ(data, read_file(dir2_path + file_name));

    // clean-up
    ASSERT_NE(fs::remove_all(dir1_path),0);
    ASSERT_NE(fs::remove_all(dir2_path),0);
}

TEST_F(DokanTests, test_max_path) {
    std::string mountpoint = "P:\\";
    std::string extended_mountpoint = "\\\\?\\" + mountpoint;
    SubProcess* mount = nullptr;
    char dir[200] = { 0 };
    char file[200] = { 0 };
    std::string data = "abcd1234";

    memset(dir, 'd', sizeof(dir) - 1);
    memset(file, 'f', sizeof(file) - 1);

    uint64_t max_path_len = 4096;
    
    map_dokan_with_maxpath(&mount,
                           mountpoint.c_str(),
                           max_path_len);
    EXPECT_EQ(get_volume_max_path(extended_mountpoint),
              max_path_len);

    std::string long_dir_path = extended_mountpoint;

    std::string dir_names[15];

    for (int i = 0; i < 15; i++) {
        std::string crt_dir = std::string(dir) + "_"
                              + get_uuid() + "\\";
        long_dir_path.append(crt_dir);
        int stat = _mkdir(long_dir_path.c_str());
        ASSERT_EQ(stat, 0) << "Error creating directory " << i
                           << ": " << GetLastError() << std::endl;
        dir_names[i] = crt_dir;
    }
    std::string file_path = long_dir_path + "\\" + std::string(file)
                            + "_" + get_uuid();

    check_write_file(file_path, data);

    // clean-up
    // fs::remove is unable to handle long Windows paths
    EXPECT_NE(DeleteFileA(file_path.c_str()), 0);

    for (int i = 14; i >= 0; i--) {
        std::string remove_dir = extended_mountpoint;
        for (int j = 0; j <= i; j++) {
            remove_dir.append(dir_names[j]);
        }
        
        EXPECT_NE(RemoveDirectoryA(remove_dir.c_str()), 0);
    }

    unmap_dokan(mount, mountpoint.c_str());

    // value exceeds 32767, so a failure is expected
    max_path_len = 32770;
    map_dokan_with_maxpath(&mount,
                           mountpoint.c_str(),
                           max_path_len);
    ASSERT_FALSE(fs::exists(mountpoint));

    // value is below 256, so a failure is expected
    max_path_len = 150;
    map_dokan_with_maxpath(&mount,
                           mountpoint.c_str(),
                           max_path_len);
    ASSERT_FALSE(fs::exists(mountpoint));

    // default value
    map_dokan(&mount, mountpoint.c_str());
    EXPECT_EQ(get_volume_max_path(mountpoint.c_str()), 256);

    unmap_dokan(mount, mountpoint.c_str());
}

TEST_F(DokanTests, test_set_eof) {
    std::string file_path = DEFAULT_MOUNTPOINT"test_eof_"
                            + get_uuid();
    HANDLE hFile = CreateFile(
        file_path.c_str(),
        GENERIC_WRITE, // open for writing
        0,             // sharing mode, none in this case
        0,             // use default security descriptor
        CREATE_NEW,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
        0);

    ASSERT_NE(hFile, INVALID_HANDLE_VALUE)
        << "Could not open file: "
        << DEFAULT_MOUNTPOINT"test_create.txt "
        << "err: " << GetLastError() << std::endl;

    LARGE_INTEGER offset;
    offset.QuadPart = 2 * MByte; // 2MB

    LARGE_INTEGER file_size;

    ASSERT_TRUE(move_eof(hFile, offset));
    ASSERT_NE(GetFileSizeEx(hFile, &file_size), 0);
    EXPECT_EQ(file_size.QuadPart, offset.QuadPart);

    offset.QuadPart = MByte; // 1MB

    ASSERT_TRUE(move_eof(hFile, offset));
    ASSERT_NE(GetFileSizeEx(hFile, &file_size), 0);
    EXPECT_EQ(file_size.QuadPart, offset.QuadPart);

    ASSERT_NE(CloseHandle(hFile), 0);
    
    // FILE_FLAG_DELETE_ON_CLOSE is used
    ASSERT_FALSE(fs::exists(file_path));
}

TEST_F(DokanTests, test_set_alloc_size) {
    std::string file_path = DEFAULT_MOUNTPOINT"test_alloc_size_"
                            + get_uuid();
    HANDLE hFile = CreateFile(
        file_path.c_str(),
        GENERIC_WRITE, // open for writing
        0,             // sharing mode, none in this case
        0,             // use default security descriptor
        CREATE_NEW,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
        0);

    ASSERT_NE(hFile, INVALID_HANDLE_VALUE)
        << "Could not open file: "
        << DEFAULT_MOUNTPOINT"test_create.txt "
        << "err: " << GetLastError() << std::endl;
    
    LARGE_INTEGER li;
    li.QuadPart = MByte;
    FILE_ALLOCATION_INFO fai;
    fai.AllocationSize = li;
    
    ASSERT_NE(SetFileInformationByHandle(
                hFile,
                FileAllocationInfo,
                &fai,
                sizeof(FILE_ALLOCATION_INFO)
             ),0) << "Error: " << GetLastError();

    LARGE_INTEGER offset;
    offset.QuadPart = 2 * MByte;
    LARGE_INTEGER file_size;

    ASSERT_TRUE(move_eof(hFile, offset));
    ASSERT_NE(GetFileSizeEx(hFile, &file_size), 0);
    EXPECT_EQ(file_size.QuadPart, offset.QuadPart);

    offset.QuadPart = MByte;

    ASSERT_TRUE(move_eof(hFile, offset));
    ASSERT_NE(GetFileSizeEx(hFile, &file_size), 0);
    EXPECT_EQ(file_size.QuadPart, offset.QuadPart);

    ASSERT_NE(CloseHandle(hFile), 0);
    
    // FILE_FLAG_DELETE_ON_CLOSE is used
    ASSERT_FALSE(fs::exists(file_path));
}

TEST_F(DokanTests, test_file_type) {
    std::string test_dir = DEFAULT_MOUNTPOINT"test_info_"
                            + get_uuid() + "\\";
    std::string file_path = test_dir + "file_"
                            + get_uuid();
    std::string dir_path = test_dir + "dir_"
                           + get_uuid() + "\\";

    ASSERT_TRUE(fs::create_directory(test_dir));

    std::ofstream{file_path};
    ASSERT_TRUE(fs::create_directory(dir_path));

    ASSERT_TRUE(fs::is_regular_file(fs::status(file_path)));
    ASSERT_TRUE(fs::is_directory(fs::status(dir_path)));

    // clean-up
    fs::remove_all(test_dir);

}

TEST_F(DokanTests, test_volume_info) {
    char volume_name[MAX_PATH + 1] = { 0 };
    char file_system_name[MAX_PATH + 1] = { 0 };
    DWORD serial_number = 0;
    DWORD max_component_len = 0;
    DWORD file_system_flags = 0;

    ASSERT_EQ(
        GetVolumeInformation(
            DEFAULT_MOUNTPOINT,
            volume_name,
            sizeof(volume_name),
            &serial_number,
            &max_component_len,
            &file_system_flags,
            file_system_name,
            sizeof(file_system_name)),TRUE) 
        << "GetVolumeInformation() failed, error: "
        << GetLastError() << std::endl;

    ASSERT_STREQ(volume_name, "TestCeph") 
        << "Received: " << volume_name << std::endl;
    ASSERT_STREQ(file_system_name, "Ceph")
        << "Received: " << file_system_name << std::endl;
    ASSERT_EQ(max_component_len, 256);
    ASSERT_EQ(serial_number, std::stoi(TEST_VOL_SERIAL))
        << "Received: " << serial_number << std::endl;

    // Consider adding specific flags 
    // and check for them
    // ASSERT_EQ(file_system_flags, 271);
}

TEST_F(DokanTests, test_get_free_space) {
    std::error_code ec;
    const std::filesystem::space_info si = 
        std::filesystem::space(DEFAULT_MOUNTPOINT, ec);
    ASSERT_EQ(ec.value(), 0);

    ASSERT_NE(static_cast<std::intmax_t>(si.capacity), 0);
    ASSERT_NE(static_cast<std::intmax_t>(si.free), 0);
    ASSERT_NE(static_cast<std::intmax_t>(si.available), 0);
}

TEST_F(DokanTests, test_file_timestamp) {
    std::string file1 = DEFAULT_MOUNTPOINT"test_time1_"
                            + get_uuid();
    std::string file2 = DEFAULT_MOUNTPOINT"test_time2_"
                            + get_uuid();
    std::string file3 = DEFAULT_MOUNTPOINT"test_time3_"
                            + get_uuid();

    std::ofstream{file1};
    Sleep(1000);
    std::ofstream{file2};
    Sleep(1000);
    std::ofstream{file3};

    int64_t file1_creation = fs::last_write_time(file1)
                                 .time_since_epoch().count();
    int64_t file2_creation = fs::last_write_time(file2)
                                 .time_since_epoch().count();
    int64_t file3_creation = fs::last_write_time(file3)
                                 .time_since_epoch().count();

    EXPECT_LT(file1_creation, file2_creation);
    EXPECT_LT(file2_creation, file3_creation);

    // add 1h to file 1 creation time
    fs::file_time_type file1_time = fs::last_write_time(file1);

    fs::last_write_time(file1, file1_time + 1h);

    int64_t file1_new_time = fs::last_write_time(file1)
                                 .time_since_epoch().count();

    EXPECT_EQ((file1_time + 1h).time_since_epoch().count(),
              file1_new_time);
    EXPECT_GT(file1_new_time, file2_creation);
    EXPECT_GT(file1_new_time, file3_creation);

    ASSERT_TRUE(fs::remove(file1));
    ASSERT_TRUE(fs::remove(file2));
    ASSERT_TRUE(fs::remove(file3));
}

TEST_F(DokanTests, test_delete_disposition) {
    std::string file_path = DEFAULT_MOUNTPOINT"test_disp_"
                            + get_uuid();
    HANDLE hFile = CreateFile(file_path.c_str(), 
                               GENERIC_ALL, // required for delete
                               0, // exclusive access
                               NULL, 
                               CREATE_ALWAYS,
                               0, 
                               NULL);
    
    ASSERT_NE(hFile, INVALID_HANDLE_VALUE)
        << "Could not open file: " << file_path
        << "err: " << GetLastError() << std::endl;
  
    FILE_DISPOSITION_INFO fdi;
    fdi.DeleteFile = TRUE; // marking for deletion

    ASSERT_NE(
        SetFileInformationByHandle(
            hFile,
            FileDispositionInfo,
            &fdi,
            sizeof(FILE_DISPOSITION_INFO)), 0);

    ASSERT_NE(CloseHandle(hFile), 0);
    ASSERT_FALSE(fs::exists(file_path));    
}

bool check_create_disposition(std::string path, DWORD disposition) {
    HANDLE hFile = CreateFile(path.c_str(), 
                              GENERIC_WRITE,
                              0, // exclusive access
                              NULL,
                              disposition,
                              0, 
                              NULL);

    if(hFile == INVALID_HANDLE_VALUE) {
        return false;
    }

    if(CloseHandle(hFile) == 0) {
        return false;
    }

    return true;
}

TEST_F(DokanTests, test_create_dispositions) {
    std::string file_path = DEFAULT_MOUNTPOINT"test_create_"
                            + get_uuid();
    std::string non_existant_file = DEFAULT_MOUNTPOINT
                                    "test_create_" + get_uuid();
  
    EXPECT_TRUE(
        check_create_disposition(file_path, CREATE_NEW));
    
    // CREATE_ALWAYS with existing file
    EXPECT_TRUE(
        check_create_disposition(file_path, CREATE_ALWAYS));
    EXPECT_EQ(GetLastError(), ERROR_ALREADY_EXISTS);

    // CREATE_NEW with existing file
    EXPECT_FALSE(
        check_create_disposition(file_path, CREATE_NEW));
    EXPECT_EQ(GetLastError(), ERROR_FILE_EXISTS);

    // OPEN_EXISTING with existing file
    EXPECT_TRUE(
        check_create_disposition(file_path, OPEN_EXISTING));

    ASSERT_FALSE(fs::exists(non_existant_file));
    // OPEN_EXISTING with non-existant file
    EXPECT_FALSE(
        check_create_disposition(non_existant_file, OPEN_EXISTING));
    EXPECT_EQ(GetLastError(), ERROR_FILE_NOT_FOUND);

    // OPEN_ALWAYS with existing file
    EXPECT_TRUE(
        check_create_disposition(file_path, OPEN_ALWAYS));
    EXPECT_EQ(GetLastError(), ERROR_ALREADY_EXISTS);

    ASSERT_FALSE(fs::exists(non_existant_file));
    // OPEN_ALWAYS with non-existant file
    EXPECT_TRUE(
        check_create_disposition(non_existant_file, OPEN_ALWAYS));
    EXPECT_EQ(GetLastError(), 0);
    
    ASSERT_TRUE(fs::remove(non_existant_file));

    // TRUNCATE_EXISTING with existing file
    EXPECT_TRUE(
        check_create_disposition(file_path, TRUNCATE_EXISTING));

    // TRUNCATE_EXISTING with non-existant file
    EXPECT_FALSE(
        check_create_disposition(non_existant_file,
                                 TRUNCATE_EXISTING));
    EXPECT_EQ(GetLastError(), ERROR_FILE_NOT_FOUND);

    // clean-up
    ASSERT_TRUE(fs::remove(file_path));
}