RedirectHash class created in restricted_html file
[libi2ncommon] / test / test_crypto.cpp
CommitLineData
69d568dd
TC
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
20/**
21 tests for the module "crypto"
22
23 @copyright Intra2net AG
24*/
25#define BOOST_TEST_DYN_LINK
26#include <boost/test/unit_test.hpp>
27#include <vector>
28#include <tmpfstream.hpp>
29#include <iostream>
30#include <fstream>
31#include <string>
32
33#include "crypto.hxx"
34
35using namespace std;
36using namespace I2n;
37
38
39class TestCryptoFixture
40{
41protected:
42
d87beebd 43 string Filename;
69d568dd
TC
44
45 tmpfstream Tempfile;
46
47 void write_tempfile()
48 {
49 string tmpfile_name = "/tmp/libi2ncommon_test_crypto_XXXXXX";
50 Tempfile.open(tmpfile_name);
51 Tempfile << "Long Data and or Password";
52 Tempfile.close();
53
d87beebd 54 Filename = Tempfile.get_tmp_filename();
69d568dd
TC
55 }
56
57public:
58 TestCryptoFixture()
59 {
60 write_tempfile();
61 }
62
63 ~TestCryptoFixture()
64 {
65 Tempfile.unlink();
66 }
67};
68
69BOOST_FIXTURE_TEST_SUITE(Crypto, TestCryptoFixture)
70
71BOOST_AUTO_TEST_CASE(CheckHashMD5)
72{
73 const string hash = hash_data("Long Data and or Password",
74 MD5);
75 BOOST_CHECK_EQUAL(hash, string("E5952C81BBD04F9CB748433B9431F674"));
76}
77
78BOOST_AUTO_TEST_CASE(CheckHashSHA1)
79{
80 const string hash = hash_data("Long Data and or Password",
81 SHA1);
82 BOOST_CHECK_EQUAL(hash, string("5935A22DAA087672FE2EA0E4485D58BC77E6068D"));
83}
84
85BOOST_AUTO_TEST_CASE(CheckHashSHA256)
86{
87 const string hash = hash_data("Long Data and or Password",
88 SHA256);
89 BOOST_CHECK_EQUAL(hash, string("6D7F287A7E01ADF1A66C19D61C8D5A3BD68C1F"
90 "10565A6D37A5920B09152D62CE"));
91}
92
93BOOST_AUTO_TEST_CASE(CheckHashSHA384)
94{
95 const string hash = hash_data("Long Data and or Password",
96 SHA384);
97 BOOST_CHECK_EQUAL(hash, string("1D879CCDFF4E50C32318A1F0FB8AF8A0B"
98 "150432CECAF41E5C7C0C75688E1F776"
99 "14CA70407A9BC97FDD1CEC4CF291B66B"));
100}
101
102BOOST_AUTO_TEST_CASE(CheckHashSHA512)
103{
104 const string hash = hash_file(Filename,
105 SHA512);
106 BOOST_CHECK_EQUAL(hash, string("CBD561B4D1E13C08672CBCCAC855FAC3F4"
107 "7672548D0B61B0BB201017E4D93B239E"
108 "969DB3588F1466F34FB27A7D0AEFE203"
109 "594808E16659BEDD4B25F34EC46CDB"));
110}
111
112BOOST_AUTO_TEST_CASE(CheckHashFileMD5)
113{
114 const string hash = hash_file(Filename,
115 MD5);
116 BOOST_CHECK_EQUAL(hash, string("E5952C81BBD04F9CB748433B9431F674"));
117}
118
119BOOST_AUTO_TEST_CASE(CheckHashFileSHA1)
120{
121 const string hash = hash_file(Filename,
122 SHA1);
123 BOOST_CHECK_EQUAL(hash, string("5935A22DAA087672FE2EA0E4485D58BC77E6068D"));
124}
125
126BOOST_AUTO_TEST_CASE(CheckHashFileSHA256)
127{
128 const string hash = hash_file(Filename,
129 SHA256);
130 BOOST_CHECK_EQUAL(hash, string("6D7F287A7E01ADF1A66C19D61C8D5A3BD68C1F"
131 "10565A6D37A5920B09152D62CE"));
132}
133
134BOOST_AUTO_TEST_CASE(CheckHashFileSHA384)
135{
136 const string hash = hash_file(Filename,
137 SHA384);
138 BOOST_CHECK_EQUAL(hash, string("1D879CCDFF4E50C32318A1F0FB8AF8A0B"
139 "150432CECAF41E5C7C0C75688E1F776"
140 "14CA70407A9BC97FDD1CEC4CF291B66B"));
141}
142
143BOOST_AUTO_TEST_CASE(CheckHashFileSHA512)
144{
145 const string hash = hash_file(Filename,
146 SHA512);
147 BOOST_CHECK_EQUAL(hash, string("CBD561B4D1E13C08672CBCCAC855FAC3F4"
148 "7672548D0B61B0BB201017E4D93B239E"
149 "969DB3588F1466F34FB27A7D0AEFE203"
150 "594808E16659BEDD4B25F34EC46CDB"));
151}
152
153BOOST_AUTO_TEST_SUITE_END()