tor-netdoc 0.49.0

Network document formats used with the Tor protocols.
Documentation
#!/usr/bin/env bash
#
# usage:
#   testdata2-download \
#       https://gitlab.torproject.org/tpo/core/arti/-/jobs/NNNN/artifacts/download \
#       [artifacts.zip]
#
# The URL is the "download artifacts archive link"
# for an integration-chutney-shadow job.
#
# For the actual URL most recently used (or when reviewing), see `testdata2/README.md`.
#
# This has to be run inside the git repository!
#
# If gitlab won't let you download this with wget (perhaps due to DDoS/scraper defences),
# log into gitlab and use your browser to download the file, and then
# pass the filename (artifacts.zip) as a 2nd argument.  The URL is then just recorded, not used.
#
# Note!  It is quite possible that this will break if the structure of
# the shadow tests in CI changes.  If that happens, you need to adjust this
# script and/or test code that uses this test data.
#
# (We could run this script in CI, detecting this situation, but then
# if adjustments are needed they would be blocking for improvements to
# the integration tests.  And then we'd want to rerun cargo tests in CI etc.
# It seems better to defer any such rework/adjustment until the test data
# needs to be refreshed for other reasons.)

set -euo pipefail

case "$#.$1" in
[12].[^-]*) ;;
*) echo >&2 'bad usage'; exit 8 ;;
esac

url="$1"; shift
override_source="${1-}"

root="$(git rev-parse --show-toplevel)"
out="$root/crates/tor-netdoc/testdata2"

# switch to the repositories' root to avoid surprises
cd "$root"

#----- download zipfile and cd into the one node we are interested in -----

rm -rf tmp
mkdir tmp

case "$override_source" in
    '') wget -O tmp/artifacts.zip "$url" ;;
    /*|./*) cp "$override_source" tmp/artifacts.zip ;;
    *) echo >&2 'bad source override, should be a path'; exit 4 ;;
esac

cd tmp

unzip -q artifacts.zip chutney-net/nodes.\*

cd chutney-net
cd nodes.*.* # will give "too many arguments" if there were several
cd 000a

rm -rf -- *.log diff-cache pid.* *.stderr *.stdout
chmod a+rx keys
chmod a+r keys/*

#----- manual adjustments -----

cat >README.md <<END
Test data from a CI run using chutney (checked-in constants, occasionally updated)

All files in this directory/module are automatically maintained.

Downloaded and massaged by $0
From CI data at <$url>
END

# v3-status-votes is the votes concatenated, etc.

split-file () {
    local f=$1
    local headline=$2
    
    perl -wpe '
        BEGIN { $seq = 0; }
	if (m{^'"$headline"' }) {
	    open STDOUT, ">", "$ARGV--".(++$seq) or die $!;
	}
    ' "$f"
}

split-file v3-status-votes network-status-version
split-file cached-certs dir-key-certificate-version

# cached-descriptors.new and cached-microdescs.new contain lines starting with
# "@", which ctor uses to store meta information; we want to remove those.

patch_atline () {
    local f=$1
    perl -i -ne 'print unless /^\@/' "$f"
}

patch_atline cached-descriptors.new
patch_atline cached-microdescs.new

#----- install -----

exec 4>generated_consts.rs
cat <<'END' >&4
#![doc = include_str!("README.md")]
//!
//! Constants are the file contents from CI, with names converted to SHOUTING_SNAKE_CASE.
//! TODO it would be nice if this data were more structured, like `testdata-live` is.

END

# Some of the files are binary, and so can't be &str.  We deal with that ad-hoc, here.
# Syntax: will be substituted into Perl, inside m{}.
binaries_re='^ed25519|secret_onion_key_ntor'

# shellcheck disable=SC2016 # complains about non-expansion of $-things in the perl script
find . -type f					\
     -name '[a-z]*'				\
     ! -name \*.md				\
     ! -name \*.rs				\
     -print0					\
| sort -z | xargs -0 perl >&4 -mautodie -we '
    use strict;
    our %files;
    foreach my $f (@ARGV) {
	$f =~ s{^\./}{};
	my $d = \%files;
        while ($f =~ s{^([^/]+)/}{}) {
            $d = $d->{$1} //= {};
        }
        $d->{$f} = 1;
    }

    #use Data::Dumper;
    #print STDERR Dumper(\%files);

    # Write a Rust module containing constants for files collected in %$d.
    sub pr ($$);
    sub pr ($$) {
        my ($dirpath, $d) = @_;
        foreach my $k (sort keys %$d) {
            my $path = $dirpath.$k;
	    my $v = $d->{$k};
            my $name = $k;
            $name =~ y/-./__/;
            my $const = $name;
            $const =~ y/-a-z/_A-Z/;
            print "/// `testdata2/$path`\n";
            if (ref $v) {
                print "pub mod $name {\n";
                pr("$path/", $v);
                print "}\n";
            } elsif ($k =~ m{'"$binaries_re"'}) { # binaries!
	        print "pub const $const: &[u8] = include_bytes!(\"$path\");\n" or die $!;
            } else {
	        print "pub const $const: &str = include_str!(\"$path\");\n" or die $!;
	    }
	}
    }
    pr("", \%files);
    flush STDOUT or die $!; # detect ENOSPC, bugs.debian.org/28250, from 1998, still unfixed
'

chmod -R a+r,u+w .

rm -rf "$out"
mkdir "$out"
mv -- * "$out"

cd "$root"
rm -rf tmp

echo "Updated $out.  Don't forget to git add."