ripset 0.1.0

Pure Rust implementation of ipset/nftset operations via netlink
Documentation
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
//! Integration tests for ipset/nftset operations.
//!
//! These tests require root privileges.
//! Build with: cargo test --no-run
//! Run with: sudo ./target/debug/deps/integration_tests-*

use std::net::IpAddr;

use ripset::{
    IpEntry, IpSetCreateOptions, IpSetFamily, NftSetCreateOptions, NftSetType, ipset_add,
    ipset_create, ipset_del, ipset_destroy, ipset_list, ipset_test, nftset_add, nftset_create_set,
    nftset_create_table, nftset_del, nftset_delete_table, nftset_list, nftset_list_tables,
    nftset_test,
};

// =====================
// ipset tests
// =====================

mod ipset_tests {
    use super::*;

    // Each test uses unique set names to avoid race conditions when running in parallel

    #[test]
    fn test_ipset_add_test_del_ipv4() {
        const SET_NAME: &str = "lipsets_test_v4";

        // Setup
        let _ = ipset_destroy(SET_NAME);
        let opts = IpSetCreateOptions::default();
        ipset_create(SET_NAME, &opts).expect("Failed to create ipset");

        let addr: IpAddr = "10.0.0.1".parse().unwrap();

        // Test that IP is not in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IP");
        assert!(!exists, "IP should not exist initially");

        // Add IP to set
        ipset_add(SET_NAME, addr).expect("Failed to add IP");

        // Test that IP is now in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IP after add");
        assert!(exists, "IP should exist after add");

        // Delete IP from set
        ipset_del(SET_NAME, addr).expect("Failed to delete IP");

        // Test that IP is no longer in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IP after del");
        assert!(!exists, "IP should not exist after delete");

        // Cleanup
        let _ = ipset_destroy(SET_NAME);
    }

    #[test]
    fn test_ipset_add_test_del_ipv6() {
        const SET_NAME: &str = "lipsets_test_v6";

        // Setup
        let _ = ipset_destroy(SET_NAME);
        let opts = IpSetCreateOptions {
            family: IpSetFamily::Inet6,
            ..Default::default()
        };
        ipset_create(SET_NAME, &opts).expect("Failed to create ipset6");

        let addr: IpAddr = "2001:db8::1".parse().unwrap();

        // Test that IP is not in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IPv6");
        assert!(!exists, "IPv6 should not exist initially");

        // Add IP to set
        ipset_add(SET_NAME, addr).expect("Failed to add IPv6");

        // Test that IP is now in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IPv6 after add");
        assert!(exists, "IPv6 should exist after add");

        // Delete IP from set
        ipset_del(SET_NAME, addr).expect("Failed to delete IPv6");

        // Test that IP is no longer in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IPv6 after del");
        assert!(!exists, "IPv6 should not exist after delete");

        // Cleanup
        let _ = ipset_destroy(SET_NAME);
    }

    #[test]
    fn test_ipset_with_timeout() {
        const SET_NAME: &str = "lipsets_test_timeout";

        // Setup
        let _ = ipset_destroy(SET_NAME);
        let opts = IpSetCreateOptions {
            timeout: Some(300),
            ..Default::default()
        };
        ipset_create(SET_NAME, &opts).expect("Failed to create ipset with timeout");

        let addr: IpAddr = "10.0.0.2".parse().unwrap();
        let entry = IpEntry::with_timeout(addr, 60);

        // Add IP with timeout
        ipset_add(SET_NAME, entry).expect("Failed to add IP with timeout");

        // Test that IP is in set
        let exists = ipset_test(SET_NAME, addr).expect("Failed to test IP");
        assert!(exists, "IP should exist after add with timeout");

        // Cleanup
        let _ = ipset_destroy(SET_NAME);
    }

