Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CC?=gcc | ||
PROVE?=prove | ||
|
||
test: build_test | ||
$(PROVE) -e '/bin/sh -c' ./test | ||
|
||
build_test: test.o | ||
$(CC) $(LDFLAGS) -o test $^ | ||
|
||
%.o: %.c | ||
$(CC) $(CFLAGS) -c $< | ||
|
||
clean: | ||
rm -f *.o | ||
rm -f test_request | ||
rm -f test_response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "hex.h" | ||
|
||
static void plan(int num) { | ||
printf("1..%d\n", num); | ||
} | ||
|
||
static void ok(int ok, const char* msg) { | ||
static int testnum = 0; | ||
printf("%s %d - %s\n", ok ? "ok" : "not ok", ++testnum, msg); | ||
} | ||
|
||
static void test_encode(void) { | ||
unsigned char binary[] = { | ||
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef | ||
}; | ||
unsigned char hexstring[sizeof(binary) * 2 + 1]; | ||
|
||
hex(binary, sizeof(binary), hexstring); | ||
|
||
ok(0 == strncmp(hexstring, "0123456789abcdef", 16), "encode ok"); | ||
} | ||
|
||
static void test_decode(void) { | ||
unsigned char hexstring[] = "0123456789abcdef"; | ||
unsigned char binary[strlen(hexstring) / 2]; | ||
unsigned char expected[] = { | ||
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef | ||
}; | ||
|
||
unhex(hexstring, strlen(hexstring), binary); | ||
|
||
ok(0 == memcmp(binary, expected, strlen(hexstring)/2), "decode ok"); | ||
} | ||
|
||
int main(void) { | ||
plan(2); | ||
|
||
test_encode(); | ||
test_decode(); | ||
|
||
return 0; | ||
} | ||
|