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