    #[test]
    fn test_ipset_multiple_ips() {
        const SET_NAME: &str = "lipsets_test_multi";

        // Setup
        let _ = ipset_destroy(SET_NAME);
        let opts = IpSetCreateOptions::default();
        ipset_create(SET_NAME, &opts).expect("Failed to create ipset");

        let addrs: Vec<IpAddr> = vec![
            "10.0.0.10".parse().unwrap(),
            "10.0.0.11".parse().unwrap(),
            "10.0.0.12".parse().unwrap(),
        ];

        // Add all IPs
        for addr in &addrs {
            ipset_add(SET_NAME, *addr).expect("Failed to add IP");
        }

        // Test all IPs exist
        for addr in &addrs {
            let exists = ipset_test(SET_NAME, *addr).expect("Failed to test IP");
            assert!(exists, "IP {} should exist", addr);
        }

        // Delete all IPs
        for addr in &addrs {
            ipset_del(SET_NAME, *addr).expect("Failed to delete IP");
        }

        // Test all IPs are gone
        for addr in &addrs {
            let exists = ipset_test(SET_NAME, *addr).expect("Failed to test IP");
            assert!(!exists, "IP {} should not exist after delete", addr);
        }

        // Cleanup
        let _ = ipset_destroy(SET_NAME);
    }

    #[test]
    fn test_ipset_nonexistent_set() {
        let addr: IpAddr = "10.0.0.1".parse().unwrap();

        let result = ipset_add("nonexistent_set_12345", addr);
        assert!(result.is_err(), "Should fail for nonexistent set");
    }

    #[test]
    fn test_ipset_list() {
        const SET_NAME: &str = "lipsets_test_list";

        // Setup
        let _ = ipset_destroy(SET_NAME);
        let opts = IpSetCreateOptions::default();
        ipset_create(SET_NAME, &opts).expect("Failed to create ipset");

        // Initially empty
        let ips = ipset_list(SET_NAME).expect("Failed to list ipset");
        assert!(ips.is_empty(), "Set should be empty initially");

        // Add some IPs
        let addr1: IpAddr = "10.0.0.1".parse().unwrap();
        let addr2: IpAddr = "10.0.0.2".parse().unwrap();
        let addr3: IpAddr = "10.0.0.3".parse().unwrap();

        ipset_add(SET_NAME, addr1).expect("Failed to add IP");
        ipset_add(SET_NAME, addr2).expect("Failed to add IP");
        ipset_add(SET_NAME, addr3).expect("Failed to add IP");

        // List should now contain all three
        let ips = ipset_list(SET_NAME).expect("Failed to list ipset");
        assert_eq!(ips.len(), 3, "Set should contain 3 IPs");
        assert!(ips.contains(&addr1), "Set should contain addr1");
        assert!(ips.contains(&addr2), "Set should contain addr2");
        assert!(ips.contains(&addr3), "Set should contain addr3");

        // Delete one and verify
        ipset_del(SET_NAME, addr2).expect("Failed to delete IP");
        let ips = ipset_list(SET_NAME).expect("Failed to list ipset");
        assert_eq!(ips.len(), 2, "Set should contain 2 IPs after delete");
        assert!(!ips.contains(&addr2), "Set should not contain addr2");

        // Cleanup
        let _ = ipset_destroy(SET_NAME);
    }
}

// =====================
// nftset tests
// =====================

mod nftset_tests {
    use super::*;

    // Each test uses unique table/set names to avoid race conditions when running in parallel

    #[test]
    fn test_nftset_add_test_del_ipv4() {
        const TABLE_NAME: &str = "lnftsets_test_v4";
        const SET_NAME: &str = "test_set";

        // Setup
        let _ = nftset_delete_table("inet", TABLE_NAME);
        nftset_create_table("inet", TABLE_NAME).expect("Failed to create table");
        let opts = NftSetCreateOptions::default();
        nftset_create_set("inet", TABLE_NAME, SET_NAME, &opts).expect("Failed to create set");

        let addr: IpAddr = "10.0.0.1".parse().unwrap();

        // Test that IP is not in set
        let exists = nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IP");
        assert!(!exists, "IP should not exist initially");

        // Add IP to set
        nftset_add("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to add IP");

        // Test that IP is now in set
        let exists =
            nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IP after add");
        assert!(exists, "IP should exist after add");

        // Delete IP from set
        nftset_del("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to delete IP");

        // Test that IP is no longer in set
        let exists =
            nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IP after del");
        assert!(!exists, "IP should not exist after delete");

        // Cleanup
        let _ = nftset_delete_table("inet", TABLE_NAME);
    }

