prop_emqx_psk.erl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(prop_emqx_psk).
  17. -include_lib("proper/include/proper.hrl").
  18. -define(ALL(Vars, Types, Exprs),
  19. ?SETUP(
  20. fun() ->
  21. State = do_setup(),
  22. fun() -> do_teardown(State) end
  23. end,
  24. ?FORALL(Vars, Types, Exprs)
  25. )
  26. ).
  27. %%--------------------------------------------------------------------
  28. %% Properties
  29. %%--------------------------------------------------------------------
  30. prop_lookup() ->
  31. ?ALL(
  32. {ClientPSKID, UserState},
  33. {client_pskid(), user_state()},
  34. begin
  35. case emqx_tls_psk:lookup(psk, ClientPSKID, UserState) of
  36. {ok, _Result} -> true;
  37. error -> true;
  38. _Other -> false
  39. end
  40. end
  41. ).
  42. %%--------------------------------------------------------------------
  43. %% Helper
  44. %%--------------------------------------------------------------------
  45. do_setup() ->
  46. ok = emqx_logger:set_log_level(emergency),
  47. ok = meck:new(emqx_hooks, [passthrough, no_history]),
  48. ok = meck:expect(
  49. emqx_hooks,
  50. run_fold,
  51. fun('tls_handshake.psk_lookup', [ClientPSKID], not_found) ->
  52. unicode:characters_to_binary(ClientPSKID)
  53. end
  54. ).
  55. do_teardown(_) ->
  56. ok = emqx_logger:set_log_level(error),
  57. ok = meck:unload(emqx_hooks).
  58. %%--------------------------------------------------------------------
  59. %% Generator
  60. %%--------------------------------------------------------------------
  61. client_pskid() -> oneof([string(), integer(), [1, [-1]]]).
  62. user_state() -> term().