########################################################################
#
#  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
import config
from test import TestBoolean
import dnstests

D_domains = [config.dnssec_domain, config.non_dnssec_domain]
D_expected_responses = [
    '556c27b3e2b3572ec50f0e83464904c8',
    '0809c636862e480d4eda33d87cd8b317',
    ]


class TestD(TestBoolean):

    def __init__(self, domain):
        TestBoolean.__init__(self, "D.%d" % (domain + 1))
        self.domain = domain

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

        try:
            myresolver = dns.resolver.Resolver()
            myresolver.use_edns(1, dns.flags.DO, 0)

            try:
                dns_result = myresolver.query(D_domains[self.domain], 'SOA', flags=dns.flags.CD)
            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")
                return

            if dns_result.response.edns != 0:
                self.msg_add("missing EDNS0 extension")
                return 

            if dns_result.response.ednsflags != dns.flags.DO:
                self.msg_add("missing DO flag")
                return 

            if not (dns_result.response.flags & dns.flags.CD):
                self.msg_add("missing CD flag")
                return

            hexdigest = dnstests.answer_digest(dns_result)
            if hexdigest <> D_expected_responses[self.domain]:
                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

