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    *      https://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.net.ftp.parser;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.Calendar;
26  
27  import org.apache.commons.net.ftp.FTPFile;
28  import org.apache.commons.net.ftp.FTPFileEntryParser;
29  import org.junit.jupiter.api.Test;
30  
31  class MacOsPeterFTPEntryParserTest extends AbstractFTPParseTest {
32  
33      private static final String[] badsamples = { "drwxr-xr-x    123       folder        0 Jan  4 14:49 Steak", };
34  
35      private static final String[] goodsamples = { "-rw-r--r--    54149       27826    81975 Jul 22  2010 09.jpg",
36              "drwxr-xr-x               folder        0 Jan  4 14:51 Alias_to_Steak",
37              "-rw-r--r--    78440       49231   127671 Jul 22  2010 Filename with whitespace.jpg",
38              "-rw-r--r--    78440       49231   127671 Jul 22 14:51 Filename with whitespace.jpg",
39              "-rw-r--r--        0      108767   108767 Jul 22  2010 presentation03.jpg",
40              "-rw-r--r--    58679       60393   119072 Jul 22  2010 presentation04.jpg",
41              "-rw-r--r--    82543       51433   133976 Jul 22  2010 presentation06.jpg",
42              "-rw-r--r--    83616     1430976  1514592 Jul 22  2010 presentation10.jpg",
43              "-rw-r--r--        0       66990    66990 Jul 22  2010 presentation11.jpg", "drwxr-xr-x               folder        0 Jan  4 14:49 Steak",
44              "-rwx------        0       12713    12713 Jul  8  2009 Twitter_Avatar.png", };
45  
46      /**
47       * Method checkPermissions. Verify that the persmissions were properly set.
48       *
49       * @param f
50       */
51      private void checkPermissions(final FTPFile f) {
52          assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION), "Should have user read permission.");
53          assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION), "Should have user write permission.");
54          assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should have user execute permission.");
55          assertTrue(f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION), "Should have group read permission.");
56          assertFalse(f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION), "Should NOT have group write permission.");
57          assertTrue(f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should have group execute permission.");
58          assertTrue(f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION), "Should have world read permission.");
59          assertFalse(f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION), "Should NOT have world write permission.");
60          assertTrue(f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should have world execute permission.");
61      }
62  
63      @Override
64      protected String[] getBadListing() {
65          return badsamples;
66      }
67  
68      @Override
69      protected String[] getGoodListing() {
70          return goodsamples;
71      }
72  
73      @Override
74      protected FTPFileEntryParser getParser() {
75          return new MacOsPeterFTPEntryParser();
76      }
77  
78      @Override
79      @Test
80      void testDefaultPrecision() {
81          testPrecision("-rw-r--r--    78440       49231   127671 Jul 22  2010 Filename with whitespace.jpg", CalendarUnit.DAY_OF_MONTH);
82      }
83  
84      @Override
85      @Test
86      void testParseFieldsOnDirectory() throws Exception {
87          final FTPFile f = getParser().parseFTPEntry("drwxr-xr-x               folder        0 Mar  2 15:13 Alias_to_Steak");
88          assertNotNull(f, "Could not parse entry.");
89          assertTrue(f.isDirectory(), "Should have been a directory.");
90          checkPermissions(f);
91          assertEquals(0, f.getHardLinkCount());
92          assertNull(f.getUser());
93          assertNull(f.getGroup());
94          assertEquals(0, f.getSize());
95          assertEquals("Alias_to_Steak", f.getName());
96  
97          final Calendar cal = Calendar.getInstance();
98          cal.set(Calendar.MONTH, Calendar.MARCH);
99  
100         cal.set(Calendar.DAY_OF_MONTH, 1);
101         cal.set(Calendar.HOUR_OF_DAY, 0);
102         cal.set(Calendar.MINUTE, 0);
103         cal.set(Calendar.SECOND, 0);
104         if (f.getTimestamp().getTime().before(cal.getTime())) {
105             cal.add(Calendar.YEAR, -1);
106         }
107         cal.set(Calendar.DAY_OF_MONTH, 2);
108         cal.set(Calendar.HOUR_OF_DAY, 15);
109         cal.set(Calendar.MINUTE, 13);
110 
111         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
112     }
113 
114     @Override
115     @Test
116     void testParseFieldsOnFile() throws Exception {
117         final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x    78440       49231   127671 Jul  2 14:51 Filename with whitespace.jpg");
118         assertNotNull(f, "Could not parse entry.");
119         assertTrue(f.isFile(), "Should have been a file.");
120         checkPermissions(f);
121         assertEquals(0, f.getHardLinkCount());
122         assertNull(f.getUser());
123         assertNull(f.getGroup());
124         assertEquals("Filename with whitespace.jpg", f.getName());
125         assertEquals(127671L, f.getSize());
126 
127         final Calendar cal = Calendar.getInstance();
128         cal.set(Calendar.MONTH, Calendar.JULY);
129 
130         cal.set(Calendar.DAY_OF_MONTH, 1);
131         cal.set(Calendar.HOUR_OF_DAY, 0);
132         cal.set(Calendar.MINUTE, 0);
133         cal.set(Calendar.SECOND, 0);
134         if (f.getTimestamp().getTime().before(cal.getTime())) {
135             cal.add(Calendar.YEAR, -1);
136         }
137         cal.set(Calendar.DAY_OF_MONTH, 2);
138         cal.set(Calendar.HOUR_OF_DAY, 14);
139         cal.set(Calendar.MINUTE, 51);
140         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
141     }
142 
143     @Override
144     @Test
145     void testRecentPrecision() {
146         testPrecision("-rw-r--r--    78440       49231   127671 Jul 22 14:51 Filename with whitespace.jpg", CalendarUnit.MINUTE);
147     }
148 
149 }