|
| 1 | +// Copyright Louis Royer and the NextMN 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 | + |
| 6 | +package rfc9433_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "net/netip" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/nextmn/rfc9433" |
| 14 | + |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | +) |
| 17 | + |
| 18 | +func ExampleMGTP4IPv6Src() { |
| 19 | + if src, ok := rfc9433.MGTP4IPv6SrcFrom( |
| 20 | + netip.MustParsePrefix("3fff::/20"), |
| 21 | + netip.MustParseAddr("203.0.113.1").As4(), |
| 22 | + ); ok { |
| 23 | + src.AsAddr().AsSlice() |
| 24 | + // ... |
| 25 | + if fields, err := rfc9433.ParseMGTP4IPv6Src(src.AsAddr().As16(), 20); err != nil { |
| 26 | + panic(err) |
| 27 | + } else { |
| 28 | + fields.IPv4() |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestMGTP4IPv6Src(t *testing.T) { |
| 34 | + ip_addr := [16]byte{ |
| 35 | + 0x20, 0x01, 0xDB, 0x08, |
| 36 | + 192, 0, 2, 1, |
| 37 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 38 | + } |
| 39 | + |
| 40 | + e, err := rfc9433.ParseMGTP4IPv6Src(ip_addr, 32) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + if e.IPv4().Compare(netip.MustParseAddr("192.0.2.1")) != 0 { |
| 45 | + t.Fatalf("Cannot extract ipv4 correctly: %s", e.IPv4()) |
| 46 | + } |
| 47 | + ip_addr2, ok := rfc9433.MGTP4IPv6SrcFrom(netip.MustParsePrefix("fd00:1:1::/48"), [4]byte{10, 0, 4, 1}) |
| 48 | + if !ok { |
| 49 | + t.Fatal("Could not create MGTP4IPv6Src") |
| 50 | + } |
| 51 | + b := ip_addr2.AsAddr().As16() |
| 52 | + res2 := [16]byte{ |
| 53 | + 0xfd, 0x00, 0x00, 0x01, 0x00, 0x01, |
| 54 | + 10, 0, 4, 1, |
| 55 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 56 | + } |
| 57 | + fmt.Println(b) |
| 58 | + fmt.Println(res2) |
| 59 | + if diff := cmp.Diff(b, res2); diff != "" { |
| 60 | + t.Error(diff) |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments