File: cryptofactoryhelper.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 (89 lines) | stat: -rw-r--r-- 2,865 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
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include <cstring>
#include <typeinfo>

#include "aes256factory.hh"
#include "blowfishfactory.hh"
#include "cryptofactoryhelper.hh"
#include "testpaths.h"
#include "openssl.hh"

class CryptoFactoryHelperTest : public CppUnit::TestFixture {
   private:
    yapet::SecureArray passwordArray{0};

   public:
    static CppUnit::TestSuite *suite() {
        CppUnit::TestSuite *suiteOfTests =
            new CppUnit::TestSuite("Crypto Factory Helper Test");

        suiteOfTests->addTest(new CppUnit::TestCaller<CryptoFactoryHelperTest>(
            "should create blowfish factory",
            &CryptoFactoryHelperTest::yapet10file));
        suiteOfTests->addTest(new CppUnit::TestCaller<CryptoFactoryHelperTest>(
            "should create AES256 factory",
            &CryptoFactoryHelperTest::yapet20file));
        suiteOfTests->addTest(new CppUnit::TestCaller<CryptoFactoryHelperTest>(
            "should gracefully handle unkown file",
            &CryptoFactoryHelperTest::unknownFile));
        suiteOfTests->addTest(new CppUnit::TestCaller<CryptoFactoryHelperTest>(
            "should gracefully handle error file",
            &CryptoFactoryHelperTest::errorFile));

        return suiteOfTests;
    }

    void yapet10file() {
        auto factory{yapet::getCryptoFactoryForFile(
            BUILDDIR "/cryptofactoryhelper-1.0.pet",
            yapet::toSecureArray("wdc"))};

        CPPUNIT_ASSERT(factory);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpotentially-evaluated-expression"
        CPPUNIT_ASSERT(typeid(*factory) == typeid(yapet::BlowfishFactory));
#pragma clang diagnostic pop
    }

    void yapet20file() {
        auto factory{yapet::getCryptoFactoryForFile(
            BUILDDIR "/cryptofactoryhelper-2.0.pet",
            yapet::toSecureArray("wdc"))};

        CPPUNIT_ASSERT(factory);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpotentially-evaluated-expression"
        CPPUNIT_ASSERT(typeid(*factory) == typeid(yapet::Aes256Factory));
#pragma clang diagnostic pop
    }

    void unknownFile() {
        auto factory{yapet::getCryptoFactoryForFile(
            BUILDDIR "/cryptofactoryhelper-unknown.pet",
            yapet::toSecureArray("wdc"))};

        CPPUNIT_ASSERT(!factory);
    }

    void errorFile() {
        auto factory{yapet::getCryptoFactoryForFile(
            BUILDDIR "/cryptofactoryhelper-tooshort.pet",
            yapet::toSecureArray("wdc"))};

        CPPUNIT_ASSERT(!factory);
    }
};

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