########################################################################
#
#  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


A_expected_responses = [
        'cce6290ca5aba249943f589c572e307f',
        'c05fa9122d3d7a63533485185f9eca95',
        '7915d3cd08d9c654a90f093d9cad8359',
        'ef03c6d047114371bacb9841603d69f6'
    ]

A_domain = '.dnstester.cz'
A_txt_payloads = [512, 1024, 4096, 8192]
A_txt_domains = ['small', 'medium', 'large', 'huge']
A_txt_recordcnt = [4, 14, 20, 75]


class TestA(TestBoolean):

    def __init__(self, tcp, bufsize, datasize, tc_expected = False):

        if tcp:
            name = "AT.%d.%d" % (bufsize + 1, datasize + 1)
        else:
            name = "AU.%d.%d" % (bufsize + 1, datasize + 1)

        TestBoolean.__init__(self, name)

        self.tcp = tcp
        self.bufsize = bufsize
        self.datasize = datasize
        self.tc_expected = tc_expected

    def run(self):

        self.set_result(False)

        try:
            myresolver = dns.resolver.Resolver()
            myresolver.use_edns(0, 0, A_txt_payloads[self.bufsize])

            try:
                dns_result = myresolver.query(A_txt_domains[self.datasize] + A_domain, 'TXT', 1, self.tcp)
            except dns.resolver.NoAnswer:
                self.msg_add("dns query no answer")
                return
            except dns.resolver.NXDOMAIN:
                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")
                if self.tc_expected:
                    self.set_result(True)
                return

            if len(dns_result.rrset) <> A_txt_recordcnt[self.datasize]:
                self.msg_add("invalid number of records")
                return
            
            hexdigest = dnstests.answer_digest(dns_result)
            if hexdigest <> A_expected_responses[self.datasize]:
                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


class TestAU(TestA):

    def __init__(self, bufsize, datasize, tc_expected):
        TestA.__init__(self, False, bufsize, datasize, tc_expected)

class TestAT(TestA):

    def __init__(self, bufsize, datasize):
        TestA.__init__(self, True, bufsize, datasize)

