aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/google/gopacket/gc
blob: 57bcdee2acfe4bee24eed8b2c5b98bbcd5ec5143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# Copyright 2012 Google, Inc. All rights reserved.

# This script provides a simple way to run benchmarks against previous code and
# keep a log of how benchmarks change over time.  When used with the --benchmark
# flag, it runs benchmarks from the current code and from the last commit run
# with --benchmark, then stores the results in the git commit description.  We
# rerun the old benchmarks along with the new ones, since there's no guarantee
# that git commits will happen on the same machine, so machine differences could
# cause wildly inaccurate results.
#
# If you're making changes to 'gopacket' which could cause performance changes,
# you may be requested to use this commit script to make sure your changes don't
# have large detrimental effects (or to show off how awesome your performance
# improvements are).
#
# If not run with the --benchmark flag, this script is still very useful... it
# makes sure all the correct go formatting, building, and testing work as
# expected.

function Usage {
  cat <<EOF
USAGE:  $0 [--benchmark regexp] [--root] [--gen] <git commit flags...>

--benchmark:  Run benchmark comparisons against last benchmark'd commit
--root:  Run tests that require root priviledges
--gen:  Generate code for MACs/ports by pulling down external data

Note, some 'git commit' flags are necessary, if all else fails, pass in -a
EOF
  exit 1
}

BENCH=""
GEN=""
ROOT=""
while [ ! -z "$1" ]; do
  case "$1" in
    "--benchmark")
      BENCH="$2"
      shift
      shift
      ;;
    "--gen")
      GEN="yes"
      shift
      ;;
    "--root")
      ROOT="yes"
      shift
      ;;
    "--help")
      Usage
      ;;
    "-h")
      Usage
      ;;
    "help")
      Usage
      ;;
    *)
      break
      ;;
  esac
done

function Root {
  if [ ! -z "$ROOT" ]; then
    local exec="$1"
    # Some folks (like me) keep source code in places inaccessible by root (like
    # NFS), so to make sure things run smoothly we copy them to a /tmp location.
    local tmpfile="$(mktemp -t gopacket_XXXXXXXX)"
    echo "Running root test executable $exec as $tmpfile"
    cp "$exec" "$tmpfile"
    chmod a+x "$tmpfile"
    shift
    sudo "$tmpfile" "$@"
  fi
}

if [ "$#" -eq "0" ]; then
  Usage
fi

cd $(dirname $0)

# Check for copyright notices.
for filename in $(find ./ -type f -name '*.go'); do
  if ! head -n 1 "$filename" | grep -q Copyright; then
    echo "File '$filename' may not have copyright notice"
    exit 1
  fi
done

set -e
set -x

if [ ! -z "$ROOT" ]; then
  echo "Running SUDO to get root priviledges for root tests"
  sudo echo "have root"
fi

if [ ! -z "$GEN" ]; then
  pushd macs
  go run gen.go | gofmt > valid_mac_prefixes.go
  popd
  pushd layers
  go run gen.go | gofmt > iana_ports.go
  popd
fi

# Make sure everything is formatted, compiles, and tests pass.
go fmt ./...
go test -i ./... 2>/dev/null >/dev/null || true
go test
go build
pushd examples/bytediff
go build
popd
if [ -f /usr/include/pcap.h ]; then
  pushd pcap
  go test ./...
  go build ./...
  go build pcap_tester.go
  Root pcap_tester --mode=basic
  Root pcap_tester --mode=filtered
  Root pcap_tester --mode=timestamp || echo "You might not support timestamp sources"
  popd
  pushd examples/pcapdump
  go build
  popd
  pushd examples/arpscan
  go build
  popd
  pushd examples/bidirectional
  go build
  popd
  pushd examples/synscan
  go build
  popd
  pushd examples/httpassembly
  go build
  popd
  pushd examples/statsassembly
  go build
  popd
fi
pushd macs
go test ./...
gofmt -w gen.go
go build gen.go
popd
pushd tcpassembly
go test ./...
popd
pushd reassembly
go test ./...
popd
pushd layers
gofmt -w gen.go
go build gen.go
go test ./...
popd
pushd pcapgo
go test ./...
go build ./...
popd
if [ -f /usr/include/linux/if_packet.h ]; then
  if grep -q TPACKET_V3 /usr/include/linux/if_packet.h; then
    pushd afpacket
    go build ./...
    go test ./...
    popd
  fi
fi
if [ -f /usr/include/pfring.h ]; then
  pushd pfring
  go test ./...
  go build ./...
  popd
  pushd examples/pfdump
  go build
  popd
fi

for travis_script in `ls .travis.*.sh`; do
  ./$travis_script
done

# Run our initial commit
git commit "$@"

if [ -z "$BENCH" ]; then
  set +x
  echo "We're not benchmarking and we've committed... we're done!"
  exit
fi

### If we get here, we want to run benchmarks from current commit, and compare
### then to benchmarks from the last --benchmark commit.

# Get our current branch.
BRANCH="$(git branch | grep '^*' | awk '{print $2}')"

# File we're going to build our commit description in.
COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"

# Add the word "BENCH" to the start of the git commit.
echo -n "BENCH " > $COMMIT_FILE

# Get the current description... there must be an easier way.
git log -n 1 | grep '^ ' | sed 's/^    //' >> $COMMIT_FILE

# Get the commit sha for the last benchmark commit
PREV=$(git log -n 1 --grep='BENCHMARK_MARKER_DO_NOT_CHANGE' | head -n 1 | awk '{print $2}')

## Run current benchmarks

cat >> $COMMIT_FILE <<EOF


----------------------------------------------------------
BENCHMARK_MARKER_DO_NOT_CHANGE
----------------------------------------------------------

Go version $(go version)


TEST BENCHMARKS "$BENCH"
EOF
# go seems to have trouble with 'go test --bench=. ./...'
go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
pushd layers
go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
popd
cat >> $COMMIT_FILE <<EOF


PCAP BENCHMARK
EOF
if [ "$BENCH" -eq ".*" ]; then
  go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
fi



## Reset to last benchmark commit, run benchmarks

git checkout $PREV

cat >> $COMMIT_FILE <<EOF
----------------------------------------------------------
BENCHMARKING AGAINST COMMIT $PREV
----------------------------------------------------------


OLD TEST BENCHMARKS
EOF
# go seems to have trouble with 'go test --bench=. ./...'
go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
pushd layers
go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
popd
cat >> $COMMIT_FILE <<EOF


OLD PCAP BENCHMARK
EOF
if [ "$BENCH" -eq ".*" ]; then
  go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
fi



## Reset back to the most recent commit, edit the commit message by appending
## benchmark results.
git checkout $BRANCH
git commit --amend -F $COMMIT_FILE