Berkeley Packet Filter (BPF) assembler.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vm_extension_test.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2019 Wijnand Modderman-Lenstra. All rights reserved.
  2. // Copyright (c) 2016 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package bpf_test
  6. import (
  7. "testing"
  8. "golang.org/x/net/bpf"
  9. )
  10. func TestVMLoadExtensionNotImplemented(t *testing.T) {
  11. _, _, err := testVM(t, []bpf.Instruction{
  12. bpf.LoadExtension{
  13. Num: 100,
  14. },
  15. bpf.RetA{},
  16. })
  17. if errStr(err) != "extension 100 not implemented" {
  18. t.Fatalf("unexpected error: %v", err)
  19. }
  20. }
  21. func TestVMLoadExtensionExtLen(t *testing.T) {
  22. vm, done, err := testVM(t, []bpf.Instruction{
  23. bpf.LoadExtension{
  24. Num: bpf.ExtLen,
  25. },
  26. bpf.RetA{},
  27. })
  28. if err != nil {
  29. t.Fatalf("failed to load BPF program: %v", err)
  30. }
  31. defer done()
  32. out, err := vm.Run([]byte{
  33. 0xff, 0xff, 0xff, 0xff,
  34. 0xff, 0xff, 0xff, 0xff,
  35. 0, 1, 2, 3,
  36. })
  37. if err != nil {
  38. t.Fatalf("unexpected error while running program: %v", err)
  39. }
  40. if want, got := 4, out; want != got {
  41. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  42. want, got)
  43. }
  44. }