-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsubnet-pool-member-add.spec.ts
More file actions
103 lines (85 loc) · 3.47 KB
/
subnet-pool-member-add.spec.ts
File metadata and controls
103 lines (85 loc) · 3.47 KB
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { describe, expect, it } from 'vitest'
import { validateMember } from './subnet-pool-member-add'
const validate = (values: Parameters<typeof validateMember>[1]) =>
validateMember('v4', values)
const validate6 = (values: Parameters<typeof validateMember>[1]) =>
validateMember('v6', values)
const valid = { subnet: '10.0.0.0/16', minPrefixLength: 20, maxPrefixLength: 28 }
type Field = 'subnet' | 'minPrefixLength' | 'maxPrefixLength'
function errMsg(result: ReturnType<typeof validate>, field: Field) {
return result[field]
}
describe('validateMember', () => {
it('accepts valid v4 input', () => {
expect(validate(valid)).toEqual({})
})
it('accepts valid v6 input', () => {
const result = validate6({
subnet: 'fd00:1000::/32',
minPrefixLength: 48,
maxPrefixLength: 64,
})
expect(result).toEqual({})
})
it('accepts omitted prefix lengths', () => {
const result = validate({
subnet: '10.0.0.0/16',
minPrefixLength: NaN,
maxPrefixLength: NaN,
})
expect(result).toEqual({})
})
it('rejects invalid CIDR', () => {
const result = validate({ ...valid, subnet: 'not-a-cidr' })
expect(errMsg(result, 'subnet')).toMatch(/IP address/)
})
it('rejects v6 subnet in v4 pool', () => {
const result = validate({ ...valid, subnet: 'fd00::/32' })
expect(errMsg(result, 'subnet')).toBe('IPv6 subnet not allowed in IPv4 pool')
})
it('rejects v4 subnet in v6 pool', () => {
const result = validate6({ ...valid, subnet: '10.0.0.0/16' })
expect(errMsg(result, 'subnet')).toBe('IPv4 subnet not allowed in IPv6 pool')
})
it('rejects min > max prefix length', () => {
const result = validate({ ...valid, minPrefixLength: 28, maxPrefixLength: 20 })
expect(errMsg(result, 'minPrefixLength')).toMatch(/≤/)
})
it('rejects min prefix length < subnet width', () => {
const result = validate({ ...valid, minPrefixLength: 8 })
expect(errMsg(result, 'minPrefixLength')).toMatch(/≥ subnet prefix length \(16\)/)
})
it('rejects max prefix length < subnet width', () => {
const result = validate({ ...valid, maxPrefixLength: 8 })
expect(errMsg(result, 'maxPrefixLength')).toMatch(/≥ subnet prefix length \(16\)/)
})
it('rejects prefix length above max bound (v4: 32)', () => {
const result = validate({ ...valid, minPrefixLength: 33 })
expect(errMsg(result, 'minPrefixLength')).toBe('Must be between 0 and 32')
})
it('rejects prefix length below 0', () => {
const result = validate({ ...valid, maxPrefixLength: -1 })
expect(errMsg(result, 'maxPrefixLength')).toBe('Must be between 0 and 32')
})
it('shows min-≤-max error even when min is also below subnet width', () => {
// min(12) > max(10) AND min(12) < subnetWidth(16): the min-≤-max error
// should take priority over the subnet-width error
const result = validate({ ...valid, minPrefixLength: 12, maxPrefixLength: 10 })
expect(errMsg(result, 'minPrefixLength')).toMatch(/≤/)
})
it('rejects prefix length above max bound (v6: 128)', () => {
const result = validate6({
subnet: 'fd00::/32',
minPrefixLength: 48,
maxPrefixLength: 200,
})
expect(errMsg(result, 'maxPrefixLength')).toBe('Must be between 0 and 128')
})
})