<commit_before>%% -------------------------------------------------------------------
%%
%% gen_utp_tests: tests for gen_utp and the utpdrv driver
%%
%% Copyright (c) 2012-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(gen_utp_tests).
-author('<NAME> <<EMAIL>>').

-include_lib("eunit/include/eunit.hrl").

setup() ->
    gen_utp:start_link().

cleanup(_) ->
    gen_utp:stop(),
    Check = fun(F) ->
                    case whereis(gen_utp) of
                        undefined -> ok;
                        _ -> F(F)
                    end
            end,
    Check(Check).

port_number_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {"uTP port number range test",
              fun port_number_range/0}
     end}.

port_number_range() ->
    Error = {error, function_clause},
    ?assertMatch(Error,
                 try gen_utp:listen(-1) catch C1:R1 -> {C1,R1} end),
    ?assertMatch(Error,
                 try gen_utp:listen(65536) catch C2:R2 -> {C2,R2} end),
    ?assertMatch(Error,
                 try gen_utp:connect("localhost", -1)
                 catch C3:R3 -> {C3,R3} end),
    ?assertMatch(Error,
                 try gen_utp:connect("localhost", 0) catch C4:R4 -> {C4,R4} end),
    ?assertMatch(Error,
                 try gen_utp:connect("localhost", 65536)
                 catch C5:R5 -> {C5,R5} end),
    ok.

listen_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {inorder,
              [{"uTP simple listen test",
                fun simple_listen/0},
               {"uTP listen not connected test",
                fun listen_notconn/0},
               {"uTP two listen test",
                fun two_listen/0},
               {"uTP specific interface listen test",
                fun specific_interface/0},
               {"uTP async accept test",
                fun async_accept/0},
               {"uTP accept timeout test",
                fun accept_timeout/0}
              ]}
     end}.

simple_listen() ->
    Ports = length(erlang:ports()),
    {ok, LSock} = gen_utp:listen(0),
    ?assert(erlang:is_port(LSock)),
    ?assertEqual(Ports+1, length(erlang:ports())),
    Self = self(),
    ?assertMatch({connected, Self}, erlang:port_info(LSock, connected)),
    ?assertMatch({error, enotconn}, gen_utp:send(LSock, <<"send">>)),
    {ok, {Addr, Port}} = gen_utp:sockname(LSock),
    ?assert(is_tuple(Addr)),
    ?assert(is_number(Port)),
    ?assertEqual({ok, Port}, gen_utp:port(LSock)),
    ?assertMatch({error, enotconn}, gen_utp:peername(LSock)),
    ?assertMatch(ok, gen_utp:close(LSock)),
    ?assertMatch(undefined, erlang:port_info(LSock)),
    ?assertEqual(Ports, length(erlang:ports())),
    ok.

listen_notconn() ->
    {ok, LSock} = gen_utp:listen(0),
    ?assertMatch({error,enotconn}, gen_utp:send(LSock, "data")),
    ?assertMatch({error,enotconn}, gen_utp:recv(LSock, 0, 1000)),
    ok.

two_listen() ->
    {ok, LSock1} = gen_utp:listen(0),
    {ok, {_, Port1}} = gen_utp:sockname(LSock1),
    ?assertMatch(ok, gen_utp:close(LSock1)),
    {ok, LSock2} = gen_utp:listen(Port1),
    ?assertNotEqual(LSock1, LSock2),
    {ok, {_, Port2}} = gen_utp:sockname(LSock2),
    ?assertEqual(Port1, Port2),
    {ok, LSock3} = gen_utp:listen(0),
    {ok, {_, Port3}} = gen_utp:sockname(LSock3),
    ?assertNotEqual(Port2, Port3),
    ?assertMatch(ok, gen_utp:close(LSock2)),
    ok.

specific_interface() ->
    {ok, LSock1} = gen_utp:listen(0, [{ip, "127.0.0.1"}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock1)),
    ?assertMatch(ok, gen_utp:close(LSock1)),
    {ok, LSock2} = gen_utp:listen(0, [{ifaddr, "127.0.0.1"}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock2)),
    ?assertMatch(ok, gen_utp:close(LSock2)),
    {ok, LSock3} = gen_utp:listen(0, [{ip, {127,0,0,1}}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock3)),
    ?assertMatch(ok, gen_utp:close(LSock3)),
    {ok, LSock4} = gen_utp:listen(0, [{ifaddr, {127,0,0,1}}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock4)),
    ?assertMatch(ok, gen_utp:close(LSock4)),
    ok.

