testdatapaths.cpp
1 //------------------------------------------------------------------------------
2 // testdatapaths.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "datapaths.h"
24 #include "strings.hpp"
25 #include "testdatapaths.h"
26 
27 TestDataPathsAppDataDirectoryAccess::TestDataPathsAppDataDirectoryAccess(bool bPortable)
28  : TestUnitBase("Data Paths - App Data directory access")
29 {
30  if (bPortable)
31  {
32  _testName += " - portable";
33  }
34 
35  this->bPortable = bPortable;
36 }
37 
38 bool TestDataPathsAppDataDirectoryAccess::executeTest()
39 {
40  DataPaths dataPaths(bPortable);
41 
42  QString appDataDir = dataPaths.systemAppDataDirectory();
43 
44  testLog << QString("App Data directory: %1").arg(appDataDir);
45 
46  return dataPaths.validateAppDataDirectory();
47 }
48 
50 
51 TestDataPathsAppDataDirectoryWrite::TestDataPathsAppDataDirectoryWrite(bool bPortable)
52  : TestUnitBase("Data Paths - App Data directory write")
53 {
54  if (bPortable)
55  {
56  _testName += " - portable";
57  }
58 
59  this->bPortable = bPortable;
60 }
61 
62 bool TestDataPathsAppDataDirectoryWrite::executeTest()
63 {
64  DataPaths dataPaths(bPortable);
65 
66  QString appDataDirPath = dataPaths.systemAppDataDirectory();
67  QDir appDataDir(appDataDirPath);
68 
69  QString randomDirName;
70  bool bEntryDoesExist = false;
71  do
72  {
73  // Continue generating random directory names as long as we don't get
74  // a directory name that doesn't exist. With 32 characters this should
75  // be quite easy.
76  randomDirName = Strings::createRandomAlphaNumericString(32);
77  bEntryDoesExist = appDataDir.exists(randomDirName);
78  } while (bEntryDoesExist == true);
79 
80  QString randomDirPath = appDataDirPath + "/" + randomDirName;
81 
82  testLog << "Attempting to create directory:";
83  testLog << randomDirPath;
84 
85  if (!appDataDir.mkdir(randomDirName))
86  {
87  testLog << "Failed.";
88  return false;
89  }
90 
91  QString testFilePath = randomDirPath + "/" + "tmpfile.tmp";
92  QFile testFile(testFilePath);
93 
94  testLog << "Attempting to open following file for writing:";
95  testLog << testFilePath;
96 
97  bool bReturnValue = true;
98 
99  if (!testFile.open(QIODevice::WriteOnly))
100  {
101  testLog << "Failed.";
102  bReturnValue = false;
103  }
104  else
105  {
106  testFile.close();
107  testFile.remove();
108  }
109 
110  // Clean up!
111  appDataDir.rmdir(randomDirName);
112 
113  return bReturnValue;
114 }