1#! /usr/bin/perl -w
2#
3#
4# RCSIDENT("$SiLK: rwflowpack-pack-silk-discard-when.pl 598c37b616f6 2018-03-26 21:57:09Z mthomas $")
5
6use strict;
7use SiLKTests;
8use File::Find;
9
10my $rwflowpack = check_silk_app('rwflowpack');
11
12# find the apps we need.  this will exit 77 if they're not available
13my $rwcat = check_silk_app('rwcat');
14
15# find the data files we use as sources, or exit 77
16my %file;
17$file{data} = get_data_or_exit77('data');
18
19# prefix any existing PYTHONPATH with the proper directories
20check_python_bin();
21
22# set the environment variables required for rwflowpack to find its
23# packing logic plug-in
24add_plugin_dirs('/site/twoway');
25
26# Skip this test if we cannot load the packing logic
27check_exit_status("$rwflowpack --sensor-conf=$srcdir/tests/sensor77.conf"
28                  ." --verify-sensor-conf")
29    or skip_test("Cannot load packing logic");
30
31# create our tempdir
32my $tmpdir = make_tempdir();
33
34# Generate the sensor.conf file
35my $sensor_conf = "$tmpdir/sensor-templ.conf";
36make_packer_sensor_conf($sensor_conf, 'silk', 0, 'polldir', 'discard_when');
37
38# the command that wraps rwflowpack
39my $cmd = join " ", ("$SiLKTests::PYTHON $srcdir/tests/rwflowpack-daemon.py",
40                     ($ENV{SK_TESTS_VERBOSE} ? "--verbose" : ()),
41                     ($ENV{SK_TESTS_LOG_DEBUG} ? "--log-level=debug" : ()),
42                     "--sensor-conf=$sensor_conf",
43                     "--copy $file{data}:incoming",
44                     "--limit=486394",
45                     "--basedir=$tmpdir",
46                     "--",
47                     "--polling-interval=5",
48    );
49
50# run it and check the MD5 hash of its output
51check_md5_output('49fcb3ee5f7b60a8c6d02ef868f05ead', $cmd);
52
53
54# the following directories should be empty
55verify_empty_dirs($tmpdir, qw(error incoming incremental sender));
56
57# path to the data directory
58my $data_dir = "$tmpdir/root";
59die "ERROR: Missing data directory '$data_dir'\n"
60    unless -d $data_dir;
61
62# number of files to find in the data directory
63my $expected_count = 0;
64my $file_count = 0;
65
66# read in the MD5s for every packed file we expect to find.  Although
67# we are packing IPv4 data, whether we write IPv4 or IPv6 files
68# depends on how SiLK was compiled.  Since the zero-packets quirk is
69# enabled, both files have separate bytes and packets values, and the
70# MD5 sums for IPv4 and IPv6 are the same.
71my %md5_map;
72my $md5_file = $0."-ipv6.txt";
73# both the discard-when and discard-unless tests give the same output
74$md5_file =~ s/(discard)-(unless|when)/$1/;
75
76open F, $md5_file
77    or die "ERROR: Cannot open $md5_file: $!\n";
78while (my $lines = <F>) {
79    my ($md5, $path) = split " ", $lines;
80    $md5_map{$path} = $md5;
81    ++$expected_count;
82}
83close F;
84
85# find the files in the data directory and compare their MD5 hashes
86File::Find::find({wanted => \&check_file, no_chdir => 1}, $data_dir);
87
88# did we find all our files?
89if ($file_count != $expected_count) {
90    die "ERROR: Found $file_count files in root; expected $expected_count\n";
91}
92
93# successful!
94exit 0;
95
96
97# this is called by File::Find::find.  The full path to the file is in
98# the $_ variable
99sub check_file
100{
101    # skip anything that is not a file
102    return unless -f $_;
103    my $path = $_;
104    # set $_ to just be the file basename
105    s,^.*/,,;
106    die "ERROR: Unexpected file $path\n"
107        unless $md5_map{$_};
108    ++$file_count;
109
110    # do the MD5 sums match?
111    check_md5_output($md5_map{$_}, ("$rwcat --ipv4-output --byte-order=little"
112                                    ." --compression-method=none $path"));
113}
114