#!/usr/share/ucs-test/runner python3
## desc: Check if modifying the DNS forward zone of a computer only affects PTR records related to him
## tags: [udm-computers,apptest]
## roles: [domaincontroller_master]
## exposure: careful
## packages:
##   - univention-config
##   - univention-directory-manager-tools


from __future__ import print_function
import sys
import atexit
import univention.uldap
import univention.testing.udm
import univention.testing.strings as uts


if __name__ == '__main__':
	ldap = univention.uldap.getMachineConnection(ldap_master=False)
	udm = univention.testing.udm.UCSTestUDM()
	atexit.register(udm.cleanup)

	dnsZone = udm.create_object('dns/forward_zone', zone='%s.%s' % (uts.random_string(numeric=False), uts.random_string(numeric=False)), nameserver='univention')
	dnsZone2 = udm.create_object('dns/forward_zone', zone='%s.%s' % (uts.random_string(numeric=False), uts.random_string(numeric=False)), nameserver='univention')

	rdnsZone = udm.create_object('dns/reverse_zone', subnet='10.20.30', nameserver='univention')
	rdnsZone2 = udm.create_object('dns/reverse_zone', subnet='10.20', nameserver='univention')

	ptrRecordEntry = '%s.%s.%s.' % (uts.random_string(numeric=False), uts.random_string(numeric=False), uts.random_string(numeric=False))
	ptrRecord = udm.create_object('dns/ptr_record', superordinate=rdnsZone, address='40', ptr_record=ptrRecordEntry)

	computerName = uts.random_string()
	computer = udm.create_object('computers/windows', name=computerName, ip='10.20.40.40', dnsEntryZoneForward='%s 10.20.40.40' % dnsZone, dnsEntryZoneReverse='%s 10.20.40.40' % rdnsZone2)
	udm.modify_object('computers/windows', dn=computer, dnsEntryZoneForward='%s 10.20.40.40' % dnsZone2)

	ptr = ldap.search(filter='(&(relativeDomainName=40)(pTRRecord=%s))' % ptrRecordEntry)[0][1]['pTRRecord']
	for entry in ptr:
		if entry.startswith(computerName.encode('UTF-8')):
			print('Test FAILED. Found entry of the modified computer in the PTR record. PTR: %r' % ptr)
			sys.exit(1)
