access test
This commit is contained in:
parent
6aa724ef31
commit
5df03ba938
|
@ -50,7 +50,6 @@ match(User, Topic, [{AllowDeny, all, TopicFilter}|Rules]) ->
|
|||
true -> AllowDeny;
|
||||
false -> match(User, Topic, Rules)
|
||||
end;
|
||||
|
||||
match(User = #mqtt_user{clientid = ClientId}, Topic, [{AllowDeny, ClientId, TopicFilter}|Rules]) when is_binary(ClientId) ->
|
||||
case emqttd_topic:match(Topic, TopicFilter) of
|
||||
true -> AllowDeny;
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
|
||||
-type pubsub() :: publish | subscribe.
|
||||
|
||||
-type who() :: all |
|
||||
-type who() :: all | binary() |
|
||||
{clientid, binary()} |
|
||||
{peername, string() | inet:ip_address()} |
|
||||
{username, binary()}.
|
||||
|
|
|
@ -48,7 +48,7 @@ check(_, undefined) -> false;
|
|||
check(Username, Password) when is_binary(Username), is_binary(Password) ->
|
||||
PasswdHash = crypto:hash(md5, Password),
|
||||
case mnesia:dirty_read(?USER_TAB, Username) of
|
||||
[#mqtt_user{passwdhash=PasswdHash}] -> true;
|
||||
[#mqtt_user{password=PasswdHash}] -> true;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
|
@ -56,7 +56,7 @@ add(Username, Password) when is_binary(Username) and is_binary(Password) ->
|
|||
mnesia:dirty_write(
|
||||
#mqtt_user{
|
||||
username = Username,
|
||||
passwdhash=crypto:hash(md5, Password)
|
||||
password = crypto:hash(md5, Password)
|
||||
}
|
||||
).
|
||||
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
-module(x).
|
||||
-behaviour(gen_server).
|
||||
-define(SERVER, ?MODULE).
|
||||
|
||||
%% ------------------------------------------------------------------
|
||||
%% API Function Exports
|
||||
%% ------------------------------------------------------------------
|
||||
|
||||
-export([start_link/0]).
|
||||
|
||||
%% ------------------------------------------------------------------
|
||||
%% gen_server Function Exports
|
||||
%% ------------------------------------------------------------------
|
||||
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
terminate/2, code_change/3]).
|
||||
|
||||
%% ------------------------------------------------------------------
|
||||
%% API Function Definitions
|
||||
%% ------------------------------------------------------------------
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
|
||||
|
||||
%% ------------------------------------------------------------------
|
||||
%% gen_server Function Definitions
|
||||
%% ------------------------------------------------------------------
|
||||
|
||||
init(Args) ->
|
||||
{ok, Args}.
|
||||
|
||||
handle_call(_Request, _From, State) ->
|
||||
{reply, ok, State}.
|
||||
|
||||
handle_cast(_Msg, State) ->
|
||||
{noreply, State}.
|
||||
|
||||
handle_info(_Info, State) ->
|
||||
{noreply, State}.
|
||||
|
||||
terminate(_Reason, _State) ->
|
||||
ok.
|
||||
|
||||
code_change(_OldVsn, State, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
%% ------------------------------------------------------------------
|
||||
%% Internal Function Definitions
|
||||
%% ------------------------------------------------------------------
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
%%%-----------------------------------------------------------------------------
|
||||
%%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
|
||||
%%%
|
||||
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
%%% of this software and associated documentation files (the "Software"), to deal
|
||||
%%% in the Software without restriction, including without limitation the rights
|
||||
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
%%% copies of the Software, and to permit persons to whom the Software is
|
||||
%%% furnished to do so, subject to the following conditions:
|
||||
%%%
|
||||
%%% The above copyright notice and this permission notice shall be included in all
|
||||
%%% copies or substantial portions of the Software.
|
||||
%%%
|
||||
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
%%% SOFTWARE.
|
||||
%%%-----------------------------------------------------------------------------
|
||||
%%% @doc
|
||||
%%% emqttd_access rules tests.
|
||||
%%%
|
||||
%%% @end
|
||||
%%%-----------------------------------------------------------------------------
|
||||
-module(emqttd_access_tests).
|
||||
|
||||
-include("emqttd.hrl").
|
||||
|
||||
-ifdef(TEST).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(RULES1, [{allow, all}]).
|
||||
-define(RULES2, [{deny, all}]).
|
||||
|
||||
match_test() ->
|
||||
User = #mqtt_user{peername = {127,0,0,1}, clientid = <<"testClient">>, username = <<"TestUser">>},
|
||||
?assertEqual(allow, emqttd_access:match(User, <<"Test/Topic">>, ?RULES1)),
|
||||
?assertEqual(deny, emqttd_access:match(User, <<"Test/Topic">>, ?RULES2)).
|
||||
|
||||
-endif.
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ merge_test() ->
|
|||
?assertEqual(1024, proplists:get_value(backlog, Opts)),
|
||||
?assertEqual(1024, proplists:get_value(max_clients, Opts)),
|
||||
?assertEqual(lists:sort(Opts), [binary, raw,
|
||||
{acceptors,4},
|
||||
{acceptors, 16},
|
||||
{backlog, 1024},
|
||||
{max_clients, 1024},
|
||||
{nodelay, false},
|
||||
|
|
Loading…
Reference in New Issue