1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import org.testng.Assert; 25 import org.testng.annotations.AfterClass; 26 import org.testng.annotations.BeforeClass; 27 import org.testng.annotations.DataProvider; 28 import org.testng.annotations.Test; 29 30 import java.io.File; 31 import java.io.IOException; 32 import java.nio.charset.StandardCharsets; 33 import java.nio.file.FileSystem; 34 import java.nio.file.Files; 35 import java.nio.file.Path; 36 import java.nio.file.Paths; 37 import java.nio.file.spi.FileSystemProvider; 38 import java.util.Map; 39 40 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 41 42 /* @test 43 * @bug 8202285 44 * @build Mismatch 45 * @run testng Mismatch 46 * @summary Unit test for the Files.mismatch method. 47 */ 48 public class Mismatch { 49 // the standard buffer size 50 final static int BUFFER_SIZE = 8192; 51 52 private static final int MISMATCH_NO = -1; 53 54 // Map to be used for creating a ZIP archive 55 private static final Map<String, String> ZIPFS_MAP = Map.of("create", "true"); 56 57 // temporary test directory where all test files will be created 58 Path testDir; 59 60 @BeforeClass 61 void setup() throws IOException { 62 testDir = Files.createTempDirectory("testMismatch"); 63 } 64 65 @AfterClass 66 void cleanup() throws IOException { 67 // clean up files created under the test directory 68 Files.walk(testDir).map(Path::toFile).forEach(File::delete); 69 Files.deleteIfExists(testDir); 70 } 71 72 /* 73 * DataProvider for mismatch test. Provides the following fields: 74 * path1 -- the path to a file 75 * path2 -- the path to another file 76 * expected -- expected result of the mismatch method 77 * note -- a note about the test 78 */ 79 @DataProvider(name = "testMismatch") 80 public Object[][] getDataForMismatch() throws IOException { 81 // an non-existent file 82 Path foo = Paths.get("nonexistentfile"); 83 84 /** 85 * File path naming convention: 86 * "test" + file size + [abm] [+ position of a modified char] + [ab] 87 * where: 88 * a or b -- is used to differentiate two files of the same size. 89 * m -- indicates the file is modified at the position specified after it 90 */ 91 92 // create empty files 93 int size = 0; 94 Path test0a = createASCIIFile(testDir, "test0a", 0, -1, ' '); 95 Path test0b = createASCIIFile(testDir, "test0b", 0, -1, ' '); 96 97 /** 98 * Since the Impl uses a standard buffer of 8192, the test files are created 99 * with sizes <= and > 8192, either multiples of the buffer size, or random. 100 * The files are then altered at the begining (0), end (size), and a random 101 * position. 102 */ 103 size = 147; 104 Path test147a = createASCIIFile(testDir, "test147a", size, -1, ' '); 105 Path test147b = createASCIIFile(testDir, "test147b", size, -1, ' '); 106 Path test147m0 = createASCIIFile(testDir, "test147m0", size, 0, '!'); 107 Path test147m70 = createASCIIFile(testDir, "test147m70", size, 70, '@'); 108 Path test147m146 = createASCIIFile(testDir, "test147m146", size, size - 1, '$'); 109 110 size = 1024; 111 Path test1024a = createASCIIFile(testDir, "test1024a", size, -1, ' '); 112 Path test1024b = createASCIIFile(testDir, "test1024b", size, -1, ' '); 113 Path test1024m0 = createASCIIFile(testDir, "test1024m0", size, 0, '!'); 114 Path test1024m512 = createASCIIFile(testDir, "test1024m512", size, size >> 1, '@'); 115 Path test1024m1023 = createASCIIFile(testDir, "test1024m1023", size, size - 1, '$'); 116 117 size = BUFFER_SIZE; 118 Path test8192a = createASCIIFile(testDir, "test8192a", size, -1, ' '); 119 Path test8192b = createASCIIFile(testDir, "test8192b", size, -1, ' '); 120 Path test8192m0 = createASCIIFile(testDir, "test8192m0", size, 0, '!'); 121 Path test8192m120 = createASCIIFile(testDir, "test8192m120", size, 120, '@'); 122 Path test8192m4096 = createASCIIFile(testDir, "test8192m4096", size, size >> 1, '%'); 123 Path test8192m8191 = createASCIIFile(testDir, "test8192m8191", size, size - 1, '$'); 124 125 126 // create files with size several times > BUFFER_SIZE to be used for tests that verify 127 // the situations where they are read into full buffers a few times 128 size = BUFFER_SIZE << 3; 129 Path test65536a = createASCIIFile(testDir, "test65536a", size, -1, ' '); 130 Path test65536b = createASCIIFile(testDir, "test65536b", size, -1, ' '); 131 Path test65536m0 = createASCIIFile(testDir, "test65536m0", size, 0, '!'); 132 Path test65536m120 = createASCIIFile(testDir, "test65536m120", size, 120, '@'); 133 Path test65536m32768 = createASCIIFile(testDir, "test65536m32768", size, size >> 1, '%'); 134 Path test65536m65535 = createASCIIFile(testDir, "test65536m65535", size, size - 1, '$'); 135 136 // create files with sizes that will be iterated several times with full buffers, and 137 // then a partial one at the last 138 size = 70025; 139 Path test70025a = createASCIIFile(testDir, "test70025a", size, -1, ' '); 140 Path test70025b = createASCIIFile(testDir, "test70025b", size, -1, ' '); 141 Path test70025m0 = createASCIIFile(testDir, "test70025m0", size, 0, '!'); 142 Path test70025m8400 = createASCIIFile(testDir, "test70025m8400", size, 8400, '@'); 143 Path test70025m35000 = createASCIIFile(testDir, "test70025m35000", size, 35000, '%'); 144 Path test70025m70024 = createASCIIFile(testDir, "test70025m70024", size, 70024, '$'); 145 146 // create larger files with >= 1048576. The mismatching will be similar. These are just 147 // tests to exercise the process with larger files 148 size = 1048576; 149 Path test1048576a = createASCIIFile(testDir, "test1048576a", size, -1, ' '); 150 Path test1048576m0 = createASCIIFile(testDir, "test1048576m0", size, 0, '!'); 151 Path test1048576m25000 = createASCIIFile(testDir, "test1048576m25000", size, 25000, '@'); 152 Path test1048576m524288 = createASCIIFile(testDir, "test1048576m524288", size, size >> 1, '%'); 153 Path test1048576m1048575 = createASCIIFile(testDir, "test1048576m1048575", size, 1048575, '$'); 154 155 size = 1065000; 156 Path test1065000a = createASCIIFile(testDir, "test1065000a", size, -1, ' '); 157 Path test1065000m0 = createASCIIFile(testDir, "test1065000m0", size, 0, '!'); 158 Path test1065000m50000 = createASCIIFile(testDir, "test1065000m50000", size, 50000, '@'); 159 Path test1065000m532500 = createASCIIFile(testDir, "test1065000m532500", size, size >> 1, '%'); 160 Path test1065000m1064999 = createASCIIFile(testDir, "test1065000m1064999", size, 1064999, '$'); 161 162 return new Object[][]{ 163 // Spec Case 1: the two paths locate the same file , even if one does not exist 164 {foo, foo, MISMATCH_NO, "Same file, no mismatch"}, 165 {test0a, test0a, MISMATCH_NO, "Same file, no mismatch"}, 166 {test1024a, test1024a, MISMATCH_NO, "Same file, no mismatch"}, 167 168 // Spec Case 2: The two files are the same size, and every byte in the first file 169 // is identical to the corresponding byte in the second file. 170 {test0a, test0b, MISMATCH_NO, "Sizes == 0, no mismatch"}, 171 {test147a, test147b, MISMATCH_NO, "size = 147 < buffer = 8192, no mismatch"}, 172 {test1024a, test1024b, MISMATCH_NO, "size = 1024 < buffer = 8192, no mismatch"}, 173 {test8192a, test8192b, MISMATCH_NO, "size = 8192 = buffer = 8192, no mismatch"}, 174 {test65536a, test65536b, MISMATCH_NO, "read 8 * full buffer, no mismatch"}, 175 {test70025a, test70025b, MISMATCH_NO, "read 8 * full buffer plus a partial buffer, no mismatch"}, 176 177 178 /** 179 * Spec Case 3: the value returned is the position of the first mismatched byte 180 * Impl: the impl uses a buffer 8192. The testcases below covers a range of files 181 * with sizes <= and > the buffer size. The last buffer is either full or partially full. 182 */ 183 184 /** 185 * Compares files of equal sizes 186 */ 187 // small files 188 {test147a, test147m0, 0, "mismatch = 0 (at the beginning)"}, 189 {test147a, test147m70, 70, "read one partial buffer, mismatch = 70"}, 190 {test147a, test147m146, 146, "read one partial buffer, mismatch = 146 (end)"}, 191 {test1024a, test1024m0, 0, "mismatch = 0 (at the beginning)"}, 192 {test1024a, test1024m512, 512, "read one partial buffer, mismatch = 512"}, 193 {test1024a, test1024m1023, 1023, "read one partial buffer, mismatch = 1023 (end)"}, 194 195 // file size >= Impl's Buffer Size 196 {test8192a, test8192m0, 0, "mismatch = 0 (at the beginning)"}, 197 {test8192a, test8192m120, 120, "read one buffer, mismatch = 120"}, 198 {test8192a, test8192m4096, 4096, "read one buffer, mismatch = 4096 "}, 199 {test8192a, test8192m8191, 8191, "read one buffer, mismatch = 8191 (at the end)"}, 200 201 // file size = n * Impl's Buffer Size 202 {test65536a, test65536m0, 0, "mismatch = 0 (at the beginning)"}, 203 {test65536a, test65536m120, 120, "mismatch in the first buffer, mismatch = 120"}, 204 {test65536a, test65536m32768, 32768, "read through half of the file, mismatch = 32768"}, 205 {test65536a, test65536m65535, 65535, "read through the whole file, mismatch = 65535 (at the end)"}, 206 207 {test1048576a, test1048576m0, 0, "mismatch = 0 (at the beginning)"}, 208 {test1048576a, test1048576m25000, 25000, "mismatch in the 2nd buffer, mismatch = 25000"}, 209 {test1048576a, test1048576m524288, 524288, "read about half of the file, mismatch = 524288"}, 210 {test1048576a, test1048576m1048575, 1048575, "read through the whole file, mismatch = 1048575 (at the end)"}, 211 212 // file size = n * Impl's Buffer Size + x 213 {test70025a, test70025m0, 0, "mismatch = 0 (at the beginning)"}, 214 {test70025a, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"}, 215 {test70025a, test70025m35000, 35000, "read about half of the file, mismatch = 35000"}, 216 {test70025a, test70025m70024, 70024, "read through the whole file, mismatch = 70024 (at the end)"}, 217 218 {test1065000a, test1065000m0, 0, "mismatch = 0 (at the beginning)"}, 219 {test1065000a, test1065000m50000, 50000, "mismatch in the 2nd buffer, mismatch = 50000"}, 220 {test1065000a, test1065000m532500, 532500, "read about half of the file, mismatch = 532500"}, 221 {test1065000a, test1065000m1064999, 1064999, "read through the whole file, mismatch = 1064999 (at the end)"}, 222 223 /** 224 * Compares files of unequal sizes 225 */ 226 {test147a, test65536m0, 0, "mismatch = 0 (at the beginning)"}, 227 {test8192m8191, test70025m35000, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"}, 228 {test65536m32768, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"}, 229 {test70025m70024, test1065000m532500, 70024, "mismatch at the end of the 1st file, mismatch = 70024"}, 230 231 /** 232 * Spec Case 4: returns the size of the smaller file (in bytes) when the files are 233 * different sizes and every byte of the smaller file is identical to the corresponding 234 * byte of the larger file. 235 * Impl: similar to case 3, covers a range of file sizes 236 */ 237 {test0a, test1024a, 0, "Size of one of files = 0, mismatch at 0"}, 238 {test147a, test1024a, 147, "mismatch is the length of the smaller file: 147"}, 239 {test1024a, test8192a, 1024, "mismatch is the length of the smaller file: 1024"}, 240 {test1024a, test65536a, 1024, "mismatch is the length of the smaller file: 1024"}, 241 {test8192a, test65536a, 8192, "mismatch is the length of the smaller file: 8192"}, 242 {test70025a, test65536a, 65536, "mismatch is the length of the smaller file: 65536"}, 243 {test1048576a, test1065000m1064999, 1048576, "mismatch is the length of the smaller file: 1048576"}, 244 245 // Spec Case 5: This method is always reflexive (for Path f , mismatch(f,f) returns -1L) 246 // See tests for Spec Case 1. 247 248 // Spec Case 6: If the file system and files remain static, then this method is symmetric 249 // (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)). 250 // The following tests are selected from tests for Spec Case 3 with the order of 251 // file paths switched, the returned values are the same as those for Case 3: 252 {test147m0, test147a, 0, "read one partial buffer, mismatch = 0 (beginning)"}, 253 {test147m70, test147a, 70, "read one partial buffer, mismatch = 70"}, 254 {test147m146, test147a, 146, "read one partial buffer, mismatch = 146 (end)"}, 255 {test1024m0, test1024a, 0, "read one partial buffer, mismatch = 0 (beginning)"}, 256 {test1024m512, test1024a, 512, "read one partial buffer, mismatch = 512"}, 257 {test1024m1023, test1024a, 1023, "read one partial buffer, mismatch = 1023 (end)"}, 258 259 {test65536m0, test147a, 0, "mismatch = 0 (at the beginning)"}, 260 {test70025m35000, test8192m8191, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"}, 261 {test70025m8400, test65536m32768, 8400, "mismatch in the 2nd buffer, mismatch = 8400"}, 262 {test1065000m532500, test70025m70024, 70024, "mismatch at the end of the 1st file, mismatch = 70024"}, 263 }; 264 } 265 266 /* 267 * DataProvider for mismatch tests involving ZipFS using a few test cases selected 268 * from those of the original mismatch tests. 269 */ 270 @DataProvider(name = "testMismatchZipfs") 271 public Object[][] getDataForMismatchZipfs() throws IOException { 272 Path test1200 = createASCIIFile(testDir, "test1200", 1200, -1, ' '); 273 Path test9500 = createASCIIFile(testDir, "test9500", 9500, -1, ' '); 274 Path test9500m4200 = createASCIIFile(testDir, "test9500m4200", 9500, 4200, '!'); 275 Path test80025 = createASCIIFile(testDir, "test80025", 80025, -1, ' '); 276 Path test1028500 = createASCIIFile(testDir, "test1028500", 1028500, -1, ' '); 277 return new Object[][]{ 278 {test1200, test1200, MISMATCH_NO, "Compares the file and its copy in zip, no mismatch"}, 279 {test9500, test9500m4200, 4200, 280 "Compares a copy of test9500m4200 in zip with test9500, shall return 4200"}, 281 {test80025, test1028500, 80025, "mismatch is the length of the smaller file: 80025"}, 282 }; 283 } 284 285 /* 286 * DataProvider for verifying null handling. 287 */ 288 @DataProvider(name = "testFileNull") 289 public Object[][] getDataForNull() throws IOException { 290 Path test = createASCIIFile(testDir, "testNonNull", 2200, -1, ' '); 291 return new Object[][]{ 292 {(Path)null, (Path)null}, 293 {(Path)null, test}, 294 {test, (Path)null}, 295 }; 296 } 297 298 /* 299 * DataProvider for verifying how the mismatch method handles the situation 300 * when one or both files do not exist. 301 */ 302 @DataProvider(name = "testFileNotExist") 303 public Object[][] getDataForFileNotExist() throws IOException { 304 Path test = createASCIIFile(testDir, "testFileNotExist", 3200, -1, ' '); 305 return new Object[][]{ 306 {Paths.get("foo"), Paths.get("bar")}, 307 {Paths.get("foo"), test}, 308 {test, Paths.get("bar")}, 309 }; 310 } 311 312 /** 313 * Tests the mismatch method. Refer to the dataProvider testMismatch for more 314 * details about the cases. 315 * @param path the path to a file 316 * @param path2 the path to another file 317 * @param expected the expected result 318 * @param msg the message about the test 319 * @throws IOException if the test fails 320 */ 321 @Test(dataProvider = "testMismatch", priority = 0) 322 public void testMismatch(Path path, Path path2, long expected, String msg) 323 throws IOException { 324 Assert.assertEquals(Files.mismatch(path, path2), expected, msg); 325 } 326 327 /** 328 * Tests the mismatch method by comparing files with those in a ZIP file. 329 * @param path the path to a file 330 * @param path2 the path to another file to be added into a ZIP file 331 * @param expected the expected result 332 * @param msg the message about the test 333 * @throws IOException if the test fails 334 */ 335 @Test(dataProvider = "testMismatchZipfs", priority = 1) 336 public void testMismatchZipfs(Path path, Path path2, long expected, String msg) 337 throws IOException { 338 Path zipPath = Paths.get(testDir.toString(), "TestWithFSZip.zip"); 339 try (FileSystem fs = getZipFSProvider().newFileSystem(zipPath, ZIPFS_MAP)) { 340 Path copy = fs.getPath(path.getFileName().toString()); 341 Files.copy(path, copy, REPLACE_EXISTING); 342 343 if (path2 == null) { 344 Assert.assertEquals(Files.mismatch(copy, path), expected, msg); 345 } else { 346 Assert.assertEquals(Files.mismatch(copy, path2), expected, msg); 347 } 348 } 349 } 350 351 /** 352 * Verifies that NullPointerException is thrown when one or both files are null. 353 * @param path the path to a file 354 * @param path2 the path to another file 355 * @throws NullPointerException as expected 356 */ 357 @Test(dataProvider = "testFileNull", priority = 2, expectedExceptions = NullPointerException.class) 358 public void testMismatchNull(Path path, Path path2) throws Exception { 359 long result = Files.mismatch(path, path2); 360 } 361 362 /** 363 * Verifies that IOException is thrown when one or both files do not exist. 364 * @param path the path to a file 365 * @param path2 the path to another file 366 * @throws IOException as expected 367 */ 368 @Test(dataProvider = "testFileNotExist", priority = 2, expectedExceptions = IOException.class) 369 public void testMismatchNotExist(Path path, Path path2) throws IOException { 370 long result = Files.mismatch(path, path2); 371 } 372 373 /** 374 * Creates a file with ASCII content with one character altered 375 * at the specified position. 376 * 377 * Note: Files.mismatch method does a byte-by-byte comparison. ASCII files 378 * are sufficient for verifying the feature. 379 * 380 * @param dir the directory in which the file is to be created 381 * @param purpose the purpose of the file 382 * @param size the size of the file 383 * @param pos the position where the alternative char is to be added. If it 384 * is smaller than zero, no alternation shall be made. 385 * @param c the character 386 * @return path of the created file 387 * @throws IOException 388 */ 389 private static Path createASCIIFile(Path dir, String purpose, int size, int pos, 390 char c) throws IOException { 391 Path path = Files.createFile(Paths.get(dir.toString(), purpose + ".txt")); 392 if (size > 0) { 393 writeASCIIFile(path, size, pos, c); 394 } 395 return path; 396 } 397 398 private static void writeASCIIFile(Path path, int size, int pos, char c) 399 throws IOException { 400 byte[] a = createASCIIArray(size); 401 if (pos >= 0) a[pos] = (byte)(c & 0xFF); // US_ASCII char only, may cast directly 402 Files.write(path, a); 403 } 404 405 private static byte[] createASCIIArray(int length) { 406 byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 \n" 407 .getBytes(StandardCharsets.US_ASCII); 408 byte[] a = new byte[length]; 409 fillArray(bytes, a); 410 return a; 411 } 412 413 private static FileSystemProvider getZipFSProvider() { 414 for (FileSystemProvider provider : FileSystemProvider.installedProviders()) { 415 if ("jar".equals(provider.getScheme())) { 416 return provider; 417 } 418 } 419 return null; 420 } 421 422 /** 423 * Fills the destination array by copying the source array repeatedly until 424 * it is completely filled. 425 * 426 * @param src the source array 427 * @param dest the destination array 428 */ 429 public static void fillArray(byte[] src, byte[] dest) { 430 int bLen = src.length; 431 int space = dest.length; 432 int iteration = 0; 433 434 while (space > 0) { 435 if (space >= bLen) { 436 System.arraycopy(src, 0, dest, iteration++ * bLen, bLen); 437 space -= bLen; 438 } else { 439 System.arraycopy(src, 0, dest, iteration++ * bLen, space); 440 break; 441 } 442 } 443 } 444 }