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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 // LLA:
32 // this file is converted to use with testshl2
33 // original was placed in sal/test/textenc.cxx
34 
35 #include <stdio.h>
36 
37 #include <osl/file.h>
38 #include <osl/process.h>
39 #include <rtl/ustring.hxx>
40 #ifdef SAL_UNX
41 #include <unistd.h>
42 #include <limits.h>
43 #include <string.h>
44 #include <sys/stat.h>
45 #define TEST_VOLUME ""
46 #else
47 // WINDOWS
48 #define TEST_VOLUME "c:/"
49 #endif
50 
51 #include <testshl/simpleheader.hxx>
52 
53 namespace osl_test_file
54 {
55 
56 // -----------------------------------------------------------------------------
57 
58 class oldtestfile : public CppUnit::TestFixture
59 {
60 public:
61     void test_file_001();
62     void test_file_002();
63     void test_file_003();
64     void test_file_004();
65 
66     CPPUNIT_TEST_SUITE( oldtestfile );
67     CPPUNIT_TEST( test_file_001 );
68     CPPUNIT_TEST( test_file_002 );
69     CPPUNIT_TEST( test_file_003 );
70     CPPUNIT_TEST( test_file_004 );
71     CPPUNIT_TEST_SUITE_END( );
72 };
73 
74 char *aSource1[] =
75 {
76 	"a"    , "file:///" TEST_VOLUME "bla/a",
77 	"a/"   , "file:///" TEST_VOLUME "bla/a",
78 	"../a" , "file:///" TEST_VOLUME "a" ,
79 	"a/.." , "file:///" TEST_VOLUME "bla",
80 	"a/../b" , "file:///" TEST_VOLUME "bla/b",
81 	".."   , "file:///" TEST_VOLUME "",
82 	"a/b/c/d/"   , "file:///" TEST_VOLUME "bla/a/b/c/d",
83 	"a/./c"   , "file:///" TEST_VOLUME "bla/a/c",
84 	"file:///bla/blub", "file:///" TEST_VOLUME "bla/blub",
85 	0 , 0
86 };
87 
88 char *aSource2[ ] =
89 {
90 	"a" , "file:///" TEST_VOLUME "bla/blubs/schnubbel/a",
91 	"a/", "file:///" TEST_VOLUME "bla/blubs/schnubbel/a",
92 	"../a", "file:///" TEST_VOLUME "bla/blubs/a",
93 	"../../a", "file:///" TEST_VOLUME "bla/a",
94 	"../../../a", "file:///" TEST_VOLUME "a",
95 	"../../../a/b/c/d", "file:///" TEST_VOLUME "a/b/c/d",
96 	0,0
97 };
98 
99 char *aSource3[ ] =
100 {
101 	".." , "/a",
102 	"../a" , "/a/a",
103 	"e/f" , "/c/e/f",
104 	"../..", "",
105 	0,0
106 };
107 
108 using namespace rtl;
109 
110 void oldtestfile::test_file_001()
111 {
112 #ifdef WIN32
113 	return;
114 #endif
115 
116 	OUString base1( RTL_CONSTASCII_USTRINGPARAM( "file:///" TEST_VOLUME "bla" ) );
117 	int i;
118 	for( i = 0 ; aSource1[i] ; i +=2 )
119 	{
120 		OUString target;
121 		OUString rel = OUString::createFromAscii( aSource1[i] );
122 		oslFileError e = osl_getAbsoluteFileURL( base1.pData, rel.pData , &target.pData );
123 		CPPUNIT_ASSERT_MESSAGE("failure #1",  osl_File_E_None == e );
124 		if( osl_File_E_None == e )
125 		{
126 			CPPUNIT_ASSERT_MESSAGE("failure #1.1",  target.equalsAscii( aSource1[i+1] ) );
127 		}
128 		OString o = OUStringToOString( target , RTL_TEXTENCODING_ASCII_US );
129 		OString obase = OUStringToOString( base1 , RTL_TEXTENCODING_ASCII_US );
130 		fprintf( stderr, "%d %s + %s = %s\n" ,e, obase.getStr(), aSource1[i], o.pData->buffer );
131 	}
132 
133 	OUString err1( RTL_CONSTASCII_USTRINGPARAM( "../.." ) );
134 	OUString target;
135 	CPPUNIT_ASSERT_MESSAGE("failure #11",  osl_File_E_None != osl_getAbsoluteFileURL( base1.pData , err1.pData , &target.pData ) );
136 
137 }
138 
139 void oldtestfile::test_file_002()
140 {
141 #ifdef WIN32
142 	return;
143 #endif
144 
145 	OUString base2( RTL_CONSTASCII_USTRINGPARAM( "file:///" TEST_VOLUME "bla/blubs/schnubbel" ) );
146 	int i;
147 	for(  i = 0 ; aSource2[i] ; i +=2 )
148 	{
149 		OUString target;
150 		OUString rel = OUString::createFromAscii( aSource2[i] );
151 		oslFileError e = osl_getAbsoluteFileURL( base2.pData, rel.pData , &target.pData );
152 		CPPUNIT_ASSERT_MESSAGE("failure #2",  osl_File_E_None == e );
153 		if( osl_File_E_None == e )
154 		{
155 			CPPUNIT_ASSERT_MESSAGE("failure #2.1",  target.equalsAscii( aSource2[i+1] ) );
156 		}
157 		OString o = OUStringToOString( target , RTL_TEXTENCODING_ASCII_US );
158 		OString obase = OUStringToOString( base2 , RTL_TEXTENCODING_ASCII_US );
159 //		fprintf( stderr, "%d %s + %s = %s\n" ,e, obase.getStr(), aSource2[i], o.pData->buffer );
160 	}
161 }
162 
163 void oldtestfile::test_file_003()
164 {
165 #ifdef WIN32
166 	return;
167 #endif
168 
169 	// links !
170 #ifdef UNX
171 	int i;
172 	char buf[PATH_MAX];
173 	if( getcwd( buf, PATH_MAX ) )
174 	{
175 		char buf2[PATH_MAX];
176 		strcpy( buf2 , buf );
177 		strcat( buf2, "/a" );
178 
179 		if( 0 == mkdir( buf2 , S_IRWXG | S_IRWXO | S_IRWXU ) )
180 		{
181 			strcat( buf2, "/b" );
182 			if( 0 == mkdir( buf2, S_IRWXU | S_IRWXO | S_IRWXU ) )
183 			{
184 				if( 0 == symlink( buf2 , "c" ) )
185 				{
186 					OUString dir;
187 					osl_getProcessWorkingDir( &(dir.pData) );
188 
189 					OUString base3 = dir;
190 					base3 += OUString( RTL_CONSTASCII_USTRINGPARAM( "/c" ) );
191 					for( i = 0 ; aSource3[i] ; i +=2 )
192 					{
193 						OUString target;
194 						OUString rel = OUString::createFromAscii( aSource3[i] );
195 						oslFileError e = osl_getAbsoluteFileURL( base3.pData, rel.pData , &target.pData );
196 						CPPUNIT_ASSERT_MESSAGE("failure #3",  osl_File_E_None == e );
197 						if( osl_File_E_None == e )
198 						{
199 							CPPUNIT_ASSERT_MESSAGE("failure #4",  target.getLength() >= dir.getLength() );
200 							if( target.getLength() >= dir.getLength() )
201 							{
202                                                             int j;
203                                                             for( j = dir.getLength() ;
204 									 j < target.getLength() &&
205 										 aSource3[i+1][j-dir.getLength()] == target[j]	 ; j++ );
206 								CPPUNIT_ASSERT_MESSAGE("failure #5",  j == target.getLength() );
207 							}
208 						}
209 						OString o = OUStringToOString( target , RTL_TEXTENCODING_ASCII_US );
210 						OString obase = OUStringToOString( base3 , RTL_TEXTENCODING_ASCII_US );
211 						fprintf( stderr, "%d %s + %s = %s\n" ,e, obase.getStr(), aSource3[i], o.pData->buffer );
212 					}
213 					unlink( "c" );
214 				}
215 				else
216 				{
217 					CPPUNIT_ASSERT_MESSAGE("failure #6",  0 );
218 				}
219 				rmdir( "a/b" );
220 			}
221 			else
222 			{
223 				CPPUNIT_ASSERT_MESSAGE("failure #7",  0 );
224 			}
225 			rmdir( "a" );
226 		}
227 		else
228 		{
229 			CPPUNIT_ASSERT_MESSAGE("failure #8",  0 );
230 		}
231 	}
232 	else
233 	{
234 		CPPUNIT_ASSERT_MESSAGE("failure #9",  0 );
235 	}
236 #endif
237 }
238 
239 void oldtestfile::test_file_004()
240 {
241 #ifdef WIN32
242 	return;
243 #endif
244 
245 	OUString base4( RTL_CONSTASCII_USTRINGPARAM( "file:///" TEST_VOLUME "bla/" ) );
246 	int i;
247 	for( i = 0 ; aSource1[i] ; i +=2 )
248 	{
249 		OUString target;
250 		OUString rel = OUString::createFromAscii( aSource1[i] );
251 		oslFileError e = osl_getAbsoluteFileURL( base4.pData, rel.pData , &target.pData );
252 		CPPUNIT_ASSERT_MESSAGE("failure #10",  osl_File_E_None == e );
253 		if( osl_File_E_None == e )
254 		{
255 			CPPUNIT_ASSERT_MESSAGE("failure #10.1",  target.equalsAscii( aSource1[i+1] ) );
256 		}
257 		OString o = OUStringToOString( target , RTL_TEXTENCODING_ASCII_US );
258 		OString obase = OUStringToOString( base4 , RTL_TEXTENCODING_ASCII_US );
259 		fprintf( stderr, "%d %s + %s = %s\n" ,e, obase.getStr(), aSource1[i], o.pData->buffer );
260 	}
261 
262 
263 //	fprintf( stderr, "test_file done\n" );
264 }
265 
266 } // namespace osl_test_file
267 
268 // -----------------------------------------------------------------------------
269 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_test_file::oldtestfile, "osl_File" );
270 
271 // -----------------------------------------------------------------------------
272 NOADDITIONAL;
273 
274