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

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

# Copyright (C) 2010  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. 

 

# 

# Tests for the rcode part of the pydnspp module 

# 

 

import unittest 

from pydnspp import * 

 

class RcodeTest(unittest.TestCase): 

    def test_init(self): 

        self.assertRaises(TypeError, Rcode, "wrong") 

        self.assertEqual(0xfff, Rcode(0xfff).get_code()) # possible max code 

 

        # should fail on attempt of construction with an out of range code 

        self.assertRaises(OverflowError, Rcode, 0x1000) 

        self.assertRaises(OverflowError, Rcode, 0xffff) 

 

        # constructor with a pair of code portions 

        self.assertEqual(3, Rcode(Rcode.NXDOMAIN_CODE, 0).get_code()) 

        self.assertEqual(Rcode.BADVERS_CODE, Rcode(0, 1).get_code()) 

        self.assertEqual(0xfff, Rcode(0xf, 0xff).get_code()) 

        self.assertRaises(OverflowError, Rcode, 0x10, 0xff) 

 

        # Range check.  We need to do this at the binding level, so we need 

        # explicit tests for it. 

        self.assertEqual(Rcode(0).get_code(), 0) 

        self.assertEqual(Rcode(4095).get_code(), 4095) 

        self.assertEqual(Rcode(0, 0).get_code(), 0) 

        self.assertEqual(Rcode(0, 0).get_extended_code(), 0) 

        self.assertEqual(Rcode(15, 255).get_code(), 4095) 

        self.assertRaises(ValueError, Rcode, 65536) 

        self.assertRaises(ValueError, Rcode, 0x10, 0x100) 

        self.assertRaises(ValueError, Rcode, 0x100, 0x10) 

 

    def test_constants(self): 

        self.assertEqual(Rcode.NOERROR_CODE, Rcode(0).get_code()) 

        self.assertEqual(Rcode.FORMERR_CODE, Rcode(1).get_code()) 

        self.assertEqual(Rcode.NOTIMP_CODE, Rcode(4).get_code()) 

        self.assertEqual(Rcode.REFUSED_CODE, Rcode(5).get_code()) 

        self.assertEqual(Rcode.RESERVED15_CODE, Rcode(15).get_code()) 

        self.assertEqual(Rcode.BADVERS_CODE, Rcode(16).get_code()) 

 

        self.assertEqual(Rcode.NOERROR_CODE, Rcode.NOERROR().get_code()) 

        self.assertEqual(Rcode.FORMERR_CODE, Rcode.FORMERR().get_code()) 

        self.assertEqual(Rcode.NOTIMP_CODE, Rcode.NOTIMP().get_code()) 

        self.assertEqual(Rcode.REFUSED_CODE, Rcode.REFUSED().get_code()) 

        self.assertEqual(Rcode.RESERVED15_CODE, Rcode.RESERVED15().get_code()) 

        self.assertEqual(Rcode.BADVERS_CODE, Rcode.BADVERS().get_code()) 

 

    def test_get_code(self): 

        self.assertEqual(0, Rcode.NOERROR().get_code()) 

        self.assertEqual(1, Rcode.FORMERR().get_code()) 

        self.assertEqual(2, Rcode.SERVFAIL().get_code()) 

        self.assertEqual(3, Rcode.NXDOMAIN().get_code()) 

        self.assertEqual(4, Rcode.NOTIMP().get_code()) 

        self.assertEqual(5, Rcode.REFUSED().get_code()) 

        self.assertEqual(6, Rcode.YXDOMAIN().get_code()) 

        self.assertEqual(7, Rcode.YXRRSET().get_code()) 

        self.assertEqual(8, Rcode.NXRRSET().get_code()) 

        self.assertEqual(9, Rcode.NOTAUTH().get_code()) 

        self.assertEqual(10, Rcode.NOTZONE().get_code()) 

        self.assertEqual(11, Rcode.RESERVED11().get_code()) 

        self.assertEqual(12, Rcode.RESERVED12().get_code()) 

        self.assertEqual(13, Rcode.RESERVED13().get_code()) 

        self.assertEqual(14, Rcode.RESERVED14().get_code()) 

        self.assertEqual(15, Rcode.RESERVED15().get_code()) 

        self.assertEqual(16, Rcode.BADVERS().get_code()) 

 

    def test_get_extended_code(self): 

        self.assertEqual(0, Rcode.NOERROR().get_extended_code()) 

        self.assertEqual(0, Rcode.YXRRSET().get_extended_code()) 

        self.assertEqual(1, Rcode.BADVERS().get_extended_code()) 

        self.assertEqual(0xab, Rcode(0xabf).get_extended_code()) 

        self.assertEqual(0xff, Rcode(0xfff).get_extended_code()) 

 

    def test_to_text(self): 

        self.assertEqual("NOERROR", Rcode(0).to_text()) 

        self.assertEqual("NOERROR", str(Rcode(0))) 

        self.assertEqual("FORMERR", Rcode(1).to_text()) 

        self.assertEqual("SERVFAIL", Rcode(2).to_text()) 

        self.assertEqual("NXDOMAIN", Rcode(3).to_text()) 

        self.assertEqual("NOTIMP", Rcode(4).to_text()) 

        self.assertEqual("REFUSED", Rcode(5).to_text()) 

        self.assertEqual("YXDOMAIN", Rcode(6).to_text()) 

        self.assertEqual("YXRRSET", Rcode(7).to_text()) 

        self.assertEqual("NXRRSET", Rcode(8).to_text()) 

        self.assertEqual("NOTAUTH", Rcode(9).to_text()) 

        self.assertEqual("NOTZONE", Rcode(10).to_text()) 

        self.assertEqual("RESERVED11", Rcode(11).to_text()) 

        self.assertEqual("RESERVED12", Rcode(12).to_text()) 

        self.assertEqual("RESERVED13", Rcode(13).to_text()) 

        self.assertEqual("RESERVED14", Rcode(14).to_text()) 

        self.assertEqual("RESERVED15", Rcode(15).to_text()) 

        self.assertEqual("BADVERS", Rcode(16).to_text()) 

 

        self.assertEqual("17", Rcode(Rcode.BADVERS().get_code() + 1).to_text()) 

        self.assertEqual("4095", Rcode(0xfff).to_text()) 

 

    def test_richcmp(self): 

        r1 = Rcode.NOERROR() 

        r2 = Rcode.FORMERR() 

        r3 = Rcode.FORMERR() 

        self.assertTrue(r2 == r3) 

        self.assertTrue(r1 != r2) 

        self.assertFalse(r1 == r2) 

        self.assertFalse(r1 != 1) 

        # can't use assertRaises here... 

        try: 

            r1 < r2 

            self.fail("operation that should have raised an error unexpectedly succeeded") 

        except Exception as err: 

            self.assertEqual(TypeError, type(err)) 

        try: 

            r1 <= r2 

            self.fail("operation that should have raised an error unexpectedly succeeded") 

        except Exception as err: 

            self.assertEqual(TypeError, type(err)) 

        try: 

            r1 > r2 

            self.fail("operation that should have raised an error unexpectedly succeeded") 

        except Exception as err: 

            self.assertEqual(TypeError, type(err)) 

        try: 

            r1 >= r2 

            self.fail("operation that should have raised an error unexpectedly succeeded") 

        except Exception as err: 

            self.assertEqual(TypeError, type(err)) 

 

exitif __name__ == '__main__': 

    unittest.main()