summaryrefslogtreecommitdiff
path: root/tests/test_wiredata.py
blob: 9274259af7eccbdba20733105c1991378b561e4f (plain)
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
# Copyright (C) 2016
# Author: Martin Basti <martin.basti@gmail.com>
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose with or without fee is hereby granted,
# provided that the above copyright notice and this permission notice
# appear in all copies.

import unittest

from dns.exception import FormError
from dns.wiredata import WireData, maybe_wrap


class WireDataSlicingTestCase(unittest.TestCase):

    def testSliceAll(self):
        """Get all data"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[:], WireData(b'0123456789'))

    def testSliceAllExplicitlyDefined(self):
        """Get all data"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[0:10], WireData(b'0123456789'))

    def testSliceLowerHalf(self):
        """Get lower half of data"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[:5], WireData(b'01234'))

    def testSliceLowerHalfWithNegativeIndex(self):
        """Get lower half of data"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[:-5], WireData(b'01234'))

    def testSliceUpperHalf(self):
        """Get upper half of data"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[5:], WireData(b'56789'))

    def testSliceMiddle(self):
        """Get data from middle"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[3:6], WireData(b'345'))

    def testSliceMiddleWithNegativeIndex(self):
        """Get data from middle"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[-6:-3], WireData(b'456'))

    def testSliceMiddleWithMixedIndex(self):
        """Get data from middle"""
        inst = WireData(b'0123456789')
        self.assertEqual(inst[-8:3], WireData(b'2'))
        self.assertEqual(inst[5:-3], WireData(b'56'))

    def testGetOne(self):
        """Get data one by one item"""
        data = b'0123456789'
        inst = WireData(data)
        for i, byte in enumerate(data):
            self.assertEqual(inst[i], byte)
        for i in range(-1, len(data) * -1, -1):
            self.assertEqual(inst[i], data[i])

    def testEmptySlice(self):
        """Test empty slice"""
        data = b'0123456789'
        inst = WireData(data)
        for i, byte in enumerate(data):
            self.assertEqual(inst[i:i], b'')
        for i in range(-1, len(data) * -1, -1):
            self.assertEqual(inst[i:i], b'')
        self.assertEqual(inst[-3:-6], b'')

    def testSliceStartOutOfLowerBorder(self):
        """Get data from out of lower border"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[-11:]  # pylint: disable=pointless-statement

    def testSliceStopOutOfLowerBorder(self):
        """Get data from out of lower border"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[:-11]  # pylint: disable=pointless-statement

    def testSliceBothOutOfLowerBorder(self):
        """Get data from out of lower border"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[-12:-11]  # pylint: disable=pointless-statement

    def testSliceStartOutOfUpperBorder(self):
        """Get data from out of upper border"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[11:]  # pylint: disable=pointless-statement

    def testSliceStopOutOfUpperBorder(self):
        """Get data from out of upper border"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[:11]  # pylint: disable=pointless-statement

    def testSliceBothOutOfUpperBorder(self):
        """Get data from out of lower border"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[10:20]  # pylint: disable=pointless-statement

    def testGetOneOutOfLowerBorder(self):
        """Get item outside of range"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[-11]  # pylint: disable=pointless-statement

    def testGetOneOutOfUpperBorder(self):
        """Get item outside of range"""
        inst = WireData(b'0123456789')
        with self.assertRaises(FormError):
            inst[10]  # pylint: disable=pointless-statement

    def testIteration(self):
        bval = b'0123'
        inst = WireData(bval)
        l = list(inst)
        self.assertEqual(l, [x for x in bval])

    def testBadWrap(self):
        def bad():
            w = maybe_wrap(123)
        self.assertRaises(ValueError, bad)