Skip to content

Commit b7a2eae

Browse files
committed
TESTrun: Add an initial test for bpf_image().
For now this is a hack in filtertest.c, later it may be better to have a separate executable for this type of tests.
1 parent 937d055 commit b7a2eae

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

testprogs/TESTrun

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,63 @@ sub skip_little_endian {
368368
# the expression)
369369

370370
my @accept_blocks = (
371+
{
372+
name => 'bpf_image',
373+
DLT => 'EN10MB',
374+
aliases => ['_enumerate_bpf_image'],
375+
opt => undef,
376+
unopt => '
377+
(000) ld #0xabcd ; 0x0000
378+
(000) ldx #0xabcd ; 0x0001
379+
(000) st M[43981] ; 0x0002
380+
(000) stx M[43981] ; 0x0003
381+
(000) add #43981 ; 0x0004
382+
(000) ja 43982 ; 0x0005
383+
(000) ret #43981 ; 0x0006
384+
(000) tax ; 0x0007
385+
(000) add x ; 0x000c
386+
(000) sub #43981 ; 0x0014
387+
(000) jeq #0xabcd jt 172 jf 206 ; 0x0015
388+
(000) ret ; 0x0016
389+
(000) sub x ; 0x001c
390+
(000) jeq x jt 172 jf 206 ; 0x001d
391+
(000) ld [43981] ; 0x0020
392+
(000) mul #43981 ; 0x0024
393+
(000) jgt #0xabcd jt 172 jf 206 ; 0x0025
394+
(000) ldh [43981] ; 0x0028
395+
(000) mul x ; 0x002c
396+
(000) jgt x jt 172 jf 206 ; 0x002d
397+
(000) ldb [43981] ; 0x0030
398+
(000) div #43981 ; 0x0034
399+
(000) jge #0xabcd jt 172 jf 206 ; 0x0035
400+
(000) div x ; 0x003c
401+
(000) jge x jt 172 jf 206 ; 0x003d
402+
(000) ld [x + 43981] ; 0x0040
403+
(000) or #0xabcd ; 0x0044
404+
(000) jset #0xabcd jt 172 jf 206 ; 0x0045
405+
(000) ldh [x + 43981] ; 0x0048
406+
(000) or x ; 0x004c
407+
(000) jset x jt 172 jf 206 ; 0x004d
408+
(000) ldb [x + 43981] ; 0x0050
409+
(000) and #0xabcd ; 0x0054
410+
(000) and x ; 0x005c
411+
(000) ld M[43981] ; 0x0060
412+
(000) ldx M[43981] ; 0x0061
413+
(000) lsh #43981 ; 0x0064
414+
(000) lsh x ; 0x006c
415+
(000) rsh #43981 ; 0x0074
416+
(000) rsh x ; 0x007c
417+
(000) ld #pktlen ; 0x0080
418+
(000) neg ; 0x0084
419+
(000) txa ; 0x0087
420+
(000) mod #43981 ; 0x0094
421+
(000) mod x ; 0x009c
422+
(000) xor #0xabcd ; 0x00a4
423+
(000) xor x ; 0x00ac
424+
(000) ldxb 4*([43981]&0xf) ; 0x00b1
425+
',
426+
},
427+
371428
{
372429
name => 'empty',
373430
DLT => 'EN10MB',

testprogs/filtertest.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ The Regents of the University of California. All rights reserved.\n";
5858

5959
#define MAXIMUM_SNAPLEN 262144
6060
#define MAX_STDIN (64 * 1024)
61+
#define BPF_IMAGE_UNIMPL "(000) unimp"
62+
#define BPF_IMAGE_ARGV "_enumerate_bpf_image"
6163

6264
#ifdef BDEBUG
6365
/*
@@ -257,6 +259,22 @@ copy_argv(char **argv)
257259
dst[-1] = '\0';
258260
}
259261

262+
static void
263+
enumerate_bpf_image(void)
264+
{
265+
struct bpf_insn insn = {
266+
.code = 0x0000,
267+
.jt = 0xab,
268+
.jf = 0xcd,
269+
.k = 0xabcd,
270+
};
271+
do {
272+
const char *image = bpf_image(&insn, 0);
273+
if (strncmp(image, BPF_IMAGE_UNIMPL, sizeof(BPF_IMAGE_UNIMPL) - 1))
274+
printf("%-50s; 0x%04x\n", image, insn.code);
275+
} while (insn.code++ != UINT16_MAX);
276+
}
277+
260278
int
261279
main(int argc, char **argv)
262280
{
@@ -432,6 +450,12 @@ main(int argc, char **argv)
432450
else
433451
read_stdin();
434452

453+
if (! strcmp(BPF_IMAGE_ARGV, cmdbuf)) {
454+
enumerate_bpf_image();
455+
cleanup();
456+
exit(EX_OK);
457+
}
458+
435459
if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
436460
error(EX_DATAERR, "%s", pcap_geterr(pd));
437461

0 commit comments

Comments
 (0)