Project

General

Profile

Patch #162 ยป linux.diff

dyblast dyblast, 03/29/2011 08:27 PM

View differences:

application/Common/Global.cpp
23 23

  
24 24
#include <QDir>
25 25
#include <QDirIterator>
26
#include <qDebug>
26
#include <QDebug>
27 27
#include <QtGlobal>
28 28

  
29 29
#ifdef Q_OS_WIN32
application/Common/TestsCommon/Tests.cpp
1
/**
2
  * D-LAN - A decentralized LAN file sharing software.
3
  * Copyright (C) 2010-2011 Greg Burri <greg.burri@gmail.com>
4
  *
5
  * This program is free software: you can redistribute it and/or modify
6
  * it under the terms of the GNU General Public License as published by
7
  * the Free Software Foundation, either version 3 of the License, or
8
  * (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, see <http://www.gnu.org/licenses/>.
17
  */
18
  
19
#include <Tests.h>
20

  
21
#include <QtDebug>
22
#include <QByteArray>
23
#include <QFile>
24
#include <QDir>
25
#include <QElapsedTimer>
26

  
27
#include <Protos/common.pb.h>
28
#include <Protos/core_settings.pb.h>
29

  
30
#include <Network/MessageHeader.h>
31
#include <PersistentData.h>
32
#include <Settings.h>
33
#include <Global.h>
34
#include <ZeroCopyStreamQIODevice.h>
35
using namespace Common;
36

  
37
Tests::Tests()
38
{
39
}
40

  
41
void Tests::initTestCase()
42
{
43
   qDebug() << "Application folder path (where the settings and persistent data are put) : " << Global::getDataFolder(Common::Global::ROAMING, false);
44
}
45

  
46
void Tests::nCombinations()
47
{
48
   QCOMPARE(Global::nCombinations(5, 4), 5);
49
   QCOMPARE(Global::nCombinations(4, 2), 6);
50
   QCOMPARE(Global::nCombinations(4, 4), 1);
51
   QCOMPARE(Global::nCombinations(42, 6), 5245786);
52

  
53
   QCOMPARE(Global::nCombinations(2, 4), 0);
54
   QCOMPARE(Global::nCombinations(-1, 1), 0);
55
   QCOMPARE(Global::nCombinations(1, -1), 0);
56
   QCOMPARE(Global::nCombinations(-1, -1), 0);
57
}
58

  
59
void Tests::formatByteSize()
60
{
61
   QCOMPARE(Global::formatByteSize(-42), QString("0 B"));
62
   QCOMPARE(Global::formatByteSize(0), QString("0 B"));
63
   QCOMPARE(Global::formatByteSize(42), QString("42 B"));
64
   QCOMPARE(Global::formatByteSize(1023), QString("1023 B"));
65
   QCOMPARE(Global::formatByteSize(1024), QString("1.0 KiB"));
66
   QCOMPARE(Global::formatByteSize(1484), QString("1.4 KiB"));
67
   QCOMPARE(Global::formatByteSize(1485), QString("1.5 KiB"));
68
   QCOMPARE(Global::formatByteSize(1996), QString("1.9 KiB"));
69
   QCOMPARE(Global::formatByteSize(1997), QString("2.0 KiB"));
70
   QCOMPARE(Global::formatByteSize(1024 * 1484), QString("1.4 MiB"));
71
   QCOMPARE(Global::formatByteSize(1024 * 1485), QString("1.5 MiB"));
72
   QCOMPARE(Global::formatByteSize(1024 * 1996), QString("1.9 MiB"));
73
   QCOMPARE(Global::formatByteSize(1024 * 1997), QString("2.0 MiB"));
74
   QCOMPARE(Global::formatByteSize(42LL * 1024 * 1024 * 1024 * 1024 * 1024), QString("42.0 PiB"));
75
   QCOMPARE(Global::formatByteSize(42LL * 1020 * 1024 * 1024 * 1024 * 1024), QString("41.8 PiB"));
76

  
77

  
78
   QCOMPARE(Global::formatByteSize(-42, 2), QString("0 B"));
79
   QCOMPARE(Global::formatByteSize(0, 2), QString("0 B"));
80
   QCOMPARE(Global::formatByteSize(42, 2), QString("42 B"));
81
   QCOMPARE(Global::formatByteSize(1023, 2), QString("1023 B"));
82
   QCOMPARE(Global::formatByteSize(1024, 2), QString("1.00 KiB"));
83
   QCOMPARE(Global::formatByteSize(1484, 2), QString("1.45 KiB"));
84
   QCOMPARE(Global::formatByteSize(1485, 2), QString("1.45 KiB"));
85
   QCOMPARE(Global::formatByteSize(1996, 2), QString("1.95 KiB"));
86
   QCOMPARE(Global::formatByteSize(1997, 2), QString("1.95 KiB"));
87
   QCOMPARE(Global::formatByteSize(1024 * 1484, 2), QString("1.45 MiB"));
88
   QCOMPARE(Global::formatByteSize(1024 * 1485, 2), QString("1.45 MiB"));
89
   QCOMPARE(Global::formatByteSize(1024 * 1996, 2), QString("1.95 MiB"));
90
   QCOMPARE(Global::formatByteSize(1024 * 1997, 2), QString("1.95 MiB"));
91
   QCOMPARE(Global::formatByteSize(42LL * 1024 * 1024 * 1024 * 1024 * 1024, 2), QString("42.00 PiB"));
92
   QCOMPARE(Global::formatByteSize(42LL * 1020 * 1024 * 1024 * 1024 * 1024, 2), QString("41.84 PiB"));
93
}
94

  
95
void Tests::availableDiskSpace()
96
{
97
   qDebug() << "Available disk space [Mo] : " << Global::availableDiskSpace(".") / 1024 / 1024;
98
}
99

  
100
void Tests::writePersistentData()
101
{
102
   this->hash = Hash::rand();
103
   Protos::Common::Hash hashMessage;
104
   hashMessage.set_hash(this->hash.getData(), Hash::HASH_SIZE);
105
   PersistentData::setValue("paul", hashMessage, Global::ROAMING);
106
}
107

  
108
void Tests::readPersistentData()
109
{
110
   Protos::Common::Hash hashMessage;
111
   PersistentData::getValue("paul", hashMessage, Global::ROAMING);
112
   Hash hashRead(hashMessage.hash().data());
113

  
114
   QVERIFY(this->hash == hashRead);
115

  
116
   try
117
   {
118
      PersistentData::getValue("john", hashMessage, Global::ROAMING);
119
      QFAIL("'john' shouldn't exist");
120
   }
121
   catch (UnknownValueException)
122
   {
123
      qDebug() << "Ok, exception UnknownValueException catched for the value 'john'";
124
   }
125
   catch (...)
126
   {
127
      QFAIL("Unknown exception occured");
128
   }
129
}
130

  
131
void Tests::removePersistentData()
132
{
133
   QVERIFY(PersistentData::rmValue("paul", Global::ROAMING));
134
}
135

  
136
void Tests::writeSettings()
137
{
138
   this->hash = Hash::rand();
139

  
140
   SETTINGS.setFilename("tests_core_settings.txt");
141
   SETTINGS.setSettingsMessage(new Protos::Core::Settings());
142

  
143
   SETTINGS.set("nick", QString("paul"));
144
   SETTINGS.set("peer_id", this->hash);
145
   SETTINGS.save();
146
}
147

  
148
void Tests::readSettings()
149
{
150
   SETTINGS.load();
151

  
152
   QString nick = SETTINGS.get<QString>("nick");
153
   Hash hash = SETTINGS.get<Hash>("peer_id");
154

  
155
   QCOMPARE(nick, QString("paul"));
156
   QCOMPARE(hash.toStr(), this->hash.toStr());
157
}
158

  
159
void Tests::removeSettings()
160
{
161
   SETTINGS.remove();
162
}
163

  
164
void Tests::generateAHash()
165
{
166
   const char array[Hash::HASH_SIZE] = {
167
      0x2d, 0x73, 0x73, 0x6f,
168
      0x34, 0xa7, 0x38, 0x37,
169
      0xd4, 0x22, 0xf7, 0xab,
170
      0xa2, 0x74, 0x0d, 0x84,
171
      0x09, 0xac, 0x60, 0xdf
172
   };
173
   QByteArray byteArray(array, Hash::HASH_SIZE);
174

  
175
   qDebug() << "Reference                     : " << byteArray.toHex();;
176

  
177
   Hash h1 = Hash::rand();
178
   qDebug() << "h1 (Randomly generated hash)  : " << h1.toStr();
179

  
180
   Hash h2(byteArray);
181
   qDebug() << "h2 (from QByteArray)          : " << h2.toStr();
182
   QVERIFY(memcmp(h2.getData(), array, Hash::HASH_SIZE) == 0);
183

  
184
   Hash h3(h2);
185
   qDebug() << "h3 (copied from h2)           : " << h3.toStr();
186
   QVERIFY(memcmp(h3.getData(), array, Hash::HASH_SIZE) == 0);
187

  
188
   Hash h4(array);
189
   qDebug() << "h4 (from char[])              : " << h4.toStr();
190
   QVERIFY(memcmp(h4.getData(), array, Hash::HASH_SIZE) == 0);
191
}
192

  
193
void Tests::buildAnHashFromAString()
194
{
195
   QString str("2d73736f34a73837d422f7aba2740d8409ac60df");
196
   Hash h = Hash::fromStr(str);
197
   QCOMPARE(h.toStr(), str);
198
}
199

  
200
void Tests::compareTwoHash()
201
{
202
   const char array[Hash::HASH_SIZE] = {
203
      0x2d, 0x73, 0x73, 0x6f,
204
      0x34, 0xa7, 0x38, 0x37,
205
      0xd4, 0x22, 0xf7, 0xab,
206
      0xa2, 0x74, 0x0d, 0x84,
207
      0x09, 0xac, 0x60, 0xdf
208
   };
209
   QByteArray byteArray(array, Hash::HASH_SIZE);
210
   QString str("2d73736f34a73837d422f7aba2740d8409ac60df");
211

  
212
   Hash h1 = Hash::fromStr(str);
213
   Hash h2(byteArray);
214
   Hash h3 = h1;
215
   Hash h4;
216
   h4 = h1;
217

  
218
   QVERIFY(h1 == h1);
219
   QVERIFY(h1 == h2);
220
   QVERIFY(h1 == h3);
221
   QVERIFY(h1 == h4);
222
   QVERIFY(h2 == h3);
223
   QVERIFY(h2 == h4);
224
}
225

  
226
void Tests::hasher()
227
{
228
   char str1[] = "abc";
229
   char str2[] = "abc";
230
   char str3[] = "cba";
231

  
232
   Hasher hasher;
233
   hasher.addData(str1, sizeof(str1));
234
   hasher.addData(str3, sizeof(str3));
235
   Hash h1 = hasher.getResult();
236

  
237
   hasher.reset();
238
   hasher.addData(str2, sizeof(str2));
239
   hasher.addData(str3, sizeof(str3));
240
   Hash h2 = hasher.getResult();
241

  
242
   hasher.reset();
243
   hasher.addData(str3, sizeof(str3));
244
   Hash h3 = hasher.getResult();
245

  
246
   hasher.reset();
247
   hasher.addPredefinedSalt();
248
   hasher.addData(str1, sizeof(str1));
249
   Hash h4 = hasher.getResult();
250

  
251
   hasher.reset();
252
   hasher.addPredefinedSalt();
253
   hasher.addData(str2, sizeof(str2));
254
   Hash h5 = hasher.getResult();
255

  
256
   QVERIFY(h1 == h2);
257
   QVERIFY(h1 != h3);
258
   QVERIFY(h2 != h3);
259
   QVERIFY(h4 != h1);
260
   QVERIFY(h4 == h5);
261
}
262

  
263
void Tests::messageHeader()
264
{
265
   const char data[MessageHeader::HEADER_SIZE] = {
266
      0x00, 0x00, 0x00, 0x01,
267
      0x00, 0x00, 0x00, 0x2a,
268
      0x2d, 0x73, 0x73, 0x6f,
269
      0x34, 0xa7, 0x38, 0x37,
270
      0xd4, 0x22, 0xf7, 0xab,
271
      0xa2, 0x74, 0x0d, 0x84,
272
      0x09, 0xac, 0x60, 0xdf
273
   };
274

  
275
   const QString peerID("2d73736f34a73837d422f7aba2740d8409ac60df");
276

  
277
   MessageHeader header = MessageHeader::readHeader(data);
278
   qDebug() << header.toStr();
279

  
280
   QVERIFY(!header.isNull());
281
   QCOMPARE(header.getType(), MessageHeader::CORE_IM_ALIVE);
282
   QCOMPARE(header.getSize(), 42u);
283
   QCOMPARE(header.getSenderID().toStr(), peerID);
284

  
285
   // We use a larger buffer to check if the last four bytes has been alterate.
286
   char buffer[MessageHeader::HEADER_SIZE + 4];
287
   memset(buffer, 0, sizeof(buffer));
288

  
289
   MessageHeader::writeHeader(buffer, header);
290
   QVERIFY(qstrncmp(data, buffer, MessageHeader::HEADER_SIZE) == 0);
291
   QVERIFY(qstrncmp(buffer + MessageHeader::HEADER_SIZE, "\0\0\0\0", 4) == 0);
292
}
293

  
294
void Tests::readAndWriteWithZeroCopyStreamQIODevice()
295
{
296
   QString filePath(QDir::tempPath().append("/test.bin"));
297
   QFile file(filePath);
298
   file.remove();
299

  
300
   Hash hash1 = Hash::fromStr("2c583d414e4a9eb956228209b367e48f59078a4b");
301
   Hash hash2 = Hash::fromStr("5c9c3741bded231f84b8a8200eaf3e30a9c0a951");
302

  
303
   qDebug() << "hash1 : " << hash1.toStr();
304
   qDebug() << "hash2 : " << hash2.toStr();
305

  
306
   Protos::Common::Hash hashMessage1;
307
   Protos::Common::Hash hashMessage2;
308

  
309
   file.open(QIODevice::WriteOnly);
310
   {
311
      ZeroCopyOutputStreamQIODevice outputStream(&file);
312

  
313
      hashMessage1.set_hash(hash1.getData(), Hash::HASH_SIZE);
314
      hashMessage1.SerializeToZeroCopyStream(&outputStream);
315

  
316
      hashMessage2.set_hash(hash2.getData(), Hash::HASH_SIZE);
317
      hashMessage2.SerializeToZeroCopyStream(&outputStream);
318
   }
319
   file.close();
320

  
321
   QFileInfo fileInfo(filePath);
322
   QCOMPARE(fileInfo.size(), static_cast<long long>(hashMessage1.ByteSize() + hashMessage2.ByteSize()));
323

  
324
   hashMessage1.Clear();
325
   hashMessage2.Clear();
326
   file.open(QIODevice::ReadOnly);
327
   {
328
      ZeroCopyInputStreamQIODevice inputStream(&file);
329
      hashMessage1.ParseFromBoundedZeroCopyStream(&inputStream, Hash::HASH_SIZE + 2);
330
      hashMessage2.ParseFromBoundedZeroCopyStream(&inputStream, Hash::HASH_SIZE + 2);
331
   }
332
   file.close();
333

  
334
   QCOMPARE(QByteArray(hashMessage1.hash().data(), Hash::HASH_SIZE), QByteArray(hash1.getData(), Hash::HASH_SIZE));
335
   QCOMPARE(QByteArray(hashMessage2.hash().data(), Hash::HASH_SIZE), QByteArray(hash2.getData(), Hash::HASH_SIZE));
336
}
337

  
338

  
339

  
application/Common/TestsCommon/Tests.h
1
/**
2
  * D-LAN - A decentralized LAN file sharing software.
3
  * Copyright (C) 2010-2011 Greg Burri <greg.burri@gmail.com>
4
  *
5
  * This program is free software: you can redistribute it and/or modify
6
  * it under the terms of the GNU General Public License as published by
7
  * the Free Software Foundation, either version 3 of the License, or
8
  * (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, see <http://www.gnu.org/licenses/>.
17
  */
