• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at https://curl.haxx.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22###########################################################################
23# Prepare a directory with known files and clean up afterwards
24use Time::Local;
25
26if ( $#ARGV < 1 )
27{
28    print "Usage: $0 prepare|postprocess dir [logfile]\n";
29    exit 1;
30}
31
32# <precheck> expects an error message on stdout
33sub errout {
34    print $_[0] . "\n";
35    exit 1;
36}
37
38if ($ARGV[0] eq "prepare")
39{
40    my $dirname = $ARGV[1];
41    mkdir $dirname || errout "$!";
42    chdir $dirname;
43
44    # Create the files in alphabetical order, to increase the chances
45    # of receiving a consistent set of directory contents regardless
46    # of whether the server alphabetizes the results or not.
47    mkdir "asubdir" || errout "$!";
48    chmod 0777, "asubdir";
49
50    open(FILE, ">plainfile.txt") || errout "$!";
51    binmode FILE;
52    print FILE "Test file to support curl test suite\n";
53    close(FILE);
54    # The mtime is specifically chosen to be an even number so that it can be
55    # represented exactly on a FAT filesystem.
56    utime time, timegm(0,0,12,1,0,100), "plainfile.txt";
57    chmod 0666, "plainfile.txt";
58
59    open(FILE, ">rofile.txt") || errout "$!";
60    binmode FILE;
61    print FILE "Read-only test file to support curl test suite\n";
62    close(FILE);
63    # The mtime is specifically chosen to be an even number so that it can be
64    # represented exactly on a FAT filesystem.
65    utime time, timegm(0,0,12,31,11,100), "rofile.txt";
66    chmod 0444, "rofile.txt";
67
68    exit 0;
69}
70elsif ($ARGV[0] eq "postprocess")
71{
72    my $dirname = $ARGV[1];
73    my $logfile = $ARGV[2];
74
75    # Clean up the test directory
76    unlink "$dirname/rofile.txt";
77    unlink "$dirname/plainfile.txt";
78    rmdir "$dirname/asubdir";
79
80    rmdir $dirname || die "$!";
81
82    if ($logfile) {
83        # Process the directory file to remove all information that
84        # could be inconsistent from one test run to the next (e.g.
85        # file date) or may be unsupported on some platforms (e.g.
86        # Windows). Also, since 7.17.0, the sftp directory listing
87        # format can be dependent on the server (with a recent
88        # enough version of libssh2) so this script must also
89        # canonicalize the format.  Here are examples of the general
90        # format supported:
91        # -r--r--r--   12 ausername grp            47 Dec 31  2000 rofile.txt
92        # -r--r--r--   1  1234  4321         47 Dec 31  2000 rofile.txt
93        # The "canonical" format is similar to the first (which is
94        # the one generated on a typical Linux installation):
95        # -r-?r-?r-?   12 U         U              47 Dec 31  2000 rofile.txt
96
97        my @canondir;
98        open(IN, "<$logfile") || die "$!";
99        while (<IN>) {
100            /^(.)(..).(..).(..).\s*(\S+)\s+\S+\s+\S+\s+(\S+)\s+(\S+\s+\S+\s+\S+)\s+(.*)$/;
101            if ($1 eq "d") {
102                # Skip current and parent directory listing, because some SSH
103                # servers (eg. OpenSSH for Windows) are not listing those
104                if ($8 eq "." || $8 eq "..") {
105                    next;
106                }
107                # Erase all directory metadata except for the name, as it is not
108                # consistent for across all test systems and filesystems
109                push @canondir, "d?????????    N U         U               N ???  N NN:NN $8\n";
110            } elsif ($1 eq "-") {
111                # Replace missing group and other permissions with user
112                # permissions (eg. on Windows) due to them being shown as *
113                my ($u, $g, $o) = ($2, $3, $4);
114                if($g eq "**") {
115                    $g = $u;
116                }
117                if($o eq "**") {
118                    $o = $u;
119                }
120                # Erase user and group names, as they are not consistent across
121                # all test systems
122                my $line = sprintf("%s%s?%s?%s?%5d U         U %15d %s %s\n", $1,$u,$g,$o,$5,$6,$7,$8);
123                push @canondir, $line;
124            } else {
125                # Unexpected format; just pass it through and let the test fail
126                push @canondir, $_;
127            }
128        }
129        close(IN);
130
131        @canondir = sort {substr($a,57) cmp substr($b,57)} @canondir;
132        my $newfile = $logfile . ".new";
133        open(OUT, ">$newfile") || die "$!";
134        print OUT join('', @canondir);
135        close(OUT);
136
137        unlink $logfile;
138        rename $newfile, $logfile;
139    }
140
141    exit 0;
142}
143print "Unsupported command $ARGV[0]\n";
144exit 1;
145