|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { describe, expect, it } from 'vitest' |
| 9 | + |
| 10 | +import { createResolver } from './subnet-pool-member-add' |
| 11 | + |
| 12 | +const resolve = createResolver('v4') |
| 13 | +const resolve6 = createResolver('v6') |
| 14 | + |
| 15 | +const valid = { subnet: '10.0.0.0/16', minPrefixLength: 20, maxPrefixLength: 28 } |
| 16 | + |
| 17 | +type Result = ReturnType<typeof resolve> |
| 18 | + |
| 19 | +function errMsg(result: Result, field: keyof Result['errors']) { |
| 20 | + return result.errors[field]?.message |
| 21 | +} |
| 22 | + |
| 23 | +describe('createResolver', () => { |
| 24 | + it('accepts valid v4 input', () => { |
| 25 | + expect(Object.keys(resolve(valid).errors)).toEqual([]) |
| 26 | + }) |
| 27 | + |
| 28 | + it('accepts valid v6 input', () => { |
| 29 | + const result = resolve6({ |
| 30 | + subnet: 'fd00:1000::/32', |
| 31 | + minPrefixLength: 48, |
| 32 | + maxPrefixLength: 64, |
| 33 | + }) |
| 34 | + expect(Object.keys(result.errors)).toEqual([]) |
| 35 | + }) |
| 36 | + |
| 37 | + it('accepts omitted prefix lengths', () => { |
| 38 | + const result = resolve({ |
| 39 | + subnet: '10.0.0.0/16', |
| 40 | + minPrefixLength: NaN, |
| 41 | + maxPrefixLength: NaN, |
| 42 | + }) |
| 43 | + expect(Object.keys(result.errors)).toEqual([]) |
| 44 | + }) |
| 45 | + |
| 46 | + it('rejects invalid CIDR', () => { |
| 47 | + const result = resolve({ ...valid, subnet: 'not-a-cidr' }) |
| 48 | + expect(errMsg(result, 'subnet')).toMatch(/IP address/) |
| 49 | + }) |
| 50 | + |
| 51 | + it('rejects v6 subnet in v4 pool', () => { |
| 52 | + const result = resolve({ ...valid, subnet: 'fd00::/32' }) |
| 53 | + expect(errMsg(result, 'subnet')).toBe('IPv6 subnet not allowed in IPv4 pool') |
| 54 | + }) |
| 55 | + |
| 56 | + it('rejects v4 subnet in v6 pool', () => { |
| 57 | + const result = resolve6({ ...valid, subnet: '10.0.0.0/16' }) |
| 58 | + expect(errMsg(result, 'subnet')).toBe('IPv4 subnet not allowed in IPv6 pool') |
| 59 | + }) |
| 60 | + |
| 61 | + it('rejects min > max prefix length', () => { |
| 62 | + const result = resolve({ ...valid, minPrefixLength: 28, maxPrefixLength: 20 }) |
| 63 | + expect(errMsg(result, 'minPrefixLength')).toMatch(/≤/) |
| 64 | + }) |
| 65 | + |
| 66 | + it('rejects min prefix length < subnet width', () => { |
| 67 | + const result = resolve({ ...valid, minPrefixLength: 8 }) |
| 68 | + expect(errMsg(result, 'minPrefixLength')).toMatch(/≥ subnet prefix length \(16\)/) |
| 69 | + }) |
| 70 | + |
| 71 | + it('rejects max prefix length < subnet width', () => { |
| 72 | + const result = resolve({ ...valid, maxPrefixLength: 8 }) |
| 73 | + expect(errMsg(result, 'maxPrefixLength')).toMatch(/≥ subnet prefix length \(16\)/) |
| 74 | + }) |
| 75 | + |
| 76 | + it('rejects prefix length above max bound (v4: 32)', () => { |
| 77 | + const result = resolve({ ...valid, minPrefixLength: 33 }) |
| 78 | + expect(errMsg(result, 'minPrefixLength')).toBe('Must be between 0 and 32') |
| 79 | + }) |
| 80 | + |
| 81 | + it('rejects prefix length below 0', () => { |
| 82 | + const result = resolve({ ...valid, maxPrefixLength: -1 }) |
| 83 | + expect(errMsg(result, 'maxPrefixLength')).toBe('Must be between 0 and 32') |
| 84 | + }) |
| 85 | + |
| 86 | + it('shows min-≤-max error even when min is also below subnet width', () => { |
| 87 | + // min(12) > max(10) AND min(12) < subnetWidth(16): the min-≤-max error |
| 88 | + // should take priority over the subnet-width error |
| 89 | + const result = resolve({ ...valid, minPrefixLength: 12, maxPrefixLength: 10 }) |
| 90 | + expect(errMsg(result, 'minPrefixLength')).toMatch(/≤/) |
| 91 | + }) |
| 92 | + |
| 93 | + it('rejects prefix length above max bound (v6: 128)', () => { |
| 94 | + const result = resolve6({ |
| 95 | + subnet: 'fd00::/32', |
| 96 | + minPrefixLength: 48, |
| 97 | + maxPrefixLength: 200, |
| 98 | + }) |
| 99 | + expect(errMsg(result, 'maxPrefixLength')).toBe('Must be between 0 and 128') |
| 100 | + }) |
| 101 | +}) |
0 commit comments