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
5package bpf_test
6
7import (
8	"testing"
9
10	"golang.org/x/net/bpf"
11)
12
13func TestVMLoadExtensionNotImplemented(t *testing.T) {
14	_, _, err := testVM(t, []bpf.Instruction{
15		bpf.LoadExtension{
16			Num: 100,
17		},
18		bpf.RetA{},
19	})
20	if errStr(err) != "extension 100 not implemented" {
21		t.Fatalf("unexpected error: %v", err)
22	}
23}
24
25func TestVMLoadExtensionExtLen(t *testing.T) {
26	vm, done, err := testVM(t, []bpf.Instruction{
27		bpf.LoadExtension{
28			Num: bpf.ExtLen,
29		},
30		bpf.RetA{},
31	})
32	if err != nil {
33		t.Fatalf("failed to load BPF program: %v", err)
34	}
35	defer done()
36
37	out, err := vm.Run([]byte{
38		0xff, 0xff, 0xff, 0xff,
39		0xff, 0xff, 0xff, 0xff,
40		0, 1, 2, 3,
41	})
42	if err != nil {
43		t.Fatalf("unexpected error while running program: %v", err)
44	}
45	if want, got := 4, out; want != got {
46		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
47			want, got)
48	}
49}
50