Example #1
0
def test_response_cname():
    dns = DNSHeader.parse(
        '\x83-'     # identification
        '\x81\x80'  # flags and codes
        '\x00\x01'  # total questions
        '\x00\x01'  # total answers
        '\x00\x00'  # total authority RRs
        '\x00\x00'  # total additional RRs
        # question RR - IN CNAME www.slackware.it
        '\x03www\tslackware\x02it\x00' '\x00\x05' '\x00\x01'
        # compression ptr - type CNAME - class IN
        '\xc0\x0c' '\x00\x05' '\x00\x01'
        '\x00\x00\x01\x13\x00\x02\xc0\x10'
    )

    # check questions
    assert dns.total_questions == 1
    assert len(dns.questions) == 1
    assert dns.questions[0].qname == ['www', 'slackware', 'it', '']

    # check answers
    assert dns.total_answer_rrs == 1
    assert len(dns.answers) == 1

    answer = dns.answers[0]
    assert answer.name == ['www', 'slackware', 'it', '']
    assert answer.type == 'CNAME'
    assert answer['class'] == 'IN'
    assert answer.ttl == 275
    assert answer.rdlength == 2     # it's a pointer

    rr = answer.rdata
    assert rr.name == ['slackware', 'it', '']
Example #2
0
def test_answer_txt():
    dns = DNSHeader.parse(
        '\x102'     # identification
        '\x81\x80'  # flags and codes
        '\x00\x01'  # total questions
        '\x00\x01'  # total answers
        '\x00\x00'  # total authority RRs
        '\x00\x00'  # total additional RRs
        # question RR - IN TXT google.com
        '\x06google\x03com\x00' '\x00\x10' '\x00\x01'
        # answer RR - no compression
        '\x06google\x03com\x00\x00\x10\x00\x01\x00\x00\x01'
        '\x0e\x00\x10\x0fv=spf1 ptr ?all'
    )
    assert dns.identification == 4146
    assert dns.total_questions == 1
    assert dns.total_answer_rrs == 1
    assert len(dns.answers) == 1
    assert dns.answers[0].rdata == 'v=spf1 ptr ?all'
Example #3
0
def test_request_dns_compression():
    packet = (
        '\x97\xc9'  # identification
        '\x81\x80'  # flags and codes
        '\x00\x01'  # total questions
        '\x00\x06'  # total answer RRs
        '\x00\x00'  # total authority RRs
        '\x00\x00'  # total additional RRs
        # questions
        '\x06google\x03com\x00'     # qname
        '\x00\x01'                  # qtype
        '\x00\x01'                  # qclass
        # Answers
        # answer #1
        '\xc0\x0c'          # pointer to absolute offset 0x0c
        '\x00\x01'          # type A
        '\x00\x01'          # class IN
        '\x00\x00\x01\x16'  # ttl 278
        '\x00\x04'          # rdlength
        'J}\x18q'           # rdata, IP address
        # answer #2
        '\xc0\x0c'          # pointer to absolute offset 0x0c
        '\x00\x01'          # type A
        '\x00\x01'          # class IN
        '\x00\x00\x01\x16'  # ttl 278
        '\x00\x04'          # rdlength
        'J}\x18f'           # rdata, IP address
        # answer #3
        '\xc0\x0c'          # pointer to absolute offset 0x0c
        '\x00\x01'          # type A
        '\x00\x01'          # class IN
        '\x00\x00\x01\x16'  # ttl 278
        '\x00\x04'          # rdlength
        'J}\x18\x8a'        # rdata, IP address
        # answer #4
        '\xc0\x0c'          # pointer to absolute offset 0x0c
        '\x00\x01'          # type A
        '\x00\x01'          # class IN
        '\x00\x00\x01\x16'  # ttl 278
        '\x00\x04'          # rdlength
        'J}\x18\x8b'        # rdata, IP address
        # answer #5
        '\xc0\x0c'          # pointer to absolute offset 0x0c
        '\x00\x01'          # type A
        '\x00\x01'          # class IN
        '\x00\x00\x01\x16'  # ttl 278
        '\x00\x04'          # rdlength
        'J}\x18d'           # rdata, IP address
        # answer #6
        '\xc0\x0c'          # pointer to absolute offset 0x0c
        '\x00\x01'          # type A
        '\x00\x01'          # class IN
        '\x00\x00\x01\x16'  # ttl 278
        '\x00\x04'          # rdlength
        'J}\x18e'           # rdata, IP address
    )
    dns = DNSHeader.parse(packet)

    # check questions
    assert len(dns.questions) == 1
    question = dns.questions[0]
    assert question.qtype == 'A'
    assert question.qclass == 'IN'
    assert question.qname == ['google', 'com', '']

    # check answer RRs
    assert len(dns.answers) == 6

    answer0 = dns.answers[0]
    assert answer0.name == ['google', 'com', '']
    assert answer0.ttl == 278
    assert answer0.type == 'A'
    assert answer0['class'] == 'IN'
    assert answer0.rdlength == 4
    assert answer0.rdata == '74.125.24.113'

    answer1 = dns.answers[1]
    assert answer1.name == ['google', 'com', '']
    assert answer1.ttl == 278
    assert answer1.type == 'A'
    assert answer1['class'] == 'IN'
    assert answer1.rdlength == 4
    assert answer1.rdata == '74.125.24.102'

    answer2 = dns.answers[2]
    assert answer2.name == ['google', 'com', '']
    assert answer2.ttl == 278
    assert answer2.type == 'A'
    assert answer2['class'] == 'IN'
    assert answer2.rdlength == 4
    assert answer2.rdata == '74.125.24.138'

    answer3 = dns.answers[3]
    assert answer3.name == ['google', 'com', '']
    assert answer3.ttl == 278
    assert answer3.type == 'A'
    assert answer3['class'] == 'IN'
    assert answer3.rdlength == 4
    assert answer3.rdata == '74.125.24.139'

    answer4 = dns.answers[4]
    assert answer4.name == ['google', 'com', '']
    assert answer4.ttl == 278
    assert answer4.type == 'A'
    assert answer4['class'] == 'IN'
    assert answer4.rdlength == 4
    assert answer4.rdata == '74.125.24.100'

    answer5 = dns.answers[5]
    assert answer5.name == ['google', 'com', '']
    assert answer5.ttl == 278
    assert answer5.type == 'A'
    assert answer5['class'] == 'IN'
    assert answer5.rdlength == 4
    assert answer5.rdata == '74.125.24.101'

    # check authority RRs
    assert len(dns.authority) == 0

    # check additional RRs
    assert len(dns.additional) == 0