summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2015-08-06 16:25:29 +0200
committerLinus Nordberg <linus@nordu.net>2015-08-06 16:25:29 +0200
commit85982c4033da4d54937078f8ae1d9c8fde072372 (patch)
treefcd675332c810a34f4cccabb4cffc34e907c2adb
parenta540dfce3c57c9bf4ead2bfee3fe6241fdba05bc (diff)
Add tlv.erl.
Also, add test/check.erl for unit tests (make check).
-rw-r--r--src/tlv.erl34
-rwxr-xr-xtest/check.erl13
2 files changed, 47 insertions, 0 deletions
diff --git a/src/tlv.erl b/src/tlv.erl
new file mode 100644
index 0000000..b1d3428
--- /dev/null
+++ b/src/tlv.erl
@@ -0,0 +1,34 @@
+%%% Copyright (c) 2015, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+-module(tlv).
+
+-export([encode/2, decode/1]).
+
+-spec encode(binary(), binary()) -> binary().
+encode(Type, Value) ->
+ 4 = byte_size(Type), % FIXME: make Type a 4 octet binary
+ Len = byte_size(Value) + 8,
+ <<Len:32, Type/binary, Value/binary>>.
+
+-spec decode(binary()) -> {binary(), binary(), binary()}.
+decode(Data) ->
+ <<TotalLength:32, Rest1/binary>> = Data,
+ Length = TotalLength - 8,
+ <<Type:4/binary-unit:8, Rest2/binary>> = Rest1,
+ <<Value:Length/binary-unit:8, Rest3/binary>> = Rest2,
+ {Type, Value, Rest3}.
+
+%%%%%%%%%%%%%%%%%%%%
+
+-include_lib("eunit/include/eunit.hrl").
+-define(TESTDATA1, {<<"DATA">>, <<"some data">>}).
+-define(TESTDATA2, {<<"DAT2">>, <<"some other data">>}).
+
+the_test_() ->
+ {setup,
+ fun() -> [?TESTDATA1, ?TESTDATA2] end,
+ fun(_) -> ok end,
+ fun(TestVectors) ->
+ [?_assertEqual({T, V, <<>>}, decode(encode(T, V))) ||
+ {T, V} <- TestVectors] end}.
diff --git a/test/check.erl b/test/check.erl
new file mode 100755
index 0000000..f4fdb8d
--- /dev/null
+++ b/test/check.erl
@@ -0,0 +1,13 @@
+#! /usr/bin/env escript
+%% -*- erlang -*- mode
+%%! -pa ebin -pa ../lager/ebin -pa ../lager/deps/goldrush/ebin -pa ../mochiweb/ebin -config test/check.config
+
+%% To enable logging, pass `-s lager' by adding it to the line above.
+%% Tweak the amount of logging by changing `lager_console_backend' in
+%% check.config.
+
+main(_) ->
+ ok = ht:test(),
+ ok = ts:test(),
+ ok = tlv:test().
+