#
# NameResolutionCheck.ps1
#

. ".\Common.ps1"

Write-Output "`r`n`r`nNAME RESOLUTION TEST ($($uaHostsFileName)):`r`n"
if ($null -ne $uaHostHashTable) {
	foreach ($ipCorrect in $uaHostHashTable.Keys) {
		$names = $uaHostHashTable[$ipCorrect]
		foreach ($name in $names) {
			if (!$name) {
				continue
			}
			$addressList = $null
			try {
				$addressList = [System.Net.Dns]::GetHostAddresses($name)
			}
			catch {
				Write-ErrorMsg "Error: $($name) doesn't resolve."
				continue
			}
			$ipList = New-Object 'System.Collections.Generic.List[string]'
			foreach ($ip in $addressList) {
				if ($ip.AddressFamily -ne "InterNetwork") {
					continue
				}
				if ($ipList.Contains($ip.IPAddressToString)) {
					continue
				}
				$ipList.Add($ip.IPAddressToString) > $null
			}
			if ($ipList.Count -eq 0) {
				Write-ErrorMsg "Error: $($name) doesn't resolve to any IPv4 addresses."
				continue
			}
			if ($ipList.Count -gt 1) {
				Write-ErrorMsg "Error: $($name) resolves to multiple IP addresses: $($ipList -join ', ')"
				continue
			}
			$ip = $ipList[0]
			if ($ipCorrect -eq $ip) {
				#Write-Output "Success: $($name) resolves to $($ipCorrect)"
				Write-Output "Success: $($ipCorrect) resolved from $($name)"
			}
			else {
				Write-ErrorMsg "Error: $($name) resolves to $($ip) instead of $($ipCorrect)."
			}
		}
	}
}