testdatapaths.cpp
1 //------------------------------------------------------------------------------
2 // testdatapaths.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 "testdatapaths.h"
24 #include "datapaths.h"
25 #include "strings.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  }
79  while (bEntryDoesExist == true);
80 
81  QString randomDirPath = appDataDirPath + "/" + randomDirName;
82 
83  testLog << "Attempting to create directory:";
84  testLog << randomDirPath;
85 
86  if (!appDataDir.mkdir(randomDirName))
87  {
88  testLog << "Failed.";
89  return false;
90  }
91 
92  QString testFilePath = randomDirPath + "/" + "tmpfile.tmp";
93  QFile testFile(testFilePath);
94 
95  testLog << "Attempting to open following file for writing:";
96  testLog << testFilePath;
97 
98  bool bReturnValue = true;
99 
100  if (!testFile.open(QIODevice::WriteOnly))
101  {
102  testLog << "Failed.";
103  bReturnValue = false;
104  }
105  else
106  {
107  testFile.close();
108  testFile.remove();
109  }
110 
111  // Clean up!
112  appDataDir.rmdir(randomDirName);
113 
114  return bReturnValue;
115 }
Base class for Test Units.
Definition: testbase.h:42
Represents directories used by Doomseeker to store data.
Definition: datapaths.h:43
static QString createRandomAlphaNumericString(unsigned numChars)
Creates a random string with specified length.
Definition: strings.cpp:169