|
| 1 | +// Copyright 2024 Louis Royer and the NextMN-json-api contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +package jsonapi |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/nextmn/json-api/jsonapi" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSegment(t *testing.T) { |
| 15 | + s := &jsonapi.Segment{} |
| 16 | + if err := s.UnmarshalText([]byte("192.168.0.0/24")); err == nil { |
| 17 | + t.Errorf("Segment should be an IPv6 Address, not an IPv4 Prefix") |
| 18 | + } |
| 19 | + if err := s.UnmarshalText([]byte("192.168.0.1")); err == nil { |
| 20 | + t.Errorf("Segment should be an IPv6 Address, not an IPv4 Address") |
| 21 | + } |
| 22 | + if err := s.UnmarshalText([]byte("fd00::/80")); err == nil { |
| 23 | + t.Errorf("Segment should be an IPv6 Address, not an IPv6 Prefix") |
| 24 | + } |
| 25 | + if err := s.UnmarshalText([]byte("")); err == nil { |
| 26 | + t.Errorf("Empty Segment should be rejected") |
| 27 | + } |
| 28 | + if err := s.UnmarshalText([]byte("fd00::")); err != nil { |
| 29 | + t.Errorf("Correct Segment should not be rejected") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestNewSegment(t *testing.T) { |
| 34 | + if seg0, err := jsonapi.NewSegment("::"); err == nil { |
| 35 | + if seg0J, err := json.Marshal(seg0); err == nil { |
| 36 | + if string(seg0J) != "\"::\"" { |
| 37 | + t.Errorf("Marshal of '::' is wrong: %s", seg0J) |
| 38 | + } |
| 39 | + } else { |
| 40 | + t.Errorf("Could not Marshal '::'") |
| 41 | + } |
| 42 | + } else { |
| 43 | + t.Errorf("Could not create segment '::'") |
| 44 | + } |
| 45 | + if seg1, err := jsonapi.NewSegment("fd00::"); err == nil { |
| 46 | + if seg1J, err := json.Marshal(seg1); err == nil { |
| 47 | + if string(seg1J) != "\"fd00::\"" { |
| 48 | + t.Errorf("Marshal of 'fd00::' is wrong: %s", seg1J) |
| 49 | + } |
| 50 | + } else { |
| 51 | + t.Errorf("Could not Marshal 'fd00::'") |
| 52 | + } |
| 53 | + } else { |
| 54 | + t.Errorf("Could not create segment 'fd00::'") |
| 55 | + } |
| 56 | + if _, err := jsonapi.NewSegment("10.0.0.0"); err == nil { |
| 57 | + t.Errorf("NewSegment should error with string '10.0.0.0'") |
| 58 | + } |
| 59 | + if _, err := jsonapi.NewSegment(""); err == nil { |
| 60 | + t.Errorf("NewSegment should error with empty string") |
| 61 | + } |
| 62 | + if _, err := jsonapi.NewSegment("fd00::/128"); err == nil { |
| 63 | + t.Errorf("NewSegment should error with IPv6 Prefix") |
| 64 | + } |
| 65 | + if _, err := jsonapi.NewSegment("10.0.0.0/32"); err == nil { |
| 66 | + t.Errorf("NewSegment should error with IPv4 Prefix") |
| 67 | + } |
| 68 | +} |
0 commit comments