1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rtl/ustring.hxx>
41 #include <neon/ne_ssl.h>
43 using namespace com::sun::star
;
45 #define OID_SUBJECT_ALTERNATIVE_NAME "2.5.29.17"
50 class Test
: public CppUnit::TestFixture
{
53 static uno::Sequence
< security::CertAltNameEntry
> altNames
;
56 uno::Reference
< xml::crypto::XSecurityEnvironment
> initUno();
58 OString
getB64CertFromFile(const char filename
[]);
59 test::OfficeConnection connection_
;
73 void test_Directory();
85 CPPUNIT_TEST_SUITE(Test
);
86 CPPUNIT_TEST(test_Others
);
87 CPPUNIT_TEST(test_RFC822
);
88 CPPUNIT_TEST(test_DNS
);
89 CPPUNIT_TEST(test_Directory
);
90 CPPUNIT_TEST(test_URI
);
91 CPPUNIT_TEST(test_IP
);
92 CPPUNIT_TEST(test_RID
);
93 CPPUNIT_TEST(test_EDI
);
94 CPPUNIT_TEST(test_X400
);
95 CPPUNIT_TEST_SUITE_END();
98 uno::Sequence
< security::CertAltNameEntry
> Test::altNames
;
99 bool Test::runOnce
= false;
101 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
116 connection_
.tearDown();
122 uno::Reference
< xml::crypto::XSecurityEnvironment
> Test::initUno()
124 uno::Reference
< uno::XComponentContext
> context(connection_
.getComponentContext(), uno::UNO_QUERY_THROW
);
125 uno::Reference
< lang::XMultiServiceFactory
> factory(context
->getServiceManager(), uno::UNO_QUERY_THROW
);
126 uno::Reference
< xml::crypto::XSEInitializer
> xSEInitializer
= xml::crypto::SEInitializer::create(context
);
127 uno::Reference
< xml::crypto::XXMLSecurityContext
> xSecurityContext(
128 xSEInitializer
->createSecurityContext(OUString()));
129 return xSecurityContext
->getSecurityEnvironment();
135 uno::Reference
< xml::crypto::XSecurityEnvironment
> xSecurityEnv
= initUno();
136 OString
b64Cert(getB64CertFromFile("User_35_Root_11.crt"));
137 uno::Reference
< security::XCertificate
> xCert
= xSecurityEnv
->createCertificateFromAscii(
138 OStringToOUString( b64Cert
, RTL_TEXTENCODING_ASCII_US
) );
139 uno::Sequence
< uno::Reference
< security::XCertificateExtension
> > extensions
= xCert
->getExtensions();
140 for (sal_Int32 i
= 0 ; i
< extensions
.getLength(); i
++)
142 uno::Reference
< security::XCertificateExtension
>element
= extensions
[i
];
143 OString
aId ( (const char *)element
->getExtensionId().getArray(), element
->getExtensionId().getLength());
144 if (aId
.equals(OID_SUBJECT_ALTERNATIVE_NAME
))
146 uno::Reference
< security::XSanExtension
> sanExtension ( element
, uno::UNO_QUERY
);
147 altNames
= sanExtension
->getAlternativeNames();
153 OString
Test::getB64CertFromFile(const char filename
[])
155 ne_ssl_certificate
* cert
= ne_ssl_cert_read(filename
);
156 char* certExportB64
= ne_ssl_cert_export(cert
);
157 OString
certB64( certExportB64
);
162 void Test::test_Others() {
163 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames
.getLength() > 0 ) );
164 for(int n
= 1; n
< altNames
.getLength(); n
++)
166 if (altNames
[n
].Type
== security::ExtAltNameType_OTHER_NAME
)
168 css::beans::NamedValue otherNameProp
;
169 if (altNames
[n
].Value
>>= otherNameProp
)
171 CPPUNIT_ASSERT_EQUAL( OUString("1.2.3.4"), otherNameProp
.Name
);
172 uno::Sequence
< sal_Int8
> ipAddress
;
173 otherNameProp
.Value
>>= ipAddress
;
174 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress
.getLength() > 0 ) );
180 void Test::test_RFC822() {
181 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames
.getLength() > 0 ) );
182 for(int n
= 1; n
< altNames
.getLength(); n
++)
184 if (altNames
[n
].Type
== security::ExtAltNameType_RFC822_NAME
)
187 altNames
[n
].Value
>>= value
;
188 CPPUNIT_ASSERT_EQUAL( OUString("my@other.address"), value
);
193 void Test::test_DNS() {
194 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames
.getLength() > 0 ) );
195 for(int n
= 1; n
< altNames
.getLength(); n
++)
197 if (altNames
[n
].Type
== security::ExtAltNameType_DNS_NAME
)
200 altNames
[n
].Value
>>= value
;
201 CPPUNIT_ASSERT_EQUAL( OUString("alt.openoffice.org"), value
);
206 void Test::test_Directory() {
210 void Test::test_URI() {
211 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames
.getLength() > 0 ) );
212 for(int n
= 1; n
< altNames
.getLength(); n
++)
214 if (altNames
[n
].Type
== security::ExtAltNameType_URL
)
217 altNames
[n
].Value
>>= value
;
218 CPPUNIT_ASSERT_EQUAL( OUString("http://my.url.here/"), value
);
223 void Test::test_IP() {
224 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames
.getLength() > 0 ) );
225 for(int n
= 1; n
< altNames
.getLength(); n
++)
227 if (altNames
[n
].Type
== security::ExtAltNameType_IP_ADDRESS
)
229 uno::Sequence
< sal_Int8
> ipAddress
;
230 altNames
[n
].Value
>>= ipAddress
;
231 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress
.getLength() > 0 ) );
237 void Test::test_RID() {
238 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames
.getLength() > 0 ) );
239 for(int n
= 1; n
< altNames
.getLength(); n
++)
241 if (altNames
[n
].Type
== security::ExtAltNameType_REGISTERED_ID
)
244 altNames
[n
].Value
>>= value
;
245 CPPUNIT_ASSERT( OUString("1.2.3.4").equals(value
));
250 void Test::test_EDI() {
254 void Test::test_X400() {
258 CPPUNIT_PLUGIN_IMPLEMENT();
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */