summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPeter Bex <peter@more-magic.net>2013-05-08 00:23:27 +0200
committerPeter Bex <peter@more-magic.net>2016-03-04 21:38:49 +0100
commitd12e9d2435c2f45583deeac671772d0229c22e0e (patch)
tree281609e305c3651c789e91708812fd435e59cdea /tests
parent5a9446604f9fea441fd20d3c37eace5329307ab4 (diff)
downloadbpf-d12e9d2435c2f45583deeac671772d0229c22e0e.tar.gz
Fix a few problems with the jump instructions (forgot to add #x05 to all of the conditional ones, too), make uint? and label? also recognise zero as valid and fix error expression of disassembler.
Implement back-and-forth transcoding of tcpdump's "decimal encoding" output of compiled packet matching instructions (using -ddd). Fixed a few bugs I ran into with bitstring, so depend on the version to be released next (this code triggers error situations in the current version)
Diffstat (limited to 'tests')
-rw-r--r--tests/run.scm99
1 files changed, 91 insertions, 8 deletions
diff --git a/tests/run.scm b/tests/run.scm
index fc7c3d6..b0e8e46 100644
--- a/tests/run.scm
+++ b/tests/run.scm
@@ -1,4 +1,4 @@
-(use test matchable)
+(use test matchable bitstring srfi-13)
(include "../bpf-assembler.scm")
@@ -21,12 +21,13 @@
;; store accumulator/index register
(st (mem 1)) (stx (mem 1))
;; jump unconditionally (sometimes called JA instead of JMP)
- (jmp 1)
+ (jmp 1) (jmp 0)
;; Jump conditionally on comparison of acc w/ immediate or idx
- (jeq 1 2 3) (jeq x 2 3) (jgt 1 2 3) (jgt x 2 3)
- (jge 1 2 3) (jge x 2 3)
+ (jeq 0 2 3) (jeq 1 2 3) (jeq x 2 3)
+ (jgt 0 2 3) (jgt 1 2 3) (jgt x 2 3)
+ (jge 0 2 3) (jge 1 2 3) (jge x 2 3)
;; Jump if bit # from immediate/index register is set in acc
- (jset 1 2 3) (jset x 2 3)
+ (jset 0 2 3) (jset 1 2 3) (jset x 2 3)
;; ALU instructions: operates on acc w/ immediate or index
(add 1) (add x) (sub 1) (sub x) (mul 1) (mul x) (div 1) (div x)
(or 1) (or x) (and 1) (and x) (lsh 1) (lsh x) (rsh 1) (rsh x)
@@ -63,9 +64,9 @@
(stx 1) (stx len) (stx a) (stx x) (stx (mem x 1))
(stx (pkt 1)) (stx (pkt 4* 1)))
- ("bogus jump types"
- (jmp x) (jmp a) (jmp (mem 1)) (jmp len))
-
+ ("bogus jump types and negative labels"
+ (jmp x) (jmp a) (jmp (mem 1)) (jmp len) (jmp -1) (jeq -1 1 2))
+
("unconditional jumps can't work conditionally"
(jmp 1 2 3) (jmp x 2 3))
@@ -87,6 +88,88 @@
("returning non-immediate, non-register values"
(ret (mem 1)) (ret (pkt 1)) (ret (pkt x 1)) (ret (pkt 4* 1))))))
+(test-group "multi-instruction assembly/disassembly invariance"
+ (for-each (match-lambda
+ ((description . exprs)
+ (test description
+ exprs
+ (bpf-bytecode->exprs (exprs->bpf-bytecode exprs)))))
+ `(("simple load and return" (ld 1234) (ret 10))
+
+ ("src localhost program"
+ (ldh (pkt 12))
+ (jeq #x0800 0 2)
+ (ld (pkt 26))
+ (jeq #x7f000001 4 5)
+ (jeq #x0806 1 0)
+ (jeq #x8035 0 3)
+ (ld (pkt 28))
+ (jeq #x7f000001 0 1)
+ (ret 65535)
+ (ret 0)))))
+
+(test-group "decimal bytecode reader"
+ (test "src localhost"
+ `((ldh (pkt 12))
+ (jeq #x0800 0 2)
+ (ld (pkt 26))
+ (jeq #x7f000001 4 5)
+ (jeq #x0806 1 0)
+ (jeq #x8035 0 3)
+ (ld (pkt 28))
+ (jeq #x7f000001 0 1)
+ (ret 65535)
+ (ret 0))
+ ;; String generated w/ tcpdump -ddd src localhost
+ (bpf-bytecode->exprs
+ (with-input-from-string
+ (string-join
+ `("10"
+ "40 0 0 12"
+ "21 0 2 2048"
+ "32 0 0 26"
+ "21 4 5 2130706433"
+ "21 1 0 2054"
+ "21 0 3 32821"
+ "32 0 0 28"
+ "21 0 1 2130706433"
+ "6 0 0 65535"
+ "6 0 0 0")
+ "\n" 'suffix)
+ (lambda () (read-decimal-bpf-bytecode))))))
+
+(test-group "decimal bytecode writer"
+ (test "src localhost"
+ ;; String generated w/ tcpdump -ddd src localhost
+ (string-join
+ `("10"
+ "40 0 0 12"
+ "21 0 2 2048"
+ "32 0 0 26"
+ "21 4 5 2130706433"
+ "21 1 0 2054"
+ "21 0 3 32821"
+ "32 0 0 28"
+ "21 0 1 2130706433"
+ "6 0 0 65535"
+ "6 0 0 0")
+ "\n" 'suffix)
+ (with-output-to-string
+ (lambda ()
+ (write-decimal-bpf-bytecode
+ (exprs->bpf-bytecode
+ `((ldh (pkt 12))
+ (jeq #x0800 0 2)
+ (ld (pkt 26))
+ (jeq #x7f000001 4 5)
+ (jeq #x0806 1 0)
+ (jeq #x8035 0 3)
+ (ld (pkt 28))
+ (jeq #x7f000001 0 1)
+ (ret 65535)
+ (ret 0))))))
+))
+
(test-end "BPF assembler")
(test-exit)