123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // Copyright 2016 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package bpf_test
- import (
- "testing"
- "golang.org/x/net/bpf"
- )
- func TestVMRetA(t *testing.T) {
- vm, done, err := testVM(t, []bpf.Instruction{
- bpf.LoadAbsolute{
- Off: 8,
- Size: 1,
- },
- bpf.RetA{},
- })
- if err != nil {
- t.Fatalf("failed to load BPF program: %v", err)
- }
- defer done()
- out, err := vm.Run([]byte{
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff,
- 9,
- })
- if err != nil {
- t.Fatalf("unexpected error while running program: %v", err)
- }
- if want, got := 1, out; want != got {
- t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
- want, got)
- }
- }
- func TestVMRetALargerThanInput(t *testing.T) {
- vm, done, err := testVM(t, []bpf.Instruction{
- bpf.LoadAbsolute{
- Off: 8,
- Size: 2,
- },
- bpf.RetA{},
- })
- if err != nil {
- t.Fatalf("failed to load BPF program: %v", err)
- }
- defer done()
- out, err := vm.Run([]byte{
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff,
- 0, 255,
- })
- if err != nil {
- t.Fatalf("unexpected error while running program: %v", err)
- }
- if want, got := 2, out; want != got {
- t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
- want, got)
- }
- }
- func TestVMRetConstant(t *testing.T) {
- vm, done, err := testVM(t, []bpf.Instruction{
- bpf.RetConstant{
- Val: 9,
- },
- })
- if err != nil {
- t.Fatalf("failed to load BPF program: %v", err)
- }
- defer done()
- out, err := vm.Run([]byte{
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff,
- 0, 1,
- })
- if err != nil {
- t.Fatalf("unexpected error while running program: %v", err)
- }
- if want, got := 1, out; want != got {
- t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
- want, got)
- }
- }
- func TestVMRetConstantLargerThanInput(t *testing.T) {
- vm, done, err := testVM(t, []bpf.Instruction{
- bpf.RetConstant{
- Val: 16,
- },
- })
- if err != nil {
- t.Fatalf("failed to load BPF program: %v", err)
- }
- defer done()
- out, err := vm.Run([]byte{
- 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff,
- 0, 1,
- })
- if err != nil {
- t.Fatalf("unexpected error while running program: %v", err)
- }
- if want, got := 2, out; want != got {
- t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
- want, got)
- }
- }
|