from Zorp.SockAddr import SockAddrInet
from Zorp.Session import MasterSession
from Zorp.Service import Service
from Zorp.Zone import *
from Zorp.Zorp import quit
from traceback import *

def test(str, res, expect):
	if res != expect:
		print str, 'failed,', res, 'should be: ', expect
	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")
		inet = InetZone("internet", "0.0.0.0/0", inbound_services=["s2"], outbound_services=["s1"])
		
		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)
		s = MasterSession()
		s.service = Service("s1", None)
		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)
		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)
	except Exception, e:
		print_exc()
		pass
		quit(1)
		return
		
	quit(0)
