File: foreign.cc

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (164 lines) | stat: -rw-r--r-- 5,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Testing whether or not files generated on the author's machine can be read
// on other machines as well...

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include <unistd.h>
#include <cstring>
#include <list>

#include "blowfishfactory.hh"
#include "file.hh"
#include "securearray.hh"
#include "testpaths.h"
#include "openssl.hh"

constexpr auto TEST_PASSWORD{"test1"};

constexpr auto ROUNDS{200};

constexpr auto NAME{"Test name"};
constexpr auto HOST{"Test host"};
constexpr auto USERNAME{"Test username"};
constexpr auto PASSWORD{"Test password"};
constexpr auto COMMENT{"Test comment"};

inline std::string makeName(int number) {
    std::string name{NAME};
    name += " " + std::to_string(number);

    return name;
}

inline yapet::PasswordRecord makePasswordRecord(int number) {
    yapet::PasswordRecord passwordRecord{};

    passwordRecord.name(makeName(number).c_str());

    std::string host = HOST;
    host += " " + std::to_string(number);
    passwordRecord.host(host.c_str());

    std::string username = USERNAME;
    username += " " + std::to_string(number);
    passwordRecord.username(username.c_str());

    std::string password = PASSWORD;
    password += " " + std::to_string(number);
    passwordRecord.password(password.c_str());

    std::string comment = COMMENT;
    comment += " " + std::to_string(number);
    passwordRecord.comment(comment.c_str());

    return passwordRecord;
}

inline void comparePasswordRecords(const yapet::PasswordRecord &actual,
                                   const yapet::PasswordRecord &expected) {
    CPPUNIT_ASSERT(
        std::strcmp(reinterpret_cast<const char *>(actual.name()),
                    reinterpret_cast<const char *>(expected.name())) == 0);
    CPPUNIT_ASSERT(
        std::strcmp(reinterpret_cast<const char *>(actual.host()),
                    reinterpret_cast<const char *>(expected.host())) == 0);
    CPPUNIT_ASSERT(
        std::strcmp(reinterpret_cast<const char *>(actual.username()),
                    reinterpret_cast<const char *>(expected.username())) == 0);
    CPPUNIT_ASSERT(
        std::strcmp(reinterpret_cast<const char *>(actual.password()),
                    reinterpret_cast<const char *>(expected.password())) == 0);
    CPPUNIT_ASSERT(
        std::strcmp(reinterpret_cast<const char *>(actual.comment()),
                    reinterpret_cast<const char *>(expected.comment())) == 0);
}

inline void testFile(const char *filename) {
    auto password{yapet::toSecureArray(TEST_PASSWORD)};
    std::shared_ptr<yapet::AbstractCryptoFactory> factory{
        new yapet::BlowfishFactory{password, yapet::MetaData{}}};

    auto crypto{factory->crypto()};

    YAPET::File file{factory, filename, false, false};

    auto passwords{file.read()};
    CPPUNIT_ASSERT(passwords.size() == ROUNDS);

    std::list<yapet::PasswordListItem>::iterator it = passwords.begin();
    for (int i = 0; it != passwords.end(); i++, it++) {
        auto name{makeName(i)};
        CPPUNIT_ASSERT(std::strcmp(name.c_str(), reinterpret_cast<const char *>(
                                                     it->name())) == 0);

        auto serializedPasswordRecord{crypto->decrypt(it->encryptedRecord())};
        yapet::PasswordRecord actual{serializedPasswordRecord};

        yapet::PasswordRecord expected{makePasswordRecord(i)};
        comparePasswordRecords(actual, expected);
    }
}

class ForeignTest : public CppUnit::TestFixture {
   public:
    static CppUnit::TestSuite *suite() {
        CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite("Blowfish");

        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "32bit little endian pre 0.6", &ForeignTest::test32lePre06));
        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "32bit big endian pre 0.6", &ForeignTest::test32bePre06));

        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "64bit little endian pre 0.6", &ForeignTest::test64lePre06));
        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "64bit big endian pre 0.6", &ForeignTest::test64bePre06));

        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "32bit little endian 0.6", &ForeignTest::test32le06));
        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "32bit big endian 0.6", &ForeignTest::test32be06));

        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "64bit little endian 0.6", &ForeignTest::test64le06));
        suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
            "64bit big endian 0.6", &ForeignTest::test64be06));

        return suiteOfTests;
    }

    void test32lePre06() { testFile(BUILDDIR "/f32le0.5.pet"); }

    void test32bePre06() { testFile(BUILDDIR "/f32be0.5.pet"); }

    void test64lePre06() { testFile(BUILDDIR "/f64le0.5.pet"); }

    void test64bePre06() { testFile(BUILDDIR "/f64be0.5.pet"); }

    void test32le06() { testFile(BUILDDIR "/f32le0.6.pet"); }

    void test32be06() { testFile(BUILDDIR "/f32be0.6.pet"); }

    void test64le06() { testFile(BUILDDIR "/f64le0.6.pet"); }

    void test64be06() { testFile(BUILDDIR "/f64be0.6.pet"); }
};

int main() {
    yapet::OpenSSL::init();
    CppUnit::TextUi::TestRunner runner;
    runner.addTest(ForeignTest ::suite());
    return runner.run() ? 0 : 1;
}