1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_sec.cxx,v $
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"
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>
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
) );
67 static Reference
< XInterface
> load(
68 const Reference
< XMultiServiceFactory
> & xMgr
,
70 const char * implName
, const char * activator
, const char * loc
) throw ()
72 Reference
< XInterface
> xRet(
73 xMgr
->createInstance( OUString::createFromAscii( service
) ) );
79 Reference
< XImplementationLoader
> xLoader(
80 xMgr
->createInstance( OUString::createFromAscii( activator
) ), UNO_QUERY
);
83 Reference
< XSingleServiceFactory
> xFac( xLoader
->activate(
84 OUString::createFromAscii( implName
), OUString(),
85 OUString::createFromAscii( loc
),
86 Reference
< XRegistryKey
>() ), UNO_QUERY
);
88 return xFac
->createInstance();
92 out( "### cannot activate loader \"" );
99 out( "### cannot activate service \"" );
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") ),
117 xContext
->insertByName(
118 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.security.AccessController") ),
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") ),
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: " );
140 //##################################################################################################
141 //##################################################################################################
142 //##################################################################################################
144 struct PrivAction
: WeakImplHelper1
< XPrivilegedAction
>
146 virtual Any SAL_CALL
run()
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
)
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()
174 Reference
< XNameContainer
> xCont( getCurrentContext(), UNO_QUERY
);
175 xCont
->insertByName( OUString::createFromAscii("a"), makeAny( (sal_Int32
)5 ) );
177 Reference
< XNameAccess
> xAccess( getCurrentContext(), UNO_QUERY
);
178 OSL_ASSERT( xAccess
->getByName( OUString::createFromAscii("b") ) == (sal_Int16
)6 );
181 // Permission( OUString::createFromAscii("java.io.FilePermission"),
182 // OUString::createFromAscii("f:\\userprofiles.dat"),
183 // OUString::createFromAscii("read") ) );
187 // Reference< XCurrentContext > xContext( getCurrentContext() );
188 // Reference< XAccessController > xACC( xContext->getAccessController() );
189 // xACC->doPrivileged( new PrivAction(), Reference< XAccessControlContext >() );
191 // catch (IllegalArgumentException & r)
193 // OSL_ASSERT( r.ArgumentPosition == 5 &&
194 // r.Message.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("testtest") ));
197 catch (RuntimeException
& rExc
)
205 void test_security( const Reference
< XMultiServiceFactory
> & xMgr
) throw ()
208 OSL_ASSERT( getCurrentContext()->getServiceManager().is() );
210 Thread1
thread1( xMgr
);
213 out( "> exiting...\n" );