1*eda14cbcSMatt Macy#
2*eda14cbcSMatt Macy# Copyright 2015 ClusterHQ
3*eda14cbcSMatt Macy#
4*eda14cbcSMatt Macy# Licensed under the Apache License, Version 2.0 (the "License");
5*eda14cbcSMatt Macy# you may not use this file except in compliance with the License.
6*eda14cbcSMatt Macy# You may obtain a copy of the License at
7*eda14cbcSMatt Macy#
8*eda14cbcSMatt Macy#    http://www.apache.org/licenses/LICENSE-2.0
9*eda14cbcSMatt Macy#
10*eda14cbcSMatt Macy# Unless required by applicable law or agreed to in writing, software
11*eda14cbcSMatt Macy# distributed under the License is distributed on an "AS IS" BASIS,
12*eda14cbcSMatt Macy# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*eda14cbcSMatt Macy# See the License for the specific language governing permissions and
14*eda14cbcSMatt Macy# limitations under the License.
15*eda14cbcSMatt Macy#
16*eda14cbcSMatt Macy
17*eda14cbcSMatt Macy"""
18*eda14cbcSMatt MacyTests for _nvlist module.
19*eda14cbcSMatt MacyThe tests convert from a `dict` to C ``nvlist_t`` and back to a `dict`
20*eda14cbcSMatt Macyand verify that no information is lost and value types are correct.
21*eda14cbcSMatt MacyThe tests also check that various error conditions like unsupported
22*eda14cbcSMatt Macyvalue types or out of bounds values are detected.
23*eda14cbcSMatt Macy"""
24*eda14cbcSMatt Macyfrom __future__ import absolute_import, division, print_function
25*eda14cbcSMatt Macy
26*eda14cbcSMatt Macyimport unittest
27*eda14cbcSMatt Macy
28*eda14cbcSMatt Macyfrom .._nvlist import nvlist_in, nvlist_out, _lib
29*eda14cbcSMatt Macyfrom ..ctypes import (
30*eda14cbcSMatt Macy    uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t,
31*eda14cbcSMatt Macy    uint64_t, int64_t, boolean_t, uchar_t
32*eda14cbcSMatt Macy)
33*eda14cbcSMatt Macy
34*eda14cbcSMatt Macy
35*eda14cbcSMatt Macyclass TestNVList(unittest.TestCase):
36*eda14cbcSMatt Macy
37*eda14cbcSMatt Macy    def _dict_to_nvlist_to_dict(self, props):
38*eda14cbcSMatt Macy        res = {}
39*eda14cbcSMatt Macy        nv_in = nvlist_in(props)
40*eda14cbcSMatt Macy        with nvlist_out(res) as nv_out:
41*eda14cbcSMatt Macy            _lib.nvlist_dup(nv_in, nv_out, 0)
42*eda14cbcSMatt Macy        return res
43*eda14cbcSMatt Macy
44*eda14cbcSMatt Macy    def _assertIntDictsEqual(self, dict1, dict2):
45*eda14cbcSMatt Macy        self.assertEqual(
46*eda14cbcSMatt Macy            len(dict1), len(dict1),
47*eda14cbcSMatt Macy            b"resulting dictionary is of different size")
48*eda14cbcSMatt Macy        for key in dict1.keys():
49*eda14cbcSMatt Macy            self.assertEqual(int(dict1[key]), int(dict2[key]))
50*eda14cbcSMatt Macy
51*eda14cbcSMatt Macy    def _assertIntArrayDictsEqual(self, dict1, dict2):
52*eda14cbcSMatt Macy        self.assertEqual(
53*eda14cbcSMatt Macy            len(dict1), len(dict1),
54*eda14cbcSMatt Macy            b"resulting dictionary is of different size")
55*eda14cbcSMatt Macy        for key in dict1.keys():
56*eda14cbcSMatt Macy            val1 = dict1[key]
57*eda14cbcSMatt Macy            val2 = dict2[key]
58*eda14cbcSMatt Macy            self.assertEqual(
59*eda14cbcSMatt Macy                len(val1), len(val2), b"array values of different sizes")
60*eda14cbcSMatt Macy            for x, y in zip(val1, val2):
61*eda14cbcSMatt Macy                self.assertEqual(int(x), int(y))
62*eda14cbcSMatt Macy
63*eda14cbcSMatt Macy    def test_empty(self):
64*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict({})
65*eda14cbcSMatt Macy        self.assertEqual(len(res), 0, b"expected empty dict")
66*eda14cbcSMatt Macy
67*eda14cbcSMatt Macy    def test_invalid_key_type(self):
68*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
69*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict({1: None})
70*eda14cbcSMatt Macy
71*eda14cbcSMatt Macy    def test_invalid_val_type__tuple(self):
72*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
73*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict({b"key": (1, 2)})
74*eda14cbcSMatt Macy
75*eda14cbcSMatt Macy    def test_invalid_val_type__set(self):
76*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
77*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict({b"key": set(1, 2)})
78*eda14cbcSMatt Macy
79*eda14cbcSMatt Macy    def test_invalid_array_val_type(self):
80*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
81*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict({b"key": [(1, 2), (3, 4)]})
82*eda14cbcSMatt Macy
83*eda14cbcSMatt Macy    def test_invalid_array_of_arrays_val_type(self):
84*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
85*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict({b"key": [[1, 2], [3, 4]]})
86*eda14cbcSMatt Macy
87*eda14cbcSMatt Macy    def test_string_value(self):
88*eda14cbcSMatt Macy        props = {b"key": b"value"}
89*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
90*eda14cbcSMatt Macy        self.assertEqual(props, res)
91*eda14cbcSMatt Macy
92*eda14cbcSMatt Macy    def test_implicit_boolean_value(self):
93*eda14cbcSMatt Macy        props = {b"key": None}
94*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
95*eda14cbcSMatt Macy        self.assertEqual(props, res)
96*eda14cbcSMatt Macy
97*eda14cbcSMatt Macy    def test_boolean_values(self):
98*eda14cbcSMatt Macy        props = {b"key1": True, b"key2": False}
99*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
100*eda14cbcSMatt Macy        self.assertEqual(props, res)
101*eda14cbcSMatt Macy
102*eda14cbcSMatt Macy    def test_explicit_boolean_true_value(self):
103*eda14cbcSMatt Macy        props = {b"key": boolean_t(1)}
104*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
105*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
106*eda14cbcSMatt Macy
107*eda14cbcSMatt Macy    def test_explicit_boolean_false_value(self):
108*eda14cbcSMatt Macy        props = {b"key": boolean_t(0)}
109*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
110*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
111*eda14cbcSMatt Macy
112*eda14cbcSMatt Macy    def test_explicit_boolean_invalid_value(self):
113*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
114*eda14cbcSMatt Macy            props = {b"key": boolean_t(2)}
115*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
116*eda14cbcSMatt Macy
117*eda14cbcSMatt Macy    def test_explicit_boolean_another_invalid_value(self):
118*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
119*eda14cbcSMatt Macy            props = {b"key": boolean_t(-1)}
120*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
121*eda14cbcSMatt Macy
122*eda14cbcSMatt Macy    def test_uint64_value(self):
123*eda14cbcSMatt Macy        props = {b"key": 1}
124*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
125*eda14cbcSMatt Macy        self.assertEqual(props, res)
126*eda14cbcSMatt Macy
127*eda14cbcSMatt Macy    def test_uint64_max_value(self):
128*eda14cbcSMatt Macy        props = {b"key": 2 ** 64 - 1}
129*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
130*eda14cbcSMatt Macy        self.assertEqual(props, res)
131*eda14cbcSMatt Macy
132*eda14cbcSMatt Macy    def test_uint64_too_large_value(self):
133*eda14cbcSMatt Macy        props = {b"key": 2 ** 64}
134*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
135*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
136*eda14cbcSMatt Macy
137*eda14cbcSMatt Macy    def test_uint64_negative_value(self):
138*eda14cbcSMatt Macy        props = {b"key": -1}
139*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
140*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
141*eda14cbcSMatt Macy
142*eda14cbcSMatt Macy    def test_explicit_uint64_value(self):
143*eda14cbcSMatt Macy        props = {b"key": uint64_t(1)}
144*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
145*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
146*eda14cbcSMatt Macy
147*eda14cbcSMatt Macy    def test_explicit_uint64_max_value(self):
148*eda14cbcSMatt Macy        props = {b"key": uint64_t(2 ** 64 - 1)}
149*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
150*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
151*eda14cbcSMatt Macy
152*eda14cbcSMatt Macy    def test_explicit_uint64_too_large_value(self):
153*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
154*eda14cbcSMatt Macy            props = {b"key": uint64_t(2 ** 64)}
155*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
156*eda14cbcSMatt Macy
157*eda14cbcSMatt Macy    def test_explicit_uint64_negative_value(self):
158*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
159*eda14cbcSMatt Macy            props = {b"key": uint64_t(-1)}
160*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
161*eda14cbcSMatt Macy
162*eda14cbcSMatt Macy    def test_explicit_uint32_value(self):
163*eda14cbcSMatt Macy        props = {b"key": uint32_t(1)}
164*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
165*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
166*eda14cbcSMatt Macy
167*eda14cbcSMatt Macy    def test_explicit_uint32_max_value(self):
168*eda14cbcSMatt Macy        props = {b"key": uint32_t(2 ** 32 - 1)}
169*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
170*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
171*eda14cbcSMatt Macy
172*eda14cbcSMatt Macy    def test_explicit_uint32_too_large_value(self):
173*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
174*eda14cbcSMatt Macy            props = {b"key": uint32_t(2 ** 32)}
175*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
176*eda14cbcSMatt Macy
177*eda14cbcSMatt Macy    def test_explicit_uint32_negative_value(self):
178*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
179*eda14cbcSMatt Macy            props = {b"key": uint32_t(-1)}
180*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
181*eda14cbcSMatt Macy
182*eda14cbcSMatt Macy    def test_explicit_uint16_value(self):
183*eda14cbcSMatt Macy        props = {b"key": uint16_t(1)}
184*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
185*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
186*eda14cbcSMatt Macy
187*eda14cbcSMatt Macy    def test_explicit_uint16_max_value(self):
188*eda14cbcSMatt Macy        props = {b"key": uint16_t(2 ** 16 - 1)}
189*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
190*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
191*eda14cbcSMatt Macy
192*eda14cbcSMatt Macy    def test_explicit_uint16_too_large_value(self):
193*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
194*eda14cbcSMatt Macy            props = {b"key": uint16_t(2 ** 16)}
195*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
196*eda14cbcSMatt Macy
197*eda14cbcSMatt Macy    def test_explicit_uint16_negative_value(self):
198*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
199*eda14cbcSMatt Macy            props = {b"key": uint16_t(-1)}
200*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
201*eda14cbcSMatt Macy
202*eda14cbcSMatt Macy    def test_explicit_uint8_value(self):
203*eda14cbcSMatt Macy        props = {b"key": uint8_t(1)}
204*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
205*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
206*eda14cbcSMatt Macy
207*eda14cbcSMatt Macy    def test_explicit_uint8_max_value(self):
208*eda14cbcSMatt Macy        props = {b"key": uint8_t(2 ** 8 - 1)}
209*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
210*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
211*eda14cbcSMatt Macy
212*eda14cbcSMatt Macy    def test_explicit_uint8_too_large_value(self):
213*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
214*eda14cbcSMatt Macy            props = {b"key": uint8_t(2 ** 8)}
215*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
216*eda14cbcSMatt Macy
217*eda14cbcSMatt Macy    def test_explicit_uint8_negative_value(self):
218*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
219*eda14cbcSMatt Macy            props = {b"key": uint8_t(-1)}
220*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
221*eda14cbcSMatt Macy
222*eda14cbcSMatt Macy    def test_explicit_byte_value(self):
223*eda14cbcSMatt Macy        props = {b"key": uchar_t(1)}
224*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
225*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
226*eda14cbcSMatt Macy
227*eda14cbcSMatt Macy    def test_explicit_byte_max_value(self):
228*eda14cbcSMatt Macy        props = {b"key": uchar_t(2 ** 8 - 1)}
229*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
230*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
231*eda14cbcSMatt Macy
232*eda14cbcSMatt Macy    def test_explicit_byte_too_large_value(self):
233*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
234*eda14cbcSMatt Macy            props = {b"key": uchar_t(2 ** 8)}
235*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
236*eda14cbcSMatt Macy
237*eda14cbcSMatt Macy    def test_explicit_byte_negative_value(self):
238*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
239*eda14cbcSMatt Macy            props = {b"key": uchar_t(-1)}
240*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
241*eda14cbcSMatt Macy
242*eda14cbcSMatt Macy    def test_explicit_int64_value(self):
243*eda14cbcSMatt Macy        props = {b"key": int64_t(1)}
244*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
245*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
246*eda14cbcSMatt Macy
247*eda14cbcSMatt Macy    def test_explicit_int64_max_value(self):
248*eda14cbcSMatt Macy        props = {b"key": int64_t(2 ** 63 - 1)}
249*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
250*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
251*eda14cbcSMatt Macy
252*eda14cbcSMatt Macy    def test_explicit_int64_min_value(self):
253*eda14cbcSMatt Macy        props = {b"key": int64_t(-(2 ** 63))}
254*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
255*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
256*eda14cbcSMatt Macy
257*eda14cbcSMatt Macy    def test_explicit_int64_too_large_value(self):
258*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
259*eda14cbcSMatt Macy            props = {b"key": int64_t(2 ** 63)}
260*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
261*eda14cbcSMatt Macy
262*eda14cbcSMatt Macy    def test_explicit_int64_too_small_value(self):
263*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
264*eda14cbcSMatt Macy            props = {b"key": int64_t(-(2 ** 63) - 1)}
265*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
266*eda14cbcSMatt Macy
267*eda14cbcSMatt Macy    def test_explicit_int32_value(self):
268*eda14cbcSMatt Macy        props = {b"key": int32_t(1)}
269*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
270*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
271*eda14cbcSMatt Macy
272*eda14cbcSMatt Macy    def test_explicit_int32_max_value(self):
273*eda14cbcSMatt Macy        props = {b"key": int32_t(2 ** 31 - 1)}
274*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
275*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
276*eda14cbcSMatt Macy
277*eda14cbcSMatt Macy    def test_explicit_int32_min_value(self):
278*eda14cbcSMatt Macy        props = {b"key": int32_t(-(2 ** 31))}
279*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
280*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
281*eda14cbcSMatt Macy
282*eda14cbcSMatt Macy    def test_explicit_int32_too_large_value(self):
283*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
284*eda14cbcSMatt Macy            props = {b"key": int32_t(2 ** 31)}
285*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
286*eda14cbcSMatt Macy
287*eda14cbcSMatt Macy    def test_explicit_int32_too_small_value(self):
288*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
289*eda14cbcSMatt Macy            props = {b"key": int32_t(-(2 ** 31) - 1)}
290*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
291*eda14cbcSMatt Macy
292*eda14cbcSMatt Macy    def test_explicit_int16_value(self):
293*eda14cbcSMatt Macy        props = {b"key": int16_t(1)}
294*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
295*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
296*eda14cbcSMatt Macy
297*eda14cbcSMatt Macy    def test_explicit_int16_max_value(self):
298*eda14cbcSMatt Macy        props = {b"key": int16_t(2 ** 15 - 1)}
299*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
300*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
301*eda14cbcSMatt Macy
302*eda14cbcSMatt Macy    def test_explicit_int16_min_value(self):
303*eda14cbcSMatt Macy        props = {b"key": int16_t(-(2 ** 15))}
304*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
305*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
306*eda14cbcSMatt Macy
307*eda14cbcSMatt Macy    def test_explicit_int16_too_large_value(self):
308*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
309*eda14cbcSMatt Macy            props = {b"key": int16_t(2 ** 15)}
310*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
311*eda14cbcSMatt Macy
312*eda14cbcSMatt Macy    def test_explicit_int16_too_small_value(self):
313*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
314*eda14cbcSMatt Macy            props = {b"key": int16_t(-(2 ** 15) - 1)}
315*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
316*eda14cbcSMatt Macy
317*eda14cbcSMatt Macy    def test_explicit_int8_value(self):
318*eda14cbcSMatt Macy        props = {b"key": int8_t(1)}
319*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
320*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
321*eda14cbcSMatt Macy
322*eda14cbcSMatt Macy    def test_explicit_int8_max_value(self):
323*eda14cbcSMatt Macy        props = {b"key": int8_t(2 ** 7 - 1)}
324*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
325*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
326*eda14cbcSMatt Macy
327*eda14cbcSMatt Macy    def test_explicit_int8_min_value(self):
328*eda14cbcSMatt Macy        props = {b"key": int8_t(-(2 ** 7))}
329*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
330*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
331*eda14cbcSMatt Macy
332*eda14cbcSMatt Macy    def test_explicit_int8_too_large_value(self):
333*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
334*eda14cbcSMatt Macy            props = {b"key": int8_t(2 ** 7)}
335*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
336*eda14cbcSMatt Macy
337*eda14cbcSMatt Macy    def test_explicit_int8_too_small_value(self):
338*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
339*eda14cbcSMatt Macy            props = {b"key": int8_t(-(2 ** 7) - 1)}
340*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
341*eda14cbcSMatt Macy
342*eda14cbcSMatt Macy    def test_nested_dict(self):
343*eda14cbcSMatt Macy        props = {b"key": {}}
344*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
345*eda14cbcSMatt Macy        self.assertEqual(props, res)
346*eda14cbcSMatt Macy
347*eda14cbcSMatt Macy    def test_nested_nested_dict(self):
348*eda14cbcSMatt Macy        props = {b"key": {b"key": {}}}
349*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
350*eda14cbcSMatt Macy        self.assertEqual(props, res)
351*eda14cbcSMatt Macy
352*eda14cbcSMatt Macy    def test_mismatching_values_array(self):
353*eda14cbcSMatt Macy        props = {b"key": [1, b"string"]}
354*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
355*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
356*eda14cbcSMatt Macy
357*eda14cbcSMatt Macy    def test_mismatching_values_array2(self):
358*eda14cbcSMatt Macy        props = {b"key": [True, 10]}
359*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
360*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
361*eda14cbcSMatt Macy
362*eda14cbcSMatt Macy    def test_mismatching_values_array3(self):
363*eda14cbcSMatt Macy        props = {b"key": [1, False]}
364*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
365*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
366*eda14cbcSMatt Macy
367*eda14cbcSMatt Macy    def test_string_array(self):
368*eda14cbcSMatt Macy        props = {b"key": [b"value", b"value2"]}
369*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
370*eda14cbcSMatt Macy        self.assertEqual(props, res)
371*eda14cbcSMatt Macy
372*eda14cbcSMatt Macy    def test_boolean_array(self):
373*eda14cbcSMatt Macy        props = {b"key": [True, False]}
374*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
375*eda14cbcSMatt Macy        self.assertEqual(props, res)
376*eda14cbcSMatt Macy
377*eda14cbcSMatt Macy    def test_explicit_boolean_array(self):
378*eda14cbcSMatt Macy        props = {b"key": [boolean_t(False), boolean_t(True)]}
379*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
380*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
381*eda14cbcSMatt Macy
382*eda14cbcSMatt Macy    def test_uint64_array(self):
383*eda14cbcSMatt Macy        props = {b"key": [0, 1, 2 ** 64 - 1]}
384*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
385*eda14cbcSMatt Macy        self.assertEqual(props, res)
386*eda14cbcSMatt Macy
387*eda14cbcSMatt Macy    def test_uint64_array_too_large_value(self):
388*eda14cbcSMatt Macy        props = {b"key": [0, 2 ** 64]}
389*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
390*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
391*eda14cbcSMatt Macy
392*eda14cbcSMatt Macy    def test_uint64_array_negative_value(self):
393*eda14cbcSMatt Macy        props = {b"key": [0, -1]}
394*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
395*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
396*eda14cbcSMatt Macy
397*eda14cbcSMatt Macy    def test_mixed_explict_int_array(self):
398*eda14cbcSMatt Macy        with self.assertRaises(TypeError):
399*eda14cbcSMatt Macy            props = {b"key": [uint64_t(0), uint32_t(0)]}
400*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
401*eda14cbcSMatt Macy
402*eda14cbcSMatt Macy    def test_explict_uint64_array(self):
403*eda14cbcSMatt Macy        props = {b"key": [uint64_t(0), uint64_t(1), uint64_t(2 ** 64 - 1)]}
404*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
405*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
406*eda14cbcSMatt Macy
407*eda14cbcSMatt Macy    def test_explict_uint64_array_too_large_value(self):
408*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
409*eda14cbcSMatt Macy            props = {b"key": [uint64_t(0), uint64_t(2 ** 64)]}
410*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
411*eda14cbcSMatt Macy
412*eda14cbcSMatt Macy    def test_explict_uint64_array_negative_value(self):
413*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
414*eda14cbcSMatt Macy            props = {b"key": [uint64_t(0), uint64_t(-1)]}
415*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
416*eda14cbcSMatt Macy
417*eda14cbcSMatt Macy    def test_explict_uint32_array(self):
418*eda14cbcSMatt Macy        props = {b"key": [uint32_t(0), uint32_t(1), uint32_t(2 ** 32 - 1)]}
419*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
420*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
421*eda14cbcSMatt Macy
422*eda14cbcSMatt Macy    def test_explict_uint32_array_too_large_value(self):
423*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
424*eda14cbcSMatt Macy            props = {b"key": [uint32_t(0), uint32_t(2 ** 32)]}
425*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
426*eda14cbcSMatt Macy
427*eda14cbcSMatt Macy    def test_explict_uint32_array_negative_value(self):
428*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
429*eda14cbcSMatt Macy            props = {b"key": [uint32_t(0), uint32_t(-1)]}
430*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
431*eda14cbcSMatt Macy
432*eda14cbcSMatt Macy    def test_explict_uint16_array(self):
433*eda14cbcSMatt Macy        props = {b"key": [uint16_t(0), uint16_t(1), uint16_t(2 ** 16 - 1)]}
434*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
435*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
436*eda14cbcSMatt Macy
437*eda14cbcSMatt Macy    def test_explict_uint16_array_too_large_value(self):
438*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
439*eda14cbcSMatt Macy            props = {b"key": [uint16_t(0), uint16_t(2 ** 16)]}
440*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
441*eda14cbcSMatt Macy
442*eda14cbcSMatt Macy    def test_explict_uint16_array_negative_value(self):
443*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
444*eda14cbcSMatt Macy            props = {b"key": [uint16_t(0), uint16_t(-1)]}
445*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
446*eda14cbcSMatt Macy
447*eda14cbcSMatt Macy    def test_explict_uint8_array(self):
448*eda14cbcSMatt Macy        props = {b"key": [uint8_t(0), uint8_t(1), uint8_t(2 ** 8 - 1)]}
449*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
450*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
451*eda14cbcSMatt Macy
452*eda14cbcSMatt Macy    def test_explict_uint8_array_too_large_value(self):
453*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
454*eda14cbcSMatt Macy            props = {b"key": [uint8_t(0), uint8_t(2 ** 8)]}
455*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
456*eda14cbcSMatt Macy
457*eda14cbcSMatt Macy    def test_explict_uint8_array_negative_value(self):
458*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
459*eda14cbcSMatt Macy            props = {b"key": [uint8_t(0), uint8_t(-1)]}
460*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
461*eda14cbcSMatt Macy
462*eda14cbcSMatt Macy    def test_explict_byte_array(self):
463*eda14cbcSMatt Macy        props = {b"key": [uchar_t(0), uchar_t(1), uchar_t(2 ** 8 - 1)]}
464*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
465*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
466*eda14cbcSMatt Macy
467*eda14cbcSMatt Macy    def test_explict_byte_array_too_large_value(self):
468*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
469*eda14cbcSMatt Macy            props = {b"key": [uchar_t(0), uchar_t(2 ** 8)]}
470*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
471*eda14cbcSMatt Macy
472*eda14cbcSMatt Macy    def test_explict_byte_array_negative_value(self):
473*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
474*eda14cbcSMatt Macy            props = {b"key": [uchar_t(0), uchar_t(-1)]}
475*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
476*eda14cbcSMatt Macy
477*eda14cbcSMatt Macy    def test_explict_int64_array(self):
478*eda14cbcSMatt Macy        props = {b"key": [
479*eda14cbcSMatt Macy            int64_t(0), int64_t(1), int64_t(2 ** 63 - 1), int64_t(-(2 ** 63))]}
480*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
481*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
482*eda14cbcSMatt Macy
483*eda14cbcSMatt Macy    def test_explict_int64_array_too_large_value(self):
484*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
485*eda14cbcSMatt Macy            props = {b"key": [int64_t(0), int64_t(2 ** 63)]}
486*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
487*eda14cbcSMatt Macy
488*eda14cbcSMatt Macy    def test_explict_int64_array_too_small_value(self):
489*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
490*eda14cbcSMatt Macy            props = {b"key": [int64_t(0), int64_t(-(2 ** 63) - 1)]}
491*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
492*eda14cbcSMatt Macy
493*eda14cbcSMatt Macy    def test_explict_int32_array(self):
494*eda14cbcSMatt Macy        props = {b"key": [
495*eda14cbcSMatt Macy            int32_t(0), int32_t(1), int32_t(2 ** 31 - 1), int32_t(-(2 ** 31))]}
496*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
497*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
498*eda14cbcSMatt Macy
499*eda14cbcSMatt Macy    def test_explict_int32_array_too_large_value(self):
500*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
501*eda14cbcSMatt Macy            props = {b"key": [int32_t(0), int32_t(2 ** 31)]}
502*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
503*eda14cbcSMatt Macy
504*eda14cbcSMatt Macy    def test_explict_int32_array_too_small_value(self):
505*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
506*eda14cbcSMatt Macy            props = {b"key": [int32_t(0), int32_t(-(2 ** 31) - 1)]}
507*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
508*eda14cbcSMatt Macy
509*eda14cbcSMatt Macy    def test_explict_int16_array(self):
510*eda14cbcSMatt Macy        props = {b"key": [
511*eda14cbcSMatt Macy            int16_t(0), int16_t(1), int16_t(2 ** 15 - 1), int16_t(-(2 ** 15))]}
512*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
513*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
514*eda14cbcSMatt Macy
515*eda14cbcSMatt Macy    def test_explict_int16_array_too_large_value(self):
516*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
517*eda14cbcSMatt Macy            props = {b"key": [int16_t(0), int16_t(2 ** 15)]}
518*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
519*eda14cbcSMatt Macy
520*eda14cbcSMatt Macy    def test_explict_int16_array_too_small_value(self):
521*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
522*eda14cbcSMatt Macy            props = {b"key": [int16_t(0), int16_t(-(2 ** 15) - 1)]}
523*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
524*eda14cbcSMatt Macy
525*eda14cbcSMatt Macy    def test_explict_int8_array(self):
526*eda14cbcSMatt Macy        props = {b"key": [
527*eda14cbcSMatt Macy            int8_t(0), int8_t(1), int8_t(2 ** 7 - 1), int8_t(-(2 ** 7))]}
528*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
529*eda14cbcSMatt Macy        self._assertIntArrayDictsEqual(props, res)
530*eda14cbcSMatt Macy
531*eda14cbcSMatt Macy    def test_explict_int8_array_too_large_value(self):
532*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
533*eda14cbcSMatt Macy            props = {b"key": [int8_t(0), int8_t(2 ** 7)]}
534*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
535*eda14cbcSMatt Macy
536*eda14cbcSMatt Macy    def test_explict_int8_array_too_small_value(self):
537*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
538*eda14cbcSMatt Macy            props = {b"key": [int8_t(0), int8_t(-(2 ** 7) - 1)]}
539*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
540*eda14cbcSMatt Macy
541*eda14cbcSMatt Macy    def test_dict_array(self):
542*eda14cbcSMatt Macy        props = {b"key": [{b"key": 1}, {b"key": None}, {b"key": {}}]}
543*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
544*eda14cbcSMatt Macy        self.assertEqual(props, res)
545*eda14cbcSMatt Macy
546*eda14cbcSMatt Macy    def test_implicit_uint32_value(self):
547*eda14cbcSMatt Macy        props = {b"rewind-request": 1}
548*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
549*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
550*eda14cbcSMatt Macy
551*eda14cbcSMatt Macy    def test_implicit_uint32_max_value(self):
552*eda14cbcSMatt Macy        props = {b"rewind-request": 2 ** 32 - 1}
553*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
554*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
555*eda14cbcSMatt Macy
556*eda14cbcSMatt Macy    def test_implicit_uint32_too_large_value(self):
557*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
558*eda14cbcSMatt Macy            props = {b"rewind-request": 2 ** 32}
559*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
560*eda14cbcSMatt Macy
561*eda14cbcSMatt Macy    def test_implicit_uint32_negative_value(self):
562*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
563*eda14cbcSMatt Macy            props = {b"rewind-request": -1}
564*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
565*eda14cbcSMatt Macy
566*eda14cbcSMatt Macy    def test_implicit_int32_value(self):
567*eda14cbcSMatt Macy        props = {b"pool_context": 1}
568*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
569*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
570*eda14cbcSMatt Macy
571*eda14cbcSMatt Macy    def test_implicit_int32_max_value(self):
572*eda14cbcSMatt Macy        props = {b"pool_context": 2 ** 31 - 1}
573*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
574*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
575*eda14cbcSMatt Macy
576*eda14cbcSMatt Macy    def test_implicit_int32_min_value(self):
577*eda14cbcSMatt Macy        props = {b"pool_context": -(2 ** 31)}
578*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
579*eda14cbcSMatt Macy        self._assertIntDictsEqual(props, res)
580*eda14cbcSMatt Macy
581*eda14cbcSMatt Macy    def test_implicit_int32_too_large_value(self):
582*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
583*eda14cbcSMatt Macy            props = {b"pool_context": 2 ** 31}
584*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
585*eda14cbcSMatt Macy
586*eda14cbcSMatt Macy    def test_implicit_int32_too_small_value(self):
587*eda14cbcSMatt Macy        with self.assertRaises(OverflowError):
588*eda14cbcSMatt Macy            props = {b"pool_context": -(2 ** 31) - 1}
589*eda14cbcSMatt Macy            self._dict_to_nvlist_to_dict(props)
590*eda14cbcSMatt Macy
591*eda14cbcSMatt Macy    def test_complex_dict(self):
592*eda14cbcSMatt Macy        props = {
593*eda14cbcSMatt Macy            b"key1": b"str",
594*eda14cbcSMatt Macy            b"key2": 10,
595*eda14cbcSMatt Macy            b"key3": {
596*eda14cbcSMatt Macy                b"skey1": True,
597*eda14cbcSMatt Macy                b"skey2": None,
598*eda14cbcSMatt Macy                b"skey3": [
599*eda14cbcSMatt Macy                    True,
600*eda14cbcSMatt Macy                    False,
601*eda14cbcSMatt Macy                    True
602*eda14cbcSMatt Macy                ]
603*eda14cbcSMatt Macy            },
604*eda14cbcSMatt Macy            b"key4": [
605*eda14cbcSMatt Macy                b"ab",
606*eda14cbcSMatt Macy                b"bc"
607*eda14cbcSMatt Macy            ],
608*eda14cbcSMatt Macy            b"key5": [
609*eda14cbcSMatt Macy                2 ** 64 - 1,
610*eda14cbcSMatt Macy                1,
611*eda14cbcSMatt Macy                2,
612*eda14cbcSMatt Macy                3
613*eda14cbcSMatt Macy            ],
614*eda14cbcSMatt Macy            b"key6": [
615*eda14cbcSMatt Macy                {
616*eda14cbcSMatt Macy                    b"skey71": b"a",
617*eda14cbcSMatt Macy                    b"skey72": b"b",
618*eda14cbcSMatt Macy                },
619*eda14cbcSMatt Macy                {
620*eda14cbcSMatt Macy                    b"skey71": b"c",
621*eda14cbcSMatt Macy                    b"skey72": b"d",
622*eda14cbcSMatt Macy                },
623*eda14cbcSMatt Macy                {
624*eda14cbcSMatt Macy                    b"skey71": b"e",
625*eda14cbcSMatt Macy                    b"skey72": b"f",
626*eda14cbcSMatt Macy                }
627*eda14cbcSMatt Macy
628*eda14cbcSMatt Macy            ],
629*eda14cbcSMatt Macy            b"type": 2 ** 32 - 1,
630*eda14cbcSMatt Macy            b"pool_context": -(2 ** 31)
631*eda14cbcSMatt Macy        }
632*eda14cbcSMatt Macy        res = self._dict_to_nvlist_to_dict(props)
633*eda14cbcSMatt Macy        self.assertEqual(props, res)
634*eda14cbcSMatt Macy
635*eda14cbcSMatt Macy
636*eda14cbcSMatt Macy# vim: softtabstop=4 tabstop=4 expandtab shiftwidth=4
637