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
setup()61     void setup() throws IOException {
62         testDir = Files.createTempDirectory("testMismatch");
63     }
64 
65     @AfterClass
cleanup()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")
getDataForMismatch()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 test1024m512 = createASCIIFile(testDir, "test1024m512", size, size >> 1, '@');
114         Path test1024m1023 = createASCIIFile(testDir, "test1024m1023", size, size - 1, '$');
115 
116         size = BUFFER_SIZE;
117         Path test8192a = createASCIIFile(testDir, "test8192a", size, -1, ' ');
118         Path test8192b = createASCIIFile(testDir, "test8192b", size, -1, ' ');
119         Path test8192m4096 = createASCIIFile(testDir, "test8192m4096", size, size >> 1, '%');
120         Path test8192m8191 = createASCIIFile(testDir, "test8192m8191", size, size - 1, '$');
121 
122 
123         // create files with size several times > BUFFER_SIZE to be used for tests that verify
124         // the situations where they are read into full buffers a few times
125         size = BUFFER_SIZE << 3;
126         Path test65536a = createASCIIFile(testDir, "test65536a", size, -1, ' ');
127         Path test65536b = createASCIIFile(testDir, "test65536b", size, -1, ' ');
128         Path test65536m0 = createASCIIFile(testDir, "test65536m0", size, 0, '!');
129         Path test65536m32768 = createASCIIFile(testDir, "test65536m32768", size, size >> 1, '%');
130         Path test65536m65535 = createASCIIFile(testDir, "test65536m65535", size, size - 1, '$');
131 
132         // create files with sizes that will be iterated several times with full buffers, and
133         // then a partial one at the last
134         size = 70025;
135         Path test70025a = createASCIIFile(testDir, "test70025a", size, -1, ' ');
136         Path test70025b = createASCIIFile(testDir, "test70025b", size, -1, ' ');
137         Path test70025m8400 = createASCIIFile(testDir, "test70025m8400", size, 8400, '@');
138         Path test70025m35000 = createASCIIFile(testDir, "test70025m35000", size, 35000, '%');
139         Path test70025m70024 = createASCIIFile(testDir, "test70025m70024", size, 70024, '$');
140 
141         // create larger files with >= 1048576. The mismatching will be similar. These are just
142         // tests to exercise the process with larger files
143         size = 1048576;
144         Path test1048576a = createASCIIFile(testDir, "test1048576a", size, -1, ' ');
145 
146         size = 1065000;
147         Path test1065000m532500 = createASCIIFile(testDir, "test1065000m532500", size, size >> 1, '%');
148         Path test1065000m1064999 = createASCIIFile(testDir, "test1065000m1064999", size, 1064999, '$');
149 
150         return new Object[][]{
151             // Spec Case 1: the two paths locate the same file , even if one does not exist
152             {foo, foo, MISMATCH_NO, "Same file, no mismatch"},
153             {test1024a, test1024a, MISMATCH_NO, "Same file, no mismatch"},
154 
155             // Spec Case 2:  The two files are the same size, and every byte in the first file
156             // is identical to the corresponding byte in the second file.
157             {test0a, test0b, MISMATCH_NO, "Sizes == 0, no mismatch"},
158             {test147a, test147b, MISMATCH_NO, "size = 147 < buffer = 8192, no mismatch"},
159             {test1024a, test1024b, MISMATCH_NO, "size = 1024 < buffer = 8192, no mismatch"},
160             {test8192a, test8192b, MISMATCH_NO, "size = 8192 = buffer = 8192, no mismatch"},
161             {test65536a, test65536b, MISMATCH_NO, "read 8 * full buffer, no mismatch"},
162             {test70025a, test70025b, MISMATCH_NO, "read 8 * full buffer plus a partial buffer, no mismatch"},
163 
164 
165             /**
166              * Spec Case 3: the value returned is the position of the first mismatched byte
167              * Impl: the impl uses a buffer 8192. The testcases below covers a range of files
168              * with sizes <= and > the buffer size. The last buffer is either full or partially full.
169             */
170 
171             // edge case, one of the file sizes is zero
172             // also covers Spec Case 4 and 6
173             {test147a, test147m0, 0, "mismatch = 0 (at the beginning)"},
174             {test65536m0, test65536a, 0, "mismatch = 0 (at the beginning)"},
175 
176             /**
177              * Compares files of equal sizes
178             */
179             // small files
180             {test147a, test147m70, 70, "read one partial buffer, mismatch = 70"},
181             {test147a, test147m146, 146, "read one partial buffer, mismatch = 146 (end)"},
182             {test1024a, test1024m512, 512, "read one partial buffer, mismatch = 512"},
183             {test1024a, test1024m1023, 1023, "read one partial buffer, mismatch = 1023 (end)"},
184 
185             // file size >= Impl's Buffer Size
186             {test8192a, test8192m4096, 4096, "read one buffer, mismatch = 4096 "},
187             {test8192a, test8192m8191, 8191, "read one buffer, mismatch = 8191 (at the end)"},
188 
189             // file size = n * Impl's Buffer Size
190             {test65536a, test65536m32768, 32768, "read through half of the file, mismatch = 32768"},
191             {test65536a, test65536m65535, 65535, "read through the whole file, mismatch = 65535 (at the end)"},
192 
193             // file size = n * Impl's Buffer Size + x
194             {test70025a, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"},
195             {test70025a, test70025m35000, 35000, "read about half of the file, mismatch = 35000"},
196             {test70025a, test70025m70024, 70024, "read through the whole file, mismatch = 70024 (at the end)"},
197 
198             /**
199              * Compares files of unequal sizes
200             */
201             {test8192m8191, test70025m35000, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"},
202             {test65536m32768, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"},
203             {test70025m70024, test1065000m532500, 70024, "mismatch at the end of the 1st file, mismatch = 70024"},
204 
205             /**
206              * Spec Case 4:  returns the size of the smaller file (in bytes) when the files are
207              * different sizes and every byte of the smaller file is identical to the corresponding
208              * byte of the larger file.
209              * Impl: similar to case 3, covers a range of file sizes
210             */
211             {test147a, test1024a, 147, "mismatch is the length of the smaller file: 147"},
212             {test1024a, test8192a, 1024, "mismatch is the length of the smaller file: 1024"},
213             {test1024a, test65536a, 1024, "mismatch is the length of the smaller file: 1024"},
214             {test8192a, test65536a, 8192, "mismatch is the length of the smaller file: 8192"},
215             {test70025a, test65536a, 65536, "mismatch is the length of the smaller file: 65536"},
216             {test1048576a, test1065000m1064999, 1048576, "mismatch is the length of the smaller file: 1048576"},
217 
218             // Spec Case 5: This method is always reflexive (for Path f , mismatch(f,f) returns -1L)
219             // See tests for Spec Case 1.
220 
221             // Spec Case 6: If the file system and files remain static, then this method is symmetric
222             // (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)).
223             // The following tests are selected from tests for Spec Case 3 with the order of
224             // file paths switched, the returned values are the same as those for Case 3:
225             {test147m70, test147a, 70, "read one partial buffer, mismatch = 70"},
226             {test147m146, test147a, 146, "read one partial buffer, mismatch = 146 (end)"},
227             {test1024m512, test1024a, 512, "read one partial buffer, mismatch = 512"},
228             {test1024m1023, test1024a, 1023, "read one partial buffer, mismatch = 1023 (end)"},
229 
230             {test70025m35000, test8192m8191, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"},
231             {test70025m8400, test65536m32768, 8400, "mismatch in the 2nd buffer, mismatch = 8400"},
232             {test1065000m532500, test70025m70024, 70024, "mismatch at the end of the 1st file, mismatch = 70024"},
233         };
234     }
235 
236     /*
237      * DataProvider for mismatch tests involving ZipFS using a few test cases selected
238      * from those of the original mismatch tests.
239      */
240     @DataProvider(name = "testMismatchZipfs")
getDataForMismatchZipfs()241     public Object[][] getDataForMismatchZipfs() throws IOException {
242         Path test1200 = createASCIIFile(testDir, "test1200", 1200, -1, ' ');
243         Path test9500 = createASCIIFile(testDir, "test9500", 9500, -1, ' ');
244         Path test9500m4200 = createASCIIFile(testDir, "test9500m4200", 9500, 4200, '!');
245         Path test80025 = createASCIIFile(testDir, "test80025", 80025, -1, ' ');
246         Path test1028500 = createASCIIFile(testDir, "test1028500", 1028500, -1, ' ');
247         return new Object[][]{
248             {test1200, test1200, MISMATCH_NO, "Compares the file and its copy in zip, no mismatch"},
249             {test9500, test9500m4200, 4200,
250                 "Compares a copy of test9500m4200 in zip with test9500, shall return 4200"},
251             {test80025, test1028500, 80025, "mismatch is the length of the smaller file: 80025"},
252         };
253     }
254 
255     /*
256      * DataProvider for verifying null handling.
257      */
258     @DataProvider(name = "testFileNull")
getDataForNull()259     public Object[][] getDataForNull() throws IOException {
260         Path test = createASCIIFile(testDir, "testNonNull", 2200, -1, ' ');
261         return new Object[][]{
262             {(Path)null, (Path)null},
263             {(Path)null, test},
264             {test, (Path)null},
265         };
266     }
267 
268     /*
269      * DataProvider for verifying how the mismatch method handles the situation
270      * when one or both files do not exist.
271      */
272     @DataProvider(name = "testFileNotExist")
getDataForFileNotExist()273     public Object[][] getDataForFileNotExist() throws IOException {
274         Path test = createASCIIFile(testDir, "testFileNotExist", 3200, -1, ' ');
275         return new Object[][]{
276             {Paths.get("foo"), Paths.get("bar")},
277             {Paths.get("foo"), test},
278             {test, Paths.get("bar")},
279         };
280     }
281 
282     /**
283      * Tests the mismatch method. Refer to the dataProvider testMismatch for more
284      * details about the cases.
285      * @param path the path to a file
286      * @param path2 the path to another file
287      * @param expected the expected result
288      * @param msg the message about the test
289      * @throws IOException if the test fails
290      */
291     @Test(dataProvider = "testMismatch", priority = 0)
testMismatch(Path path, Path path2, long expected, String msg)292     public void testMismatch(Path path, Path path2, long expected, String msg)
293         throws IOException {
294         Assert.assertEquals(Files.mismatch(path, path2), expected, msg);
295     }
296 
297     /**
298      * Tests the mismatch method by comparing files with those in a ZIP file.
299      * @param path the path to a file
300      * @param path2 the path to another file to be added into a ZIP file
301      * @param expected the expected result
302      * @param msg the message about the test
303      * @throws IOException if the test fails
304      */
305     @Test(dataProvider = "testMismatchZipfs", priority = 1)
testMismatchZipfs(Path path, Path path2, long expected, String msg)306     public void testMismatchZipfs(Path path, Path path2, long expected, String msg)
307         throws IOException {
308         Path zipPath = Paths.get(testDir.toString(), "TestWithFSZip.zip");
309         try (FileSystem fs = getZipFSProvider().newFileSystem(zipPath, ZIPFS_MAP)) {
310             Path copy = fs.getPath(path.getFileName().toString());
311             Files.copy(path, copy, REPLACE_EXISTING);
312 
313             if (path2 == null) {
314                 Assert.assertEquals(Files.mismatch(copy, path), expected, msg);
315             } else {
316                 Assert.assertEquals(Files.mismatch(copy, path2), expected, msg);
317             }
318         }
319     }
320 
321     /**
322      * Verifies that NullPointerException is thrown when one or both files are null.
323      * @param path the path to a file
324      * @param path2 the path to another file
325      * @throws NullPointerException as expected
326      */
327     @Test(dataProvider = "testFileNull", priority = 2, expectedExceptions = NullPointerException.class)
testMismatchNull(Path path, Path path2)328     public void testMismatchNull(Path path, Path path2) throws Exception {
329         long result = Files.mismatch(path, path2);
330     }
331 
332     /**
333      * Verifies that IOException is thrown when one or both files do not exist.
334      * @param path the path to a file
335      * @param path2 the path to another file
336      * @throws IOException as expected
337      */
338     @Test(dataProvider = "testFileNotExist", priority = 2, expectedExceptions = IOException.class)
testMismatchNotExist(Path path, Path path2)339     public void testMismatchNotExist(Path path, Path path2) throws IOException {
340         long result = Files.mismatch(path, path2);
341     }
342 
343     /**
344      * Creates a file with ASCII content with one character altered
345      * at the specified position.
346      *
347      * Note: Files.mismatch method does a byte-by-byte comparison. ASCII files
348      * are sufficient for verifying the feature.
349      *
350      * @param dir the directory in which the file is to be created
351      * @param purpose the purpose of the file
352      * @param size the size of the file
353      * @param pos the position where the alternative char is to be added. If it
354      *            is smaller than zero, no alternation shall be made.
355      * @param c the character
356      * @return path of the created file
357      * @throws IOException
358      */
createASCIIFile(Path dir, String purpose, int size, int pos, char c)359     private static Path createASCIIFile(Path dir, String purpose, int size, int pos,
360                                         char c) throws IOException {
361         Path path = Files.createFile(Paths.get(dir.toString(), purpose + ".txt"));
362         if (size > 0) {
363             writeASCIIFile(path, size, pos, c);
364         }
365         return path;
366     }
367 
writeASCIIFile(Path path, int size, int pos, char c)368     private static void writeASCIIFile(Path path, int size, int pos, char c)
369         throws IOException {
370         byte[] a = createASCIIArray(size);
371         if (pos >= 0) a[pos] = (byte)(c & 0xFF); // US_ASCII char only, may cast directly
372         Files.write(path, a);
373     }
374 
createASCIIArray(int length)375     private static byte[] createASCIIArray(int length) {
376         byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 \n"
377             .getBytes(StandardCharsets.US_ASCII);
378         byte[] a = new byte[length];
379         fillArray(bytes, a);
380         return a;
381     }
382 
getZipFSProvider()383     private static FileSystemProvider getZipFSProvider() {
384         for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
385             if ("jar".equals(provider.getScheme())) {
386                 return provider;
387             }
388         }
389         return null;
390     }
391 
392     /**
393      * Fills the destination array by copying the source array repeatedly until
394      * it is completely filled.
395      *
396      * @param src the source array
397      * @param dest the destination array
398      */
fillArray(byte[] src, byte[] dest)399     public static void fillArray(byte[] src, byte[] dest) {
400         int bLen = src.length;
401         int space = dest.length;
402         int iteration = 0;
403 
404         while (space > 0) {
405             if (space >= bLen) {
406                 System.arraycopy(src, 0, dest, iteration++ * bLen, bLen);
407                 space -= bLen;
408             } else {
409                 System.arraycopy(src, 0, dest, iteration++ * bLen, space);
410                 break;
411             }
412         }
413     }
414 }
415