18
  
19
#ifndef TESTS_COMMON_H
20
#define TESTS_COMMON_H
21

  
22
#include <QTest>
23

  
24
#include <Hash.h>
25

  
26
class Tests : public QObject
27
{
28
   Q_OBJECT
29
public:
30
   Tests();
31

  
32
private slots:
33
   void initTestCase();
34

  
35
   // Global class.
36
   void nCombinations();
37
   void formatByteSize();
38
   void availableDiskSpace();
39

  
40
   // PersistentData class.
41
   void writePersistentData();
42
   void readPersistentData();
43
   void removePersistentData();
44

  
45
   // Settings class.
46
   void writeSettings();
47
   void readSettings();
48
   void removeSettings();
49

  
50
   // Hash class.
51
   void generateAHash();
52
   void buildAnHashFromAString();
53
   void compareTwoHash();
54
   void hasher();
55

  
56
   void messageHeader();
57

  
58
   // ZeroCopyOutputStreamQIODevice and ZeroCopyInputStreamQIODevice classes.
59
   void readAndWriteWithZeroCopyStreamQIODevice();
60

  
61
private:
62
   Common::Hash hash;
63
};
64

  
65
#endif
application/Common/TestsCommon/TestsCommon.pro
1
# -------------------------------------------------
2
# Project created by QtCreator 2009-10-04T02:24:09
3
# -------------------------------------------------
4
QT += testlib
5
QT -= gui
6
TARGET = TestsCommon
7

  
8
CONFIG += link_prl
9

  
10
include(../common.pri)
11
include(../../Libs/protobuf.pri)
12

  
13
LIBS += -L"../output/$$FOLDER" -lCommon
14
POST_TARGETDEPS += ../output/$$FOLDER/libCommon.a
15

  
16
INCLUDEPATH += . \
17
   .. \