async_accept() ->
    {ok, LSock} = gen_utp:listen(0),
    ?assertMatch(ok, gen_utp:async_accept(LSock)),
    ok.

accept_timeout() ->
    {ok, LSock} = gen_utp:listen(0),
    ?assertMatch({error, etimedout}, gen_utp:accept(LSock, 2000)),
    ok.

client_timeout_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {timeout, 15,
              [{"uTP client timeout test",
                ?_test(
                   begin
                       {ok, LSock} = gen_utp:listen(0),
                       {ok, {_, Port}} = gen_utp:sockname(LSock),
                       ok = gen_utp:close(LSock),
                       ?assertMatch({error, etimedout},
                                    gen_utp:connect("localhost", Port))
                   end)}
              ]}
     end}.

client_server_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {inorder,
              [{"uTP simple connect test",
                fun simple_connect/0},
               {"uTP simple send binary test, active true",
                fun() -> simple_send(binary, true) end},
               {"uTP simple send binary test, active once",
                fun() -> simple_send(binary, once) end},
               {"uTP simple send binary test, active false",
                fun() -> simple_send(binary, false) end},
               {"uTP simple send list test",
                fun() -> simple_send(list, true) end},
               {"uTP two clients test",
                fun two_clients/0},
               {"uTP client large send",
                fun large_send/0},
               {"uTP two servers test",
                fun two_servers/0},
               {"uTP send timeout test",
                fun send_timeout/0},
               {"uTP invalid accept test",
                fun invalid_accept/0},
               {"uTP packet size test",
                fun packet_size/0}
              ]}
     end}.

simple_connect() ->
    Self = self(),
    Ref = make_ref(),
    spawn_link(fun() -> ok = simple_connect_server(Self, Ref) end),<commit_msg>minor change to test description strings<commit_after>%% -------------------------------------------------------------------
%%
%% gen_utp_tests: tests for gen_utp and the utpdrv driver
%%
%% Copyright (c) 2012-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(gen_utp_tests).
-author('<NAME> <<EMAIL>>').

-include_lib("eunit/include/eunit.hrl").

setup() ->
    gen_utp:start_link().

cleanup(_) ->
    gen_utp:stop(),
    Check = fun(F) ->
                    case whereis(gen_utp) of
                        undefined -> ok;
                        _ -> F(F)
                    end
            end,
    Check(Check).

port_number_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {"port number range test",
              fun port_number_range/0}
     end}.

port_number_range() ->
    Error = {error, function_clause},
    ?assertMatch(Error,
                 try gen_utp:listen(-1) catch C1:R1 -> {C1,R1} end),
    ?assertMatch(Error,
                 try gen_utp:listen(65536) catch C2:R2 -> {C2,R2} end),
    ?assertMatch(Error,
                 try gen_utp:connect("localhost", -1)
                 catch C3:R3 -> {C3,R3} end),
    ?assertMatch(Error,
                 try gen_utp:connect("localhost", 0) catch C4:R4 -> {C4,R4} end),
    ?assertMatch(Error,
                 try gen_utp:connect("localhost", 65536)
                 catch C5:R5 -> {C5,R5} end),
    ok.

listen_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {inorder,
              [{"simple listen test",
                fun simple_listen/0},
               {"listen not connected test",
                fun listen_notconn/0},
               {"two listen test",
                fun two_listen/0},
               {"specific interface listen test",
                fun specific_interface/0},
               {"async accept test",
                fun async_accept/0},
               {"accept timeout test",
                fun accept_timeout/0}
              ]}
     end}.

simple_listen() ->
    Ports = length(erlang:ports()),
    {ok, LSock} = gen_utp:listen(0),
    ?assert(erlang:is_port(LSock)),
    ?assertEqual(Ports+1, length(erlang:ports())),
    Self = self(),
    ?assertMatch({connected, Self}, erlang:port_info(LSock, connected)),
    ?assertMatch({error, enotconn}, gen_utp:send(LSock, <<"send">>)),
    {ok, {Addr, Port}} = gen_utp:sockname(LSock),
    ?assert(is_tuple(Addr)),
    ?assert(is_number(Port)),
    ?assertEqual({ok, Port}, gen_utp:port(LSock)),
    ?assertMatch({error, enotconn}, gen_utp:peername(LSock)),
    ?assertMatch(ok, gen_utp:close(LSock)),
    ?assertMatch(undefined, erlang:port_info(LSock)),
    ?assertEqual(Ports, length(erlang:ports())),
    ok.

