Bläddra i källkod

x/net/bpf: cleanup TestAsmDisasm

The "fake" jump conditions as well as the LoadExtension instructions
are now disassembled correctly. Therefore the workaround to reassemble
the disassembly is no longer necessary.

This simplification was annonced already in golang/go#18470.

Result of `go test -cover .` stays the same with this simplification.

$ go test -cover golang.org/x/net/bpf
ok  	golang.org/x/net/bpf	0.495s	coverage: 92.3% of statements

Change-Id: I3f9eb46148287c76059437b773b80c4c99eb5b53
Reviewed-on: https://go-review.googlesource.com/34951
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Lucas Bremgartner 9 år sedan
förälder
incheckning
da2b4fa285
1 ändrade filer med 8 tillägg och 48 borttagningar
  1. 8 48
      bpf/instructions_test.go

+ 8 - 48
bpf/instructions_test.go

@@ -143,11 +143,6 @@ func TestInterop(t *testing.T) {
 }
 
 // Check that assembly and disassembly match each other.
-//
-// Because we offer "fake" jump conditions that don't appear in the
-// machine code, disassembly won't be a 1:1 match with the original
-// source, although the behavior will be identical. However,
-// reassembling the disassembly should produce an identical program.
 func TestAsmDisasm(t *testing.T) {
 	prog1, err := Assemble(allInstructions)
 	if err != nil {
@@ -155,59 +150,24 @@ func TestAsmDisasm(t *testing.T) {
 	}
 	t.Logf("Assembled program is %d instructions long", len(prog1))
 
-	src, allDecoded := Disassemble(prog1)
+	got, allDecoded := Disassemble(prog1)
 	if !allDecoded {
 		t.Errorf("Disassemble(Assemble(allInstructions)) produced unrecognized instructions:")
-		for i, inst := range src {
+		for i, inst := range got {
 			if r, ok := inst.(RawInstruction); ok {
 				t.Logf("  insn %d, %#v --> %#v", i+1, allInstructions[i], r)
 			}
 		}
 	}
 
-	prog2, err := Assemble(src)
-	if err != nil {
-		t.Fatalf("assembly of Disassemble(Assemble(allInstructions)) failed: %s", err)
-	}
-
-	if len(prog2) != len(prog1) {
-		t.Fatalf("disassembly changed program size: %d insns before, %d insns after", len(prog1), len(prog2))
+	if len(allInstructions) != len(got) {
+		t.Fatalf("disassembly changed program size: %d insns before, %d insns after", len(allInstructions), len(got))
 	}
-	if !reflect.DeepEqual(prog1, prog2) {
+	if !reflect.DeepEqual(allInstructions, got) {
 		t.Errorf("program mutated by disassembly:")
-		for i := range prog2 {
-			if !reflect.DeepEqual(prog1[i], prog2[i]) {
-				t.Logf("  insn %d, s: %#v, p1: %#v, p2: %#v", i+1, allInstructions[i], prog1[i], prog2[i])
-			}
-		}
-	}
-}
-
-func TestDisasmJumpIf(t *testing.T) {
-	for _, instr := range allInstructions {
-		if jumpIfInstr, ok := instr.(JumpIf); ok {
-			gotAsm, err := jumpIfInstr.Assemble()
-			if err != nil {
-				t.Fatalf("assembly of '%#v' failed: %s", jumpIfInstr, err)
-			}
-			got := gotAsm.Disassemble()
-			if !reflect.DeepEqual(jumpIfInstr, got) {
-				t.Errorf("program mutated by disassembly, expected: %#v, got: %#v", jumpIfInstr, got)
-			}
-		}
-	}
-}
-
-func TestDisasmExtensions(t *testing.T) {
-	for _, instr := range allInstructions {
-		if extInstr, ok := instr.(LoadExtension); ok {
-			gotAsm, err := extInstr.Assemble()
-			if err != nil {
-				t.Fatalf("assembly of '%#v' failed: %s", extInstr, err)
-			}
-			got := gotAsm.Disassemble()
-			if !reflect.DeepEqual(extInstr, got) {
-				t.Errorf("program mutated by disassembly, expected: %#v, got: %#v", extInstr, got)
+		for i := range got {
+			if !reflect.DeepEqual(allInstructions[i], got[i]) {
+				t.Logf("  insn %d, s: %#v, p1: %#v, got: %#v", i+1, allInstructions[i], prog1[i], got[i])
 			}
 		}
 	}