github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/golang.org/x/net/bpf/vm_ret_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bpf_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/net/bpf"
    11  )
    12  
    13  func TestVMRetA(t *testing.T) {
    14  	vm, done, err := testVM(t, []bpf.Instruction{
    15  		bpf.LoadAbsolute{
    16  			Off:  8,
    17  			Size: 1,
    18  		},
    19  		bpf.RetA{},
    20  	})
    21  	if err != nil {
    22  		t.Fatalf("failed to load BPF program: %v", err)
    23  	}
    24  	defer done()
    25  
    26  	out, err := vm.Run([]byte{
    27  		0xff, 0xff, 0xff, 0xff,
    28  		0xff, 0xff, 0xff, 0xff,
    29  		9,
    30  	})
    31  	if err != nil {
    32  		t.Fatalf("unexpected error while running program: %v", err)
    33  	}
    34  	if want, got := 1, out; want != got {
    35  		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
    36  			want, got)
    37  	}
    38  }
    39  
    40  func TestVMRetALargerThanInput(t *testing.T) {
    41  	vm, done, err := testVM(t, []bpf.Instruction{
    42  		bpf.LoadAbsolute{
    43  			Off:  8,
    44  			Size: 2,
    45  		},
    46  		bpf.RetA{},
    47  	})
    48  	if err != nil {
    49  		t.Fatalf("failed to load BPF program: %v", err)
    50  	}
    51  	defer done()
    52  
    53  	out, err := vm.Run([]byte{
    54  		0xff, 0xff, 0xff, 0xff,
    55  		0xff, 0xff, 0xff, 0xff,
    56  		0, 255,
    57  	})
    58  	if err != nil {
    59  		t.Fatalf("unexpected error while running program: %v", err)
    60  	}
    61  	if want, got := 2, out; want != got {
    62  		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
    63  			want, got)
    64  	}
    65  }
    66  
    67  func TestVMRetConstant(t *testing.T) {
    68  	vm, done, err := testVM(t, []bpf.Instruction{
    69  		bpf.RetConstant{
    70  			Val: 9,
    71  		},
    72  	})
    73  	if err != nil {
    74  		t.Fatalf("failed to load BPF program: %v", err)
    75  	}
    76  	defer done()
    77  
    78  	out, err := vm.Run([]byte{
    79  		0xff, 0xff, 0xff, 0xff,
    80  		0xff, 0xff, 0xff, 0xff,
    81  		0, 1,
    82  	})
    83  	if err != nil {
    84  		t.Fatalf("unexpected error while running program: %v", err)
    85  	}
    86  	if want, got := 1, out; want != got {
    87  		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
    88  			want, got)
    89  	}
    90  }
    91  
    92  func TestVMRetConstantLargerThanInput(t *testing.T) {
    93  	vm, done, err := testVM(t, []bpf.Instruction{
    94  		bpf.RetConstant{
    95  			Val: 16,
    96  		},
    97  	})
    98  	if err != nil {
    99  		t.Fatalf("failed to load BPF program: %v", err)
   100  	}
   101  	defer done()
   102  
   103  	out, err := vm.Run([]byte{
   104  		0xff, 0xff, 0xff, 0xff,
   105  		0xff, 0xff, 0xff, 0xff,
   106  		0, 1,
   107  	})
   108  	if err != nil {
   109  		t.Fatalf("unexpected error while running program: %v", err)
   110  	}
   111  	if want, got := 2, out; want != got {
   112  		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
   113  			want, got)
   114  	}
   115  }