18
   ../..
19

  
20
CONFIG += console
21
CONFIG -= app_bundle
22
TEMPLATE = app
23
SOURCES += main.cpp \
24
    Tests.cpp \
25
    ../../Protos/common.pb.cc \
26
    ../../Protos/core_settings.pb.cc
27
HEADERS += Tests.h
application/Common/TestsCommon/main.cpp
1
/**
2
  * D-LAN - A decentralized LAN file sharing software.
3
  * Copyright (C) 2010-2011 Greg Burri <greg.burri@gmail.com>
4
  *
5
  * This program is free software: you can redistribute it and/or modify
6
  * it under the terms of the GNU General Public License as published by
7
  * the Free Software Foundation, either version 3 of the License, or
8
  * (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, see <http://www.gnu.org/licenses/>.
17
  */
18
  
19
#include <QCoreApplication>
20
#include <QTest>
21

  
22
#include <Tests.h>
23

  
24
int main(int argc, char *argv[])
25
{
26
   Tests tests;
27
   return QTest::qExec(&tests, argc, argv);
28
}
application/Common/Version.h
23 23
#define VERSION_TAG "Beta10"
24 24

  
25 25
// These two values are automatically updated during the release building process. See the script 'Application/Tools/update_version.sh'
26
#define BUILD_TIME "2011-03-16_18-02"
27
#define GIT_VERSION "d95667c422c53c67353299de443bfa4ad905a162"
26
#define BUILD_TIME "2011-03-28_13-03"
27
#define GIT_VERSION "57073952baf03b969debc1c36411eb755a03e2ca"
28 28

  
29 29
#endif
/dev/null
1
/**
2
  * D-LAN - A decentralized LAN file sharing software.
3
  * Copyright (C) 2010-2011 Greg Burri <greg.burri@gmail.com>
4
  *
5
  * This program is free software: you can redistribute it and/or modify
6
  * it under the terms of the GNU General Public License as published by
7
  * the Free Software Foundation, either version 3 of the License, or
8
  * (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, see <http://www.gnu.org/licenses/>.
17
  */
