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