emqx_plugin_libs_ssl_tests.erl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021 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(emqx_plugin_libs_ssl_tests).
  17. -include_lib("proper/include/proper.hrl").
  18. -include_lib("eunit/include/eunit.hrl").
  19. no_crash_test_() ->
  20. Opts = [{numtests, 1000}, {to_file, user}],
  21. {timeout, 60,
  22. fun() -> ?assert(proper:quickcheck(prop_run(), Opts)) end}.
  23. prop_run() ->
  24. ?FORALL(Generated, prop_opts_input(), test_opts_input(Generated)).
  25. %% proper type to generate input value.
  26. prop_opts_input() ->
  27. [{keyfile, prop_file_or_content()},
  28. {certfile, prop_file_or_content()},
  29. {cacertfile, prop_file_or_content()},
  30. {verify, proper_types:boolean()},
  31. {versions, prop_tls_versions()},
  32. {ciphers, prop_tls_ciphers()},
  33. {other, proper_types:binary()}].
  34. prop_file_or_content() ->
  35. proper_types:oneof([prop_cert_file_name(),
  36. {prop_cert_file_name(), proper_types:binary()}]).
  37. prop_cert_file_name() ->
  38. proper_types:oneof(["certname1", <<"certname2">>, "", <<>>, undefined]).
  39. prop_tls_versions() ->
  40. proper_types:oneof(["tlsv1.3",
  41. <<"tlsv1.3,tlsv1.2">>,
  42. "tlsv1.2 , tlsv1.1",
  43. "1.2",
  44. "v1.3",
  45. "",
  46. <<>>,
  47. undefined]).
  48. prop_tls_ciphers() ->
  49. proper_types:oneof(["TLS_AES_256_GCM_SHA384,TLS_AES_128_GCM_SHA256",
  50. <<>>,
  51. "",
  52. undefined]).
  53. test_opts_input(Inputs) ->
  54. KF = fun(K) -> {_, V} = lists:keyfind(K, 1, Inputs), V end,
  55. Generated = #{<<"keyfile">> => file_or_content(KF(keyfile)),
  56. <<"certfile">> => file_or_content(KF(certfile)),
  57. <<"cafile">> => file_or_content(KF(cacertfile)),
  58. <<"verify">> => file_or_content(KF(verify)),
  59. <<"tls_versions">> => KF(versions),
  60. <<"ciphers">> => KF(ciphers),
  61. <<"other">> => KF(other)},
  62. _ = emqx_plugin_libs_ssl:save_files_return_opts(Generated, "test-data"),
  63. true.
  64. file_or_content({Name, Content}) ->
  65. #{<<"file">> => Content, <<"filename">> => Name};
  66. file_or_content(Name) ->
  67. Name.