File: test_zone.py

package info (click to toggle)
zorp 3.3.6-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,260 kB
  • ctags: 4,707
  • sloc: ansic: 30,937; sh: 10,171; python: 4,051; makefile: 487
file content (101 lines) | stat: -rw-r--r-- 4,226 bytes parent folder | download
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
from Zorp.Core import *
from Zorp.Zorp import quit
from Zorp.Zone import root_zone
from Zorp.SockAddr import inet_ntoa, htonl
from Zorp.Session import MasterSession
from traceback import *
from time import time

config.options.kzorp_enabled = FALSE

def test(str, res, expect):
	if res != expect:
		print str, 'failed,', res, 'should be: ', expect
		raise 'test error'
	else:
		print str, 'ok,', res

def init(name):
	try:
		t1 = InetZone("test1", "192.168.0.0/24", inbound_services=["s1"], outbound_services=["s2"])
		t2 = InetZone("test2", "192.168.0.32/27")
		t3 = InetZone("test3", "192.168.0.0/26")
		t4 = InetZone("test4", "192.168.0.64/27")
		t5 = InetZone("test5", "192.168.0.96/27")
		t6 = InetZone("test6", "192.168.0.0/25")
		t7 = InetZone("test7", "192.168.0.0/16")
		t8 = InetZone("test8", "192.168.1.1/32", admin_parent="test1")
		t9 = InetZone("test9", "192.168.1.2/32", admin_parent="test8")
		t10 = InetZone("test10", "192.168.1.3/32", admin_parent="test9", umbrella=1)
		t11 = InetZone("test11", "192.168.1.4/32", admin_parent="test9")
		t12 = InetZone("test12", "192.168.1.5/32", inbound_services=['*'])
		t13 = InetZone("test13", "192.168.1.6/32", outbound_services=['*'])
		t14 = InetZone("test14", "192.168.0.184", outbound_services=['*'])
		
		test('192.168.0.1', root_zone.findZone(SockAddrInet('192.168.0.1', 10)), t3)
		test('192.168.0.33', root_zone.findZone(SockAddrInet('192.168.0.33', 10)), t2)
		test('192.168.0.65', root_zone.findZone(SockAddrInet('192.168.0.65', 10)), t4)
		test('192.168.0.97', root_zone.findZone(SockAddrInet('192.168.0.97', 10)), t5)
		test('192.168.0.129', root_zone.findZone(SockAddrInet('192.168.0.129', 10)), t1)
		test('192.168.1.129', root_zone.findZone(SockAddrInet('192.168.1.129', 10)), t7)
		test('192.168.0.184', root_zone.findZone(SockAddrInet('192.168.0.184', 10)), t14)

		inet = InetZone("internet", "0.0.0.0/0", inbound_services=["s2"], outbound_services=["s1"])
		test('1.1.1.1', root_zone.findZone(SockAddrInet('1.1.1.1', 10)), inet)
		#for i in range(1,100):
		#	test('masstest1', root_zone.findZone(SockAddrInet(inet_ntoa(htonl(i)), 10)), inet)
		#for i in range(1,100):
		#	test('masstest2', root_zone.findZone(SockAddrInet('192.168.1.129', 10)), t7)
		s = MasterSession()
		s.setService(Service("s1", None))
		s.setServer(SockAddrInet('192.168.1.2', 9999))

		#print time()
		#for i in range(1, 100000):
		#	if s.isServerPermitted() != Z_ACCEPT:
		#		raise 'problema'
		#print time()

		test('service s1#1', t1.isInboundServicePermitted(s), Z_ACCEPT)
		test('service s1#2', t1.isOutboundServicePermitted(s), Z_REJECT)
		test('service s1#3', inet.isInboundServicePermitted(s), Z_REJECT)
		test('service s1#4', inet.isOutboundServicePermitted(s), Z_ACCEPT)
		###
		test('service s1#5', t10.isOutboundServicePermitted(s), Z_REJECT)
		test('service s1#6', t10.isInboundServicePermitted(s), Z_REJECT)
		
		test('service s1#7', t11.isOutboundServicePermitted(s), Z_REJECT)
		test('service s1#8', t11.isInboundServicePermitted(s), Z_ACCEPT)

		test('service s1#9', t12.isInboundServicePermitted(s), Z_ACCEPT)
		test('service s1#10', t12.isOutboundServicePermitted(s), Z_REJECT)

		test('service s1#11', t13.isOutboundServicePermitted(s), Z_ACCEPT)
		test('service s1#12', t13.isInboundServicePermitted(s), Z_REJECT)
		
		
		s.service = Service("s2", None)
		test('service s2#1', t1.isInboundServicePermitted(s), Z_REJECT)
		test('service s2#2', t1.isOutboundServicePermitted(s), Z_ACCEPT)
		test('service s2#3', inet.isInboundServicePermitted(s), Z_ACCEPT)
		test('service s2#4', inet.isOutboundServicePermitted(s), Z_REJECT)
		###
		test('service s2#5', t10.isInboundServicePermitted(s), Z_REJECT)
		test('service s2#6', t10.isOutboundServicePermitted(s), Z_REJECT)

		test('service s2#7', t11.isOutboundServicePermitted(s), Z_ACCEPT)
		test('service s2#8', t11.isInboundServicePermitted(s), Z_REJECT)

		test('service s2#9', t12.isInboundServicePermitted(s), Z_ACCEPT)
		test('service s2#10', t12.isOutboundServicePermitted(s), Z_REJECT)

		test('service s2#11', t13.isOutboundServicePermitted(s), Z_ACCEPT)
		test('service s2#12', t13.isInboundServicePermitted(s), Z_REJECT)

	except Exception, e:
		print_exc()
		quit(1)
		return 1
		
	quit(0)
	return 1