keepassxc

Форк
0
/
TestRandomGenerator.cpp 
60 строк · 1.8 Кб
1
/*
2
 *  Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "TestRandomGenerator.h"
19

20
#include "core/Global.h"
21
#include "crypto/Random.h"
22

23
#include <QTest>
24

25
QTEST_GUILESS_MAIN(TestRandomGenerator)
26

27
void TestRandomGenerator::testArray()
28
{
29
    auto ba = randomGen()->randomArray(10);
30
    QCOMPARE(ba.size(), 10);
31
    QVERIFY(ba != QByteArray(10, '\0'));
32

33
    auto ba2 = ba;
34
    randomGen()->randomize(ba2);
35
    QVERIFY(ba2 != ba);
36
}
37

38
void TestRandomGenerator::testUInt()
39
{
40
    QVERIFY(randomGen()->randomUInt(0) == 0);
41
    QVERIFY(randomGen()->randomUInt(1) == 0);
42

43
    // Run a bunch of trials creating random numbers to ensure we meet the standard
44
    for (int i = 0; i < 100; ++i) {
45
        QVERIFY(randomGen()->randomUInt(5) < 5);
46
        QVERIFY(randomGen()->randomUInt(100) < 100);
47
        QVERIFY(randomGen()->randomUInt(100000U) < 100000U);
48
        QVERIFY(randomGen()->randomUInt((QUINT32_MAX / 2U) + 1U) < QUINT32_MAX / 2U + 1U);
49
    }
50
}
51

52
void TestRandomGenerator::testUIntRange()
53
{
54
    // Run a bunch of trials to ensure we stay within the range
55
    for (int i = 0; i < 100; ++i) {
56
        auto rand = randomGen()->randomUIntRange(100, 200);
57
        QVERIFY(rand >= 100);
58
        QVERIFY(rand < 200);
59
    }
60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.