Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / qa / certext / SanCertExt.cxx
blob12f03173d5f0071bfb5f3df5ac9d50408bb3eb7b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <test/officeconnection.hxx>
22 #include <com/sun/star/security/XSanExtension.hpp>
23 #include <com/sun/star/security/ExtAltNameType.hpp>
24 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
25 #include <com/sun/star/xml/crypto/SEInitializer.hpp>
26 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
27 #include <com/sun/star/security/XCertificate.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/beans/NamedValue.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/uno/Reference.hxx>
33 #include <cppuhelper/bootstrap.hxx>
34 #include "cppunit/TestAssert.h"
35 #include "cppunit/TestFixture.h"
36 #include "cppunit/extensions/HelperMacros.h"
37 #include "cppunit/plugin/TestPlugIn.h"
38 #include <sal/types.h>
39 #include <comphelper/sequence.hxx>
40 #include <rtl/ustring.hxx>
42 #include <neon/ne_ssl.h>
44 using namespace com::sun::star;
46 #define OID_SUBJECT_ALTERNATIVE_NAME "2.5.29.17"
49 namespace {
51 class Test: public CppUnit::TestFixture {
53 private:
54 static uno::Sequence< security::CertAltNameEntry > altNames;
55 static bool runOnce;
57 uno::Reference< xml::crypto::XSecurityEnvironment > initUno();
58 void init();
59 OString getB64CertFromFile(const char filename[]);
60 test::OfficeConnection connection_;
62 public:
64 Test();
66 ~Test();
68 virtual void setUp();
70 virtual void tearDown();
72 void test_Others();
74 void test_RFC822();
76 void test_DNS();
78 void test_Direcory();
80 void test_URI();
82 void test_IP();
84 void test_RID();
86 void test_EDI();
88 void test_X400();
90 CPPUNIT_TEST_SUITE(Test);
91 CPPUNIT_TEST(test_Others);
92 CPPUNIT_TEST(test_RFC822);
93 CPPUNIT_TEST(test_DNS);
94 CPPUNIT_TEST(test_Direcory);
95 CPPUNIT_TEST(test_URI);
96 CPPUNIT_TEST(test_IP);
97 CPPUNIT_TEST(test_RID);
98 CPPUNIT_TEST(test_EDI);
99 CPPUNIT_TEST(test_X400);
100 CPPUNIT_TEST_SUITE_END();
103 uno::Sequence< security::CertAltNameEntry > Test::altNames;
104 bool Test::runOnce = false;
106 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
108 Test::Test()
110 if (runOnce)
111 return;
112 runOnce = true;
113 connection_.setUp();
114 init();
117 Test::~Test()
119 if (runOnce)
121 connection_.tearDown();
122 runOnce = false;
127 uno::Reference< xml::crypto::XSecurityEnvironment > Test::initUno()
129 uno::Reference< uno::XComponentContext > context(connection_.getComponentContext(), uno::UNO_QUERY_THROW);
130 uno::Reference< lang::XMultiServiceFactory > factory(context->getServiceManager(), uno::UNO_QUERY_THROW);
131 uno::Reference< xml::crypto::XSEInitializer > xSEInitializer = xml::crypto::SEInitializer::create(context);
132 uno::Reference< xml::crypto::XXMLSecurityContext > xSecurityContext(
133 xSEInitializer->createSecurityContext(OUString()));
134 return xSecurityContext->getSecurityEnvironment();
138 void Test::init()
140 uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnv = initUno();
141 OString b64Cert(getB64CertFromFile("User_35_Root_11.crt"));
142 uno::Reference< security::XCertificate > xCert = xSecurityEnv->createCertificateFromAscii(
143 OStringToOUString( b64Cert, RTL_TEXTENCODING_ASCII_US ) );
144 uno::Sequence< uno::Reference< security::XCertificateExtension > > extensions = xCert->getExtensions();
145 for (sal_Int32 i = 0 ; i < extensions.getLength(); i++)
147 uno::Reference< security::XCertificateExtension >element = extensions[i];
148 OString aId ( (const sal_Char *)element->getExtensionId().getArray(), element->getExtensionId().getLength());
149 if (aId.equals(OID_SUBJECT_ALTERNATIVE_NAME))
151 uno::Reference< security::XSanExtension > sanExtension ( element, uno::UNO_QUERY );
152 altNames = sanExtension->getAlternativeNames();
153 break;
158 OString Test::getB64CertFromFile(const char filename[])
160 ne_ssl_certificate* cert = ne_ssl_cert_read(filename);
161 char* certExportB64 = ne_ssl_cert_export(cert);
162 OString certB64( certExportB64 );
163 return certB64;
167 void Test::setUp() {
170 void Test::tearDown() {
173 void Test::test_Others() {
174 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
175 for(int n = 1; n < altNames.getLength(); n++)
177 if (altNames[n].Type == security::ExtAltNameType_OTHER_NAME)
179 ::com::sun::star::beans::NamedValue otherNameProp;
180 if (altNames[n].Value >>= otherNameProp)
182 CPPUNIT_ASSERT_EQUAL( OUString("1.2.3.4"), otherNameProp.Name);
183 uno::Sequence< sal_Int8 > ipAddress;
184 otherNameProp.Value >>= ipAddress;
185 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress.getLength() > 0 ) );
191 void Test::test_RFC822() {
192 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
193 for(int n = 1; n < altNames.getLength(); n++)
195 if (altNames[n].Type == security::ExtAltNameType_RFC822_NAME)
197 OUString value;
198 altNames[n].Value >>= value;
199 CPPUNIT_ASSERT_EQUAL( OUString("my@other.address"), value);
204 void Test::test_DNS() {
205 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
206 for(int n = 1; n < altNames.getLength(); n++)
208 if (altNames[n].Type == security::ExtAltNameType_DNS_NAME)
210 OUString value;
211 altNames[n].Value >>= value;
212 CPPUNIT_ASSERT_EQUAL( OUString("alt.openoffice.org"), value);
217 void Test::test_Direcory() {
218 // Not implemented
221 void Test::test_URI() {
222 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
223 for(int n = 1; n < altNames.getLength(); n++)
225 if (altNames[n].Type == security::ExtAltNameType_URL)
227 OUString value;
228 altNames[n].Value >>= value;
229 CPPUNIT_ASSERT_EQUAL( OUString("http://my.url.here/"), value);
234 void Test::test_IP() {
235 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
236 for(int n = 1; n < altNames.getLength(); n++)
238 if (altNames[n].Type == security::ExtAltNameType_IP_ADDRESS)
240 uno::Sequence< sal_Int8 > ipAddress;
241 altNames[n].Value >>= ipAddress;
242 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress.getLength() > 0 ) );
248 void Test::test_RID() {
249 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
250 for(int n = 1; n < altNames.getLength(); n++)
252 if (altNames[n].Type == security::ExtAltNameType_REGISTERED_ID)
254 OUString value;
255 altNames[n].Value >>= value;
256 CPPUNIT_ASSERT( OUString("1.2.3.4").equals(value));
261 void Test::test_EDI() {
262 // Not implemented
265 void Test::test_X400() {
266 // Not implemented
269 CPPUNIT_PLUGIN_IMPLEMENT();
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */