emqx_restricted_shell_SUITE.erl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 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_restricted_shell_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/include/emqx.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("common_test/include/ct.hrl").
  22. all() -> emqx_common_test_helpers:all(?MODULE).
  23. init_per_suite(Config) ->
  24. emqx_common_test_helpers:start_apps([]),
  25. Config.
  26. end_per_suite(_Config) ->
  27. emqx_common_test_helpers:stop_apps([]).
  28. t_local_allowed(_Config) ->
  29. LocalProhibited = [halt, q],
  30. State = undefined,
  31. lists:foreach(
  32. fun(LocalFunc) ->
  33. ?assertEqual({false, State}, emqx_restricted_shell:local_allowed(LocalFunc, [], State))
  34. end,
  35. LocalProhibited
  36. ),
  37. LocalAllowed = [ls, pwd],
  38. lists:foreach(
  39. fun(LocalFunc) ->
  40. ?assertEqual({true, State}, emqx_restricted_shell:local_allowed(LocalFunc, [], State))
  41. end,
  42. LocalAllowed
  43. ),
  44. ok.
  45. t_non_local_allowed(_Config) ->
  46. RemoteProhibited = [{erlang, halt}, {c, q}, {init, stop}, {init, restart}, {init, reboot}],
  47. State = undefined,
  48. lists:foreach(
  49. fun(RemoteFunc) ->
  50. ?assertEqual(
  51. {false, State}, emqx_restricted_shell:non_local_allowed(RemoteFunc, [], State)
  52. )
  53. end,
  54. RemoteProhibited
  55. ),
  56. RemoteAllowed = [{erlang, date}, {erlang, system_time}],
  57. lists:foreach(
  58. fun(RemoteFunc) ->
  59. ?assertEqual({true, State}, emqx_restricted_shell:local_allowed(RemoteFunc, [], State))
  60. end,
  61. RemoteAllowed
  62. ),
  63. ok.
  64. t_lock(_Config) ->
  65. State = undefined,
  66. emqx_restricted_shell:lock(),
  67. ?assertEqual({false, State}, emqx_restricted_shell:local_allowed(q, [], State)),
  68. ?assertEqual({true, State}, emqx_restricted_shell:local_allowed(ls, [], State)),
  69. ?assertEqual({false, State}, emqx_restricted_shell:non_local_allowed({init, stop}, [], State)),
  70. ?assertEqual(
  71. {true, State}, emqx_restricted_shell:non_local_allowed({inet, getifaddrs}, [], State)
  72. ),
  73. emqx_restricted_shell:unlock(),
  74. ?assertEqual({true, State}, emqx_restricted_shell:local_allowed(q, [], State)),
  75. ?assertEqual({true, State}, emqx_restricted_shell:local_allowed(ls, [], State)),
  76. ?assertEqual({true, State}, emqx_restricted_shell:non_local_allowed({init, stop}, [], State)),
  77. ?assertEqual(
  78. {true, State}, emqx_restricted_shell:non_local_allowed({inet, getifaddrs}, [], State)
  79. ),
  80. emqx_restricted_shell:lock(),
  81. ok.