    #[test]
    fn test_nftset_add_test_del_ipv6() {
        const TABLE_NAME: &str = "lnftsets_test_v6";
        const SET_NAME: &str = "test_set";

        // Setup
        let _ = nftset_delete_table("inet", TABLE_NAME);
        nftset_create_table("inet", TABLE_NAME).expect("Failed to create table");
        let opts = NftSetCreateOptions {
            set_type: NftSetType::Ipv6Addr,
            ..Default::default()
        };
        nftset_create_set("inet", TABLE_NAME, SET_NAME, &opts).expect("Failed to create set6");

        let addr: IpAddr = "2001:db8::1".parse().unwrap();

        // Test that IP is not in set
        let exists = nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IPv6");
        assert!(!exists, "IPv6 should not exist initially");

        // Add IP to set
        nftset_add("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to add IPv6");

        // Test that IP is now in set
        let exists =
            nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IPv6 after add");
        assert!(exists, "IPv6 should exist after add");

        // Delete IP from set
        nftset_del("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to delete IPv6");

        // Test that IP is no longer in set
        let exists =
            nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IPv6 after del");
        assert!(!exists, "IPv6 should not exist after delete");

        // Cleanup
        let _ = nftset_delete_table("inet", TABLE_NAME);
    }

    #[test]
    fn test_nftset_with_timeout() {
        const TABLE_NAME: &str = "lnftsets_test_timeout";
        const SET_NAME: &str = "test_set";

        // Setup
        let _ = nftset_delete_table("inet", TABLE_NAME);
        nftset_create_table("inet", TABLE_NAME).expect("Failed to create table");
        let opts = NftSetCreateOptions {
            timeout: Some(300),
            ..Default::default()
        };
        nftset_create_set("inet", TABLE_NAME, SET_NAME, &opts)
            .expect("Failed to create set with timeout");

        let addr: IpAddr = "10.0.0.2".parse().unwrap();
        let entry = IpEntry::with_timeout(addr, 60);

        // Add IP with timeout
        nftset_add("inet", TABLE_NAME, SET_NAME, entry).expect("Failed to add IP with timeout");

        // Test that IP is in set
        let exists = nftset_test("inet", TABLE_NAME, SET_NAME, addr).expect("Failed to test IP");
        assert!(exists, "IP should exist after add with timeout");

        // Cleanup
        let _ = nftset_delete_table("inet", TABLE_NAME);
    }

    #[test]
    fn test_nftset_multiple_ips() {
        const TABLE_NAME: &str = "lnftsets_test_multi";
        const SET_NAME: &str = "test_set";

        // Setup
        let _ = nftset_delete_table("inet", TABLE_NAME);
        nftset_create_table("inet", TABLE_NAME).expect("Failed to create table");
        let opts = NftSetCreateOptions::default();
        nftset_create_set("inet", TABLE_NAME, SET_NAME, &opts).expect("Failed to create set");

        let addrs: Vec<IpAddr> = vec![
            "10.0.0.10".parse().unwrap(),
            "10.0.0.11".parse().unwrap(),
            "10.0.0.12".parse().unwrap(),
        ];

        // Add all IPs
        for addr in &addrs {
            nftset_add("inet", TABLE_NAME, SET_NAME, *addr).expect("Failed to add IP");
        }

        // Test all IPs exist
        for addr in &addrs {
            let exists =
                nftset_test("inet", TABLE_NAME, SET_NAME, *addr).expect("Failed to test IP");
            assert!(exists, "IP {} should exist", addr);
        }

        // Delete all IPs
        for addr in &addrs {
            nftset_del("inet", TABLE_NAME, SET_NAME, *addr).expect("Failed to delete IP");
        }

        // Test all IPs are gone
        for addr in &addrs {
            let exists =
                nftset_test("inet", TABLE_NAME, SET_NAME, *addr).expect("Failed to test IP");
            assert!(!exists, "IP {} should not exist after delete", addr);
        }

        // Cleanup
        let _ = nftset_delete_table("inet", TABLE_NAME);
    }

    #[test]
    fn test_nftset_nonexistent_set() {
        let addr: IpAddr = "10.0.0.1".parse().unwrap();

        let result = nftset_add("inet", "nonexistent_table", "nonexistent_set", addr);
        assert!(result.is_err(), "Should fail for nonexistent set");
    }

