summaryrefslogtreecommitdiffstats
blob: 6932881e901abc1ee573253ca4dcb2f3fa539bdf (plain)
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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Base/ByteArray.h>

#include <QA/Checker/IO.h>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/bind.hpp>

#include <Swiften/TLS/CertificateFactory.h>
#include "SwifTools/Application/PlatformApplicationPathProvider.h"

using namespace Swift;

template<typename CERTIFICATE_FACTORY>
class CertificateTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(CertificateTest);
		CPPUNIT_TEST(testConstructFromDER);
		CPPUNIT_TEST(testToDER);
		//CPPUNIT_TEST(testGetSubjectName);
		CPPUNIT_TEST(testGetCommonNames);
		CPPUNIT_TEST(testGetSRVNames);
		CPPUNIT_TEST(testGetDNSNames);
		CPPUNIT_TEST(testGetXMPPAddresses);
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			pathProvider = new PlatformApplicationPathProvider("FileReadBytestreamTest");
			readByteArrayFromFile(certificateData, (pathProvider->getExecutableDir() / "jabber_org.crt").string());
			certificateFactory = new CERTIFICATE_FACTORY();
		}

		void tearDown() {
			delete certificateFactory;
			delete pathProvider;
		}

		void testConstructFromDER() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]);
		}
		
		void testToDER() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(certificateData, testling->toDER());
		}

/*
		void testGetSubjectName() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(std::string("/description=114072-VMk8pdi1aj5kTXxO/C=US/ST=Colorado/L=Denver/O=Peter Saint-Andre/OU=StartCom Trusted Certificate Member/CN=*.jabber.org/emailAddress=hostmaster@jabber.org"), testling->getSubjectName());
		}
		*/

		void testGetCommonNames() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getCommonNames().size()));
			CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]);
		}

		void testGetSRVNames() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getSRVNames().size()));
			CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getSRVNames()[0]);
		}

		void testGetDNSNames() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(testling->getDNSNames().size()));
			CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getDNSNames()[0]);
			CPPUNIT_ASSERT_EQUAL(std::string("jabber.org"), testling->getDNSNames()[1]);
		}

		void testGetXMPPAddresses() {
			Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getXMPPAddresses().size()));
			CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getXMPPAddresses()[0]);
		}
	
	private:
		PlatformApplicationPathProvider* pathProvider;
		ByteArray certificateData;
		CertificateFactory* certificateFactory;
};

#ifdef HAVE_OPENSSL
#include <Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h>
CPPUNIT_TEST_SUITE_REGISTRATION(CertificateTest<OpenSSLCertificateFactory>);
#endif