summaryrefslogtreecommitdiff
path: root/src/aegis/test/aegis_server_test.erl
blob: 0f96798b7f770b605a7064168df595222b89bf4f (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
% Licensed 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(aegis_server_test).

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

-define(DB, #{uuid => <<0:64>>}).
-define(VALUE, <<0:8>>).
-define(ENCRYPTED, <<1,155,242,89,190,54,112,151,18,145,25,251,217,
    49,147,125,14,162,146,201,189,100,232,38,239,111,163,84,25,60,
    147,167,237,107,24,204,171,232,227,16,72,203,101,118,150,252,
    204,80,245,66,98,213,223,63,111,105,101,154>>).
-define(TIMEOUT, 10000).



basic_test_() ->
    {
        foreach,
        fun setup/0,
        fun teardown/1,
        [
            {"init_db returns true when encryption enabled",
            {timeout, ?TIMEOUT, fun test_init_db/0}},
            {"open_db returns true when encryption enabled",
            {timeout, ?TIMEOUT, fun test_open_db/0}},
            {"init_db caches key",
            {timeout, ?TIMEOUT, fun test_init_db_cache/0}},
            {"open_db caches key",
            {timeout, ?TIMEOUT, fun test_open_db_cache/0}},
            {"encrypt fetches and caches key when it's missing",
            {timeout, ?TIMEOUT, fun test_encrypt_cache/0}},
            {"decrypt fetches and caches key when it's missing",
            {timeout, ?TIMEOUT, fun test_decrypt_cache/0}}
        ]
    }.


setup() ->
    Ctx = test_util:start_couch([fabric]),
    meck:new([?AEGIS_KEY_MANAGER], [passthrough]),
    ok = meck:expect(?AEGIS_KEY_MANAGER, init_db, 2, {ok, <<0:256>>}),
    ok = meck:expect(?AEGIS_KEY_MANAGER, open_db, 1, {ok, <<0:256>>}),
    Ctx.


teardown(Ctx) ->
    meck:unload(),
    test_util:stop_couch(Ctx).


test_init_db() ->
    ?assert(aegis_server:init_db(?DB, [])),
    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, init_db, 2)).


test_open_db() ->
    ?assert(aegis_server:open_db(?DB)),
    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_init_db_cache() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, init_db, 2)),

    ?assert(aegis_server:init_db(?DB, [])),

    lists:foreach(fun(I) ->
        Encrypted = aegis_server:encrypt(?DB, <<I:64>>, ?VALUE),
        ?assertNotEqual(?VALUE, Encrypted),
        ?assertMatch(<<1:8, _/binary>>, Encrypted)
    end, lists:seq(1, 12)),

    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, init_db, 2)).


test_open_db_cache() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    ?assert(aegis_server:open_db(?DB)),

    lists:foreach(fun(I) ->
        Encrypted = aegis_server:encrypt(?DB, <<I:64>>, ?VALUE),
        ?assertNotEqual(?VALUE, Encrypted),
        ?assertMatch(<<1:8, _/binary>>, Encrypted)
    end, lists:seq(1, 12)),

    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_encrypt_cache() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    Encrypted = aegis_server:encrypt(?DB, <<1:64>>, ?VALUE),
    ?assertNotEqual(?VALUE, Encrypted),
    ?assertMatch(<<1:8, _/binary>>, Encrypted),

    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_decrypt_cache() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    Decrypted = aegis_server:decrypt(?DB, <<1:64>>, ?ENCRYPTED),
    ?assertEqual(<<0>>, Decrypted),

    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).



disabled_test_() ->
    {
        foreach,
        fun() ->
            Ctx = setup(),
            ok = meck:delete(?AEGIS_KEY_MANAGER, init_db, 2),
            ok = meck:expect(?AEGIS_KEY_MANAGER, init_db, 2, false),
            ok = meck:delete(?AEGIS_KEY_MANAGER, open_db, 1),
            ok = meck:expect(?AEGIS_KEY_MANAGER, open_db, 1, false),
            Ctx
        end,
        fun teardown/1,
        [
            {"init_db returns false when encryptions disabled",
            {timeout, ?TIMEOUT, fun test_disabled_init_db/0}},
            {"open_db returns false when encryptions disabled",
            {timeout, ?TIMEOUT, fun test_disabled_open_db/0}},
            {"pass through on encrypt when encryption disabled",
            {timeout, ?TIMEOUT, fun test_disabled_encrypt/0}},
            {"pass through on decrypt when encryption disabled",
            {timeout, ?TIMEOUT, fun test_disabled_decrypt/0}}
        ]
    }.


test_disabled_init_db() ->
    ?assertNot(aegis_server:init_db(?DB, [])),
    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, init_db, 2)).


