1 #include "systemTests.h"
2 #include <system/GTFile.h>
3
4 using namespace HI;
5
TEST_CLASS_DEFINITION(FilePermissionTest)6 TEST_CLASS_DEFINITION(FilePermissionTest) {
7 QDir dir("TestFile.dir");
8 bool mk = dir.mkdir(".");
9 qDebug() << "created dir: " << mk;
10 QString testDir = dir.path();
11
12 // create a file and check we can write to it
13 QString f1(testDir + "/" + "testfile1");
14 GTFile::create(os, f1);
15 qint64 f1_size = GTFile::readAll(os, f1).size();
16 QFile ff1(f1);
17 qDebug() << "Initial testfile1 perms " << ff1.permissions();
18 bool res = ff1.open(QFile::ReadWrite);
19 CHECK_SET_ERR(res, "cannot open testfile1 to write");
20 res = (5 == ff1.write("aaaaa"));
21 ff1.close();
22 CHECK_SET_ERR(res, "cannot write to testfile1");
23 f1_size = GTFile::getSize(os, f1);
24 CHECK_SET_ERR(f1_size == 5, "wrong size of testfile1");
25
26 // set the file RO and check we cannot modify it
27 // GTFile::setReadOnly(os, f1); TODO: restore after merging with master
28 qDebug() << "RO testfile1 perms " << ff1.permissions();
29 res = ff1.open(QFile::ReadWrite);
30 CHECK_SET_ERR(!res, "should not open testfile1 to write");
31 res = (-1 == ff1.write("bb"));
32 ff1.close();
33 CHECK_SET_ERR(res, "should not write to testfile1");
34 f1_size = GTFile::getSize(os, f1);
35 CHECK_SET_ERR(f1_size == 5, "wrong size of testfile1");
36
37 // set the file RW and check we can modify it
38 GTFile::setReadWrite(os, f1);
39 qDebug() << "RW testfile1 perms " << ff1.permissions();
40 QFile ff2(f1);
41 res = ff2.open(QFile::Append);
42 CHECK_SET_ERR(res, "should open testfile1 to write");
43 res = (3 == ff2.write("ccc"));
44 ff2.close();
45 CHECK_SET_ERR(res, "should write to testfile1");
46 f1_size = GTFile::getSize(os, f1);
47 CHECK_SET_ERR(f1_size == 8, "wrong size of testfile1");
48
49 // set the file RO and check we can remove it
50 // TODO this does not work on Windows due to RO attr set
51 // GTFile::setReadOnly(os, f1);
52 // res = ff1.remove();
53 // CHECK_SET_ERR(res, "failed to delete RO testfile1");
54 }
55
TEST_CLASS_DEFINITION(DirPermissionTest)56 TEST_CLASS_DEFINITION(DirPermissionTest) {
57 QDir dir("TestDir.dir");
58 bool mk = dir.mkdir(".");
59 qDebug() << "created dir: " << mk;
60 QString testDir = dir.path();
61
62 // check we can create a file in it
63 QString f1(testDir + "/" + "testfile1");
64 GTFile::create(os, f1);
65
66 // set the dir RO and check we can read the file
67 // GTFile::setReadOnly(os, testDir); TODO: restore after merging with master
68 QFile ff1(f1);
69 qDebug() << "Initial testfile1 perms " << ff1.permissions();
70 qint64 f1_size = GTFile::readAll(os, f1).size();
71 CHECK_SET_ERR(f1_size == 0, "cannot access testfile1");
72
73 // but cannot create a new file
74 QString f2(testDir + "/" + "testfile2");
75 bool res = QFile(f2).open(QIODevice::WriteOnly);
76 CHECK_SET_ERR(!res, "should not create new file");
77
78 // set the dir RW and check we can create the new file again
79 GTFile::setReadWrite(os, testDir);
80 GTFile::create(os, f2);
81
82 // // set the dir RO and check we can delete it
83 // GTFile::setReadOnly(os, testDir);
84 }
85