########################################################################
#
#  Copyright (C) 2009, CZ.NIC, z.s.p.o.
# 
#  This file is part of dnstester.
#
#  dnstester is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  dnstester is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################

import traceback
import dns.resolver
from test import TestBoolean
import dnstests

G_queries = [
    {'domain' : 'test.dnstester.cz', 'rrtype': 'AAAA', 'hash': '05420c780e89bffcb4183ceb46872b08'},
    {'domain' : 'test.dnstester.cz', 'rrtype': 'SSHFP', 'hash': '258278be58faab829ef3aa163734bfc8'},
    {'domain' : '_sip._udp.dnstester.cz', 'rrtype': 'SRV', 'hash': '8e4c40bac63d21c85ca2775281f8f4df'},
    {'domain' : 'dnstester.cz', 'rrtype': 'NAPTR', 'hash': '898453ef80e62ba96a249d103ef96e06'},
    {'domain' : '1.0.168.192.in-addr.arpa', 'rrtype': 'PTR', 'hash': ''}
    ]


class TestG(TestBoolean):

    def __init__(self, t):
        TestBoolean.__init__(self, "G.%d" % (t + 1))
        self.t = t #type

    def run(self):
        self.set_result(False)

        try:
            myresolver = dns.resolver.Resolver()

            try:
                dns_result = myresolver.query(G_queries[self.t]['domain'], G_queries[self.t]['rrtype'])
            except dns.resolver.NoAnswer:
                self.msg_add("dns query no answer")
                return
            except dns.resolver.NXDOMAIN:
                if G_queries[self.t]['hash'] == '':
                    self.msg_add("pass")
                    self.set_result(True)
                    return
                self.msg_add("dns query nonexistent domain")
                return
            except dns.resolver.NoNameservers:
                self.msg_add("dns query no nameservers")
                return
            except dns.resolver.Timeout:
                self.msg_add("dns query timeout")
                return
            except dns.resolver.Truncated:
                self.msg_add("query truncated")
                return

            texts = [];
            for rec in dns_result:
                texts.append(str(rec))
            texts.sort()

            hexdigest = dnstests.answer_digest(dns_result)
            if hexdigest <> G_queries[self.t]['hash']:
                self.msg_add("invalid response")
                self.msg_add(str(dns_result.response))
                self.msg_add("response digest = \"%s\"" % hexdigest)
                return

            self.msg_add("pass")
            self.set_result(True)
            return
        except Exception, e:
            log = traceback.format_exc()
            self.msg_add(log)
            return
