import WebInterfaceTestClass
from network import CHANGE_IP_DELAY


class unittests(WebInterfaceTestClass.WebInterfaceTestClass):

	def atest_ip_change(self):
		'''change of network address'''
		## the time module is necessary for the CHANGE_IP_DELAY
		import time
		self.register_auth(self.URL + "plugins/network")
		self.cmd.go(self.URL + "plugins/network")
		## extract the current IP from the network plugin output
		def getCurrentIP():
			self.cmd.go(self.URL + "plugins/network")
			self.cmd.show()
			self.cmd.find(u'Data.Status.Plugins.network=([0-9\.]*)$', "m")
			return self.locals["__match__"]
		origIPtext = getCurrentIP()
		origIPocts = origIPtext.split(".")
		## check, if the original IP is valid (contains four octets)
		self.assertEquals(4, len(origIPocts))
		self.cmd.echo(origIPocts)
		wrongIP = "192.168.123.321"
		def setIP((ip1, ip2, ip3, ip4)):
			self.cmd.go(self.URL + "plugins/network")
			self.cmd.formvalue("set_ip", "ip1", str(ip1))
			self.cmd.formvalue("set_ip", "ip2", str(ip2))
			self.cmd.formvalue("set_ip", "ip3", str(ip3))
			self.cmd.formvalue("set_ip", "ip4", str(ip4))
			self.cmd.submit()
			## sleep a little bit longer than the delay necessary for ip-change
			time.sleep(CHANGE_IP_DELAY + 0.2)
		setIP([1,-2,0,1])
		self.assertEquals(origIPtext, getCurrentIP())
		setIP([1,0,0,256])
		self.assertEquals(origIPtext, getCurrentIP())
		setIP([1,"foo",0,1])
		self.assertEquals(origIPtext, getCurrentIP())
		setIP([10,12,0,2])
		self.assertEquals("10.12.0.2", getCurrentIP())
		setIP(origIPocts)
		self.assertEquals(origIPtext, getCurrentIP())