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