listen_notconn() ->
    {ok, LSock} = gen_utp:listen(0),
    ?assertMatch({error,enotconn}, gen_utp:send(LSock, "data")),
    ?assertMatch({error,enotconn}, gen_utp:recv(LSock, 0, 1000)),
    ok.

two_listen() ->
    {ok, LSock1} = gen_utp:listen(0),
    {ok, {_, Port1}} = gen_utp:sockname(LSock1),
    ?assertMatch(ok, gen_utp:close(LSock1)),
    {ok, LSock2} = gen_utp:listen(Port1),
    ?assertNotEqual(LSock1, LSock2),
    {ok, {_, Port2}} = gen_utp:sockname(LSock2),
    ?assertEqual(Port1, Port2),
    {ok, LSock3} = gen_utp:listen(0),
    {ok, {_, Port3}} = gen_utp:sockname(LSock3),
    ?assertNotEqual(Port2, Port3),
    ?assertMatch(ok, gen_utp:close(LSock2)),
    ok.

specific_interface() ->
    {ok, LSock1} = gen_utp:listen(0, [{ip, "127.0.0.1"}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock1)),
    ?assertMatch(ok, gen_utp:close(LSock1)),
    {ok, LSock2} = gen_utp:listen(0, [{ifaddr, "127.0.0.1"}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock2)),
    ?assertMatch(ok, gen_utp:close(LSock2)),
    {ok, LSock3} = gen_utp:listen(0, [{ip, {127,0,0,1}}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock3)),
    ?assertMatch(ok, gen_utp:close(LSock3)),
    {ok, LSock4} = gen_utp:listen(0, [{ifaddr, {127,0,0,1}}]),
    ?assertMatch({ok, {{127,0,0,1}, _}}, gen_utp:sockname(LSock4)),
    ?assertMatch(ok, gen_utp:close(LSock4)),
    ok.

async_accept() ->
    {ok, LSock} = gen_utp:listen(0),
    ?assertMatch(ok, gen_utp:async_accept(LSock)),
    ok.

accept_timeout() ->
    {ok, LSock} = gen_utp:listen(0),
    ?assertMatch({error, etimedout}, gen_utp:accept(LSock, 2000)),
    ok.

client_timeout_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {timeout, 15,
              [{"client timeout test",
                ?_test(
                   begin
                       {ok, LSock} = gen_utp:listen(0),
                       {ok, {_, Port}} = gen_utp:sockname(LSock),
                       ok = gen_utp:close(LSock),
                       ?assertMatch({error, etimedout},
                                    gen_utp:connect("localhost", Port))
                   end)}
              ]}
     end}.

client_server_test_() ->
    {setup,
     fun setup/0,
     fun cleanup/1,
     fun(_) ->
             {inorder,
              [{"simple connect test",
                fun simple_connect/0},
               {"simple send binary test, active true",
                fun() -> simple_send(binary, true) end},
               {"simple send binary test, active once",
                fun() -> simple_send(binary, once) end},
               {"simple send binary test, active false",
                fun() -> simple_send(binary, false) end},
               {"simple send list test",
                fun() -> simple_send(list, true) end},
               {"two clients test",
                fun two_clients/0},
               {"client large send",
                fun large_send/0},
               {"two servers test",
                fun two_servers/0},
               {"send timeout test",
                fun send_timeout/0},
               {"invalid accept test",
                fun invalid_accept/0},
               {"packet size test",
                fun packet_size/0}
              ]}
     end}.

simple_connect() ->
    Self = self(),
    Ref = make_ref(),
    spawn_link(fun() -> ok = simple_connect_server(Self, Ref) end),
{'fraction_non_alphanumeric': 0.1699251399084236, 'fraction_numerical': 0.02442037938803692, 'mean_word_length': 2.081746920492721, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1840876', 'n_tokens_mistral': 4645, 'n_tokens_neox': 4461, 'n_words': 975}