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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 // Documentation about bootstraping can be found at:
28 // http://udk.openoffice.org/common/man/concept/micro_deployment.html
29 
30 #include <math.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <algorithm> // STL
34 
35 #include "gtest/gtest.h"
36 //#include "stringhelper.hxx"
37 //#include "valueequal.hxx"
38 #include <rtl/bootstrap.hxx>
39 
40 #include <rtl/ustrbuf.hxx>
41 #include <rtl/ustring.h>
42 #include <rtl/ustring.hxx>
43 #include <osl/file.hxx>
44 #include <osl/module.hxx>
45 #include <osl/process.h> /* osl_getExecutableFile() */
46 
47 #include <osl/thread.hxx>
48 
49 // using namespace osl;
50 using namespace rtl;
51 
52 #define TESTSHL2_INI "testshl2"
53 #define PSEUDO_INI   "pseudo"
54 
55 /** print a UNI_CODE String. And also print some comments of the string.
56  */
57 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
58 {
59 
60     if (strlen(msg) > 0)
61     {
62         printf("%s: ", msg );
63     }
64     rtl::OString aString;
65     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
66     printf("%s\n", (char *)aString.getStr( ) );
67 }
68 
69 /** if the file exist
70  */
71 bool t_fileExist(rtl::OUString const& _sFilename)
72 {
73     ::osl::FileBase::RC   nError1;
74     ::osl::File aTestFile( _sFilename );
75     nError1 = aTestFile.open ( OpenFlag_Read );
76     if ( ( ::osl::FileBase::E_NOENT != nError1 ) && ( ::osl::FileBase::E_ACCES != nError1 ) )
77     {
78         aTestFile.close( );
79         return true;
80     }
81     return false;
82 }
83 
84 /** get the exectutable path ( here is bootstrap_process), on Linux, such as "sal/unxlngi4.pro/bin/"
85   */
86 inline ::rtl::OUString getModulePath( void )
87 {
88     ::rtl::OUString suDirPath;
89     ::osl::Module::getUrlFromAddress( ( oslGenericFunction ) &getModulePath, suDirPath );
90 
91     suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') );
92     suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') + 1);
93     suDirPath += rtl::OUString::createFromAscii("bin");
94     return suDirPath;
95 }
96 
97 #define TESTSHL2_INI "testshl2"
98 #define PSEUDO_INI   "pseudo"
99 
100 
101 static rtl::OUString getExecutableDirectory()
102 {
103     rtl::OUString fileName;
104     osl_getExecutableFile(&fileName.pData);
105 
106     sal_Int32 nDirEnd = fileName.lastIndexOf('/');
107 
108     OSL_ENSURE(nDirEnd >= 0, "Cannot locate executable directory");
109 
110     rtl::OUString aDirURL = fileName.copy(0, nDirEnd);
111     return aDirURL;
112 }
113 
114 
115 // get the URL of testshl2rc/rtlrc/pseudorc
116 inline rtl::OUString t_getSourcePath(rtl::OString const& _sFilename)
117 {
118 
119      rtl::OUString aDirURL(getExecutableDirectory());
120      aDirURL += OUString::createFromAscii( "/");
121      aDirURL += OUString::createFromAscii( _sFilename.getStr() );
122 #if defined(WNT) || defined(OS2)
123     aDirURL += rtl::OUString::createFromAscii(".ini");
124 #else
125     aDirURL += rtl::OUString::createFromAscii("rc");
126 #endif
127     return aDirURL;
128 
129 // LLA: does not right work on my personal laptop, SRC_ROOT does not show where the source is :-(.
130 /*
131   sal_Char *  pStr = getenv("SRC_ROOT");
132   rtl::OUString suPath;
133   if (filename != "")
134   {
135   suPath = rtl::OUString::createFromAscii(pStr) + rtl::OUString::createFromAscii( "/sal/qa/rtl/bootstrap/" )
136   + rtl::OUString::createFromAscii( filename );
137   }
138   else
139   {
140   suPath = rtl::OUString::createFromAscii(pStr) + rtl::OUString::createFromAscii( "/sal/qa/rtl/bootstrap" );
141   }
142   rtl::OUString suURL;
143   ::osl::FileBase::getFileURLFromSystemPath( suPath, suURL );
144   return suURL;
145 */
146 }
147 
148 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
149 {
150 #ifdef WNT      //Windows
151     Sleep(_nTenthSec * 100 );
152 #endif
153 #if ( defined UNX ) || ( defined OS2 )  //Unix
154     TimeValue nTV;
155     nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
156     nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
157     osl_waitThread(&nTV);
158 #endif
159 }
160 
161 // -----------------------------------------------------------------------------
162 
163 namespace rtl_Bootstrap
164 {
165     class ctor : public ::testing::Test
166     {
167     public:
168         // initialise your test code values here.
169         void SetUp()
170         {
171         }
172 
173         void TearDown()
174         {
175         }
176     }; // class ctor
177 
178     // ctor with ini name
179     TEST_F(ctor, ctor_001)
180     {
181         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
182         printUString( suIniname );
183         Bootstrap aBootstrap( suIniname );
184         rtl::OUString suGetname; // = rtl::OUString::createFromAscii("");
185         aBootstrap.getIniName( suGetname );
186         printUString( suGetname );
187 
188         // LLA: first: this seems to be a wrong test.
189         //      second: there seems to be a design hole, if I give a absolute path ini file,
190         //              but try to use ${file::KEYVALUE} than 'file' will only used out of the 'executable path'/file
191         //              not from the path given from the absolute path.
192 
193         // Due to the fact, we create at this position a file (createTestshl2rc() ), we check for existence
194         bool bFileExist = t_fileExist( suGetname );
195         ASSERT_TRUE(bFileExist == true) << "ctor error with initial file.";
196     }
197 
198     TEST_F(ctor, ctor_002)
199     {
200         rtl::Bootstrap aBootstrap;
201         rtl::OUString suGetname;
202         aBootstrap.getIniName( suGetname );
203         printUString( suGetname );
204         ASSERT_TRUE(suGetname.getLength() != 0) << "ctor error without initial file.";
205     }
206 
207     class getFrom : public ::testing::Test
208     {
209     public:
210         // initialise your test code values here.
211         void SetUp()
212         {
213         }
214 
215         void TearDown()
216         {
217         }
218 
219 	/** helper function: return the child process's ret value( typedef sal_uInt32 oslProcessExitCode;)
220 	   * param1 is the process's name(only file name, not include path)
221 	  */
222 	 oslProcessExitCode ini_execProcess( const sal_Char* process_name, const sal_Char * flag )
223 	 {
224 	 	rtl::OUString suCWD = getModulePath();
225     		oslProcess hProcess = NULL;
226    		rtl::OUString suFileURL = suCWD;
227         	suFileURL += rtl::OUString::createFromAscii("/") +  rtl::OUString::createFromAscii(process_name) ;
228 #if defined(WNT) || defined(OS2)
229 		suFileURL += rtl::OUString::createFromAscii(".exe");
230 #endif
231         	const int nParameterCount = 3;
232     		rtl_uString* pParameters[ nParameterCount ];
233     		OUString suFlag( OUString::createFromAscii(flag) );
234 		OUString suEnv1( OUString::createFromAscii("-env:UNO_SERVICES=service.rdb"));
235 		OUString suIniname = t_getSourcePath("rtl");
236 		printUString( suIniname, "rtl path:");
237 		//OUString suEnv2( OUString::createFromAscii("-env:MYENV=bootstrap_process"));
238 
239         	pParameters[0] = suFlag.pData;
240         	pParameters[1] = suEnv1.pData;
241         	// the custom ini/rc file's URL
242         	pParameters[2] = suIniname.pData;
243 
244         	oslProcessError osl_error = osl_executeProcess(
245         	    suFileURL.pData,
246         	    pParameters,
247             	    nParameterCount,
248         	    osl_Process_WAIT,
249         	    0,
250         	    suCWD.pData,
251         	    NULL,
252         	    0,
253         	    &hProcess );
254 
255         	EXPECT_TRUE(osl_error == osl_Process_E_None)
256         	    << "osl_createProcess failed";
257 	       	osl_joinProcess(hProcess);
258         	oslProcessInfo* pInfo = new oslProcessInfo;
259 		pInfo->Size = sizeof( oslProcessInfo );
260 		osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo );
261 		EXPECT_TRUE(osl_Process_E_None == osl_error)
262         	    << "osl_getProcessInfo returned with failure";
263 
264 		printf("the exit code is %d.\n", pInfo->Code );
265 		oslProcessExitCode nCode = pInfo->Code;
266 		delete pInfo;
267 	 	return nCode;
268 	}
269 
270     }; // class getFrom
271 
272     // get the value of env variable
273     TEST_F(getFrom, getFrom_001)
274         {
275             Bootstrap aBootstrap;
276             rtl::OUString suValue;
277             rtl::OUString suValuename = rtl::OUString::createFromAscii( "SOLAR_JAVA" );
278             //aBootstrap.getFrom( suValuename, suValue );
279             aBootstrap.getFrom( suValuename, suValue );
280             sal_Char *  pStr = getenv("SOLAR_JAVA");
281             //      printUString( suGetname );
282             ASSERT_TRUE(suValue.compareToAscii( pStr ) == 0) << "get the value of environment variable.";
283         }
284     /* Notes on Windows:
285        void getFrom_001_1()
286        {
287        Bootstrap aBootstrap;
288        rtl::OUString suValue;
289        rtl::OUString suValuename = rtl::OUString::createFromAscii( "SRC_ROOT" );
290        //aBootstrap.getFrom( suValuename, suValue );
291        aBootstrap.getFrom( suValuename, suValue );
292        sal_Char *  pStr = getenv("SRC_ROOT");
293        //   printUString( suGetname );
294        ASSERT_TRUE(suValue.compareToAscii( pStr ) == 0) << "get the value of environment variable.";
295        }
296        The result on Windows:
297        # # the SRC_ROOT is e:\Qadev\cvs\m19
298        # # suValue is e:Qadevcvsm19
299        reason:
300        The problem is that the internally getenv()ed variable SRC_ROOT is macro expanded,
301        thus every \ will introduce an escape.
302     */
303 
304     // get the value of a variable in ini file
305     TEST_F(getFrom, getFrom_002)
306     {
307         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
308         Bootstrap aBootstrap( suIniname );
309         rtl::OUString suGetname;
310         rtl::OUString suValuename = rtl::OUString::createFromAscii( "INHERITED_VALUE" );
311         aBootstrap.getFrom( suValuename, suGetname );
312         printUString( suGetname );
313         ASSERT_TRUE(suGetname.getLength() != 0) << "get the value of a variable in ini file.";
314     }
315 
316     //use defaut value
317     TEST_F(getFrom, getFrom_003)
318     {
319         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
320         Bootstrap aBootstrap( suIniname );
321         rtl::OUString suGetname;
322         rtl::OUString suValuename = rtl::OUString::createFromAscii( "MY_VALUE" );
323         rtl::OUString myDefault = rtl::OUString::createFromAscii( "2" );
324         aBootstrap.getFrom( suValuename, suGetname, myDefault );
325         //printUString( suGetname );
326         ASSERT_TRUE(suGetname.compareTo( myDefault ) == 0) << "getFrom use default.";
327     }
328 
329     TEST_F(getFrom, getFrom_004)
330     {
331         printf("1\n");
332         // initialise Bootstrap with an own ini file
333         // PSEUDO_INI is pseudo(rc|.ini) created be create_pseudorc()
334         rtl::OUString suIniname = t_getSourcePath(PSEUDO_INI);
335         Bootstrap aBootstrap( suIniname );
336 
337         rtl::OUString suGetIniName;
338         aBootstrap.getIniName( suGetIniName );
339 
340         printUString(suGetIniName, "Current bootstrap file");
341         sal_Int32 nIndex = suGetIniName.indexOf(rtl::OUString::createFromAscii( "pseudo" ));
342         ASSERT_TRUE(nIndex > 0) << "ini name must have 'pseudo' in name.";
343 
344         // rtlBootstrapHandle bsHandle = aBootstrap.getHandle();
345         // ASSERT_TRUE(bsHandle != 0) << "getHandle return NULL!";
346 
347         rtl::OUString suValue;
348         rtl::OUString suKeyName = rtl::OUString::createFromAscii( "FILE" );
349         aBootstrap.getFrom( suKeyName, suValue );
350         printUString( suValue );
351         sal_Int32 nCompare = suValue.compareTo( rtl::OUString::createFromAscii("pseudo file") );
352 
353         ASSERT_TRUE(nCompare == 0)
354             << "<Bootstrap('pseudo')>.getFrom('FILE', ...) result is unexpected.";
355     }
356 
357     TEST_F(getFrom, getFrom_004_1)
358     {
359         // get the same key out of the default context
360         rtl::OUString suKeyName = rtl::OUString::createFromAscii( "FILE" );
361         rtl::OUString suGetValue;
362         Bootstrap::get( suKeyName, suGetValue );
363         printUString( suGetValue );
364 
365         ASSERT_TRUE(suGetValue.compareTo( rtl::OUString::createFromAscii("testshl2 file") ) == 0)
366             << "Bootstrap::get('FILE', ...)";
367     }
368 
369     TEST_F(getFrom, getFrom_005_1)
370     {
371         oslProcessExitCode nExitCode = ini_execProcess( "bootstrap_process", "1" );
372         ASSERT_TRUE(nExitCode == 10) << "Parameters passed by command line can not be gotten!";
373     }
374     TEST_F(getFrom, getFrom_005_2)
375     {
376         oslProcessExitCode nExitCode = ini_execProcess( "bootstrap_process", "2" );
377         ASSERT_TRUE(nExitCode == 20) << "Parameters passed by .ini/rc file can not be gotten!";
378     }
379     TEST_F(getFrom, getFrom_005_3)
380     {
381 #if (defined WNT) || (defined SOLARIS)
382         putenv(const_cast< char * >("QADEV_BOOTSTRAP=sun&ms"));
383 #else
384         setenv("QADEV_BOOTSTRAP", "sun&ms", 0);
385 #endif
386         oslProcessExitCode nExitCode = ini_execProcess( "bootstrap_process", "3" );
387         ASSERT_TRUE(nExitCode == 30) << "Parameters passed by environment variables can not be gotten!";
388     }
389     TEST_F(getFrom, getFrom_005_4)
390     {
391         oslProcessExitCode nExitCode = ini_execProcess( "bootstrap_process", "4" );
392         ASSERT_TRUE(nExitCode == 40) << "Parameters passed by customed .ini/rc file can not be gotten!";
393     }
394     TEST_F(getFrom, getFrom_005_5)
395     {
396         oslProcessExitCode nExitCode = ini_execProcess( "bootstrap_process", "5" );
397         ASSERT_TRUE(nExitCode == 50) << "Parameters passed by inheritance can not be gotten!";
398     }
399     TEST_F(getFrom, getFrom_005_6)
400     {
401         oslProcessExitCode nExitCode = ini_execProcess( "bootstrap_process", "6" );
402         ASSERT_TRUE(nExitCode == 60) << "Parameters passed by default can not be gotten!";
403     }
404 
405     class setIniFilename : public ::testing::Test
406     {
407     public:
408         // initialise your test code values here.
409         void SetUp()
410         {
411         }
412 
413         void TearDown()
414         {
415         }
416     }; // class setIniFilename
417 
418     TEST_F(setIniFilename, setIniFilename_001)
419     {
420         Bootstrap aBootstrap;
421 
422         rtl::OUString suGetIniname;
423         aBootstrap.getIniName( suGetIniname );
424         //which should be .....testshl2rc
425         //printUString( suGetIniname );
426 
427         rtl::OUString suIniname = t_getSourcePath(PSEUDO_INI);
428         Bootstrap::setIniFilename( suIniname );
429 
430         rtl::OUString suGetname;
431         aBootstrap.getIniName( suGetname );
432 
433         printUString( suGetname );
434         ASSERT_TRUE(suGetname.compareTo( suIniname ) == 0
435                                && suGetname.compareTo( suGetIniname ) != 0) << "setIniFilename then get it.";
436     }
437 
438     TEST_F(setIniFilename, setIniFilename_002)
439     {
440         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
441         // ASSERT_TRUE(t_fileExist(suIniname ) == true) <<
442         //     "test failed, Bootstrap ini does not exist."
443 
444         Bootstrap::setIniFilename( suIniname );
445         //rtl_bootstrap_args_open( suIniname.pData );
446         rtl::OUString suGetname;
447         rtl::OUString suValuename = rtl::OUString::createFromAscii( "INHERITED_VALUE" );
448         //aBootstrap.getFrom( suValuename, suGetname  );
449         Bootstrap::get( suValuename, suGetname  );
450         printUString( suGetname );
451         ASSERT_TRUE(suGetname.getLength() != 0) << "setIniFilename and get value of the argument.";
452     }
453 
454     class getHandle : public ::testing::Test
455     {
456     public:
457         // initialise your test code values here.
458         void SetUp()
459         {
460         }
461 
462         void TearDown()
463         {
464         }
465     }; // class getHandle
466 
467     TEST_F(getHandle, getHandle_001)
468     {
469         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
470         Bootstrap aBootstrap;
471         rtlBootstrapHandle bsHandle = aBootstrap.getHandle();
472         ASSERT_TRUE(bsHandle == 0) << "getHandle should return 0 if the bootstrap has no ini file!";
473     }
474     TEST_F(getHandle, getHandle_002)
475     {
476         rtl::OUString suIniname = t_getSourcePath(PSEUDO_INI);
477         Bootstrap aBootstrap( suIniname );
478 
479         rtlBootstrapHandle bsHandle = aBootstrap.getHandle();
480         ASSERT_TRUE(bsHandle != 0) << "getHandle return NULL!";
481 
482         //rtl::OUString iniName;
483         //rtl_bootstrap_get_iniName_from_handle( bsHandle, &iniName.pData );
484 
485         rtl::OUString suValue;
486         rtl::OUString suKeyName = rtl::OUString::createFromAscii( "PSEUDOFILE" );
487         rtl_bootstrap_get_from_handle(bsHandle, suKeyName.pData, &suValue.pData, NULL);
488         printUString( suValue);
489 
490         ASSERT_TRUE(suValue.equals( rtl::OUString::createFromAscii("be pseudo") ) == sal_True) << "Can not use the handle which is returned by getHandle!";
491 
492         // ASSERT_TRUE(//      suGetname.equalsIgnoreAsciiCase( iniName ) == sal_True) << "Can not use the handle which is returned by getHandle!";
493     }
494 
495     class set : public ::testing::Test
496     {
497     public:
498         // initialise your test code values here.
499         void SetUp()
500         {
501         }
502 
503         void TearDown()
504         {
505         }
506     }; // class set
507 
508     TEST_F(set, set_001)
509     {
510         //in ini fle, INHERITED_VALUE=inherited_value
511         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
512         Bootstrap aBootstrap( suIniname);
513         rtl::OUString suName = rtl::OUString::createFromAscii( "INHERITED_VALUE" );
514         rtl::OUString suValue = rtl::OUString::createFromAscii( "ok" );
515         // set to another value
516         Bootstrap::set( suName, suValue );
517         rtl::OUString suGetValue;
518         Bootstrap::get( suName, suGetValue);
519         ASSERT_TRUE(suGetValue.compareTo(suValue) == 0) << "set and get argument failed.";
520     }
521     TEST_F(set, set_002)
522     {
523         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
524         Bootstrap myBootstrap( suIniname);
525         rtl::OUString suName = rtl::OUString::createFromAscii( "INHERITED_VALUE" );
526         rtl::OUString suGetOrientValue;
527         Bootstrap::get( suName, suGetOrientValue);
528         // ??  INHERITED_VALUE = ok now, which is set in set_001
529         printUString( suGetOrientValue );
530 
531         rtl::OUString suValue = rtl::OUString::createFromAscii( TESTSHL2_INI );
532         // set to another value
533         Bootstrap::set( suName, suValue );
534         rtl::OUString suGetValue;
535         Bootstrap::get( suName, suGetValue);
536         ASSERT_TRUE(suGetValue.compareTo(suValue) == 0) << "set and get argument failed.";
537     }
538 
539     class expandMacrosFrom : public ::testing::Test
540     {
541     public:
542         void SetUp()
543         {
544         }
545 
546         void TearDown()
547         {
548         }
549     }; // class expandMacrosFrom
550 
551     TEST_F(expandMacrosFrom, expandMacrosFrom_001)
552     {
553         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
554         Bootstrap aBootstrap( suIniname);
555         rtl::OUString suMacro = rtl::OUString::createFromAscii( "$MYVAR/expand1" );
556         //printUString( suMacro );
557         //expandMacro now
558         aBootstrap.expandMacrosFrom( suMacro );
559         rtl::OUString suExpectedMacro = rtl::OUString::createFromAscii( "src680_test/expand1" );
560         //printUString( suMacro );
561         ASSERT_TRUE(suMacro.compareTo(suExpectedMacro) == 0) << "expandMacrosFrom failed.";
562     }
563 
564     /** here a special macro should expand
565       * if rtlrc is under sal/qa/rtl/bootstrap/, "${rtlrc:Bootstrap:RTLVALUE}" could be expanded
566       * else rtlrc is under solver/680/unxlngi4.pro/bin/, "${file:/// ....solver/680/unxlngi4.pro/bin/rtlrc:Bootstrap:RTLVALUE}"
567   * could not be expanded
568   */
569     TEST_F(expandMacrosFrom, expandMacrosFrom_002)
570     {
571         // Build a string with '${rtl.ini:RTLVALUE}' and try to expand it.
572         // In function 'create_rtlrc() is the content of the rtl.ini file.
573 
574         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
575         printf("inifile is:");
576         printUString( suIniname );
577         Bootstrap aBootstrap( suIniname) ;
578         rtl::OUString suMacro = rtl::OUString::createFromAscii( "${" );         //rtlrc:Bootstrap:RTLVALUE}");
579 
580 rtl::OUString aDirURL = OUString::createFromAscii( "$ORIGIN");
581 aDirURL += OUString::createFromAscii( "/");
582 aDirURL += OUString::createFromAscii( "rtl" );
583 #if defined(WNT) || defined(OS2)
584 aDirURL += rtl::OUString::createFromAscii(".ini");
585 #else
586 aDirURL += rtl::OUString::createFromAscii("rc");
587 #endif
588 
589         suMacro += aDirURL;//t_getSourcePath("rtl");
590         suMacro += rtl::OUString::createFromAscii( "::RTLVALUE}");
591 
592         printf("created macro is: ");
593         printUString( suMacro );
594         //expandMacro now
595         aBootstrap.expandMacrosFrom( suMacro );
596         printf("expanded macro is:");
597         printUString( suMacro );
598         rtl::OUString suExpectedMacro = rtl::OUString::createFromAscii( "qadev17" );
599         ASSERT_TRUE(suMacro.compareTo(suExpectedMacro) == 0 )
600             << "failed, can't expand '${file:///.../" SAL_CONFIGFILE("rtl") "::RTLVALUE}' to 'qadev17'";
601     }
602     TEST_F(expandMacrosFrom, expandMacrosFrom_002_1)
603     {
604         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
605         printf("inifile is:");
606         printUString( suIniname );
607         Bootstrap aBootstrap( suIniname);
608 
609         rtl::OUString suMacro;
610         // just a simple test, if this really work.
611         aBootstrap.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE2" ), suMacro );
612         printf("SOFROMVALUE2:");
613         printUString( suMacro );
614         ASSERT_TRUE(suMacro.getLength() > 0) << "'SOFROMVALUE2' seems to do not exist.";
615 
616         aBootstrap.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE" ), suMacro );
617 
618         printf("SOFROMVALUE:");
619         printUString( suMacro );
620 
621         //expandMacro now
622         // seems to be, that getFrom() already expand the string
623         // printf("expanded macro is:");
624         // aBootstrap.expandMacrosFrom( suMacro );
625         // printUString( suMacro );
626         rtl::OUString suExpectedMacro = rtl::OUString::createFromAscii( "src680_qadev" );
627         ASSERT_TRUE(suMacro.compareTo(suExpectedMacro) == 0)
628             << "failed, can't expand '${" SAL_CONFIGFILE("rtl") "::SOVALUE}' to 'src680_qadev'";
629     }
630     TEST_F(expandMacrosFrom, expandMacrosFrom_002_2)
631     {
632         // test, to read and expand SOFROMVALUE3
633         // SOFROMVALUE3 is 'rtl(.ini|rc)::TESTSHL_SOVALUE' which should expand to 'rtlfile' if all is ok.
634 
635         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
636         printf("inifile is:");
637         printUString( suIniname );
638         Bootstrap aBootstrap( suIniname);
639 
640         rtl::OUString suMacro;
641         aBootstrap.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE3" ), suMacro );
642 
643         printf("SOFROMVALUE3:");
644         printUString( suMacro );
645 
646         if (suMacro.equals(rtl::OUString::createFromAscii("testshl2_file") ) == sal_True)
647         {
648             ASSERT_TRUE(0) << "Value 'SOFROMVALUE3' is read from the wrong ini file.";
649         }
650         else
651         {
652             ASSERT_TRUE(suMacro.equals(rtl::OUString::createFromAscii("rtlfile") ) == sal_True) << "SOFROMVALUE3 should contain 'rtlfile'.";
653         }
654     }
655 //? I don't know if this is a right test.
656 //         void expandMacrosFrom_002_3()
657 //             {
658 //                 // test, to read and expand SOFROMVALUE4
659 //                 // SOFROMVALUE4 is 'rtl(.ini|rc):Other_Section:TESTSHL_SOVALUE' which should expand to '' if all is ok.
660 //
661 //                 rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
662 //                 printf("inifile is:");
663 //              printUString( suIniname );
664 //                 Bootstrap aBootstrap( suIniname);
665 //
666 //                 rtl::OUString suMacro;
667 //                 aBootstrap.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE4" ), suMacro );
668 //
669 //                 printf("SOFROMVALUE4:");
670 //                 printUString( suMacro );
671 //
672 //                 if (suMacro.equals(rtl::OUString::createFromAscii("testshl2_file") ) == sal_True)
673 //                 {
674 //                     ASSERT_TRUE(0) << "Value 'SOFROMVALUE4' is read from the wrong section out of the wrong ini file.";
675 //                 }
676 //                 else if (suMacro.equals(rtl::OUString::createFromAscii("testshl2_file_other") ) == sal_True)
677 //                 {
678 //                     ASSERT_TRUE(0) << "Value 'SOFROMVALUE4' is read from the wrong ini file.";
679 //                 }
680 //                 else
681 //                 {
682 //                     ASSERT_TRUE(suMacro.equals(rtl::OUString::createFromAscii("rtlfile_other") ) == sal_True) << "Value 'SOFROMVALUE4' should contain 'rtlfile_other'.";
683 //                 }
684 //             }
685     TEST_F(expandMacrosFrom, expandMacrosFrom_003)
686     {
687         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
688         Bootstrap aBootstrap( suIniname);
689         rtl::OUString suMacro[4];
690         suMacro[0] = rtl::OUString::createFromAscii( "$SYSUSERCONFIG" );
691         suMacro[1] = rtl::OUString::createFromAscii( "$SYSUSERHOME" );
692         suMacro[2] = rtl::OUString::createFromAscii( "$SYSBINDIR" );
693         suMacro[3] = rtl::OUString::createFromAscii( "$ORIGIN" );
694 
695         for ( int i = 0; i < 4; i++ )
696         {
697             aBootstrap.expandMacrosFrom( suMacro[i] );
698             printUString(suMacro[i]);
699         }
700         // printUString( t_getSourcePath("") );
701         // ASSERT_TRUE(suMacro[3].equalsIgnoreAsciiCase(t_getSourcePath("")) == sal_True) << "some integral variables.";
702         ASSERT_TRUE(suMacro[0].getLength() > 0 &&
703                                suMacro[1].getLength() > 0 &&
704                                suMacro[2].getLength() > 0 &&
705                                suMacro[3].getLength() > 0) << "some integral variables.";
706     }
707 
708     TEST_F(expandMacrosFrom, testRecursion) {
709         rtl::OUString t(RTL_CONSTASCII_USTRINGPARAM("$RECURSIVE"));
710         Bootstrap(t_getSourcePath(TESTSHL2_INI)).expandMacrosFrom(t);
711         ASSERT_TRUE(t.equalsAsciiL(
712                 RTL_CONSTASCII_STRINGPARAM("***RECURSION DETECTED***"))) << "recursion detection";
713     }
714 
715     TEST_F(expandMacrosFrom, testLink) {
716         rtl::OUString t(RTL_CONSTASCII_USTRINGPARAM("$LINKED"));
717         Bootstrap(t_getSourcePath(TESTSHL2_INI)).expandMacrosFrom(t);
718         ASSERT_TRUE(t.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("qadev17"))) << "link file";
719     }
720 
721     TEST_F(expandMacrosFrom, testOverride) {
722         rtl::OUString t1(
723             RTL_CONSTASCII_USTRINGPARAM(
724                 "${.override:$ORIGIN/" SAL_CONFIGFILE("rtl") ":ORIGIN}"));
725         Bootstrap(t_getSourcePath("rtl")).expandMacrosFrom(t1);
726         ASSERT_TRUE(t1.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("direct"))) << "override ORIGIN";
727         rtl::OUString t2(
728             RTL_CONSTASCII_USTRINGPARAM(
729                 "${.override:$ORIGIN/" SAL_CONFIGFILE("none") ":MYVAR}"));
730         Bootstrap::expandMacros(t2);
731         ASSERT_TRUE(t2.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("src680_test"))) << "override MYVAR";
732     }
733 
734     TEST_F(expandMacrosFrom, testNonexisting) {
735         rtl::OUString t(
736             RTL_CONSTASCII_USTRINGPARAM(
737                 "${$ORIGIN/" SAL_CONFIGFILE("none") ":MYVAR}"));
738         Bootstrap::expandMacros(t);
739         ASSERT_TRUE(t.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("src680_test"))) << "nonexisting";
740     }
741 
742     TEST_F(expandMacrosFrom, testSection) {
743         rtl::OUStringBuffer b;
744         b.appendAscii(RTL_CONSTASCII_STRINGPARAM("${"));
745         rtl::OUString p(t_getSourcePath(TESTSHL2_INI));
746         for (sal_Int32 i = 0; i < p.getLength(); ++i) {
747             if (p[i] != 'u') {
748                 b.append(static_cast< sal_Unicode >('\\'));
749             }
750             b.append(p[i]);
751         }
752         b.appendAscii(RTL_CONSTASCII_STRINGPARAM(":Other_Section:EXPAND}"));
753         rtl::OUString t(b.makeStringAndClear());
754         Bootstrap(t_getSourcePath(TESTSHL2_INI)).expandMacrosFrom(t);
755         ASSERT_TRUE(t.equalsAsciiL(
756                 RTL_CONSTASCII_STRINGPARAM("$FILE"))) << "section expansion";
757             // the correct answer would be "testshl2 file" instead, but
758             // expansion including a section currently erroneously does not
759             // recursively expand macros in the resulting replacement text
760     }
761 
762     class expandMacros : public ::testing::Test
763     {
764     public:
765         // initialise your test code values here.
766         void SetUp()
767         {
768         }
769 
770         void TearDown()
771         {
772         }
773     }; // class expandMacros
774 
775 
776     TEST_F(expandMacros, expandMacros_001)
777     {
778         rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
779         Bootstrap aBootstrap( suIniname) ;
780         rtl::OUString suMacro = rtl::OUString::createFromAscii( "$INHERITED_VALUE/well" );
781         Bootstrap::expandMacros( suMacro );
782 
783         rtl::OUString suName = rtl::OUString::createFromAscii( "INHERITED_VALUE" );
784         OUString suGetValue;
785         Bootstrap::get( suName, suGetValue );
786         suGetValue += OUString::createFromAscii( "/well" );
787         ASSERT_TRUE(suGetValue.compareTo(suMacro) == 0) << "expandMacros failed.";
788     }
789 } // namespace rtl_Bootstrap
790 
791 // -----------------------------------------------------------------------------
792 
793 // this macro creates an empty function, which will called by the RegisterAllFunctions()
794 // to let the user the possibility to also register some functions by hand.
795 // NOADDITIONAL;
796 
797 
798 // Here are some helpers, which create a new file 'rtlrc' at the executable path position
799 // and fill the file with some information.
800 // static rtl::OUString getExecutableDirectory()
801 // {
802 //     rtl::OUString fileName;
803 //     osl_getExecutableFile(&fileName.pData);
804 //
805 //     sal_Int32 nDirEnd = fileName.lastIndexOf('/');
806 //
807 //     OSL_ENSURE(nDirEnd >= 0, "Cannot locate executable directory");
808 //
809 //     rtl::OUString aDirURL = fileName.copy(0, nDirEnd);
810 //     return aDirURL;
811 // }
812 
813 static void removeAndCreateFile(rtl::OUString const& _suFileURL, rtl::OString const& _sContent)
814 {
815     osl::File::remove(_suFileURL);
816 
817     ::std::auto_ptr<osl::File> pFile( new osl::File( _suFileURL ) );
818     ::osl::FileBase::RC nError = pFile->open( OpenFlag_Write | OpenFlag_Create );
819     if ( ::osl::FileBase::E_None == nError || ::osl::FileBase::E_EXIST == nError )
820     {
821         printf("%s\n" , OString(_suFileURL.getStr(), _suFileURL.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
822         sal_uInt64 nWritenBytes;
823         pFile->write(_sContent.getStr(), _sContent.getLength(), nWritenBytes);
824         // printf("nBytes: %ld\n", nBytes);
825 
826         rtl::OString sError = "can't write enough bytes to file";
827         sError += OString(_suFileURL.getStr(), _suFileURL.getLength(), RTL_TEXTENCODING_ASCII_US);
828         OSL_ENSURE(nWritenBytes == _sContent.getLength(), sError.getStr());
829 
830         pFile->close();
831     }
832     else
833     {
834         rtl::OString sError = "can't create file URL: '";
835         rtl::OString sFile(_suFileURL.getStr(), _suFileURL.getLength(), RTL_TEXTENCODING_ASCII_US);
836         sError += sFile;
837         sError += "' maybe no write access. If it is true with no write access, please create a local environment and start these tests again. rtl::Bootstrap test must quit.";
838         printf("%s\n", sError.getStr() );
839         exit(1);
840     }
841     OSL_ASSERT(t_fileExist(_suFileURL) == true);
842 }
843 
844 // -----------------------------------------------------------------------------
845 static void create_rtlrc()
846 {
847     rtl::OUString aFileURL(getExecutableDirectory());
848 #if defined(WNT) || defined(OS2)
849     aFileURL += rtl::OUString::createFromAscii("/rtl.ini");
850 #else
851     aFileURL += rtl::OUString::createFromAscii("/rtlrc");
852 #endif
853 
854     rtl::OString sLines;
855     sLines += "[Bootstrap]\n";
856     sLines += "SOVALUE=src680_qadev\n";
857     sLines += "RTLVALUE=qadev17\n";
858     sLines += "TESTSHL_SOVALUE=rtlfile\n";
859     sLines += "RECURSIVE=${$ORIGIN/" SAL_CONFIGFILE("testshl2") ":RECURSIVE}\n";
860     sLines += "ORIGIN=direct\n";
861     sLines += "[Other_Section]\n";
862     sLines += "TESTSHL_SOVALUE=rtlfile_other\n";
863 
864     removeAndCreateFile(aFileURL, sLines);
865 }
866 
867 // -----------------------------------------------------------------------------
868 static void create_testshl2rc()
869 {
870     rtl::OUString aFileURL(getExecutableDirectory());
871 #if defined(WNT) || defined(OS2)
872     aFileURL += rtl::OUString::createFromAscii("/testshl2.ini");
873 #else
874     aFileURL += rtl::OUString::createFromAscii("/testshl2rc");
875 #endif
876     rtl::OString sLines;
877     sLines += "[Bootstrap]\n";
878     sLines += "FILE=testshl2 file\n";
879     sLines += "MYBOOTSTRAPTESTVALUE=file\n";
880     sLines += "INHERITED_VALUE=inherited_value\n";
881     sLines += "INHERITED_OVERWRITTEN_VALUE=not_overwritten\n";
882     sLines += "MYVAR=src680_test\n";
883     sLines += "SOFROMVALUE=${$ORIGIN/" SAL_CONFIGFILE("rtl") "::SOVALUE}\n";
884     sLines += "SOFROMVALUE2=test\n";
885     sLines += "SOFROMVALUE3=${$ORIGIN/" SAL_CONFIGFILE("rtl") "::TESTSHL_SOVALUE}\n";
886     sLines += "TESTSHL_SOVALUE=testshl2_file\n";
887     //? sLines += "SOFROMVALUE4=${" SAL_CONFIGFILE("rtl") ":Other_Section:TESTSHL_SOVALUE}\n";
888     sLines += "ILLEGAL VALUE=test\n";
889     sLines += "ILLEGAL.DOT=test\n";
890     sLines += "ILLEGAL;SEMICOLON=test\n";
891     sLines += "ILLEGAL:COLON=test\n";
892     sLines += "  KEY_FOR_TRIM_TEST  =   value for trim test    \n";
893     sLines += "RECURSIVE=${$ORIGIN/" SAL_CONFIGFILE("rtl") ":RECURSIVE}\n";
894     sLines += "LINKED=${${.link:$ORIGIN/testshl2-link}:RTLVALUE}\n";
895     sLines += "[Other_Section]\n";
896     sLines += "FILE=testshl2 file other\n";
897     sLines += "EXPAND=$FILE\n";
898     //? sLines += "TESTSHL_SOVALUE=testshl2_file_other\n";
899 
900     removeAndCreateFile(aFileURL, sLines);
901 
902     removeAndCreateFile(
903         (getExecutableDirectory() +
904          rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/testshl2-link"))),
905         SAL_CONFIGFILE("rtl"));
906 }
907 
908 // -----------------------------------------------------------------------------
909 
910 static void create_pseudorc()
911 {
912     rtl::OUString aFileURL(getExecutableDirectory());
913 #if defined(WNT) || defined(OS2)
914     aFileURL += rtl::OUString::createFromAscii("/pseudo.ini");
915 #else
916     aFileURL += rtl::OUString::createFromAscii("/pseudorc");
917 #endif
918     rtl::OString sLines;
919     sLines += "[Bootstrap]\n";
920     sLines += "FILE=pseudo file\n";
921     sLines += "PSEUDOFILE=be pseudo\n";
922 
923     removeAndCreateFile(aFileURL, sLines);
924 }
925 
926 // -----------------------------------------------------------------------------
927 void create_bootstrap_processrc()
928 {
929     rtl::OUString aDirURL(getModulePath());
930 #if defined(WNT) || defined(OS2)
931     aDirURL += rtl::OUString::createFromAscii("/bootstrap_process.ini");
932 #else
933     aDirURL += rtl::OUString::createFromAscii("/bootstrap_processrc");
934 #endif
935     rtl::OString sLines;
936     sLines += "[Bootstrap]\n";
937     sLines += "EXECUTABLE_RC=true\n";
938     sLines += "IF_CUSTOM_RC=false\n";
939 
940     removeAndCreateFile(aDirURL, sLines);
941 }
942 // -----------------------------------------------------------------------------
943 
944 
945 int main(int argc, char **argv)
946 {
947     // start message
948     printf("Initializing ...\n" );
949     osl_setCommandArgs(argc, argv);
950     create_rtlrc();
951     create_testshl2rc();
952     create_pseudorc();
953     create_bootstrap_processrc();
954 
955     printf("Initialization Done.\n" );
956     ::testing::InitGoogleTest(&argc, argv);
957     return RUN_ALL_TESTS();
958 }
959