Debian Codegraph @

pkg/networkmanager/test-utils.js

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import * as utils from "./utils.js";
7 import cockpit from "cockpit";
8 import QUnit from "qunit-tests";
9
10 function assert_throws(assert, func, checks) {
11 assert.expect(checks.length);
12
13 checks.forEach(function(c) {
14 assert.throws(function() {
15 func(c);
16 });
17 });
18 }
19
20 QUnit.test("ip_prefix_from_text", function (assert) {
21 const checks = [
22 ["0", 0],
23 ["12", 12],
24 [" 12 ", 12]
25 ];
26
27 assert.expect(checks.length);
28
29 checks.forEach(function(c) {
30 assert.strictEqual(utils.ip_prefix_from_text(c[0]), c[1]);
31 });
32 });
33
34 QUnit.test("ip_prefix_from_text invalids", function (assert) {
35 const checks = [
36 "",
37 "-1",
38 "foo",
39 "1foo",
40 "1.5",
41 "1 2 3"
42 ];
43
44 assert_throws(assert, utils.ip_prefix_from_text, checks);
45 });
46
47 QUnit.test("ip_metric_from_text", function (assert) {
48 const checks = [
49 ["", 0],
50 ["0", 0],
51 ["12", 12],
52 [" 12 ", 12]
53 ];
54
55 assert.expect(checks.length);
56
57 checks.forEach(function(c) {
58 assert.strictEqual(utils.ip_metric_from_text(c[0]), c[1]);
59 });
60 });
61
62 QUnit.test("ip_metric_from_text invalids", function (assert) {
63 const checks = [
64 "-1",
65 "foo",
66 "1foo",
67 "1.5",
68 "1 2 3"
69 ];
70
71 assert_throws(assert, utils.ip_metric_from_text, checks);
72 });
73
74 QUnit.test("ip4_to/from_text be", function (assert) {
75 const checks = [
76 ["0.0.0.0", 0x00000000],
77 ["255.255.255.255", 0xFFFFFFFF],
78 ["1.2.3.4", 0x01020304],
79 [" 1.2.3.4 ", 0x01020304],
80 [" 1 . 2 . 3. 4 ", 0x01020304]
81 ];
82
83 assert.expect(2 * checks.length);
84
85 utils.set_byteorder("be");
86 checks.forEach(function(c) {
87 assert.strictEqual(utils.ip4_to_text(c[1]), c[0].replaceAll(" ", ""));
88 assert.strictEqual(utils.ip4_from_text(c[0]), c[1]);
89 });
90 });
91
92 QUnit.test("ip4_to/from_text le", function (assert) {
93 const checks = [
94 ["0.0.0.0", 0x00000000],
95 ["255.255.255.255", 0xFFFFFFFF],
96 ["1.2.3.4", 0x04030201],
97 [" 1.2.3.4 ", 0x04030201],
98 [" 1 . 2 . 3. 4 ", 0x04030201]
99 ];
100
101 assert.expect(2 * checks.length);
102
103 utils.set_byteorder("le");
104 checks.forEach(function(c) {
105 assert.strictEqual(utils.ip4_to_text(c[1]), c[0].replaceAll(" ", ""));
106 assert.strictEqual(utils.ip4_from_text(c[0]), c[1]);
107 });
108 });
109
110 QUnit.test("ip4_from_text invalids", function (assert) {
111 const checks = [
112 "",
113 "0",
114 "0.0",
115 "0.0.0",
116 "0.0.0.0.0",
117 "-1.2.3.4",
118 "foo",
119 "1.foo.3.4",
120 "1foo.2.3.4",
121 "1.2.3.400",
122 "1,2,3,4",
123 "1 1.2.3 3.4"
124 ];
125
126 assert_throws(assert, utils.ip4_from_text, checks);
127 });
128
129 QUnit.test("ip4_to_text zero", function (assert) {
130 utils.set_byteorder("be");
131 assert.strictEqual(utils.ip4_to_text(0, true), "");
132 });
133
134 QUnit.test("ip4_from_text empty", function (assert) {
135 utils.set_byteorder("be");
136 assert.strictEqual(utils.ip4_from_text("", true), 0);
137 });
138
139 QUnit.test("ip4_to/from_text invalid byteorder", function (assert) {
140 utils.set_byteorder(undefined);
141 assert.throws(function() { utils.ip4_from_text("1.2.3.4") });
142 assert.throws(function() { utils.ip4_to_text(0x01020304) });
143 });
144
145 QUnit.test("ip4_prefix_from_text", function (assert) {
146 const checks = [
147 "0.0.0.0",
148
149 " 128.0.0.0",
150 "192.0.0.0 ",
151 "224.0.0.0",
152 "240.0.0.0",
153 "248.0.0.0",
154 "252.0.0.0",
155 "254.0.0.0",
156 "255.0.0.0",
157
158 "255.128.0.0",
159 "255.192.0.0",
160 "255.224.0.0",
161 "255.240.0.0",
162 "255.248.0.0",
163 "255.252.0.0",
164 "255.254.0.0",
165 "255.255.0.0",
166
167 "255.255.128.0",
168 "255.255.192.0",
169 "255.255.224.0",
170 "255.255.240.0",
171 "255.255.248.0",
172 "255.255.252.0",
173 "255.255.254.0",
174 "255.255.255.0",
175
176 "255.255.255.128",
177 "255.255.255.192",
178 "255.255.255.224",
179 "255.255.255.240",
180 "255.255.255.248",
181 "255.255.255.252",
182 "255.255.255.254",
183 "255.255.255.255"
184 ];
185
186 assert.expect(checks.length);
187
188 checks.forEach(function(c, i) {
189 assert.strictEqual(utils.ip4_prefix_from_text(c), i);
190 });
191 });
192
193 QUnit.test("ip4_prefix_from_text invalids", function (assert) {
194 const checks = [
195 "",
196 "-1",
197 "foo",
198 "1foo",
199 "1.5",
200
201 "0.0",
202 "0.0.0",
203 "0.0.0.0.0",
204 "1.2.3.4",
205 "255.255.255.8",
206 "255.192.0.10",
207 "224. 0. 0.0",
208 "240. 0.0 .0",
209 "252. 0.0.0"
210 ];
211
212 assert_throws(assert, utils.ip4_prefix_from_text, checks);
213 });
214
215 QUnit.test("ip6_to/from_text", function (assert) {
216 const checks = [
217 [[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
219 "::"
220 ],
221 [[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
222 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F],
223 "1:203:405:607:809:a0b:c0d:e0f"
224 ],
225 [[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
226 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F],
227 " 1: 203 : 405: 607: 809:a0b :c0d:e0f"
228 ],
229 ];
230
231 assert.expect(2 * checks.length);
232
233 checks.forEach(function(c) {
234 assert.strictEqual(utils.ip6_to_text(cockpit.base64_encode(c[0])), c[1].replaceAll(" ", ""));
235 assert.deepEqual(cockpit.base64_decode(utils.ip6_from_text(c[1])), c[0]);
236 });
237 });
238
239 QUnit.test("ip6_from_text abbrevs", function (assert) {
240 const checks = [
241 ["::",
242 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
244 ],
245 ["::1",
246 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
248 ],
249 ["1::",
250 [0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
252 ],
253 ["1:2:3::2:1",
254 [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
255 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01],
256 ],
257 ["2001::1",
258 [0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
260 ],
261 ];
262
263 checks.forEach(function(c) {
264 assert.deepEqual(cockpit.base64_decode(utils.ip6_from_text(c[0])), c[1]);
265 });
266 });
267
268 QUnit.test("ip6_from_text invalids", function (assert) {
269 const checks = [
270 "",
271 "0",
272 "0:0",
273 "0:0:0",
274 "0:0:0:0",
275 "0:0:0:0:0",
276 "0:0:0:0:0:0",
277 "0:0:0:0:0:0:0",
278 "0:0:0:0:0:0:0:0:0",
279 "foo",
280 "1:2:3:four:5:6:7:8",
281 "1:2:3:-4:5:6:7:8",
282 "1:2:3:4.0:5:6:7:8",
283 "1:2:3:4foo:5:6:7:8",
284 "1:2:3:10000:5:6:7:8",
285 "1::4::8",
286 "::8::",
287 "1:2:3:4 4:5:6:7:8",
288 ];
289
290 assert_throws(assert, utils.ip6_from_text, checks);
291 });
292
293 QUnit.test("ip6_to_text zero", function (assert) {
294 const zero = [0, 0, 0, 0, 0, 0, 0, 0,
295 0, 0, 0, 0, 0, 0, 0, 0
296 ];
297 assert.strictEqual(utils.ip6_to_text(cockpit.base64_encode(zero), true), "");
298 });
299
300 QUnit.test("ip6_from_text empty", function (assert) {
301 const zero = [0, 0, 0, 0, 0, 0, 0, 0,
302 0, 0, 0, 0, 0, 0, 0, 0
303 ];
304 assert.deepEqual(cockpit.base64_decode(utils.ip6_from_text("", true)), zero);
305 });
306
307 QUnit.start();