Research Article

Behaviour Preservation across Code Versions in Erlang

Algorithm 5

etorrent source files (original and buggy versions).
etorrent_dht_net_old.erl
(1) decode_msg(InMsg) ->
(2)   io:format("0: ~", [InMsg]),
(3)   Msg = etorrent_bcoding_old:decode(InMsg),
(4)   io:format("0: ~", [Msg]),
(5)   MsgID = get_value(<<"t">>, Msg),
(6)   case get_value(<<"y">>, Msg) of
(7)     <<"q">> ->
(8)      MString = get_value(<<"q">>, Msg),
(9)      Method = string_to_method(MString),
(10)      Params = get_value(<<"a">>, Msg),
(11)      Method, MsgID, Params;
(12)    <<"r">> ->
(13)      Values = get_value(<<"r">>, Msg),
(14)      response, MsgID, Values;
(15)    <<"e">> ->
(16)      [ECode, EMsg] = get_value(<<"e">>, Msg),
(17)      error, MsgID, ECode, EMsg
(18)   end.
(19) query_ping_0_test() ->
(20)   Enc = "d1:ad2:id20:abcdefghij0123456789e1:
(21)       q4:ping1:t2:aa1:y1:qe",
(22)   ping, ID, Params = decode_msg(Enc),
(23)   ?assertEqual(<<"aa">>, ID),
(24)   ?assertEqual(
(25)   <<"abcdefghij0123456789">>,
(26)   fetch_id(Params)).
etorrent_dht_net_new.erl
(27) decode_msg(InMsg) ->
(28)   io:format("0: ~", [InMsg]),
(29)   Msg = etorrent_bcoding_new:decode(InMsg),
(30)   io:format("0: ~", [Msg]),
(31)   MsgID = get_value(<<"t">>, Msg),
(32)   case get_value(<<"y">>, Msg) of
(33)    <<"q">> ->
(34)      MString = get_value(<<"q">>, Msg),
(35)      Method = string_to_method(MString),
(36)      Params = get_value(<<"a">>, Msg),
(37)      Method, MsgID, Params;
(38)    <<"r">> ->
(39)      Values = get_value(<<"r">>, Msg),
(40)      response, MsgID, Values;
(41)    <<"e">> ->
(42)      [ECode, EMsg] = get_value(<<"e">>, Msg),
(43)      error, MsgID, ECode, EMsg
(44)   end.
(45) query_ping_0_test() ->
(46)   Enc = "d1:ad2:id20:abcdefghij0123456789e1:
(47)       q4:ping1:t2:aa1:y1:qe",
(48)   ping, ID, Params = decode_msg(Enc),
(49)   ?assertEqual(<<"aa">>, ID),
(50)   ?assertEqual(
(51)   <<"abcdefghij0123456789">>,
(52)   fetch_id(Params)).
etorrent_bcoding_old.erl
(1)  -spec decode(string() binary()) -> bcode().
(2)  decode(Bin) when is_binary(Bin) ->
(3)   decode(binary_to_list(Bin));
(4)  decode(String) when is_list(String) ->
(5)     Res, _Extra = decode_b(String),
(6)     Res.
(7)
etorrent_bcoding_new.erl
(8)  -spec decode(string() binary()) ->
(9)      ok, bcode()error, _Reason.
(10)  decode(Bin) when is_binary(Bin) ->
(11)     decode(binary_to_list(Bin));
(12)  decode(String) when is_list(String) ->
(13)     try
(14)      Res, _Extra = decode_b(String),
(15)      ok, Res
(16)   catch
(17)    error:Reason -> error, Reason
(18)   end.