Ver Fonte

ipv4: simplify log message format

This change replaces the existing log format separated by commas and
spaces with space-separated one.

Also removes unnecessary log output in test.

Change-Id: I1bf2b8c463db9174b2ac562533cdd89b72ed39de
Reviewed-on: https://go-review.googlesource.com/16322
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Mikio Hara há 10 anos atrás
pai
commit
09f95eed15
6 ficheiros alterados com 18 adições e 17 exclusões
  1. 1 1
      ipv4/control.go
  2. 1 1
      ipv4/header.go
  3. 5 0
      ipv4/header_test.go
  4. 3 7
      ipv4/multicast_test.go
  5. 5 1
      ipv4/readwrite_test.go
  6. 3 7
      ipv4/unicast_test.go

+ 1 - 1
ipv4/control.go

@@ -48,7 +48,7 @@ func (cm *ControlMessage) String() string {
 	if cm == nil {
 		return "<nil>"
 	}
-	return fmt.Sprintf("ttl: %v, src: %v, dst: %v, ifindex: %v", cm.TTL, cm.Src, cm.Dst, cm.IfIndex)
+	return fmt.Sprintf("ttl=%d src=%v dst=%v ifindex=%d", cm.TTL, cm.Src, cm.Dst, cm.IfIndex)
 }
 
 // Ancillary data socket options

+ 1 - 1
ipv4/header.go

@@ -46,7 +46,7 @@ func (h *Header) String() string {
 	if h == nil {
 		return "<nil>"
 	}
-	return fmt.Sprintf("ver: %v, hdrlen: %v, tos: %#x, totallen: %v, id: %#x, flags: %#x, fragoff: %#x, ttl: %v, proto: %v, cksum: %#x, src: %v, dst: %v", h.Version, h.Len, h.TOS, h.TotalLen, h.ID, h.Flags, h.FragOff, h.TTL, h.Protocol, h.Checksum, h.Src, h.Dst)
+	return fmt.Sprintf("ver=%d hdrlen=%d tos=%#x totallen=%d id=%#x flags=%#x fragoff=%#x ttl=%d proto=%d cksum=%#x src=%v dst=%v", h.Version, h.Len, h.TOS, h.TotalLen, h.ID, h.Flags, h.FragOff, h.TTL, h.Protocol, h.Checksum, h.Src, h.Dst)
 }
 
 // Marshal returns the binary encoding of the IPv4 header h.

+ 5 - 0
ipv4/header_test.go

@@ -9,6 +9,7 @@ import (
 	"net"
 	"reflect"
 	"runtime"
+	"strings"
 	"testing"
 )
 
@@ -111,4 +112,8 @@ func TestParseHeader(t *testing.T) {
 	if !reflect.DeepEqual(h, testHeader) {
 		t.Fatalf("got %#v; want %#v", h, testHeader)
 	}
+	s := h.String()
+	if strings.Contains(s, ",") {
+		t.Fatalf("should be space-separated values: %s", s)
+	}
 }

+ 3 - 7
ipv4/multicast_test.go

@@ -98,12 +98,10 @@ func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
 				t.Fatalf("got %v; want %v", n, len(wb))
 			}
 			rb := make([]byte, 128)
-			if n, cm, _, err := p.ReadFrom(rb); err != nil {
+			if n, _, _, err := p.ReadFrom(rb); err != nil {
 				t.Fatal(err)
 			} else if !bytes.Equal(rb[:n], wb) {
 				t.Fatalf("got %v; want %v", rb[:n], wb)
-			} else {
-				t.Logf("rcvd cmsg: %v", cm)
 			}
 		}
 	}
@@ -198,10 +196,9 @@ func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
 				t.Fatalf("got %v; want %v", n, len(wb))
 			}
 			rb := make([]byte, 128)
-			if n, cm, _, err := p.ReadFrom(rb); err != nil {
+			if n, _, _, err := p.ReadFrom(rb); err != nil {
 				t.Fatal(err)
 			} else {
-				t.Logf("rcvd cmsg: %v", cm)
 				m, err := icmp.ParseMessage(iana.ProtocolICMP, rb[:n])
 				if err != nil {
 					t.Fatal(err)
@@ -314,10 +311,9 @@ func TestRawConnReadWriteMulticastICMP(t *testing.T) {
 				t.Fatal(err)
 			}
 			rb := make([]byte, ipv4.HeaderLen+128)
-			if rh, b, cm, err := r.ReadFrom(rb); err != nil {
+			if rh, b, _, err := r.ReadFrom(rb); err != nil {
 				t.Fatal(err)
 			} else {
-				t.Logf("rcvd cmsg: %v", cm)
 				m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
 				if err != nil {
 					t.Fatal(err)

+ 5 - 1
ipv4/readwrite_test.go

@@ -8,6 +8,7 @@ import (
 	"bytes"
 	"net"
 	"runtime"
+	"strings"
 	"sync"
 	"testing"
 
@@ -129,7 +130,10 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
 			t.Errorf("got %v; want %v", rb[:n], wb)
 			return
 		} else {
-			t.Logf("rcvd cmsg: %v", cm)
+			s := cm.String()
+			if strings.Contains(s, ",") {
+				t.Errorf("should be space-separated values: %s", s)
+			}
 		}
 	}
 	writer := func(toggle bool) {

+ 3 - 7
ipv4/unicast_test.go

@@ -64,12 +64,10 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
 		if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
 			t.Fatal(err)
 		}
-		if n, cm, _, err := p.ReadFrom(rb); err != nil {
+		if n, _, _, err := p.ReadFrom(rb); err != nil {
 			t.Fatal(err)
 		} else if !bytes.Equal(rb[:n], wb) {
 			t.Fatalf("got %v; want %v", rb[:n], wb)
-		} else {
-			t.Logf("rcvd cmsg: %v", cm)
 		}
 	}
 }
@@ -133,7 +131,7 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
 		if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
 			t.Fatal(err)
 		}
-		if n, cm, _, err := p.ReadFrom(rb); err != nil {
+		if n, _, _, err := p.ReadFrom(rb); err != nil {
 			switch runtime.GOOS {
 			case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
 				t.Logf("not supported on %s", runtime.GOOS)
@@ -141,7 +139,6 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
 			}
 			t.Fatal(err)
 		} else {
-			t.Logf("rcvd cmsg: %v", cm)
 			m, err := icmp.ParseMessage(iana.ProtocolICMP, rb[:n])
 			if err != nil {
 				t.Fatal(err)
@@ -225,7 +222,7 @@ func TestRawConnReadWriteUnicastICMP(t *testing.T) {
 		if err := r.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
 			t.Fatal(err)
 		}
-		if _, b, cm, err := r.ReadFrom(rb); err != nil {
+		if _, b, _, err := r.ReadFrom(rb); err != nil {
 			switch runtime.GOOS {
 			case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
 				t.Logf("not supported on %s", runtime.GOOS)
@@ -233,7 +230,6 @@ func TestRawConnReadWriteUnicastICMP(t *testing.T) {
 			}
 			t.Fatal(err)
 		} else {
-			t.Logf("rcvd cmsg: %v", cm)
 			m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
 			if err != nil {
 				t.Fatal(err)