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