18
  
19
#include <Tests.h>
20

  
21
#include <QtDebug>
22
#include <QByteArray>
23
#include <QFile>
24
#include <QDir>
25
#include <QElapsedTimer>
26

  
27
#include <Protos/common.pb.h>
28
#include <Protos/core_settings.pb.h>
29

  
30
#include <Network/MessageHeader.h>
31
#include <PersistentData.h>
32
#include <Settings.h>
33
#include <Global.h>
34
#include <ZeroCopyStreamQIODevice.h>
35
using namespace Common;
36

  
37
Tests::Tests()
38
{
39
}
40

  
41
void Tests::initTestCase()
42
{
43
   qDebug() << "Application folder path (where the settings and persistent data are put) : " << Global::getDataFolder(Common::Global::ROAMING, false);
44
}
45

  
46
void Tests::nCombinations()
47
{
48
   QCOMPARE(Global::nCombinations(5, 4), 5);
49
   QCOMPARE(Global::nCombinations(4, 2), 6);
50
   QCOMPARE(Global::nCombinations(4, 4), 1);
51
   QCOMPARE(Global::nCombinations(42, 6), 5245786);
52

  
53
   QCOMPARE(Global::nCombinations(2, 4), 0);
54
   QCOMPARE(Global::nCombinations(-1, 1), 0);
55
   QCOMPARE(Global::nCombinations(1, -1), 0);
56
   QCOMPARE(Global::nCombinations(-1, -1), 0);
57
}
58

  
59
void Tests::formatByteSize()
60
{
61
   QCOMPARE(Global::formatByteSize(-42), QString("0 B"));
62
   QCOMPARE(Global::formatByteSize(0), QString("0 B"));
63
   QCOMPARE(Global::formatByteSize(42), QString("42 B"));
64
   QCOMPARE(Global::formatByteSize(1023), QString("1023 B"));
65
   QCOMPARE(Global::formatByteSize(1024), QString("1.0 KiB"));
66
   QCOMPARE(Global::formatByteSize(1484), QString("1.4 KiB"));
67
   QCOMPARE(Global::formatByteSize(1485), QString("1.5 KiB"));
68
   QCOMPARE(Global::formatByteSize(1996), QString("1.9 KiB"));
69
   QCOMPARE(Global::formatByteSize(1997), QString("2.0 KiB"));
70
   QCOMPARE(Global::formatByteSize(1024 * 1484), QString("1.4 MiB"));
71
   QCOMPARE(Global::formatByteSize(1024 * 1485), QString("1.5 MiB"));
72
   QCOMPARE(Global::formatByteSize(1024 * 1996), QString("1.9 MiB"));
73
   QCOMPARE(Global::formatByteSize(1024 * 1997), QString("2.0 MiB"));
74
   QCOMPARE(Global::formatByteSize(42LL * 1024 * 1024 * 1024 * 1024 * 1024), QString("42.0 PiB"));
75
   QCOMPARE(Global::formatByteSize(42LL * 1020 * 1024 * 1024 * 1024 * 1024), QString("41.8 PiB"));
76

  
77

  
78
   QCOMPARE(Global::formatByteSize(-42, 2), QString("0 B"));
79
   QCOMPARE(Global::formatByteSize(0, 2), QString("0 B"));
80
   QCOMPARE(Global::formatByteSize(42, 2), QString("42 B"));
81
   QCOMPARE(Global::formatByteSize(1023, 2), QString("1023 B"));
82
   QCOMPARE(Global::formatByteSize(1024, 2), QString("1.00 KiB"));
83
   QCOMPARE(Global::formatByteSize(1484, 2), QString("1.45 KiB"));
84
   QCOMPARE(Global::formatByteSize(1485, 2), QString("1.45 KiB"));
85
   QCOMPARE(Global::formatByteSize(1996, 2), QString("1.95 KiB"));
86
   QCOMPARE(Global::formatByteSize(1997, 2), QString("1.95 KiB"));
87
   QCOMPARE(Global::formatByteSize(1024 * 1484, 2), QString("1.45 MiB"));
88
   QCOMPARE(Global::formatByteSize(1024 * 1485, 2), QString("1.45 MiB"));
89
   QCOMPARE(Global::formatByteSize(1024 * 1996, 2), QString("1.95 MiB"));
90
   QCOMPARE(Global::formatByteSize(1024 * 1997, 2), QString("1.95 MiB"));
91
   QCOMPARE(Global::formatByteSize(42LL * 1024 * 1024 * 1024 * 1024 * 1024, 2), QString("42.00 PiB"));
92
   QCOMPARE(Global::formatByteSize(42LL * 1020 * 1024 * 1024 * 1024 * 1024, 2), QString("41.84 PiB"));
93
}
94

  
95
void Tests::availableDiskSpace()
96
{
97
   qDebug() << "Available disk space [Mo] : " << Global::availableDiskSpace(".") / 1024 / 1024;
98
}
99

  
100
void Tests::writePersistentData()
101
{
102
   this->hash = Hash::rand();
103
   Protos::Common::Hash hashMessage;
104
   hashMessage.set_hash(this->hash.getData(), Hash::HASH_SIZE);
105
   PersistentData::setValue("paul", hashMessage, Global::ROAMING);
106
}
107

  
108
void Tests::readPersistentData()
109
{
110
   Protos::Common::Hash hashMessage;
111
   PersistentData::getValue("paul", hashMessage, Global::ROAMING);
112
   Hash hashRead(hashMessage.hash().data());
113

  
114
   QVERIFY(this->hash == hashRead);
115

  
116
   try
117
   {
118
      PersistentData::getValue("john", hashMessage, Global::ROAMING);
119
      QFAIL("'john' shouldn't exist");
120
   }
121
   catch (UnknownValueException)
122
   {
123
      qDebug() << "Ok, exception UnknownValueException catched for the value 'john'";
124
   }
125
   catch (...)
126
   {
127
      QFAIL("Unknown exception occured");
128
   }
129
}
130

  
131
void Tests::removePersistentData()
132
{
133
   QVERIFY(PersistentData::rmValue("paul", Global::ROAMING));
134
}
135

  
136
void Tests::writeSettings()
137
{
138
   this->hash = Hash::rand();
139

  
140
   SETTINGS.setFilename("tests_core_settings.txt");
141
   SETTINGS.setSettingsMessage(new Protos::Core::Settings());
142

  
143
   SETTINGS.set("nick", QString("paul"));
144
   SETTINGS.set("peer_id", this->hash);
145
   SETTINGS.save();
146
}
147

  
148
void Tests::readSettings()
149
{
150
   SETTINGS.load();
151

  
152
   QString nick = SETTINGS.get<QString>("nick");
153
   Hash hash = SETTINGS.get<Hash>("peer_id");
154

  
155
   QCOMPARE(nick, QString("paul"));
156
   QCOMPARE(hash.toStr(), this->hash.toStr());
157
}
158

  
159
void Tests::removeSettings()
160
{
161
   SETTINGS.remove();
162
}
163

  
164
void Tests::generateAHash()
165
{
166
   const char array[Hash::HASH_SIZE] = {
167
      0x2d, 0x73, 0x73, 0x6f,
168
      0x34, 0xa7, 0x38, 0x37,
169
      0xd4, 0x22, 0xf7, 0xab,
170
      0xa2, 0x74, 0x0d, 0x84,
171
      0x09, 0xac, 0x60, 0xdf
172
   };
173
   QByteArray byteArray(array, Hash::HASH_SIZE);
174

  
175
   qDebug() << "Reference                     : " << byteArray.toHex();;
176

  
177
   Hash h1 = Hash::rand();
178
   qDebug() << "h1 (Randomly generated hash)  : " << h1.toStr();
179

  
180
   Hash h2(byteArray);
181
   qDebug() << "h2 (from QByteArray)          : " << h2.toStr();
182
   QVERIFY(memcmp(h2.getData(), array, Hash::HASH_SIZE) == 0);
183

  
184
   Hash h3(h2);
185
   qDebug() << "h3 (copied from h2)           : " << h3.toStr();
186
   QVERIFY(memcmp(h3.getData(), array, Hash::HASH_SIZE) == 0);
187

  
188
   Hash h4(array);
189
   qDebug() << "h4 (from char[])              : " << h4.toStr();
190
   QVERIFY(memcmp(h4.getData(), array, Hash::HASH_SIZE) == 0);
191
}
192

  
193
void Tests::buildAnHashFromAString()
194
{
195
   QString str("2d73736f34a73837d422f7aba2740d8409ac60df");
196
   Hash h = Hash::fromStr(str);
197
   QCOMPARE(h.toStr(), str);
198
}
199

  
200
void Tests::compareTwoHash()
201
{
202
   const char array[Hash::HASH_SIZE] = {
203
      0x2d, 0x73, 0x73, 0x6f,
204
      0x34, 0xa7, 0x38, 0x37,
205
      0xd4, 0x22, 0xf7, 0xab,
206
      0xa2, 0x74, 0x0d, 0x84,
207
      0x09, 0xac, 0x60, 0xdf
208
   };
209
   QByteArray byteArray(array, Hash::HASH_SIZE);
210
   QString str("2d73736f34a73837d422f7aba2740d8409ac60df");
211

  
212
   Hash h1 = Hash::fromStr(str);
213
   Hash h2(byteArray);
214
   Hash h3 = h1;
215
   Hash h4;
216
   h4 = h1;
217

  
218
   QVERIFY(h1 == h1);
219
   QVERIFY(h1 == h2);
220
   QVERIFY(h1 == h3);
221
   QVERIFY(h1 == h4);
222
   QVERIFY(h2 == h3);
223
   QVERIFY(h2 == h4);
224
}
225

  
226
void Tests::hasher()
227
{
228
   char str1[] = "abc";
229
   char str2[] = "abc";
230
   char str3[] = "cba";
231

  
232
   Hasher hasher;
233
   hasher.addData(str1, sizeof(str1));
234
   hasher.addData(str3, sizeof(str3));
235
   Hash h1 = hasher.getResult();
236

  
237
   hasher.reset();
238
   hasher.addData(str2, sizeof(str2));
239
   hasher.addData(str3, sizeof(str3));
240
   Hash h2 = hasher.getResult();
241

  
242
   hasher.reset();
243
   hasher.addData(str3, sizeof(str3));
244
   Hash h3 = hasher.getResult();
245

  
246
   hasher.reset();
247
   hasher.addPredefinedSalt();
248
   hasher.addData(str1, sizeof(str1));
249
   Hash h4 = hasher.getResult();
250

  
251
   hasher.reset();
252
   hasher.addPredefinedSalt();
253
   hasher.addData(str2, sizeof(str2));
254
   Hash h5 = hasher.getResult();
255

  
256
   QVERIFY(h1 == h2);
257
   QVERIFY(h1 != h3);
258
   QVERIFY(h2 != h3);
259
   QVERIFY(h4 != h1);
260
   QVERIFY(h4 == h5);
261
}
262

  
263
void Tests::messageHeader()
264
{
265
   const char data[MessageHeader::HEADER_SIZE] = {
266
      0x00, 0x00, 0x00, 0x01,
267
      0x00, 0x00, 0x00, 0x2a,
268
      0x2d, 0x73, 0x73, 0x6f,
269
      0x34, 0xa7, 0x38, 0x37,
270
      0xd4, 0x22, 0xf7, 0xab,
271
      0xa2, 0x74, 0x0d, 0x84,
272
      0x09, 0xac, 0x60, 0xdf
273
   };
274

  
275
   const QString peerID("2d73736f34a73837d422f7aba2740d8409ac60df");
276

  
277
   MessageHeader header = MessageHeader::readHeader(data);
278
   qDebug() << header.toStr();
279

  
280
   QVERIFY(!header.isNull());
281
   QCOMPARE(header.getType(), MessageHeader::CORE_IM_ALIVE);
282
   QCOMPARE(header.getSize(), 42u);
283
   QCOMPARE(header.getSenderID().toStr(), peerID);
284

  
285
   // We use a larger buffer to check if the last four bytes has been alterate.
286
   char buffer[MessageHeader::HEADER_SIZE + 4];
287
   memset(buffer, 0, sizeof(buffer));
288

  
289
   MessageHeader::writeHeader(buffer, header);
290
   QVERIFY(qstrncmp(data, buffer, MessageHeader::HEADER_SIZE) == 0);
291
   QVERIFY(qstrncmp(buffer + MessageHeader::HEADER_SIZE, "\0\0\0\0", 4) == 0);
292
}
293

  
294
void Tests::readAndWriteWithZeroCopyStreamQIODevice()
295
{
296
   QString filePath(QDir::tempPath().append("/test.bin"));
297
   QFile file(filePath);
298
   file.remove();
299

  
300
   Hash hash1 = Hash::fromStr("2c583d414e4a9eb956228209b367e48f59078a4b");
301
   Hash hash2 = Hash::fromStr("5c9c3741bded231f84b8a8200eaf3e30a9c0a951");
302

  
303
   qDebug() << "hash1 : " << hash1.toStr();
304
   qDebug() << "hash2 : " << hash2.toStr();
305

  
306
   Protos::Common::Hash hashMessage1;
307
   Protos::Common::Hash hashMessage2;
308

  
309
   file.open(QIODevice::WriteOnly);
310
   {
311
      ZeroCopyOutputStreamQIODevice outputStream(&file);
312

  
313
      hashMessage1.set_hash(hash1.getData(), Hash::HASH_SIZE);
314
      hashMessage1.SerializeToZeroCopyStream(&outputStream);
315

  
316
      hashMessage2.set_hash(hash2.getData(), Hash::HASH_SIZE);
317
      hashMessage2.SerializeToZeroCopyStream(&outputStream);
318
   }
319
   file.close();
320

  
321
   QFileInfo fileInfo(filePath);
322
   QCOMPARE(fileInfo.size(), static_cast<long long>(hashMessage1.ByteSize() + hashMessage2.ByteSize()));
323

  
324
   hashMessage1.Clear();
325
   hashMessage2.Clear();
326
   file.open(QIODevice::ReadOnly);
327
   {
328
      ZeroCopyInputStreamQIODevice inputStream(&file);
329
      hashMessage1.ParseFromBoundedZeroCopyStream(&inputStream, Hash::HASH_SIZE + 2);
330
      hashMessage2.ParseFromBoundedZeroCopyStream(&inputStream, Hash::HASH_SIZE + 2);
331
   }
332
   file.close();
333

  
334
   QCOMPARE(QByteArray(hashMessage1.hash().data(), Hash::HASH_SIZE), QByteArray(hash1.getData(), Hash::HASH_SIZE));
335
   QCOMPARE(QByteArray(hashMessage2.hash().data(), Hash::HASH_SIZE), QByteArray(hash2.getData(), Hash::HASH_SIZE));
336
}
337

  
338

  
339

  
/dev/null
1
/**
2
  * D-LAN - A decentralized LAN file sharing software.
3
  * Copyright (C) 2010-2011 Greg Burri <greg.burri@gmail.com>
4
  *
5
  * This program is free software: you can redistribute it and/or modify
6
  * it under the terms of the GNU General Public License as published by
7
  * the Free Software Foundation, either version 3 of the License, or
8
  * (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, see <http://www.gnu.org/licenses/>.
17
  */
