View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.archivers.zip;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  
22  import java.nio.file.attribute.FileTime;
23  import java.time.Instant;
24  import java.util.Date;
25  
26  import org.junit.jupiter.api.Test;
27  
28  public class X000A_NTFSTest {
29  
30      @Test
31      public void testSimpleRoundtrip() throws Exception {
32          final X000A_NTFS xf = new X000A_NTFS();
33          xf.setModifyJavaTime(new Date(0));
34          // one second past midnight
35          xf.setAccessJavaTime(new Date(-11644473601000L));
36          xf.setCreateJavaTime(null);
37          final byte[] b = xf.getLocalFileDataData();
38  
39          final X000A_NTFS xf2 = new X000A_NTFS();
40          xf2.parseFromLocalFileData(b, 0, b.length);
41          assertEquals(new Date(0), xf2.getModifyJavaTime());
42          assertEquals(new Date(-11644473601000L), xf2.getAccessJavaTime());
43          assertNull(xf2.getCreateJavaTime());
44      }
45  
46      @Test
47      public void testSimpleRoundtripWithHighPrecisionDatesWithBigValues() throws Exception {
48          final X000A_NTFS xf = new X000A_NTFS();
49          xf.setModifyFileTime(FileTime.from(Instant.ofEpochSecond(123456789101L, 123456700)));
50          // one second past midnight
51          xf.setAccessFileTime(FileTime.from(Instant.ofEpochSecond(-11644473601L)));
52          // 765432100ns past midnight
53          xf.setCreateFileTime(FileTime.from(Instant.ofEpochSecond(-11644473600L, 765432100)));
54          final byte[] b = xf.getLocalFileDataData();
55  
56          final X000A_NTFS xf2 = new X000A_NTFS();
57          xf2.parseFromLocalFileData(b, 0, b.length);
58          assertEquals(FileTime.from(Instant.ofEpochSecond(123456789101L, 123456700)), xf2.getModifyFileTime());
59          assertEquals(new Date(123456789101123L), xf2.getModifyJavaTime());
60          assertEquals(FileTime.from(Instant.ofEpochSecond(-11644473601L)), xf2.getAccessFileTime());
61          assertEquals(new Date(-11644473601000L), xf2.getAccessJavaTime());
62          assertEquals(FileTime.from(Instant.ofEpochSecond(-11644473600L, 765432100)), xf2.getCreateFileTime());
63          assertEquals(new Date(-11644473599235L).toInstant(), xf2.getCreateJavaTime().toInstant());
64      }
65  
66      @Test
67      public void testSimpleRoundtripWithHighPrecisionDatesWithSmallValues() throws Exception {
68          final X000A_NTFS xf = new X000A_NTFS();
69          // The last 2 digits should not be written due to the 100ns precision
70          xf.setModifyFileTime(FileTime.from(Instant.ofEpochSecond(0, 1234)));
71          // one second past midnight
72          xf.setAccessFileTime(FileTime.from(Instant.ofEpochSecond(-11644473601L)));
73          xf.setCreateFileTime(null);
74          final byte[] b = xf.getLocalFileDataData();
75  
76          final X000A_NTFS xf2 = new X000A_NTFS();
77          xf2.parseFromLocalFileData(b, 0, b.length);
78          assertEquals(FileTime.from(Instant.ofEpochSecond(0, 1200)), xf2.getModifyFileTime());
79          assertEquals(new Date(0), xf2.getModifyJavaTime());
80          assertEquals(FileTime.from(Instant.ofEpochSecond(-11644473601L)), xf2.getAccessFileTime());
81          assertEquals(new Date(-11644473601000L), xf2.getAccessJavaTime());
82          assertNull(xf2.getCreateFileTime());
83          assertNull(xf2.getCreateJavaTime());
84      }
85  }