#!/bin/sh

IPSUMDUMP=/home/mallman/bin/ipsumdump
IPSUMDUMP=ipsumdump

## prelims

echo "CHECKING: $1"
../tcptrunc -r $1 -w all.dmp
../tcptrunc -s -r $1 -w known.dmp

## test 1
##   - check different methods of reading and writing

cat $1 |../tcptrunc -w test1a.dmp
../tcptrunc -r $1 > test1b.dmp
X=`md5sum all.dmp test1?.dmp |awk '{print $1}' |sort -u |wc -l`
([ $X -eq 1 ] && echo "test 1: passed") || echo "test 1: failed"

## test 2
##   - ensure we dump all packets

$IPSUMDUMP --no-headers -t $1 > test2a.pkts
$IPSUMDUMP --no-headers -t all.dmp > test2b.pkts
cmp -s test2?.pkts
([ $? -eq 0 ] && echo "test 2: passed") || echo "test 2: failed"

## test 3
##   - ensure -s actually skips non-TCP and non-UDP packets

$IPSUMDUMP -f 'tcp or udp' --no-headers -tpgG $1 > test3a.pkts
$IPSUMDUMP --no-headers -tpgG known.dmp > test3b.pkts
cmp -s test3?.pkts
([ $? -eq 0 ] && echo "test 3: passed") || echo "test 3: failed"

## test 4
##   - ensure TCP packets generally look the same

$IPSUMDUMP -f 'tcp' --no-headers -tsSdDp --ip-sum -QKO $1 > test4a.pkts
$IPSUMDUMP -f 'tcp' --no-headers -tsSdDp --ip-sum -QKO known.dmp > test4b.pkts
cmp -s test4?.pkts
([ $? -eq 0 ] && echo "test 4: passed") || echo "test 4: failed"

## test 5
##   - ensure UDP packets generally look the same

$IPSUMDUMP -f 'udp' --no-headers -tsSdDp --udp-length $1 > test5a.pkts
$IPSUMDUMP -f 'udp' --no-headers -tsSdDp --udp-length known.dmp > test5b.pkts
cmp -s test5?.pkts
([ $? -eq 0 ] && echo "test 5: passed") || echo "test 5: failed"

## test 6
##   - look for any payload reported by ipsumdump

$IPSUMDUMP --no-headers -t --payload known.dmp > test6a.pkts
X=`grep -c -v '\"\"$' test6a.pkts`
([ $X -eq 0 ] && echo "test 6: passed") || echo "test 6: failed"

## test 7
##   - ensure we don't do anything to an already truncated trace

../tcptrunc -r all.dmp -w test7a.dmp
cmp -s all.dmp test7a.dmp
([ $? -eq 0 ] && echo "test 7: passed") || echo "test 7: failed"

exit 0