18
  
19
#ifndef TESTS_COMMON_H
20
#define TESTS_COMMON_H
21

  
22
#include <QTest>
23

  
24
#include <Hash.h>
25

  
26
class Tests : public QObject
27
{
28
   Q_OBJECT
29
public:
30
   Tests();
31

  
32
private slots:
33
   void initTestCase();
34

  
35
   // Global class.
36
   void nCombinations();
37
   void formatByteSize();
38
   void availableDiskSpace();
39

  
40
   // PersistentData class.
41
   void writePersistentData();
42
   void readPersistentData();
43
   void removePersistentData();
44

  
45
   // Settings class.
46
   void writeSettings();
47
   void readSettings();
48
   void removeSettings();
49

  
50
   // Hash class.
51
   void generateAHash();
52
   void buildAnHashFromAString();
53
   void compareTwoHash();
54
   void hasher();
55

  
56
   void messageHeader();
57

  
58
   // ZeroCopyOutputStreamQIODevice and ZeroCopyInputStreamQIODevice classes.
59
   void readAndWriteWithZeroCopyStreamQIODevice();
60

  
61
private:
62
   Common::Hash hash;
63
};
64

  
65
#endif
/dev/null
1
# -------------------------------------------------
2
# Project created by QtCreator 2009-10-04T02:24:09
3
# -------------------------------------------------
4
QT += testlib
5
QT -= gui
6
TARGET = TestsCommon
7

  
8
CONFIG += link_prl
9

  
10
include(../common.pri)
11
include(../../Libs/protobuf.pri)
12

  
13
LIBS += -L"../output/$$FOLDER" -lCommon
14
POST_TARGETDEPS += ../output/$$FOLDER/libCommon.a
15

  
16
INCLUDEPATH += . \
17
   .. \
