vm_ret_test.go 2.3 KB

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