summaryrefslogtreecommitdiffstats
path: root/ipatests/test_ipapython/test_ipautil.py
blob: 8c0b9c43469bd0653b2f37e2f0bcd84daac7a430 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# encoding: utf-8

# Authors:
#   Jan Cholasta <jcholast@redhat.com>
#
# Copyright (C) 2011  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Test the `ipapython/ipautil.py` module.
"""

import nose
import pytest
import six

from ipapython import ipautil

pytestmark = pytest.mark.tier0


def make_ipaddress_checker(addr, words=None, prefixlen=None):
    def check_ipaddress():
        try:
            ip = ipautil.CheckedIPAddress(addr, match_local=False)
            assert ip.words == words and ip.prefixlen == prefixlen
        except Exception:
            assert words is None and prefixlen is None
    check_ipaddress.description = "Test IP address parsing and verification (%s)" % addr
    return check_ipaddress


def test_ip_address():
    addrs = [
        ('10.11.12.13',     (10, 11, 12, 13),   8),
        ('10.11.12.13/14',  (10, 11, 12, 13),   14),
        ('10.11.12.13%zoneid',),
        ('10.11.12.13%zoneid/14',),
        ('10.11.12.1337',),
        ('10.11.12.13/33',),
        ('127.0.0.1',),
        ('241.1.2.3',),
        ('169.254.1.2',),
        ('10.11.12.0/24',),
        ('224.5.6.7',),
        ('10.11.12.255/24',),

        ('2001::1',         (0x2001, 0, 0, 0, 0, 0, 0, 1), 64),
        ('2001::1/72',      (0x2001, 0, 0, 0, 0, 0, 0, 1), 72),
        ('2001::1%zoneid',  (0x2001, 0, 0, 0, 0, 0, 0, 1), 64),
        ('2001::1%zoneid/72',),
        ('2001::1beef',),
        ('2001::1/129',),
        ('::1',),
        ('6789::1',),
        ('fe89::1',),
        ('2001::/64',),
        ('ff01::1',),

        ('junk',)
    ]

    for addr in addrs:
        yield make_ipaddress_checker(*addr)


class TestCIDict(object):
    def setup(self):
        self.cidict = ipautil.CIDict()
        self.cidict["Key1"] = "val1"
        self.cidict["key2"] = "val2"
        self.cidict["KEY3"] = "VAL3"

    def test_init(self):
        cidict = ipautil.CIDict()
        assert dict(cidict.items()) == {}
        cidict = ipautil.CIDict([('a', 2), ('b', 3), ('C', 4)])
        assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
        cidict = ipautil.CIDict([('a', 2), ('b', None)], b=3, C=4)
        assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
        cidict = ipautil.CIDict({'a': 2, 'b': None}, b=3, C=4)
        assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
        cidict = ipautil.CIDict(a=2, b=3, C=4)
        assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}

    def test_len(self):
        nose.tools.assert_equal(3, len(self.cidict))

    def test_getitem(self):
        nose.tools.assert_equal("val1", self.cidict["Key1"])
        nose.tools.assert_equal("val1", self.cidict["key1"])
        nose.tools.assert_equal("val2", self.cidict["KEY2"])
        nose.tools.assert_equal("VAL3", self.cidict["key3"])
        nose.tools.assert_equal("VAL3", self.cidict["KEY3"])
        with nose.tools.assert_raises(KeyError):
            self.cidict["key4"]

    def test_get(self):
        nose.tools.assert_equal("val1", self.cidict.get("Key1"))
        nose.tools.assert_equal("val1", self.cidict.get("key1"))
        nose.tools.assert_equal("val2", self.cidict.get("KEY2"))
        nose.tools.assert_equal("VAL3", self.cidict.get("key3"))
        nose.tools.assert_equal("VAL3", self.cidict.get("KEY3"))
        nose.tools.assert_equal("default", self.cidict.get("key4", "default"))

    def test_setitem(self):
        self.cidict["key4"] = "val4"
        nose.tools.assert_equal("val4", self.cidict["key4"])
        self.cidict["KEY4"] = "newval4"
        nose.tools.assert_equal("newval4", self.cidict["key4"])

    def test_del(self):
        assert "Key1" in self.cidict
        del(self.cidict["Key1"])
        assert "Key1" not in self.cidict

        assert "key2" in self.cidict
        del(self.cidict["KEY2"])
        assert "key2" not in self.cidict

    def test_clear(self):
        nose.tools.assert_equal(3, len(self.cidict))
        self.cidict.clear()
        nose.tools.assert_equal(0, len(self.cidict))
        assert self.cidict == {}
        assert list(self.cidict) == []
        assert list(self.cidict.values()) == []
        assert list(self.cidict.items()) == []
        if six.PY2:
            assert self.cidict.keys() == []
            assert self.cidict.values() == []
            assert self.cidict.items() == []
        assert self.cidict._keys == {}

    def test_copy(self):
        copy = self.cidict.copy()
        assert copy == self.cidict
        nose.tools.assert_equal(3, len(copy))
        assert "Key1" in copy
        assert "key1" in copy
        nose.tools.assert_equal("val1", copy["Key1"])

    @pytest.mark.skipif(not six.PY2, reason="Python 2 only")
    def test_haskey(self):
        assert self.cidict.has_key("KEY1")
        assert self.cidict.has_key("key2")
        assert self.cidict.has_key("key3")

        assert not self.cidict.has_key("Key4")

    def test_contains(self):
        assert "KEY1" in self.cidict
        assert "key2" in self.cidict
        assert "key3" in self.cidict

        assert "Key4" not in self.cidict

    def test_items(self):
        items = list(self.cidict.items())
        nose.tools.assert_equal(3, len(items))
        items_set = set(items)
        assert ("Key1", "val1") in items_set
        assert ("key2", "val2") in items_set
        assert ("KEY3", "VAL3") in items_set

        assert list(self.cidict.items()) == list(self.cidict.iteritems()) == list(zip(
            self.cidict.keys(), self.cidict.values()))

    def test_iter(self):
        items = []
        assert list(self.cidict) == list(self.cidict.keys())
        assert sorted(self.cidict) == sorted(['Key1', 'key2', 'KEY3'])

    def test_iteritems(self):
        items = []
        for (k,v) in self.cidict.iteritems():
            items.append((k,v))
        nose.tools.assert_equal(3, len(items))
        items_set = set(items)
        assert ("Key1", "val1") in items_set
        assert ("key2", "val2") in items_set
        assert ("KEY3", "VAL3") in items_set

    def test_iterkeys(self):
        keys = []
        for k in self.cidict.iterkeys():
            keys.append(k)
        nose.tools.assert_equal(3, len(keys))
        keys_set = set(keys)
        assert "Key1" in keys_set
        assert "key2" in keys_set
        assert "KEY3" in keys_set

    def test_itervalues(self):
        values = []
        for k in self.cidict.itervalues():
            values.append(k)
        nose.tools.assert_equal(3, len(values))
        values_set = set(values)
        assert "val1" in values_set
        assert "val2" in values_set
        assert "VAL3" in values_set

    def test_keys(self):
        keys = list(self.cidict.keys())
        nose.tools.assert_equal(3, len(keys))
        keys_set = set(keys)
        assert "Key1" in keys_set
        assert "key2" in keys_set
        assert "KEY3" in keys_set

        assert list(self.cidict.keys()) == list(self.cidict.iterkeys())

    def test_values(self):
        values = list(self.cidict.values())
        nose.tools.assert_equal(3, len(values))
        values_set = set(values)
        assert "val1" in values_set
        assert "val2" in values_set
        assert "VAL3" in values_set

        assert list(self.cidict.values()) == list(self.cidict.itervalues())

    def test_update(self):
        newdict = { "KEY2": "newval2",
                    "key4": "val4" }
        self.cidict.update(newdict)
        nose.tools.assert_equal(4, len(self.cidict))

        items = list(self.cidict.items())
        nose.tools.assert_equal(4, len(items))
        items_set = set(items)
        assert ("Key1", "val1") in items_set
        # note the update "overwrites" the case of the key2
        assert ("KEY2", "newval2") in items_set
        assert ("KEY3", "VAL3") in items_set
        assert ("key4", "val4") in items_set

    def test_update_dict_and_kwargs(self):
        self.cidict.update({'a': 'va', 'b': None}, b='vb', key2='v2')
        assert dict(self.cidict.items()) == {
            'a': 'va', 'b': 'vb',
            'Key1': 'val1', 'key2': 'v2', 'KEY3': 'VAL3'}

    def test_update_list_and_kwargs(self):
        self.cidict.update([('a', 'va'), ('b', None)], b='vb', key2='val2')
        assert dict(self.cidict.items()) == {
            'a': 'va', 'b': 'vb',
            'Key1': 'val1', 'key2': 'val2', 'KEY3': 'VAL3'}

    def test_update_duplicate_values_dict(self):
        with nose.tools.assert_raises(ValueError):
            self.cidict.update({'a': 'va', 'A': None, 'b': 3})

    def test_update_duplicate_values_list(self):
        with nose.tools.assert_raises(ValueError):
            self.cidict.update([('a', 'va'), ('A', None), ('b', 3)])

    def test_update_duplicate_values_kwargs(self):
        with nose.tools.assert_raises(ValueError):
            self.cidict.update(a='va', A=None, b=3)

    def test_update_kwargs(self):
        self.cidict.update(b='vb', key2='val2')
        assert dict(self.cidict.items()) == {
            'b': 'vb', 'Key1': 'val1', 'key2': 'val2', 'KEY3': 'VAL3'}

    def test_setdefault(self):
        nose.tools.assert_equal("val1", self.cidict.setdefault("KEY1", "default"))

        assert "KEY4" not in self.cidict
        nose.tools.assert_equal("default", self.cidict.setdefault("KEY4", "default"))
        assert "KEY4" in self.cidict
        nose.tools.assert_equal("default", self.cidict["key4"])

        assert "KEY5" not in self.cidict
        nose.tools.assert_equal(None, self.cidict.setdefault("KEY5"))
        assert "KEY5" in self.cidict
        nose.tools.assert_equal(None, self.cidict["key5"])

    def test_pop(self):
        nose.tools.assert_equal("val1", self.cidict.pop("KEY1", "default"))
        assert "key1" not in self.cidict

        nose.tools.assert_equal("val2", self.cidict.pop("KEY2"))
        assert "key2" not in self.cidict

        nose.tools.assert_equal("default", self.cidict.pop("key4", "default"))
        with nose.tools.assert_raises(KeyError):
            self.cidict.pop("key4")

    def test_popitem(self):
        items = set(self.cidict.items())
        nose.tools.assert_equal(3, len(self.cidict))

        item = self.cidict.popitem()
        nose.tools.assert_equal(2, len(self.cidict))
        assert item in items
        items.discard(item)

        item = self.cidict.popitem()
        nose.tools.assert_equal(1, len(self.cidict))
        assert item in items
        items.discard(item)

        item = self.cidict.popitem()
        nose.tools.assert_equal(0, len(self.cidict))
        assert item in items
        items.discard(item)

    def test_fromkeys(self):
        dct = ipautil.CIDict.fromkeys(('A', 'b', 'C'))
        assert sorted(dct.keys()) == sorted(['A', 'b', 'C'])
        assert list(dct.values()) == [None] * 3


class TestTimeParser(object):
    def test_simple(self):
        timestr = "20070803"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(2007, time.year)
        nose.tools.assert_equal(8, time.month)
        nose.tools.assert_equal(3, time.day)
        nose.tools.assert_equal(0, time.hour)
        nose.tools.assert_equal(0, time.minute)
        nose.tools.assert_equal(0, time.second)

    def test_hour_min_sec(self):
        timestr = "20051213141205"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(2005, time.year)
        nose.tools.assert_equal(12, time.month)
        nose.tools.assert_equal(13, time.day)
        nose.tools.assert_equal(14, time.hour)
        nose.tools.assert_equal(12, time.minute)
        nose.tools.assert_equal(5, time.second)

    def test_fractions(self):
        timestr = "2003092208.5"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(2003, time.year)
        nose.tools.assert_equal(9, time.month)
        nose.tools.assert_equal(22, time.day)
        nose.tools.assert_equal(8, time.hour)
        nose.tools.assert_equal(30, time.minute)
        nose.tools.assert_equal(0, time.second)

        timestr = "199203301544,25"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(1992, time.year)
        nose.tools.assert_equal(3, time.month)
        nose.tools.assert_equal(30, time.day)
        nose.tools.assert_equal(15, time.hour)
        nose.tools.assert_equal(44, time.minute)
        nose.tools.assert_equal(15, time.second)

        timestr = "20060401185912,8"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(2006, time.year)
        nose.tools.assert_equal(4, time.month)
        nose.tools.assert_equal(1, time.day)
        nose.tools.assert_equal(18, time.hour)
        nose.tools.assert_equal(59, time.minute)
        nose.tools.assert_equal(12, time.second)
        nose.tools.assert_equal(800000, time.microsecond)

    def test_time_zones(self):
        # pylint: disable=no-member

        timestr = "20051213141205Z"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(0, time.tzinfo.houroffset)
        nose.tools.assert_equal(0, time.tzinfo.minoffset)
        offset = time.tzinfo.utcoffset(time.tzinfo.dst())
        nose.tools.assert_equal(0, offset.seconds)

        timestr = "20051213141205+0500"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(5, time.tzinfo.houroffset)
        nose.tools.assert_equal(0, time.tzinfo.minoffset)
        offset = time.tzinfo.utcoffset(time.tzinfo.dst())
        nose.tools.assert_equal(5 * 60 * 60, offset.seconds)

        timestr = "20051213141205-0500"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(-5, time.tzinfo.houroffset)
        nose.tools.assert_equal(0, time.tzinfo.minoffset)
        # NOTE - the offset is always positive - it's minutes
        #        _east_ of UTC
        offset = time.tzinfo.utcoffset(time.tzinfo.dst())
        nose.tools.assert_equal((24 - 5) * 60 * 60, offset.seconds)

        timestr = "20051213141205-0930"

        time = ipautil.parse_generalized_time(timestr)
        nose.tools.assert_equal(-9, time.tzinfo.houroffset)
        nose.tools.assert_equal(-30, time.tzinfo.minoffset)
        offset = time.tzinfo.utcoffset(time.tzinfo.dst())
        nose.tools.assert_equal(((24 - 9) * 60 * 60) - (30 * 60), offset.seconds)


def test_run():
    result = ipautil.run(['echo', 'foo\x02bar'],
                         capture_output=True,
                         capture_error=True)
    assert result.returncode == 0
    assert result.output == 'foo\x02bar\n'
    assert result.raw_output == b'foo\x02bar\n'
    assert result.error_output == ''
    assert result.raw_error_output == b''


def test_run_no_capture_output():
    result = ipautil.run(['echo', 'foo\x02bar'])
    assert result.returncode == 0
    assert result.output is None
    assert result.raw_output == b'foo\x02bar\n'
    assert result.error_output is None
    assert result.raw_error_output == b''


def test_run_bytes():
    result = ipautil.run(['echo', b'\x01\x02'], capture_output=True)
    assert result.returncode == 0
    assert result.raw_output == b'\x01\x02\n'


def test_run_decode():
    result = ipautil.run(['echo', u'á'.encode('utf-8')],
                         encoding='utf-8', capture_output=True)
    assert result.returncode == 0
    if six.PY3:
        assert result.output == \n'
    else:
        assert result.output == \n'.encode('utf-8')


def test_run_decode_bad():
    if six.PY3:
        with pytest.raises(UnicodeDecodeError):
            ipautil.run(['echo', b'\xa0\xa1'],
                        capture_output=True,
                        encoding='utf-8')
    else:
        result = ipautil.run(['echo', '\xa0\xa1'],
                             capture_output=True,
                             encoding='utf-8')
        assert result.returncode == 0
        assert result.output == '\xa0\xa1\n'


def test_backcompat():
    result = out, err, rc = ipautil.run(['echo', 'foo\x02bar'],
                                        capture_output=True,
                                        capture_error=True)
    assert rc is result.returncode
    assert out is result.output
    assert err is result.error_output