1*9d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9d7e27acSAndrew Rist  * distributed with this work for additional information
6*9d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
9*9d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9d7e27acSAndrew Rist  *
11*9d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9d7e27acSAndrew Rist  *
13*9d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9d7e27acSAndrew Rist  * software distributed under the License is distributed on an
15*9d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
17*9d7e27acSAndrew Rist  * specific language governing permissions and limitations
18*9d7e27acSAndrew Rist  * under the License.
19*9d7e27acSAndrew Rist  *
20*9d7e27acSAndrew Rist  *************************************************************/
21*9d7e27acSAndrew Rist 
22*9d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include "sal/main.h"
26cdf0e10cSrcweir #include "osl/file.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "typelib/typedescription.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx"
31cdf0e10cSrcweir #include "cppuhelper/shlib.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "uno/environment.hxx"
36cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
37cdf0e10cSrcweir #include "cppu/EnvGuards.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <iostream>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #ifndef SAL_DLLPREFIX
43cdf0e10cSrcweir # define SAL_DLLPREFIX  ""
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir using namespace com::sun::star;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir static rtl::OUString s_comment;
51cdf0e10cSrcweir 
s_check_object_is_in(void * pObject)52cdf0e10cSrcweir static bool s_check_object_is_in(void * pObject)
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	uno::Environment currentEnv(uno::Environment::getCurrent());
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	rtl_uString * pOId = NULL;
57cdf0e10cSrcweir 	currentEnv.get()->pExtEnv->getObjectIdentifier(currentEnv.get()->pExtEnv, &pOId, pObject);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	uno::TypeDescription typeDescription(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XInterface")));
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	void * pRegisteredObject = NULL;
63cdf0e10cSrcweir 	currentEnv.get()->pExtEnv->getRegisteredInterface(currentEnv.get()->pExtEnv,
64cdf0e10cSrcweir 													  &pRegisteredObject,
65cdf0e10cSrcweir 													  pOId,
66cdf0e10cSrcweir 													  (typelib_InterfaceTypeDescription *)typeDescription.get());
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	if (pOId) rtl_uString_release(pOId);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	bool result = pRegisteredObject != NULL;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	if (result)
74cdf0e10cSrcweir 		currentEnv.get()->pExtEnv->releaseInterface(currentEnv.get()->pExtEnv, pRegisteredObject);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir  	return result;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
s_test__loadSharedLibComponentFactory(rtl::OUString const & clientPurpose,rtl::OUString const & servicePurpose)79cdf0e10cSrcweir static void s_test__loadSharedLibComponentFactory(rtl::OUString const & clientPurpose,
80cdf0e10cSrcweir 												  rtl::OUString const & servicePurpose)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))
83cdf0e10cSrcweir                                                + clientPurpose, NULL));
84cdf0e10cSrcweir     if (clientPurpose.getLength() && !envGuard.is())
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't get purpose env: \""));
87cdf0e10cSrcweir         s_comment += clientPurpose;
88cdf0e10cSrcweir         s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         return;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	rtl::OString os_clientPurpose(rtl::OUStringToOString(clientPurpose, RTL_TEXTENCODING_ASCII_US));
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	uno::Reference<uno::XInterface> xItf(
96cdf0e10cSrcweir 		cppu::loadSharedLibComponentFactory(
97cdf0e10cSrcweir 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SAL_DLLPREFIX "TestComponent.uno" SAL_DLLEXTENSION)),
98cdf0e10cSrcweir #ifdef WIN32
99cdf0e10cSrcweir 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("")),
100cdf0e10cSrcweir #else
101cdf0e10cSrcweir 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file://../lib/")),
102cdf0e10cSrcweir #endif
103cdf0e10cSrcweir 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent")) + servicePurpose,
104cdf0e10cSrcweir 			uno::Reference<lang::XMultiServiceFactory>(),
105cdf0e10cSrcweir 			uno::Reference<registry::XRegistryKey>()
106cdf0e10cSrcweir 			)
107cdf0e10cSrcweir 		);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	if (!xItf.is())
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tgot no object - FAILURE\n"));
112cdf0e10cSrcweir 		return;
113cdf0e10cSrcweir 	}
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	if (!clientPurpose.equals(servicePurpose) && !s_check_object_is_in(xItf.get()))
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't find object in current purpose \""));
118cdf0e10cSrcweir 		s_comment += clientPurpose;
119cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
120cdf0e10cSrcweir 	}
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if (!cppu::EnvDcp::getPurpose(uno::Environment::getCurrent().getTypeName()).equals(clientPurpose))
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tdid not enter client purpose \""));
125cdf0e10cSrcweir 		s_comment += clientPurpose;
126cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
s_test__loadSharedLibComponentFactory__free_free()130cdf0e10cSrcweir static void s_test__loadSharedLibComponentFactory__free_free()
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__free_free\n"));
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory(rtl::OUString(), rtl::OUString());
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
s_test__loadSharedLibComponentFactory__free_purpose()137cdf0e10cSrcweir static void s_test__loadSharedLibComponentFactory__free_purpose()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__free_purpose\n"));
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory(rtl::OUString(),
142cdf0e10cSrcweir 										  rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
s_test__loadSharedLibComponentFactory__purpose_free()145cdf0e10cSrcweir static void s_test__loadSharedLibComponentFactory__purpose_free()
146cdf0e10cSrcweir {
147cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__purpose_free\n"));
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")),
150cdf0e10cSrcweir 										  rtl::OUString());
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
s_test__loadSharedLibComponentFactory__purpose_purpose()153cdf0e10cSrcweir static void s_test__loadSharedLibComponentFactory__purpose_purpose()
154cdf0e10cSrcweir {
155cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__purpose_purpose\n"));
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")),
158cdf0e10cSrcweir 										  rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
s_getSDrive(void)161cdf0e10cSrcweir static rtl::OUString s_getSDrive(void)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	rtl::OUString path;//(RTL_CONSTASCII_USTRINGPARAM("file://"));
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	char const * tmp = getenv("SOLARVER");
166cdf0e10cSrcweir 	path += rtl::OUString(tmp, rtl_str_getLength(tmp), RTL_TEXTENCODING_ASCII_US);
167cdf0e10cSrcweir 	path += rtl::OUString(SAL_PATHDELIMITER);
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	tmp = getenv("INPATH");
170cdf0e10cSrcweir 	path += rtl::OUString(tmp, rtl_str_getLength(tmp), RTL_TEXTENCODING_ASCII_US);
171cdf0e10cSrcweir 	path += rtl::OUString(SAL_PATHDELIMITER);
172cdf0e10cSrcweir #ifdef WIN32
173cdf0e10cSrcweir 	path += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bin"));
174cdf0e10cSrcweir 
175cdf0e10cSrcweir #else
176cdf0e10cSrcweir 	path += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("lib"));
177cdf0e10cSrcweir #endif
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	tmp = getenv("UPDMINOREXT");
180cdf0e10cSrcweir     if (tmp)
181cdf0e10cSrcweir         path += rtl::OUString(tmp, rtl_str_getLength(tmp), RTL_TEXTENCODING_ASCII_US);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     osl::FileBase::getFileURLFromSystemPath(path, path);
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	return path;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
s_test__createSimpleRegistry(rtl::OUString const & clientPurpose)188cdf0e10cSrcweir static void s_test__createSimpleRegistry(rtl::OUString const & clientPurpose)
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))
191cdf0e10cSrcweir                                                + clientPurpose, NULL));
192cdf0e10cSrcweir     if (clientPurpose.getLength() && !envGuard.is())
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't get purpose env: \""));
195cdf0e10cSrcweir         s_comment += clientPurpose;
196cdf0e10cSrcweir         s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         return;
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	uno::Reference<registry::XSimpleRegistry> registry(cppu::createSimpleRegistry(
202cdf0e10cSrcweir 														   s_getSDrive()));
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 	if (!registry.is())
205cdf0e10cSrcweir 	{
206cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tgot no object - FAILURE\n"));
207cdf0e10cSrcweir 		return;
208cdf0e10cSrcweir 	}
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	if (clientPurpose.getLength() != 0 && !s_check_object_is_in(registry.get()))
211cdf0e10cSrcweir 	{
212cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't find object in current purpose \""));
213cdf0e10cSrcweir 		s_comment += clientPurpose;
214cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
s_test__createSimpleRegistry__free(void)218cdf0e10cSrcweir static void s_test__createSimpleRegistry__free(void)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__createSimpleRegistry__free\n"));
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 	s_test__createSimpleRegistry(rtl::OUString());
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
s_test__createSimpleRegistry__purpose(void)225cdf0e10cSrcweir static void s_test__createSimpleRegistry__purpose(void)
226cdf0e10cSrcweir {
227cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__createSimpleRegistry__purpose\n"));
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 	s_test__createSimpleRegistry(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 
s_test__bootstrap_InitialComponentContext(rtl::OUString const & clientPurpose)233cdf0e10cSrcweir static void s_test__bootstrap_InitialComponentContext(rtl::OUString const & clientPurpose)
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))
236cdf0e10cSrcweir                                                + clientPurpose, NULL));
237cdf0e10cSrcweir     if (clientPurpose.getLength() && !envGuard.is())
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't get purpose env: \""));
240cdf0e10cSrcweir         s_comment += clientPurpose;
241cdf0e10cSrcweir         s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         return;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	uno::Reference<uno::XComponentContext> xContext(
247cdf0e10cSrcweir 		cppu::bootstrap_InitialComponentContext(
248cdf0e10cSrcweir 			uno::Reference<registry::XSimpleRegistry>(),
249cdf0e10cSrcweir 			s_getSDrive())
250cdf0e10cSrcweir 		);
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	if (!xContext.is())
253cdf0e10cSrcweir 	{
254cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tgot no object - FAILURE\n"));
255cdf0e10cSrcweir 		return;
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	if (clientPurpose.getLength() != 0 && !s_check_object_is_in(xContext.get()))
259cdf0e10cSrcweir 	{
260cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't find object in current purpose \""));
261cdf0e10cSrcweir 		s_comment += clientPurpose;
262cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
263cdf0e10cSrcweir 	}
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	uno::Reference<lang::XComponent> xComponent(xContext, uno::UNO_QUERY_THROW);
266cdf0e10cSrcweir 	xComponent->dispose();
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
s_test__bootstrap_InitialComponentContext__free(void)269cdf0e10cSrcweir static void s_test__bootstrap_InitialComponentContext__free(void)
270cdf0e10cSrcweir {
271cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__bootstrap_InitialComponentContext__free\n"));
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 	s_test__bootstrap_InitialComponentContext(rtl::OUString());
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
s_test__bootstrap_InitialComponentContext__purpose(void)276cdf0e10cSrcweir static void s_test__bootstrap_InitialComponentContext__purpose(void)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir 	s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__bootstrap_InitialComponentContext__purpose\n"));
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	s_test__bootstrap_InitialComponentContext(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 
284cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv)
285cdf0e10cSrcweir {
286cdf0e10cSrcweir 	s_test__createSimpleRegistry__free();
287cdf0e10cSrcweir 	s_test__createSimpleRegistry__purpose();
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory__free_free();
290cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory__free_purpose();
291cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory__purpose_free();
292cdf0e10cSrcweir 	s_test__loadSharedLibComponentFactory__purpose_purpose();
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 	s_test__bootstrap_InitialComponentContext__free();
295cdf0e10cSrcweir 	s_test__bootstrap_InitialComponentContext__purpose();
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	int ret;
298cdf0e10cSrcweir 	if (s_comment.indexOf(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILURE"))) == -1)
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS PASSED\n"));
301cdf0e10cSrcweir 		ret = 0;
302cdf0e10cSrcweir 	}
303cdf0e10cSrcweir 	else
304cdf0e10cSrcweir 	{
305cdf0e10cSrcweir 		s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS _NOT_ PASSED\n"));
306cdf0e10cSrcweir 		ret = -1;
307cdf0e10cSrcweir 	}
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 	std::cerr
310cdf0e10cSrcweir 		<< argv[0]
311cdf0e10cSrcweir 		<< std::endl
312cdf0e10cSrcweir 		<< rtl::OUStringToOString(s_comment, RTL_TEXTENCODING_ASCII_US).getStr()
313cdf0e10cSrcweir 		<< std::endl;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	return ret;
316cdf0e10cSrcweir }
317