emqx_schema_tests.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2017-2022 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_schema_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. ssl_opts_dtls_test() ->
  19. Sc = emqx_schema:server_ssl_opts_schema(#{versions => dtls_all_available,
  20. ciphers => dtls_all_available}, false),
  21. Checked = validate(Sc, #{<<"versions">> => [<<"dtlsv1.2">>, <<"dtlsv1">>]}),
  22. ?assertMatch(#{versions := ['dtlsv1.2', 'dtlsv1'],
  23. ciphers := ["ECDHE-ECDSA-AES256-GCM-SHA384" | _]
  24. }, Checked).
  25. ssl_opts_tls_1_3_test() ->
  26. Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
  27. Checked = validate(Sc, #{<<"versions">> => [<<"tlsv1.3">>]}),
  28. ?assertNot(maps:is_key(handshake_timeout, Checked)),
  29. ?assertMatch(#{versions := ['tlsv1.3'],
  30. ciphers := [_ | _]
  31. }, Checked).
  32. ssl_opts_tls_for_ranch_test() ->
  33. Sc = emqx_schema:server_ssl_opts_schema(#{}, true),
  34. Checked = validate(Sc, #{<<"versions">> => [<<"tlsv1.3">>]}),
  35. ?assertMatch(#{versions := ['tlsv1.3'],
  36. ciphers := [_ | _],
  37. handshake_timeout := _
  38. }, Checked).
  39. ssl_opts_cipher_array_test() ->
  40. Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
  41. Checked = validate(Sc, #{<<"versions">> => [<<"tlsv1.3">>],
  42. <<"ciphers">> => [<<"TLS_AES_256_GCM_SHA384">>,
  43. <<"ECDHE-ECDSA-AES256-GCM-SHA384">>]}),
  44. ?assertMatch(#{versions := ['tlsv1.3'],
  45. ciphers := ["TLS_AES_256_GCM_SHA384", "ECDHE-ECDSA-AES256-GCM-SHA384"]
  46. }, Checked).
  47. ssl_opts_cipher_comma_separated_string_test() ->
  48. Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
  49. Checked = validate(Sc, #{<<"versions">> => [<<"tlsv1.3">>],
  50. <<"ciphers">> => <<"TLS_AES_256_GCM_SHA384,ECDHE-ECDSA-AES256-GCM-SHA384">>}),
  51. ?assertMatch(#{versions := ['tlsv1.3'],
  52. ciphers := ["TLS_AES_256_GCM_SHA384", "ECDHE-ECDSA-AES256-GCM-SHA384"]
  53. }, Checked).
  54. ssl_opts_tls_psk_test() ->
  55. Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
  56. Checked = validate(Sc, #{<<"versions">> => [<<"tlsv1.2">>]}),
  57. ?assertMatch(#{versions := ['tlsv1.2']}, Checked).
  58. bad_cipher_test() ->
  59. Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
  60. Reason = {bad_ciphers, ["foo"]},
  61. ?assertThrow({_Sc, [{validation_error, #{reason := Reason}}]},
  62. validate(Sc, #{<<"versions">> => [<<"tlsv1.2">>],
  63. <<"ciphers">> => [<<"foo">>]})),
  64. ok.
  65. validate(Schema, Data0) ->
  66. Sc = #{ roots => [ssl_opts]
  67. , fields => #{ssl_opts => Schema}
  68. },
  69. Data = Data0#{ cacertfile => <<"cacertfile">>
  70. , certfile => <<"certfile">>
  71. , keyfile => <<"keyfile">>
  72. },
  73. #{ssl_opts := Checked} =
  74. hocon_schema:check_plain(Sc, #{<<"ssl_opts">> => Data},
  75. #{atom_key => true}),
  76. Checked.
  77. ciperhs_schema_test() ->
  78. Sc = emqx_schema:ciphers_schema(undefined),
  79. WSc = #{roots => [{ciphers, Sc}]},
  80. ?assertThrow({_, [{validation_error, _}]},
  81. hocon_schema:check_plain(WSc, #{<<"ciphers">> => <<"foo,bar">>})).
  82. bad_tls_version_test() ->
  83. Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
  84. Reason = {unsupported_ssl_versions, [foo]},
  85. ?assertThrow({_Sc, [{validation_error, #{reason := Reason}}]},
  86. validate(Sc, #{<<"versions">> => [<<"foo">>]})),
  87. ok.