test_disabled_open_db() ->
    ?assertNot(aegis_server:open_db(?DB)),
    ?assertEqual(1, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_disabled_encrypt() ->
    Db = ?DB#{is_encrypted => aegis_server:open_db(?DB)},
    Encrypted = aegis:encrypt(Db, <<1:64>>, ?VALUE),
    ?assertEqual(?VALUE, Encrypted).


test_disabled_decrypt() ->
    Db = ?DB#{is_encrypted => aegis_server:open_db(?DB)},
    Decrypted = aegis:decrypt(Db, <<1:64>>, ?ENCRYPTED),
    ?assertEqual(?ENCRYPTED, Decrypted).



lru_cache_with_expiration_test_() ->
    {
        foreach,
        fun() ->
            %% this has to be be set before start of aegis server
            %% for config param "cache_expiration_check_sec" to be picked up
            meck:new([config, aegis_server, fabric2_util], [passthrough]),
            ok = meck:expect(config, get_integer, fun
                ("aegis", "cache_limit", _) -> 5;
                ("aegis", "cache_max_age_sec", _) -> 130;
                ("aegis", "cache_expiration_check_sec", _) -> 1;
                (_, _, Default) -> Default
            end),
            Ctx = setup(),
            ok = meck:expect(fabric2_util, now, fun(sec) ->
                get(time) == undefined andalso put(time, 10),
                Now = get(time),
                put(time, Now + 10),
                Now
            end),
            Ctx
        end,
        fun teardown/1,
        [
            {"counter moves forward on access bump",
            {timeout, ?TIMEOUT, fun test_advance_counter/0}},
            {"oldest entries evicted",
            {timeout, ?TIMEOUT, fun test_evict_old_entries/0}},
            {"access bump preserves entries",
            {timeout, ?TIMEOUT, fun test_bump_accessed/0}},
            {"expired entries removed",
            {timeout, ?TIMEOUT, fun test_remove_expired/0}}
        ]
    }.


test_advance_counter() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    ok = meck:expect(aegis_server, handle_cast, fun({accessed, _} = Msg, St) ->
        #{counter := Counter} = St,
        get(counter) == undefined andalso put(counter, 0),
        OldCounter = get(counter),
        put(counter, Counter),
        ?assert(Counter > OldCounter),
        meck:passthrough([Msg, St])
    end),

    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<I:64>>, ?VALUE),
        aegis_server:encrypt(Db, <<(I+1):64>>, ?VALUE)
    end, lists:seq(1, 10)),

    ?assertEqual(10, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_evict_old_entries() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% overflow cache
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<I:64>>, ?VALUE)
    end, lists:seq(1, 10)),

    ?assertEqual(10, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% confirm that newest keys are still in cache
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<(I+1):64>>, ?VALUE)
    end, lists:seq(6, 10)),

    ?assertEqual(10, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% confirm that oldest keys been eviced and needed re-fetch
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<(I+1):64>>, ?VALUE)
    end, lists:seq(1, 5)),

    ?assertEqual(15, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_bump_accessed() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% fill the cache
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<I:64>>, ?VALUE)
    end, lists:seq(1, 5)),

    ?assertEqual(5, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% bump oldest key and then insert a new key to trigger eviction
    aegis_server:encrypt(?DB#{uuid => <<1:64>>}, <<1:64>>, ?VALUE),
    aegis_server:encrypt(?DB#{uuid => <<6:64>>}, <<6:64>>, ?VALUE),
    ?assertEqual(6, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% confirm that former oldest key is still in cache
    aegis_server:encrypt(?DB#{uuid => <<1:64>>}, <<2:64>>, ?VALUE),
    ?assertEqual(6, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% confirm that the second oldest key been evicted by the new insert
    aegis_server:encrypt(?DB#{uuid => <<2:64>>}, <<3:64>>, ?VALUE),
    ?assertEqual(7, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).


test_remove_expired() ->
    ?assertEqual(0, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% to detect when maybe_remove_expired called
    ok = meck:expect(aegis_server, handle_info,fun
        (maybe_remove_expired, St) ->
            meck:passthrough([maybe_remove_expired, St])
    end),

    %% fill the cache. first key expires a 140, last at 180 of "our" time
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<I:64>>, ?VALUE)
    end, lists:seq(1, 5)),

    ?assertEqual(5, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% confirm enties are still in cache and wind up our "clock" to 160
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<I:64>>, ?VALUE)
    end, lists:seq(1, 5)),

    ?assertEqual(5, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)),

    %% wait for remove_expired_entries to be triggered
    meck:reset(aegis_server),
    meck:wait(aegis_server, handle_info, [maybe_remove_expired, '_'], 2500),

    %% 3 "oldest" entries should be removed, 2 yet to expire still in cache
    lists:foreach(fun(I) ->
        Db = ?DB#{uuid => <<I:64>>},
        aegis_server:encrypt(Db, <<I:64>>, ?VALUE)
    end, lists:seq(1, 5)),

    ?assertEqual(8, meck:num_calls(?AEGIS_KEY_MANAGER, open_db, 1)).