2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (c) 2012 Valient Gough
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
2013-10-20 00:35:26 +02:00
|
|
|
* later version.
|
2013-01-29 04:07:54 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "fs/testing.h"
|
|
|
|
|
|
|
|
#include "cipher/MemoryPool.h"
|
|
|
|
|
|
|
|
#include "fs/CipherFileIO.h"
|
|
|
|
#include "fs/FileUtils.h"
|
|
|
|
#include "fs/FSConfig.h"
|
|
|
|
#include "fs/MACFileIO.h"
|
|
|
|
#include "fs/MemFileIO.h"
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
using namespace encfs;
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(MemIOTest, BasicIO) {
|
|
|
|
MemFileIO io(1024);
|
|
|
|
ASSERT_EQ(1024, io.getSize());
|
|
|
|
|
|
|
|
MemBlock mb;
|
|
|
|
mb.allocate(256);
|
|
|
|
|
|
|
|
IORequest req;
|
|
|
|
req.offset = 0;
|
|
|
|
req.data = mb.data;
|
|
|
|
req.dataLen = 256;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
req.offset = i * 256;
|
|
|
|
memset(req.data, 0, req.dataLen);
|
|
|
|
ASSERT_TRUE(io.write(req));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
req.offset = i * 256;
|
|
|
|
ASSERT_EQ(req.dataLen, io.read(req));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testMacIO(FSConfigPtr& cfg) {
|
|
|
|
shared_ptr<MemFileIO> base(new MemFileIO(0));
|
|
|
|
shared_ptr<MACFileIO> test(new MACFileIO(base, cfg));
|
|
|
|
|
|
|
|
shared_ptr<MemFileIO> dup(new MemFileIO(0));
|
|
|
|
comparisonTest(cfg, test.get(), dup.get());
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
TEST(IOTest, NullMacIO) { runWithCipher("Null", 512, testMacIO); }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
TEST(IOTest, MacIO) { runWithAllCiphers(testMacIO); }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-03-05 07:39:51 +01:00
|
|
|
void testBasicCipherIO(FSConfigPtr& cfg) {
|
|
|
|
shared_ptr<MemFileIO> base(new MemFileIO(0));
|
|
|
|
shared_ptr<CipherFileIO> test(new CipherFileIO(base, cfg));
|
|
|
|
|
|
|
|
byte buf[1024];
|
|
|
|
cfg->cipher->pseudoRandomize(buf, sizeof(buf));
|
|
|
|
|
|
|
|
IORequest req;
|
|
|
|
req.data = new byte[sizeof(buf)];
|
|
|
|
req.offset = 0;
|
|
|
|
req.dataLen = sizeof(buf);
|
|
|
|
|
|
|
|
memcpy(req.data, buf, sizeof(buf));
|
|
|
|
ASSERT_TRUE(test->write(req));
|
|
|
|
|
|
|
|
memset(req.data, 0, sizeof(buf));
|
|
|
|
ASSERT_EQ(req.dataLen, test->read(req));
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < sizeof(buf); ++i) {
|
|
|
|
bool match = (buf[i] == req.data[i]);
|
|
|
|
ASSERT_TRUE(match) << "mismatched data at offset " << i;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (!match) break;
|
2013-03-05 07:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] req.data;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
TEST(IOTest, BasicCipherFileIO) { runWithAllCiphers(testBasicCipherIO); }
|
2013-03-05 07:39:51 +01:00
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
void testCipherIO(FSConfigPtr& cfg) {
|
|
|
|
shared_ptr<MemFileIO> base(new MemFileIO(0));
|
|
|
|
shared_ptr<CipherFileIO> test(new CipherFileIO(base, cfg));
|
|
|
|
|
|
|
|
shared_ptr<MemFileIO> dup(new MemFileIO(0));
|
|
|
|
comparisonTest(cfg, test.get(), dup.get());
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
TEST(IOTest, NullCipherFileIO) { runWithCipher("Null", 512, testCipherIO); }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
TEST(IOTest, CipherFileIO) { runWithAllCiphers(testCipherIO); }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
} // namespace
|