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_ret_test.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. package bpf_test
  5. import (
  6. "testing"
  7. "golang.org/x/net/bpf"
  8. )
  9. func TestVMRetA(t *testing.T) {
  10. vm, err := testVM(t, []bpf.Instruction{
  11. bpf.LoadAbsolute{
  12. Off: 8,
  13. Size: 1,
  14. },
  15. bpf.RetA{},
  16. })
  17. if err != nil {
  18. t.Fatalf("failed to load BPF program: %v", err)
  19. }
  20. out, err := vm.Run([]byte{
  21. 0xff, 0xff, 0xff, 0xff,
  22. 0xff, 0xff, 0xff, 0xff,
  23. 9,
  24. })
  25. if err != nil {
  26. t.Fatalf("unexpected error while running program: %v", err)
  27. }
  28. if want, got := uint32(1), out; want != got {
  29. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  30. want, got)
  31. }
  32. }
  33. func TestVMRetALargerThanInput(t *testing.T) {
  34. vm, err := testVM(t, []bpf.Instruction{
  35. bpf.LoadAbsolute{
  36. Off: 8,
  37. Size: 2,
  38. },
  39. bpf.RetA{},
  40. })
  41. if err != nil {
  42. t.Fatalf("failed to load BPF program: %v", err)
  43. }
  44. out, err := vm.Run([]byte{
  45. 0xff, 0xff, 0xff, 0xff,
  46. 0xff, 0xff, 0xff, 0xff,
  47. 0, 255,
  48. })
  49. if err != nil {
  50. t.Fatalf("unexpected error while running program: %v", err)
  51. }
  52. if want, got := uint32(2), out; want != got {
  53. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  54. want, got)
  55. }
  56. }
  57. func TestVMRetConstant(t *testing.T) {
  58. vm, err := testVM(t, []bpf.Instruction{
  59. bpf.RetConstant{
  60. Val: 9,
  61. },
  62. })
  63. if err != nil {
  64. t.Fatalf("failed to load BPF program: %v", err)
  65. }
  66. out, err := vm.Run([]byte{
  67. 0xff, 0xff, 0xff, 0xff,
  68. 0xff, 0xff, 0xff, 0xff,
  69. 0, 1,
  70. })
  71. if err != nil {
  72. t.Fatalf("unexpected error while running program: %v", err)
  73. }
  74. if want, got := uint32(1), out; want != got {
  75. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  76. want, got)
  77. }
  78. }
  79. func TestVMRetConstantLargerThanInput(t *testing.T) {
  80. vm, err := testVM(t, []bpf.Instruction{
  81. bpf.RetConstant{
  82. Val: 16,
  83. },
  84. })
  85. if err != nil {
  86. t.Fatalf("failed to load BPF program: %v", err)
  87. }
  88. out, err := vm.Run([]byte{
  89. 0xff, 0xff, 0xff, 0xff,
  90. 0xff, 0xff, 0xff, 0xff,
  91. 0, 1,
  92. })
  93. if err != nil {
  94. t.Fatalf("unexpected error while running program: %v", err)
  95. }
  96. if want, got := uint32(2), out; want != got {
  97. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  98. want, got)
  99. }
  100. }