| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
FileTests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the file implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <cppunit/extensions/HelperMacros.h> |
| 36 |
#include "FileTests.h" |
| 37 |
#include "File.h" |
| 38 |
#include "Directory.h" |
| 39 |
#include "CryptoFactory.h" |
| 40 |
#include "RNG.h" |
| 41 |
|
| 42 |
CPPUNIT_TEST_SUITE_REGISTRATION(FileTests); |
| 43 |
|
| 44 |
// FIXME: all pathnames in this file are *NIX/BSD specific |
| 45 |
|
| 46 |
void FileTests::setUp() |
| 47 |
{ |
| 48 |
#ifndef _WIN32 |
| 49 |
int rv = system("rm -rf testdir"); |
| 50 |
#else |
| 51 |
int rv = system("rmdir /s /q testdir 2> nul"); |
| 52 |
#endif |
| 53 |
(void) rv; |
| 54 |
|
| 55 |
CPPUNIT_ASSERT(!system("mkdir testdir")); |
| 56 |
} |
| 57 |
|
| 58 |
void FileTests::tearDown() |
| 59 |
{ |
| 60 |
#ifndef _WIN32 |
| 61 |
CPPUNIT_ASSERT(!system("rm -rf testdir")); |
| 62 |
#else |
| 63 |
CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul")); |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
void FileTests::testExistNotExist() |
| 68 |
{ |
| 69 |
// Test pre-condition |
| 70 |
CPPUNIT_ASSERT(!exists("nonExistentFile")); |
| 71 |
|
| 72 |
// Attempt to open a file known not to exist |
| 73 |
#ifndef _WIN32 |
| 74 |
File doesntExist("testdir/nonExistentFile"); |
| 75 |
#else |
| 76 |
File doesntExist("testdir\\nonExistentFile"); |
| 77 |
#endif |
| 78 |
|
| 79 |
CPPUNIT_ASSERT(!doesntExist.isValid()); |
| 80 |
|
| 81 |
// Attempt to open a file known to exist |
| 82 |
#ifndef _WIN32 |
| 83 |
CPPUNIT_ASSERT(!system("echo someStuff > testdir/existingFile")); |
| 84 |
#else |
| 85 |
CPPUNIT_ASSERT(!system("echo someStuff > testdir\\existingFile")); |
| 86 |
#endif |
| 87 |
CPPUNIT_ASSERT(exists("existingFile")); |
| 88 |
|
| 89 |
#ifndef _WIN32 |
| 90 |
File exists("testdir/existingFile"); |
| 91 |
#else |
| 92 |
File exists("testdir\\existingFile"); |
| 93 |
#endif |
| 94 |
|
| 95 |
CPPUNIT_ASSERT(exists.isValid()); |
| 96 |
} |
| 97 |
|
| 98 |
void FileTests::testCreateNotCreate() |
| 99 |
{ |
| 100 |
// Test pre-condition |
| 101 |
CPPUNIT_ASSERT(!exists("nonExistentFile")); |
| 102 |
CPPUNIT_ASSERT(!exists("nonExistentFile2")); |
| 103 |
|
| 104 |
// Attempt to open a file known not to exist |
| 105 |
#ifndef _WIN32 |
| 106 |
File doesntExist("testdir/nonExistentFile", true, true, false); |
| 107 |
#else |
| 108 |
File doesntExist("testdir\\nonExistentFile", true, true, false); |
| 109 |
#endif |
| 110 |
|
| 111 |
CPPUNIT_ASSERT(!doesntExist.isValid()); |
| 112 |
CPPUNIT_ASSERT(!exists("nonExistentFile")); |
| 113 |
|
| 114 |
// Attempt to open a file known not to exist in create mode |
| 115 |
#ifndef _WIN32 |
| 116 |
File willBeCreated("testdir/nonExistentFile2", true, true, true); |
| 117 |
#else |
| 118 |
File willBeCreated("testdir\\nonExistentFile2", true, true, true); |
| 119 |
#endif |
| 120 |
|
| 121 |
CPPUNIT_ASSERT(willBeCreated.isValid()); |
| 122 |
CPPUNIT_ASSERT(exists("nonExistentFile2")); |
| 123 |
} |
| 124 |
|
| 125 |
void FileTests::testLockUnlock() |
| 126 |
{ |
| 127 |
// Create pre-condition |
| 128 |
#ifndef _WIN32 |
| 129 |
CPPUNIT_ASSERT(!system("echo someStuff > testdir/existingFile")); |
| 130 |
#else |
| 131 |
CPPUNIT_ASSERT(!system("echo someStuff > testdir\\existingFile")); |
| 132 |
#endif |
| 133 |
CPPUNIT_ASSERT(exists("existingFile")); |
| 134 |
|
| 135 |
#ifndef _WIN32 |
| 136 |
File file1("testdir/existingFile"); |
| 137 |
File file2("testdir/existingFile"); |
| 138 |
#else |
| 139 |
File file1("testdir\\existingFile"); |
| 140 |
File file2("testdir\\existingFile"); |
| 141 |
#endif |
| 142 |
|
| 143 |
CPPUNIT_ASSERT(file1.lock(false)); |
| 144 |
CPPUNIT_ASSERT(!file1.lock(false)); |
| 145 |
CPPUNIT_ASSERT(file2.lock(false)); |
| 146 |
CPPUNIT_ASSERT(file2.unlock()); |
| 147 |
CPPUNIT_ASSERT(file1.unlock()); |
| 148 |
CPPUNIT_ASSERT(file1.lock()); |
| 149 |
CPPUNIT_ASSERT(file2.lock()); |
| 150 |
CPPUNIT_ASSERT(file2.unlock()); |
| 151 |
CPPUNIT_ASSERT(file1.unlock()); |
| 152 |
} |
| 153 |
|
| 154 |
void FileTests::testWriteRead() |
| 155 |
{ |
| 156 |
// Generate some test data |
| 157 |
RNG* rng = CryptoFactory::i()->getRNG(); |
| 158 |
|
| 159 |
ByteString testData1; |
| 160 |
|
| 161 |
CPPUNIT_ASSERT(rng->generateRandom(testData1, 187)); |
| 162 |
|
| 163 |
// More test data |
| 164 |
std::string testString = "This is a test of the File class"; |
| 165 |
std::set<CK_MECHANISM_TYPE> testSet; |
| 166 |
testSet.insert(CKM_RSA_PKCS); |
| 167 |
testSet.insert(CKM_SHA256_RSA_PKCS); |
| 168 |
|
| 169 |
// Create a file for writing |
| 170 |
{ |
| 171 |
#ifndef _WIN32 |
| 172 |
File newFile("testdir/newFile", false, true); |
| 173 |
#else |
| 174 |
File newFile("testdir\\newFile", false, true); |
| 175 |
#endif |
| 176 |
|
| 177 |
CPPUNIT_ASSERT(newFile.isValid()); |
| 178 |
|
| 179 |
// Write two booleans into the file |
| 180 |
CPPUNIT_ASSERT(newFile.writeBool(true)); |
| 181 |
CPPUNIT_ASSERT(newFile.writeBool(false)); |
| 182 |
|
| 183 |
// Write an ulong into the file |
| 184 |
CPPUNIT_ASSERT(newFile.writeULong(0x12345678)); |
| 185 |
|
| 186 |
// Write a ByteString into the file |
| 187 |
CPPUNIT_ASSERT(newFile.writeByteString(testData1)); |
| 188 |
|
| 189 |
// Write a string into the file |
| 190 |
CPPUNIT_ASSERT(newFile.writeString(testString)); |
| 191 |
|
| 192 |
// Write a set into the file |
| 193 |
CPPUNIT_ASSERT(newFile.writeMechanismTypeSet(testSet)); |
| 194 |
} |
| 195 |
|
| 196 |
CPPUNIT_ASSERT(exists("newFile")); |
| 197 |
|
| 198 |
// Read the created file back |
| 199 |
{ |
| 200 |
#ifndef _WIN32 |
| 201 |
File newFile("testdir/newFile"); |
| 202 |
#else |
| 203 |
File newFile("testdir\\newFile"); |
| 204 |
#endif |
| 205 |
|
| 206 |
CPPUNIT_ASSERT(newFile.isValid()); |
| 207 |
|
| 208 |
// Read back the two booleans |
| 209 |
bool b1, b2; |
| 210 |
|
| 211 |
CPPUNIT_ASSERT(newFile.readBool(b1) && newFile.readBool(b2)); |
| 212 |
CPPUNIT_ASSERT(b1 && !b2); |
| 213 |
|
| 214 |
// Read back the ulong |
| 215 |
unsigned long ulongValue; |
| 216 |
|
| 217 |
CPPUNIT_ASSERT(newFile.readULong(ulongValue)); |
| 218 |
CPPUNIT_ASSERT(ulongValue == 0x12345678); |
| 219 |
|
| 220 |
// Read back the byte string |
| 221 |
ByteString bsValue; |
| 222 |
|
| 223 |
CPPUNIT_ASSERT(newFile.readByteString(bsValue)); |
| 224 |
CPPUNIT_ASSERT(bsValue == testData1); |
| 225 |
|
| 226 |
// Read back the string value |
| 227 |
std::string stringVal; |
| 228 |
|
| 229 |
CPPUNIT_ASSERT(newFile.readString(stringVal)); |
| 230 |
CPPUNIT_ASSERT(!testString.compare(stringVal)); |
| 231 |
|
| 232 |
// Read back the set value |
| 233 |
std::set<CK_MECHANISM_TYPE> setVal; |
| 234 |
|
| 235 |
CPPUNIT_ASSERT(newFile.readMechanismTypeSet(setVal)); |
| 236 |
CPPUNIT_ASSERT(setVal == testSet); |
| 237 |
|
| 238 |
// Check for EOF |
| 239 |
CPPUNIT_ASSERT(!newFile.readBool(b1)); |
| 240 |
CPPUNIT_ASSERT(newFile.isEOF()); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
void FileTests::testSeek() |
| 245 |
{ |
| 246 |
ByteString t1 = "112233445566778899"; // 9 long |
| 247 |
ByteString t2 = "AABBCCDDEEFFAABBCCDDEEFF"; // 12 long |
| 248 |
|
| 249 |
{ |
| 250 |
// Create the test file |
| 251 |
#ifndef _WIN32 |
| 252 |
File testFile("testdir/testFile", false, true, true); |
| 253 |
#else |
| 254 |
File testFile("testdir\\testFile", false, true, true); |
| 255 |
#endif |
| 256 |
|
| 257 |
CPPUNIT_ASSERT(testFile.isValid()); |
| 258 |
|
| 259 |
// Write the test data to the test file |
| 260 |
CPPUNIT_ASSERT(testFile.writeByteString(t1) && testFile.writeByteString(t2)); |
| 261 |
} |
| 262 |
|
| 263 |
// Open the test file for reading |
| 264 |
#ifndef _WIN32 |
| 265 |
File testFile("testdir/testFile"); |
| 266 |
#else |
| 267 |
File testFile("testdir\\testFile"); |
| 268 |
#endif |
| 269 |
|
| 270 |
CPPUNIT_ASSERT(testFile.isValid()); |
| 271 |
|
| 272 |
// First, read back the test data |
| 273 |
ByteString tr1, tr2; |
| 274 |
|
| 275 |
CPPUNIT_ASSERT(testFile.readByteString(tr1) && testFile.readByteString(tr2)); |
| 276 |
CPPUNIT_ASSERT(tr1 == t1); |
| 277 |
CPPUNIT_ASSERT(tr2 == t2); |
| 278 |
|
| 279 |
// Seek to the length field of the second byte string |
| 280 |
CPPUNIT_ASSERT(testFile.seek(8+9)); |
| 281 |
|
| 282 |
// Read back the size as an ulong value |
| 283 |
unsigned long value; |
| 284 |
unsigned long expectedValue = (unsigned long)0x1122334455667788ULL; |
| 285 |
|
| 286 |
CPPUNIT_ASSERT(testFile.readULong(value)); |
| 287 |
CPPUNIT_ASSERT(value == 12); |
| 288 |
|
| 289 |
// Seek to the start of the first byte string's data |
| 290 |
CPPUNIT_ASSERT(testFile.seek(8)); |
| 291 |
|
| 292 |
// Read back the ulong value stored there |
| 293 |
CPPUNIT_ASSERT(testFile.readULong(value)); |
| 294 |
|
| 295 |
CPPUNIT_ASSERT(value == expectedValue); |
| 296 |
|
| 297 |
// Seek to the start of second byte string |
| 298 |
CPPUNIT_ASSERT(testFile.seek(8+9)); |
| 299 |
|
| 300 |
// Read it |
| 301 |
ByteString trr2; |
| 302 |
|
| 303 |
CPPUNIT_ASSERT(testFile.readByteString(trr2)); |
| 304 |
CPPUNIT_ASSERT(trr2 == t2); |
| 305 |
|
| 306 |
// Rewind the file |
| 307 |
CPPUNIT_ASSERT(testFile.rewind()); |
| 308 |
|
| 309 |
// Read back both byte strings |
| 310 |
ByteString trrr1, trrr2; |
| 311 |
|
| 312 |
CPPUNIT_ASSERT(testFile.readByteString(trrr1) && testFile.readByteString(trrr2)); |
| 313 |
CPPUNIT_ASSERT(trrr1 == t1); |
| 314 |
CPPUNIT_ASSERT(trrr2 == t2); |
| 315 |
} |
| 316 |
|
| 317 |
bool FileTests::exists(std::string name) |
| 318 |
{ |
| 319 |
#ifndef _WIN32 |
| 320 |
Directory dir("./testdir"); |
| 321 |
#else |
| 322 |
Directory dir(".\\testdir"); |
| 323 |
#endif |
| 324 |
|
| 325 |
|
| 326 |
CPPUNIT_ASSERT(dir.isValid()); |
| 327 |
|
| 328 |
std::vector<std::string> files = dir.getFiles(); |
| 329 |
|
| 330 |
for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); i++) |
| 331 |
{ |
| 332 |
if (!i->compare(name)) |
| 333 |
{ |
| 334 |
return true; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
return false; |
| 339 |
} |
| 340 |
|