Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

# Copyright (C) 2012  Internet Systems Consortium. 

# 

# Permission to use, copy, modify, and distribute this software for any 

# purpose with or without fee is hereby granted, provided that the above 

# copyright notice and this permission notice appear in all copies. 

# 

# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM 

# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 

# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 

# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, 

# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 

# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 

# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 

# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

 

import unittest 

from pydnspp import * 

 

class NSEC3HashTest(unittest.TestCase): 

    '''These tests are mostly straightforward conversion of C++ tests 

    except for python specific type checks. 

 

    ''' 

 

    def setUp(self): 

        self.nsec3_common = "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG" 

        self.test_hash = NSEC3Hash(Rdata(RRType.NSEC3PARAM(), RRClass.IN(), 

                                         "1 0 12 aabbccdd")) 

        self.test_hash_nsec3 = NSEC3Hash(Rdata(RRType.NSEC3(), RRClass.IN(), 

                                               "1 0 12 aabbccdd " + 

                                               self.nsec3_common)) 

    def test_bad_construct(self): 

        # missing parameter 

        self.assertRaises(TypeError, NSEC3Hash) 

 

        # invalid type of argument 

        self.assertRaises(TypeError, NSEC3Hash, "1 0 12 aabbccdd") 

 

        # additional parameter 

        self.assertRaises(TypeError, NSEC3Hash, Rdata(RRType.NSEC3PARAM(), 

                                                      RRClass.IN(), 

                                                      "1 0 12 aabbccdd"), 1) 

 

        # Invaid type of RDATA 

        self.assertRaises(TypeError, NSEC3Hash, Rdata(RRType.A(), RRClass.IN(), 

                                                      "192.0.2.1")) 

 

    def test_unknown_algorithm(self): 

        self.assertRaises(UnknownNSEC3HashAlgorithm, NSEC3Hash, 

                          Rdata(RRType.NSEC3PARAM(), RRClass.IN(), 

                                "2 0 12 aabbccdd")) 

        self.assertRaises(UnknownNSEC3HashAlgorithm, NSEC3Hash, 

                          Rdata(RRType.NSEC3(), RRClass.IN(), 

                                "2 0 12 aabbccdd " + self.nsec3_common)) 

 

    def calculate_check(self, hash): 

        # A couple of normal cases from the RFC5155 example. 

        self.assertEqual("0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM", 

                         hash.calculate(Name("example"))) 

        self.assertEqual("35MTHGPGCU1QG68FAB165KLNSNK3DPVL", 

                         hash.calculate(Name("a.example"))) 

 

        # Check case-insensitiveness 

        self.assertEqual("0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM", 

                         hash.calculate(Name("EXAMPLE"))) 

 

 

    def test_calculate(self): 

        self.calculate_check(self.test_hash) 

        self.calculate_check(self.test_hash_nsec3) 

 

        # Using unusually large iterations, something larger than the 8-bit 

        #range.  (expected hash value generated by BIND 9's dnssec-signzone) 

        self.test_hash = NSEC3Hash(Rdata(RRType.NSEC3PARAM(), 

                                         RRClass.IN(), "1 0 256 AABBCCDD")) 

        self.assertEqual("COG6A52MJ96MNMV3QUCAGGCO0RHCC2Q3", 

                         self.test_hash.calculate(Name("example.org"))) 

 

        # Some boundary cases: 0-iteration and empty salt.  Borrowed from the 

        # .com zone data. 

        self.test_hash = NSEC3Hash(Rdata(RRType.NSEC3PARAM(), 

                                         RRClass.IN(),"1 0 0 -")) 

        self.assertEqual("CK0POJMG874LJREF7EFN8430QVIT8BSM", 

                         self.test_hash.calculate(Name("com"))) 

 

    def test_calculate_badparam(self): 

        self.assertRaises(TypeError, self.test_hash.calculate, "example") 

        self.assertRaises(TypeError, self.test_hash.calculate) 

        self.assertRaises(TypeError, self.test_hash.calculate, Name("."), 1) 

 

    def check_match(self, hash, rrtype, postfix): 

        # If all parameters match, it's considered to be matched. 

        self.assertTrue(hash.match(Rdata(rrtype, RRClass.IN(), 

                                         "1 0 12 aabbccdd" + postfix))) 

        # Algorithm doesn't match 

        self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(), 

                                          "2 0 12 aabbccdd" + postfix))) 

        # Iterations doesn't match 

        self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(), 

                                          "1 0 1 aabbccdd" + postfix))) 

        # Salt doesn't match 

        self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(), 

                                          "1 0 12 aabbccde" + postfix))) 

        # Salt doesn't match: the other has an empty salt 

        self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(), 

                                          "1 0 12 -" + postfix))) 

        # Flag doesn't matter 

        self.assertTrue(hash.match(Rdata(rrtype, RRClass.IN(), 

                                         "1 1 12 aabbccdd" + postfix))) 

 

    def test_match(self): 

        self.check_match(self.test_hash, RRType.NSEC3(), 

                         " " + self.nsec3_common) 

        self.check_match(self.test_hash_nsec3, RRType.NSEC3(), 

                         " " + self.nsec3_common) 

        self.check_match(self.test_hash, RRType.NSEC3PARAM(), "") 

        self.check_match(self.test_hash_nsec3, RRType.NSEC3PARAM(), "") 

 

        # bad parameter checks 

        self.assertRaises(TypeError, self.test_hash.match, 1) 

        self.assertRaises(TypeError, self.test_hash.match, 

                          Rdata(RRType.NSEC3(), RRClass.IN(), 

                                "1 0 12 aabbccdd " + self.nsec3_common), 1) 

        self.assertRaises(TypeError, self.test_hash.match, 

                          Rdata(RRType.A(), RRClass.IN(), "192.0.2.1")) 

 

exitif __name__ == '__main__': 

    unittest.main()