18
   ../..
19

  
20
CONFIG += console
21
CONFIG -= app_bundle
22
TEMPLATE = app
23
SOURCES += main.cpp \
24
    Tests.cpp \
25
    ../../Protos/common.pb.cc \
26
    ../../Protos/core_settings.pb.cc
27
HEADERS += Tests.h
/dev/null
1
/**
2
  * D-LAN - A decentralized LAN file sharing software.
3
  * Copyright (C) 2010-2011 Greg Burri <greg.burri@gmail.com>
4
  *
5
  * This program is free software: you can redistribute it and/or modify
6
  * it under the terms of the GNU General Public License as published by
7
  * the Free Software Foundation, either version 3 of the License, or
8
  * (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, see <http://www.gnu.org/licenses/>.
17
  */
18
  
19
#include <QCoreApplication>
20
#include <QTest>
21

  
22
#include <Tests.h>
23

  
24
int main(int argc, char *argv[])
25
{
26
   Tests tests;
27
   return QTest::qExec(&tests, argc, argv);
28
}
application/Core/ConsoleReader.cpp
20 20
using namespace CoreSpace;
21 21

  
22 22
#include <cstdio>
23
#include <windows.h>
23

  
24
#if defined(Q_OS_LINUX)
25
#elif defined(Q_OS_WIN32)
26
   #include <windows.h>
