-
Notifications
You must be signed in to change notification settings - Fork 7
/
ipcalc-tests.el
163 lines (145 loc) · 8 KB
/
ipcalc-tests.el
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
;; eval this buffer to run the tests
(load-file "ipcalc.el")
(require 'ipcalc)
(ert-deftest int-to-bin-string-test ()
"Test the conversion of a integer to a binary"
(should (equal "00000000" (ipcalc-int-to-bin-string 0)))
(should (equal "00000001" (ipcalc-int-to-bin-string 1)))
(should (equal "01010101" (ipcalc-int-to-bin-string 85)))
(should (equal "10101010" (ipcalc-int-to-bin-string 170)))
(should (equal "11111111" (ipcalc-int-to-bin-string 255))))
(ert-deftest network-test ()
"Test the network function"
(should (string-equal (ipcalc-network "192.168.0.23" "21")
"11000000101010000000000000000000"))
(should (string-equal (ipcalc-network "128.0.0.0" "1")
"10000000000000000000000000000000"))
(should (string-equal (ipcalc-network "0.0.0.0" "12")
"00000000000000000000000000000000"))
(should (string-equal (ipcalc-network "255.255.255.255" "12")
"11111111111100000000000000000000"))
(should (string-equal (ipcalc-network "255.255.255.255" "16")
"11111111111111110000000000000000"))
(should (string-equal (ipcalc-network "255.255.255.255" "32")
"11111111111111111111111111111111"))
(should (string-equal (ipcalc-network "170.170.170.170" "16")
"10101010101010100000000000000000"))
(should (string-equal (ipcalc-network "170.170.170.170" "16")
"10101010101010100000000000000000"))
(should-error (ipcalc-network "123.123.123" "0"))
(should-error (ipcalc-network "123.123.123.123.123" "512")))
(ert-deftest octets-as-binary-test ()
(should (string-equal (ipcalc-octets-as-binary '("192" "168" "0" "23"))
"11000000101010000000000000010111"))
(should (string-equal (ipcalc-octets-as-binary '("1" "1" "1" "1"))
"00000001000000010000000100000001"))
(should (string-equal (ipcalc-octets-as-binary '("170" "170" "170" "170"))
"10101010101010101010101010101010"))
(should (string-equal (ipcalc-octets-as-binary '("85" "85" "85" "85"))
"01010101010101010101010101010101"))
(should (string-equal (ipcalc-octets-as-binary '("255" "255" "255" "255"))
"11111111111111111111111111111111")))
(ert-deftest ip-to-octets-test ()
"That a IP address gets split into octets"
(should (equal (ipcalc-ip-to-octets "192.168.0.23")
'("192" "168" "0" "23")))
(should (equal (ipcalc-ip-to-octets "1.1.1.1")
'("1" "1" "1" "1")))
(should-error (ipcalc-ip-to-octets "1.1.1"))
(should-error (ipcalc-ip-to-octets "1.1.1.1.1")))
(ert-deftest ones-and-pad-test ()
"That padding occurs for a given value"
(should (string-equal "10000000000000000000000000000000" (ipcalc-ones-and-pad 1)))
(should (string-equal "11111111111111111111111111111110" (ipcalc-ones-and-pad 31)))
(should (string-equal "11111111111111111111111111111111" (ipcalc-ones-and-pad 32)))
(should-error (ipcalc-ones-and-pad 33))
(should-error (ipcalc-ones-and-pad 0)))
(ert-deftest invert-binary-test ()
"Tests that it inverts 1s & 0s"
(should (string-equal (ipcalc-invert-binary "1") "0"))
(should (string-equal (ipcalc-invert-binary "0") "1"))
(should (string-equal (ipcalc-invert-binary "00000000000000000000000000000000") "11111111111111111111111111111111"))
(should (string-equal (ipcalc-invert-binary "11111111111111111111111111111111") "00000000000000000000000000000000"))
(should (string-equal (ipcalc-invert-binary "00000000000000001111111111111111") "11111111111111110000000000000000"))
(should (string-equal (ipcalc-invert-binary "11111111111111110000000000000000") "00000000000000001111111111111111"))
(should (string-equal (ipcalc-invert-binary "10101010101010101010101010101010") "01010101010101010101010101010101"))
(should (string-equal (ipcalc-invert-binary "01010101010101010101010101010101") "10101010101010101010101010101010")))
(ert-deftest host+1-test ()
"Add 1 to an binary number"
(should (equal (ipcalc-host+1 "11000000101010000000000000000000")
"11000000101010000000000000000001"))
(should (equal (ipcalc-host+1 "00000000000000000000000000000000")
"00000000000000000000000000000001"))
(should (equal (ipcalc-host+1 "00000000000000000000000000000001")
"00000000000000000000000000000001"))
(should (equal (ipcalc-host+1 "01111111111111111111111111111110")
"01111111111111111111111111111111")))
(ert-deftest host-max-test ()
"Return the maximum host as a binary value"
(should (equal (ipcalc-host-max "11000000101010000000000000000000" "21")
"11000000101010000000011111111110"))
(should (equal (ipcalc-host-max "00000000000000000000000000000000" "16")
"00000000000000001111111111111110"))
(should (equal (ipcalc-host-max "11111111111111111111111111111111" "16")
"11111111111111111111111111111111")))
(ert-deftest binary-to-ip-test ()
"Convert a IP in binary format to four octets separated by dots"
(should (equal (ipcalc-binary-to-ip "11000000101010000000000000010111")
"192.168.0.23"))
(should (equal (ipcalc-binary-to-ip "00000001000000010000000100000001")
"1.1.1.1"))
(should (equal (ipcalc-binary-to-ip "10101010101010101010101010101010")
"170.170.170.170"))
(should (equal (ipcalc-binary-to-ip "00000000000000000000000000000000")
"0.0.0.0"))
;; I think this should be an error
(should (equal (ipcalc-binary-to-ip "1111111111111111111111111111111100000000000000000000000000000000")
"255.255.255.255"))
(should-error (ipcalc-binary-to-ip "0000000000000000")))
(ert-deftest ipcalc-test ()
"Check that the output should be correctly formatted"
(let ((temp-buffer-name (make-temp-name "ipcalc-tests-")))
(with-current-buffer (get-buffer-create temp-buffer-name)
(ipcalc "192.168.1.1/24" (buffer-name))
(should (equal (buffer-string) "Address: 192.168.1.1 11000000101010000000000100000001
Netmask: 255.255.255.0 = 24 11111111111111111111111100000000
Wildcard: 0.0.0.255 00000000000000000000000011111111
=>
Network: 192.168.1.0 11000000101010000000000100000000
HostMin: 192.168.1.1 11000000101010000000000100000001
HostMax: 192.168.1.254 11000000101010000000000111111110
Broadcast: 192.168.1.255 11000000101010000000000111111111
Hosts/Net: 254
"))
(kill-buffer temp-buffer-name))))
(ert-deftest ipcalc-test-*ipcalc*-buffer ()
"Check that the output should be correctly formatted"
(ipcalc "192.168.1.1/24")
(with-current-buffer (get-buffer-create "*ipcalc*")
(should (equal (buffer-string) "Address: 192.168.1.1 11000000101010000000000100000001
Netmask: 255.255.255.0 = 24 11111111111111111111111100000000
Wildcard: 0.0.0.255 00000000000000000000000011111111
=>
Network: 192.168.1.0 11000000101010000000000100000000
HostMin: 192.168.1.1 11000000101010000000000100000001
HostMax: 192.168.1.254 11000000101010000000000111111110
Broadcast: 192.168.1.255 11000000101010000000000111111111
Hosts/Net: 254
"))))
(ert-deftest ipcalc-current-buffer-test ()
"Check that the output should be correctly formatted"
(let ((temp-buffer-name (make-temp-name "ipcalc-tests-")))
(with-current-buffer (get-buffer-create temp-buffer-name)
(ipcalc-current-buffer "192.168.1.1/24")
(should (equal (buffer-string) "Address: 192.168.1.1 11000000101010000000000100000001
Netmask: 255.255.255.0 = 24 11111111111111111111111100000000
Wildcard: 0.0.0.255 00000000000000000000000011111111
=>
Network: 192.168.1.0 11000000101010000000000100000000
HostMin: 192.168.1.1 11000000101010000000000100000001
HostMax: 192.168.1.254 11000000101010000000000111111110
Broadcast: 192.168.1.255 11000000101010000000000111111111
Hosts/Net: 254
"))
(kill-buffer temp-buffer-name))))
(ert t)