update dev300-m58
[ooovba.git] / cppu / test / test_sec.cxx
blob9f5f34bc202dbe2a12b9f73a5492b9103f1cb538
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_sec.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
34 #include <stdio.h>
37 #include <uno/current_context.hxx>
39 #include <cppuhelper/implbase1.hxx>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43 #include <com/sun/star/loader/XImplementationLoader.hpp>
44 #include <com/sun/star/container/XNameContainer.hpp>
46 using namespace rtl;
47 using namespace cppu;
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::security;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::loader;
52 using namespace com::sun::star::registry;
53 using namespace com::sun::star::container;
56 static inline void out( const sal_Char * p )
58 ::fprintf( stderr, p );
60 static inline void out( const OUString & r )
62 OString aStr( OUStringToOString( r, RTL_TEXTENCODING_ASCII_US ) );
63 out( aStr.getStr() );
67 static Reference< XInterface > load(
68 const Reference< XMultiServiceFactory > & xMgr,
69 const char * service,
70 const char * implName, const char * activator, const char * loc ) throw ()
72 Reference< XInterface > xRet(
73 xMgr->createInstance( OUString::createFromAscii( service ) ) );
74 if (xRet.is())
75 return xRet;
77 try
79 Reference< XImplementationLoader > xLoader(
80 xMgr->createInstance( OUString::createFromAscii( activator ) ), UNO_QUERY );
81 if (xLoader.is())
83 Reference< XSingleServiceFactory > xFac( xLoader->activate(
84 OUString::createFromAscii( implName ), OUString(),
85 OUString::createFromAscii( loc ),
86 Reference< XRegistryKey >() ), UNO_QUERY );
87 if (xFac.is())
88 return xFac->createInstance();
90 else
92 out( "### cannot activate loader \"" );
93 out( activator );
94 out( "\"!\n" );
97 catch (Exception &)
99 out( "### cannot activate service \"" );
100 out( service );
101 out( "\"!\n" );
103 return Reference< XInterface >();
106 static void setEnv( const Reference< XMultiServiceFactory > & xMgr ) throw ()
108 OSL_ASSERT( xMgr.is() );
109 Reference< XNameContainer > xContext( getCurrentContext(), UNO_QUERY );
110 OSL_ASSERT( xContext.is() );
114 xContext->insertByName(
115 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceManager") ),
116 makeAny( xMgr ) );
117 xContext->insertByName(
118 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.security.AccessController") ),
119 makeAny( load( xMgr,
120 "com.sun.star.security.AccessController",
121 "com.sun.star.comp.security.AccessController",
122 "com.sun.star.loader.Java2",
123 "com.sun.star.comp.security.AccessController" ) ) );
124 xContext->insertByName(
125 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.security.auth.login.LoginContext") ),
126 makeAny( load( xMgr,
127 "com.sun.star.security.auth.login.LoginContext",
128 "com.sun.star.comp.security.auth.login.LoginContext",
129 "com.sun.star.loader.Java2",
130 "com.sun.star.comp.security.auth.login.LoginContext" ) ) );
132 catch (Exception & rExc)
134 out( "### exception occured: " );
135 out( rExc.Message );
136 out( "\n" );
140 //##################################################################################################
141 //##################################################################################################
142 //##################################################################################################
144 struct PrivAction : WeakImplHelper1< XPrivilegedAction >
146 virtual Any SAL_CALL run()
147 throw (Exception)
149 throw IllegalArgumentException(
150 OUString::createFromAscii("testtest"), (OWeakObject *)this, (sal_Int16)5 );
154 struct Thread1 : public OThread
156 Reference< XMultiServiceFactory > _xMgr;
158 Thread1( const Reference< XMultiServiceFactory > & xMgr )
159 : _xMgr( xMgr )
162 void f()
164 Reference< XNameAccess > xAccess( getCurrentContext(), UNO_QUERY );
165 OSL_ASSERT( xAccess->getByName( OUString::createFromAscii("a") ) == (sal_Int16)5 );
166 Reference< XNameContainer > xCont( getCurrentContext(), UNO_QUERY );
167 xCont->insertByName( OUString::createFromAscii("b"), makeAny( (sal_Int32)6 ) );
169 virtual void SAL_CALL run()
173 setEnv( _xMgr );
174 Reference< XNameContainer > xCont( getCurrentContext(), UNO_QUERY );
175 xCont->insertByName( OUString::createFromAscii("a"), makeAny( (sal_Int32)5 ) );
176 f();
177 Reference< XNameAccess > xAccess( getCurrentContext(), UNO_QUERY );
178 OSL_ASSERT( xAccess->getByName( OUString::createFromAscii("b") ) == (sal_Int16)6 );
180 // checkPermission(
181 // Permission( OUString::createFromAscii("java.io.FilePermission"),
182 // OUString::createFromAscii("f:\\userprofiles.dat"),
183 // OUString::createFromAscii("read") ) );
185 // try
186 // {
187 // Reference< XCurrentContext > xContext( getCurrentContext() );
188 // Reference< XAccessController > xACC( xContext->getAccessController() );
189 // xACC->doPrivileged( new PrivAction(), Reference< XAccessControlContext >() );
190 // }
191 // catch (IllegalArgumentException & r)
192 // {
193 // OSL_ASSERT( r.ArgumentPosition == 5 &&
194 // r.Message.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("testtest") ));
195 // }
197 catch (RuntimeException & rExc)
199 out( rExc.Message );
205 void test_security( const Reference< XMultiServiceFactory > & xMgr ) throw ()
207 setEnv( xMgr );
208 OSL_ASSERT( getCurrentContext()->getServiceManager().is() );
210 Thread1 thread1( xMgr );
211 thread1.create();
212 thread1.join();
213 out( "> exiting...\n" );