27
#endif
24 28

  
25 29
#include <QCoreApplication>
26 30

  
application/Core/FileManager/priv/FileUpdater/DirWatcher.cpp
32 32
#if defined(Q_OS_WIN32)
33 33
   return new DirWatcherWin();
34 34
#else
35
   LOG_WARN("Cannot create a watcher for the current platform, no implementation.");
35
   L_WARN("Cannot create a watcher for the current platform, no implementation.");
36 36
   return 0;
37 37
#endif
38 38
}
application/Core/NetworkListener/priv/UDPListener.cpp
87 87
   mreq.imr_multiaddr.s_addr = htonl(MULTICAST_GROUP.toIPv4Address());
88 88
   mreq.imr_interface.s_addr = htonl(INADDR_ANY);
89 89
#if defined(Q_OS_LINUX)
90
   if (int error = setsockopt(socketDescriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof mreq))
90
   if (int error = setsockopt(multicastSocketDescriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof mreq))
91 91
#elif defined(Q_OS_WIN32)
92 92
   if (int error = setsockopt(multicastSocketDescriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mreq, sizeof mreq))
93 93
#endif
application/Core/PeerManager/priv/PeerManager.cpp
26 26
#include <Common/Settings.h>
27 27
#include <Common/Network/MessageHeader.h>
28 28

  
29
#include <Priv/Constants.h>
29
#include <priv/Constants.h>
30 30

  
31 31
/**
32 32
  * @class PM::PeerManager
application/D-LAN.pro
1
TEMPLATE = subdirs
2
CONFIG += ordered
3
SUBDIRS = Common \
4
   Common/TestsCommon \
5
   Common/LogManager \
6
   Common/RemoteCoreController \
7
   Core/FileManager \
8
   Core/FileManager/TestsFileManager \
9
   Core/PeerManager \
10
   Core/PeerManager/TestsPeerManager \
11
   Core/UploadManager \
12
   Core/DownloadManager \
13
   Core/NetworkListener \
14
   Core/RemoteControlManager \
15
   Core \
16
   GUI \
17
   Tools/LogViewer \
18
   Tools/PasswordHasher 
19

  
application/GUI/CheckBoxList.cpp
19 19
#include <CheckBoxList.h>
20 20
using namespace GUI;
21 21

  
22
#include <QCHeckBox>
22
#include <QCheckBox>
23 23
#include <QApplication>
24 24
#include <QStylePainter>
25 25
#include <QAbstractItemView>
application/configure_linux.sh
1
qmake D-LAN.pro -r -spec linux-g++
application/configure_windows.sh
1
qmake D-LAN.pro -r -spec win32-g++
    (1-1/1)