Repository navigation
Expand file tree
/
Copy pathbgptest.py
More file actions
executable file
·98 lines (79 loc) · 2.6 KB
/
Copy pathbgptest.py
File metadata and controls
executable file
·98 lines (79 loc) · 2.6 KB
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
#!/usr/bin/python2.5 # pylint: disable-msg=C6301
#
# Copyright 2009 Google Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A script to test bmpreciever.py."""
import array
import struct
import BGP
def TestBgpHeader():
"""BGP message header related tests."""
buf = array.array("B", [0] * BGP.HEADER_LEN)
# pretend it's an update
#
struct.pack_into(">HB", buf, 16, BGP.HEADER_LEN, BGP.UPDATE)
# invalid marker at position 'y'
#
for y in range(0, 16):
for x in range(0, 16):
if x == y:
buf[x] = 127
else:
buf[x] = 255
try:
BGP.ParseBgpHeader(buf, verbose=False)
except ValueError:
print "marker incorrect - ParseBgpHeader raised ValueError"
# correct the marker for further tests
#
for x in range(0, 16):
buf[x] = 255
# try setting invalid lengths (too small, too large)
#
struct.pack_into(">HB", buf, 16, BGP.MIN_LENGTH - 1, BGP.UPDATE)
try:
BGP.ParseBgpHeader(buf, verbose=False)
except ValueError:
print "packet too short - ParseBgpHeader raised ValueError"
struct.pack_into(">HB", buf, 16, BGP.MAX_LENGTH + 1, BGP.UPDATE)
try:
BGP.ParseBgpHeader(buf, verbose=False)
except ValueError:
print "packet too long - ParseBgpHeader raised ValueError"
# set an incorrect type
#
struct.pack_into(">HB", buf, 16, BGP.MIN_LENGTH, 99)
try:
BGP.ParseBgpHeader(buf, verbose=False)
except ValueError:
print "invalid type - ParseBgpHeader raised ValueError"
def TestBgpRouteRefresh():
"""BGP ROUTE-REFRESH related tests."""
buf = array.array("B", [0] * 4)
for afi in range(0, 4):
for reserved in (0, 1):
for safi in range(0, 4):
struct.pack_into(">HBB", buf, 0, afi, reserved, safi)
try:
print_msg = BGP.ParseBgpRouteRefresh(buf, len(buf))
print "ParseRouteRefresh succeeded: %s" % "".join(print_msg),
except ValueError:
fmt_string = "ParseRouteRefresh raised ValueError for (%d, %d, %d)"
print fmt_string % (afi, reserved, safi)
def main():
TestBgpHeader()
TestBgpRouteRefresh()
if __name__ == "__main__":
main()