test: sort ferm output
[ferm.git] / test / sort.pl
bloba497c7097f0b25d9ac8025ba51582f441038d8dd
1 #!/usr/bin/perl -w
4 # ferm, a firewall setup program that makes firewall rules easy!
6 # Copyright (C) 2001-2014 Max Kellermann
8 # Comments, questions, greetings and additions to this program
9 # may be sent to <ferm@foo-projects.org>
12 # This script is sorts the tables and chains in the ferm output so the
13 # unit test suite can use "diff" to verify it. It's a kludge that is
14 # necessary because ferm outputs these in random order (because Perl
15 # does).
17 use strict;
19 my %rules;
21 my $table;
23 while (<>) {
24 next if /^#/;
26 if (/^(\w+)tables -t (\w+) -([NAP]) (\S+)/ or
27 /^(\w+)tables -t (\w+) -([FX])()$/) {
28 my $key = $3 eq 'P' ? "$1 $2 $4" : "$1 $2 $4";
29 $key .= ' z' if $4 eq '';
30 my $array = $rules{$key} ||= [];
31 push @$array, $_;
32 } elsif (/^\*(\S+)/) {
33 $table = $1;
34 my $key = $table;
35 my $array = $rules{$key} ||= [];
36 push @$array, $_;
37 } elsif (/^COMMIT/) {
38 my $key = $table . 'z';
39 my $array = $rules{$key} ||= [];
40 push @$array, $_;
41 } elsif (/^(:)(\S+)/ or /^-(A) (\S+)/) {
42 my $key = $table . $1 . $2;
43 my $array = $rules{$key} ||= [];
44 push @$array, $_;
45 } else {
46 die;
50 foreach my $key (sort keys %rules) {
51 foreach my $line (@{$rules{$key}}) {
52 print $line;