8
8
package unix_test
9
9
10
10
import (
11
+ "fmt"
12
+ "os/exec"
13
+ "strings"
11
14
"testing"
12
15
13
16
"golang.org/x/sys/unix"
@@ -24,3 +27,43 @@ func TestLifreqSetName(t *testing.T) {
24
27
t .Errorf (`Lifreq.SetName("tun0") failed: %v` , err )
25
28
}
26
29
}
30
+
31
+ func TestLifreqGetMTU (t * testing.T ) {
32
+ // Find links and their MTU using CLI tooling
33
+ // $ dladm show-link -p -o link,mtu
34
+ // net0:1500
35
+ out , err := exec .Command ("dladm" , "show-link" , "-p" , "-o" , "link,mtu" ).Output ()
36
+ if err != nil {
37
+ t .Fatalf ("unable to use dladm to find data links: %v" , err )
38
+ }
39
+ lines := strings .Split (string (out ), "\n " )
40
+ tc := make (map [string ]string )
41
+ for _ , line := range lines {
42
+ v := strings .Split (line , ":" )
43
+ if len (v ) == 2 {
44
+ tc [v [0 ]] = v [1 ]
45
+ }
46
+ }
47
+ ip_fd , err := unix .Socket (unix .AF_INET , unix .SOCK_DGRAM , 0 )
48
+ if err != nil {
49
+ t .Fatalf ("could not open udp socket: %v" , err )
50
+ }
51
+ // SIOCGLIFMTU is negative which confuses the compiler if used inline:
52
+ // Using "unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l)" results in
53
+ // "constant -1065850502 overflows uint"
54
+ reqnum := int (unix .SIOCGLIFMTU )
55
+ var l unix.Lifreq
56
+ for link , mtu := range tc {
57
+ err = l .SetName (link )
58
+ if err != nil {
59
+ t .Fatalf ("Lifreq.SetName(%q) failed: %v" , link , err )
60
+ }
61
+ if err = unix .IoctlLifreq (ip_fd , uint (reqnum ), & l ); err != nil {
62
+ t .Fatalf ("unable to SIOCGLIFMTU: %v" , err )
63
+ }
64
+ m := l .GetLifruUint ()
65
+ if fmt .Sprintf ("%d" , m ) != mtu {
66
+ t .Errorf ("unable to read MTU correctly: expected %s, got %d" , mtu , m )
67
+ }
68
+ }
69
+ }
0 commit comments