summaryrefslogtreecommitdiff
path: root/p11p-daemon/src/p11p_config.erl
blob: ed29833fff8bb24d79ca88ce6787c6db30b10ffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-module(p11p_config).
-behaviour(gen_server).

%% API
-export([start_link/0]).
-export([config/0]).
-export([tokens/0]).
-export([modules_for_token/1]).

%% Genserver callbacks.
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

%% Records and types.
-include("p11p_defs.hrl").

%% Genserver state.
-record(state, { tokens :: [token()] }).

%%%%%%%%%%%%%%%%%%%%
%% API.
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

config() ->
    gen_server:call(?MODULE, config).

-spec tokens() -> [token()].
tokens() ->
    gen_server:call(?MODULE, tokens).

-spec modules_for_token(token()) -> [p11module()].
modules_for_token(Token) ->
    gen_server:call(?MODULE, {modules_for_token, Token}).


%%%%%%%%%%%%%%%%%%%%
%% Genserver callbacks.
init(_Args) ->
    State = init_state(),
    {ok, State}.

handle_call(config, _From, State) ->
    {reply, State, State};
handle_call(tokens, _From, #state{tokens = Tokens} = State) ->
    {reply, Tokens, State};
handle_call({modules_for_token, _Token}, _From,
	    #state{tokens = _Tokens} = State) ->
    Reply = [#p11module{name="FIXME", path="FIXME"}],
    {reply, Reply, State};
handle_call(Request, _From, State) ->
    lager:warning("Unhandled call: ~p", [Request]),
    {reply, unhandled, State}.

handle_cast(Message, State) ->
    lager:warning("Unhandled cast: ~p", [Message]),
    {noreply, State}.

handle_info(Info, State) ->
    lager:warning("Unhandled info: ~p", [Info]),
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVersion, State, _Extra) ->
    {ok, State}.

%%%%%%%%%%%%%%%%%%%%
%% Private.

init_state() ->
    #state { 
       tokens = init_tokens(application:get_env(p11p, groups, []))
      }.

init_tokens(Tokens) ->
    [new_token(T) || T <- Tokens].

-spec new_token({string(), [tuple()]}) -> token().
new_token({Name, Settings}) ->
    #token{
       name = Name,
       modules = [new_module(M) || M <- proplists:get_value(modules, Settings, [])]
      }.

new_module({Name, Path}) ->
    #p11module{
       name = Name,
       path = Path
      }.

%%%%%%%%%%%%%%
%% Unit tests.

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

tokens_test_() ->
    {setup,
     fun() -> init_tokens(
		[
		 {"vtoken0", 
		  [{modules, [{"bogusmod0_0", "/path/to/bogusmod0_0"},
			      {"bogusmod0_1", "/path/to/bogusmod0_1"}
			     ]}]},
		 {"vtoken1",
		  [{modules, [{"bogusmod1_0", "/path/to/bogusmod1_0"}]}]}
		]) end,
     fun(_) -> ok end,
     fun(Conf) ->
             [?_assertEqual(
                 [
		  {token,"vtoken0",[{p11module,"bogusmod0_0", "/path/to/bogusmod0_0"},
				    {p11module,"bogusmod0_1", "/path/to/bogusmod0_1"}]},
		  {token,"vtoken1",[{p11module,"bogusmod1_0", "/path/to/bogusmod1_0"}]}
		 ],
		 Conf)] end}.