    #[test]
    fn test_nftset_list() {
        const TABLE_NAME: &str = "lnftsets_test_list";
        const SET_NAME: &str = "test_set";

        // Setup
        let _ = nftset_delete_table("inet", TABLE_NAME);
        nftset_create_table("inet", TABLE_NAME).expect("Failed to create table");
        let opts = NftSetCreateOptions::default();
        nftset_create_set("inet", TABLE_NAME, SET_NAME, &opts).expect("Failed to create set");

        // Initially empty
        let ips = nftset_list("inet", TABLE_NAME, SET_NAME).expect("Failed to list nftset");
        assert!(ips.is_empty(), "Set should be empty initially");

        // Add some IPs
        let addr1: IpAddr = "10.0.0.1".parse().unwrap();
        let addr2: IpAddr = "10.0.0.2".parse().unwrap();
        let addr3: IpAddr = "10.0.0.3".parse().unwrap();

        nftset_add("inet", TABLE_NAME, SET_NAME, addr1).expect("Failed to add IP");
        nftset_add("inet", TABLE_NAME, SET_NAME, addr2).expect("Failed to add IP");
        nftset_add("inet", TABLE_NAME, SET_NAME, addr3).expect("Failed to add IP");

        // Verify with nftset_test
        assert!(
            nftset_test("inet", TABLE_NAME, SET_NAME, addr1).unwrap(),
            "addr1 should exist via test"
        );
        assert!(
            nftset_test("inet", TABLE_NAME, SET_NAME, addr2).unwrap(),
            "addr2 should exist via test"
        );
        assert!(
            nftset_test("inet", TABLE_NAME, SET_NAME, addr3).unwrap(),
            "addr3 should exist via test"
        );

        // List should now contain all three
        let ips = nftset_list("inet", TABLE_NAME, SET_NAME).expect("Failed to list nftset");
        assert_eq!(ips.len(), 3, "Set should contain 3 IPs");
        assert!(ips.contains(&addr1), "Set should contain addr1");
        assert!(ips.contains(&addr2), "Set should contain addr2");
        assert!(ips.contains(&addr3), "Set should contain addr3");

        // Delete one and verify
        nftset_del("inet", TABLE_NAME, SET_NAME, addr2).expect("Failed to delete IP");
        let ips = nftset_list("inet", TABLE_NAME, SET_NAME).expect("Failed to list nftset");
        assert_eq!(ips.len(), 2, "Set should contain 2 IPs after delete");
        assert!(!ips.contains(&addr2), "Set should not contain addr2");

        // Cleanup
        let _ = nftset_delete_table("inet", TABLE_NAME);
    }

    #[test]
    fn test_nftset_list_tables() {
        const TABLE_NAME1: &str = "lnftsets_test_tables_1";
        const TABLE_NAME2: &str = "lnftsets_test_tables_2";

        // Setup - ensure clean state
        let _ = nftset_delete_table("inet", TABLE_NAME1);
        let _ = nftset_delete_table("inet", TABLE_NAME2);

        // Create two tables
        nftset_create_table("inet", TABLE_NAME1).expect("Failed to create table1");
        nftset_create_table("inet", TABLE_NAME2).expect("Failed to create table2");

        // List tables
        let tables = nftset_list_tables("inet").expect("Failed to list tables");

        // Should contain both tables we created
        assert!(
            tables.contains(&TABLE_NAME1.to_string()),
            "Should contain table1"
        );
        assert!(
            tables.contains(&TABLE_NAME2.to_string()),
            "Should contain table2"
        );

        // Delete one and verify
        nftset_delete_table("inet", TABLE_NAME1).expect("Failed to delete table1");
        let tables = nftset_list_tables("inet").expect("Failed to list tables after delete");
        assert!(
            !tables.contains(&TABLE_NAME1.to_string()),
            "Should not contain deleted table1"
        );
        assert!(
            tables.contains(&TABLE_NAME2.to_string()),
            "Should still contain table2"
        );

        // Cleanup
        let _ = nftset_delete_table("inet", TABLE_NAME2);
    }
}