Skip to content

Commit 42fc157

Browse files
committed
Add examples
1 parent 51b42c2 commit 42fc157

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

m-gtp4-ipv6-dst_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ func ExampleMGTP4IPv6Dst() {
1919
); ok {
2020
dst.AsAddr().AsSlice()
2121
// ...
22+
dst.IPv4()
23+
dst.ArgsMobSession().PDUSessionID()
2224
}
2325
}

m-gtp4-ipv6-src-nextmn_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ func ExampleMGTP4IPv6SrcNextMN() {
2323
); ok {
2424
src.AsAddr().AsSlice()
2525
// ...
26+
if fields, err := rfc9433.ParseMGTP4IPv6SrcNextMN(src.AsAddr().As16()); err != nil {
27+
panic(err)
28+
} else {
29+
fields.IPv4()
30+
fields.UDPPortNumber()
31+
}
2632
}
2733
}
2834

m-gtp4-ipv6-src_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)