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 #include "sal/main.h"
29 #include <cppuhelper/shlib.hxx>
30 #include <cppuhelper/implbase1.hxx>
31 #include <cppu/EnvDcp.hxx>
32 
33 #include "../testcmp/TestComponent.hxx"
34 
35 #include <iostream>
36 #include <cstring>
37 
38 #ifndef SAL_DLLPREFIX
39 # define SAL_DLLPREFIX  ""
40 #endif
41 
42 
43 using namespace ::com::sun::star;
44 
45 
46 class MyKey : public cppu::WeakImplHelper1<registry::XRegistryKey>
47 
48 {
49 public:
50     virtual rtl::OUString SAL_CALL getKeyName() throw (uno::RuntimeException) { return rtl::OUString(); };
51 
52     // Methods
53     virtual sal_Bool SAL_CALL isReadOnly(  ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return sal_False;};
54     virtual sal_Bool SAL_CALL isValid(  ) throw (uno::RuntimeException) {return sal_False;};
55     virtual registry::RegistryKeyType SAL_CALL getKeyType( const rtl::OUString& /*rKeyName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return registry::RegistryKeyType_KEY;};
56     virtual registry::RegistryValueType SAL_CALL getValueType(  ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return registry::RegistryValueType_NOT_DEFINED;};
57     virtual sal_Int32 SAL_CALL getLongValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) {return 0;};
58     virtual void SAL_CALL setLongValue( sal_Int32 /*value*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
59     virtual uno::Sequence< sal_Int32 > SAL_CALL getLongListValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) { return uno::Sequence<sal_Int32>(); };
60     virtual void SAL_CALL setLongListValue( const uno::Sequence< sal_Int32 >& /*seqValue*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
61     virtual rtl::OUString SAL_CALL getAsciiValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) {return rtl::OUString();};
62     virtual void SAL_CALL setAsciiValue( const rtl::OUString& /*value*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
63     virtual uno::Sequence< rtl::OUString > SAL_CALL getAsciiListValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) {return uno::Sequence<rtl::OUString>();};
64     virtual void SAL_CALL setAsciiListValue( const uno::Sequence< rtl::OUString >& /*seqValue*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
65     virtual rtl::OUString SAL_CALL getStringValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) {return rtl::OUString();};
66     virtual void SAL_CALL setStringValue( const rtl::OUString& /*value*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
67     virtual uno::Sequence< rtl::OUString > SAL_CALL getStringListValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) {return uno::Sequence<rtl::OUString>();};
68     virtual void SAL_CALL setStringListValue( const uno::Sequence< rtl::OUString >& /*seqValue*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
69     virtual uno::Sequence< sal_Int8 > SAL_CALL getBinaryValue(  ) throw (registry::InvalidRegistryException, registry::InvalidValueException, uno::RuntimeException) {return uno::Sequence<sal_Int8>();};
70     virtual void SAL_CALL setBinaryValue( const uno::Sequence< sal_Int8 >& /*value*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
71     virtual uno::Reference< registry::XRegistryKey > SAL_CALL openKey( const rtl::OUString& /*aKeyName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return uno::Reference<registry::XRegistryKey>();};
72     virtual uno::Reference< registry::XRegistryKey > SAL_CALL createKey( const rtl::OUString& /*aKeyName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return uno::Reference<registry::XRegistryKey>();};
73     virtual void SAL_CALL closeKey(  ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
74     virtual void SAL_CALL deleteKey( const rtl::OUString& /*rKeyName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
75     virtual uno::Sequence< uno::Reference< registry::XRegistryKey > > SAL_CALL openKeys(  ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return uno::Sequence<uno::Reference<registry::XRegistryKey> >();};
76     virtual uno::Sequence< rtl::OUString > SAL_CALL getKeyNames(  ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return uno::Sequence<rtl::OUString>();};
77     virtual sal_Bool SAL_CALL createLink( const rtl::OUString& /*aLinkName*/, const rtl::OUString& /*aLinkTarget*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return sal_False;};
78     virtual void SAL_CALL deleteLink( const rtl::OUString& /*rLinkName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {};
79     virtual rtl::OUString SAL_CALL getLinkTarget( const rtl::OUString& /*rLinkName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return rtl::OUString();};
80     virtual rtl::OUString SAL_CALL getResolvedName( const rtl::OUString& /*aKeyName*/ ) throw (registry::InvalidRegistryException, uno::RuntimeException) {return rtl::OUString();};
81 };
82 
83 
84 
85 static rtl::OUString s_test__cppu_loadSharedLibComponentFactory(char const * pServicePurpose)
86 {
87 	rtl::OUString result;
88 
89 	rtl::OUString servicePurpose = rtl::OUString(pServicePurpose,
90                                                  rtl_str_getLength(pServicePurpose),
91                                                  RTL_TEXTENCODING_ASCII_US);
92 
93 	result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__cppu_loadSharedLibComponentFactory "));
94 	result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("(\""));
95 	result += servicePurpose;
96 	result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\") - "));
97 
98 	try {
99 		uno::Reference<uno::XInterface> xObject(
100 			cppu::loadSharedLibComponentFactory(
101 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SAL_DLLPREFIX "TestComponent.uno" SAL_DLLEXTENSION)),
102 				rtl::OUString(),
103                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent")) + servicePurpose,
104 				uno::Reference<lang::XMultiServiceFactory>(),
105 				uno::Reference<registry::XRegistryKey>())
106 			);
107 
108 		rtl::OUString envDcp_purpose(cppu::EnvDcp::getPurpose(g_envDcp));
109 		if (envDcp_purpose == servicePurpose)
110 			result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("passed\n"));
111 
112 		else
113 		{
114 			result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED - got: \""));
115 			result += envDcp_purpose;
116 			result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"\n"));
117 		}
118 	}
119 	catch(uno::Exception & exception) {
120 		result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED - got: \""));
121 		result += exception.Message;
122 		result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"\n"));
123 	}
124 
125 	return result;
126 }
127 
128 static rtl::OUString s_test__cppu_writeSharedLibComponentInfo(char const * pServicePurpose)
129 {
130 	rtl::OUString result;
131 
132 	rtl::OUString servicePurpose = rtl::OUString(pServicePurpose,
133                                                  rtl_str_getLength(pServicePurpose),
134                                                  RTL_TEXTENCODING_ASCII_US);
135 
136 	result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__cppu_writeSharedLibComponentInfo "));
137 	result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("(\""));
138 	result += servicePurpose;
139 	result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\") - "));
140 
141 	char buff[256];
142 	strcpy(buff, "TestComponent.uno=");
143 	strcat(buff, pServicePurpose);
144 
145 	putenv(buff);
146 
147 	try {
148 		cppu::writeSharedLibComponentInfo(
149 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SAL_DLLPREFIX "TestComponent.uno" SAL_DLLEXTENSION)),
150 			rtl::OUString(),
151 			uno::Reference<lang::XMultiServiceFactory>(),
152 			uno::Reference<registry::XRegistryKey>(new MyKey)
153 			);
154 
155 		rtl::OUString envDcp_purpose(cppu::EnvDcp::getPurpose(g_envDcp));
156 		if (envDcp_purpose == servicePurpose)
157 			result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("passed\n"));
158 
159 		else
160 		{
161 			result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED - got: \""));
162 			result += envDcp_purpose;
163 			result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"\n"));
164 		}
165 	}
166 	catch(uno::Exception & exception) {
167 		result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED - got: \""));
168 		result += exception.Message;
169 		result += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"\n"));
170 	}
171 
172 	return result;
173 }
174 
175 
176 SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv)
177 {
178 	int result = 0;
179 
180 	rtl::OUString message;
181 
182 	message += rtl::OUString(argv[0], rtl_str_getLength(argv[0]), RTL_TEXTENCODING_ASCII_US);
183 	message += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
184 
185 	message += s_test__cppu_loadSharedLibComponentFactory(":unsafe");
186 	message += s_test__cppu_loadSharedLibComponentFactory(":affine");
187 	message += s_test__cppu_loadSharedLibComponentFactory("");
188 
189 	message += s_test__cppu_writeSharedLibComponentInfo(":unsafe");
190 	message += s_test__cppu_writeSharedLibComponentInfo(":affine");
191 	message += s_test__cppu_writeSharedLibComponentInfo("");
192 
193 	if (message.indexOf(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED"))) == -1)
194 		message += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS PASSED\n"));
195 
196 	else
197 	{
198 		message += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS _NOT_ PASSED\n"));
199 		result = -1;
200 	}
201 
202 	std::cout << rtl::OUStringToOString(message, RTL_TEXTENCODING_ASCII_US).getStr();
203 
204 	return result;
205 }
206