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