chiark / gitweb /
make-secnet-sites etc.: Use unicode
[secnet.git] / ipaddrset-test.py
1 #!/usr/bin/python
2
3 # This file is Free Software.  It was originally written for secnet.
4 #
5 # Copyright 2014 Ian Jackson
6 #
7 # You may redistribute secnet as a whole and/or modify it under the
8 # terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3, or (at your option) any
10 # later version.
11 #
12 # You may redistribute this fileand/or modify it under the terms of
13 # the GNU General Public License as published by the Free Software
14 # Foundation; either version 2, or (at your option) any later version.
15 # Note however that this version of ipaddrset.py uses the Python
16 # ipaddr library from Google, which is licenced only under the Apache
17 # Licence, version 2.0, which is only compatible with the GNU GPL v3
18 # (or perhaps later versions), and not with the GNU GPL v2.
19 #
20 # This software is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this software; if not, see
27 # https://www.gnu.org/licenses/gpl.html.
28 #
29 # The corresponding test vector file ise ipaddrset-test.expected.  I
30 # don't believe it is a creative work that attracts copyright.  -iwj.
31
32 from __future__ import print_function
33 from __future__ import unicode_literals
34
35 import ipaddr
36 from ipaddr import IPNetwork, IPAddress
37
38 import ipaddrset
39 from ipaddrset import IPAddressSet
40
41 v4a=IPAddress('172.18.45.6')
42
43 s=IPAddressSet()
44 print('s =', s)
45 s.append([IPNetwork('172.18.45.0/24')])
46 s.append([IPNetwork('2001:23:24::/48')])
47 print(s)
48
49 t=IPAddressSet(map(IPNetwork,['172.31.80.8/32','172.18.45.192/28']))
50 print('t =', t)
51 print(t <= s)
52 print(t == s)
53
54 for n1s in ['172.18.44.0/23','172.18.45.6/32','172.18.45.0/24']:
55     n1=IPNetwork(n1s)
56     print(n1)
57     print(s.contains(n1))
58     print(t.contains(n1))
59
60 n=s.networks()[0]
61
62 a=ipaddrset.complete_set()
63 print('a =', a)
64 print(a >= s)
65 print(a >= t)
66
67 print('^')
68 print(s.intersection(t))
69 print(t.intersection(s))
70
71 print('u')
72 print(s.union(t))
73 print(t.union(s))