1: -module(config_parser_SUITE).
    2: -compile([export_all, nowarn_export_all]).
    3: 
    4: -include_lib("eunit/include/eunit.hrl").
    5: -include("log_helper.hrl").
    6: 
    7: -define(HOST, <<"example.com">>).
    8: 
    9: -define(eq(Expected, Actual), ?assertEqual(Expected, Actual)).
   10: 
   11: %% Assertions
   12: 
   13: %% global config options
   14: -define(cfg(Key, Value, RawConfig), ?cfg([{Key, Value}], RawConfig)).
   15: -define(cfg(ExpectedOpts, RawConfig), assert_options(ExpectedOpts, parse(RawConfig))).
   16: 
   17: %% global config error
   18: -define(err(RawConfig), ?err(_, RawConfig)).
   19: -define(err(Pattern, RawConfig), ?assertError({config_error, _, Pattern}, parse(RawConfig))).
   20: 
   21: %% host-or-global config options
   22: -define(cfgh(KeyPrefix, Value, RawConfig), ?cfgh([{KeyPrefix, Value}], RawConfig)).
   23: -define(cfgh(ExpectedOpts, RawConfig),
   24:         begin
   25:             ?cfg(host_opts(ExpectedOpts), RawConfig),
   26:             ?cfg(host_opts(ExpectedOpts), host_config(RawConfig))
   27:         end).
   28: 
   29: %% host-or-global config error
   30: -define(errh(RawConfig), ?errh(_, RawConfig)).
   31: -define(errh(Pattern, RawConfig),
   32:         begin
   33:             ?err(Pattern, RawConfig),
   34:             ?err(Pattern, host_config(RawConfig))
   35:         end).
   36: 
   37: -import(mongoose_config_parser_toml, [extract_errors/1]).
   38: -import(config_parser_helper, [default_s2s/0,
   39:                                extra_service_listener_config/0,
   40:                                mod_event_pusher_http_handler/0,
   41:                                default_c2s_tls/1,
   42:                                mod_config/2, default_mod_config/1,
   43:                                config/2, default_config/1]).
   44: 
   45: -type key_prefix() :: top_level_key_prefix() | key_path_prefix().
   46: -type top_level_key_prefix() :: atom().
   47: -type key_path_prefix() :: [atom() | binary()].
   48: 
   49: all() ->
   50:     [{group, file},
   51:      {group, dynamic_domains},
   52:      {group, general},
   53:      {group, listen},
   54:      {group, auth},
   55:      {group, pool},
   56:      {group, internal_databases},
   57:      {group, shaper_acl_access},
   58:      {group, s2s},
   59:      {group, modules},
   60:      {group, services},
   61:      {group, instrumentation},
   62:      {group, logs}].
   63: 
   64: groups() ->
   65:     [{file, [parallel], [sample_pgsql,
   66:                          miscellaneous,
   67:                          s2s,
   68:                          modules,
   69:                          outgoing_pools,
   70:                          host_types_file]},
   71:      {dynamic_domains, [parallel], [supported_features,
   72:                                     unsupported_features]},
   73:      {general, [parallel], [loglevel,
   74:                             hosts,
   75:                             host_types,
   76:                             default_server_domain,
   77:                             registration_timeout,
   78:                             language,
   79:                             all_metrics_are_global,
   80:                             sm_backend,
   81:                             component_backend,
   82:                             s2s_backend,
   83:                             max_fsm_queue,
   84:                             http_server_name,
   85:                             rdbms_server_type,
   86:                             route_subdomains,
   87:                             routing_modules,
   88:                             replaced_wait_timeout,
   89:                             hide_service_name,
   90:                             domain_certfile,
   91:                             max_users_per_domain]},
   92:      {listen, [parallel], [listen_duplicate,
   93:                            listen_c2s,
   94:                            listen_c2s_fast_tls,
   95:                            listen_c2s_just_tls,
   96:                            listen_s2s,
   97:                            listen_s2s_tls,
   98:                            listen_service,
   99:                            listen_http,
  100:                            listen_http_tls,
  101:                            listen_http_transport,
  102:                            listen_http_handlers_invalid,
  103:                            listen_http_handlers_bosh,
  104:                            listen_http_handlers_websockets,
  105:                            listen_http_handlers_client_api,
  106:                            listen_http_handlers_admin_api,
  107:                            listen_http_handlers_graphql]},
  108:      {auth, [parallel], [auth_methods,
  109:                          auth_password,
  110:                          auth_sasl_external,
  111:                          auth_allow_multiple_connections,
  112:                          auth_anonymous_protocol,
  113:                          auth_sasl_mechanisms,
  114:                          auth_ldap_pool,
  115:                          auth_ldap_bind_pool,
  116:                          auth_ldap_base,
  117:                          auth_ldap_uids,
  118:                          auth_ldap_filter,
  119:                          auth_ldap_dn_filter,
  120:                          auth_ldap_local_filter,
  121:                          auth_ldap_deref,
  122:                          auth_external,
  123:                          auth_http_basic_auth,
  124:                          auth_jwt,
  125:                          auth_rdbms_users_number_estimate,
  126:                          auth_dummy]},
  127:      {pool, [parallel], [pool_basics,
  128:                          pool_scope,
  129:                          pool_rdbms,
  130:                          pool_rdbms_connection_odbc,
  131:                          pool_rdbms_connection_pgsql,
  132:                          pool_rdbms_connection_mysql,
  133:                          pool_rdbms_connection_tls_pgsql,
  134:                          pool_rdbms_connection_tls_mysql,
  135:                          pool_http,
  136:                          pool_http_connection,
  137:                          pool_http_connection_tls,
  138:                          pool_redis,
  139:                          pool_redis_connection,
  140:                          pool_cassandra,
  141:                          pool_cassandra_connection,
  142:                          pool_cassandra_connection_auth_plain,
  143:                          pool_cassandra_connection_servers,
  144:                          pool_cassandra_connection_tls,
  145:                          pool_elastic,
  146:                          pool_elastic_connection,
  147:                          pool_rabbit,
  148:                          pool_rabbit_connection,
  149:                          pool_ldap,
  150:                          pool_ldap_connection,
  151:                          pool_ldap_connection_tls]},
  152:      {internal_databases, [parallel], [internal_database_cets]},
  153:      {shaper_acl_access, [parallel], [shaper,
  154:                                       acl,
  155:                                       acl_merge_host_and_global,
  156:                                       access,
  157:                                       access_merge_host_and_global]},
  158:      {s2s, [parallel], [s2s_host_config,
  159:                         s2s_dns_timeout,
  160:                         s2s_dns_retries,
  161:                         s2s_outgoing_port,
  162:                         s2s_outgoing_ip_versions,
  163:                         s2s_outgoing_timeout,
  164:                         s2s_use_starttls,
  165:                         s2s_certfile,
  166:                         s2s_default_policy,
  167:                         s2s_host_policy,
  168:                         s2s_address,
  169:                         s2s_ciphers,
  170:                         s2s_shared,
  171:                         s2s_max_retry_delay]},
  172:      {modules, [parallel], [mod_adhoc,
  173:                             mod_auth_token,
  174:                             mod_blocking,
  175:                             mod_bosh,
  176:                             mod_caps,
  177:                             mod_cache_users,
  178:                             mod_carboncopy,
  179:                             mod_csi,
  180:                             mod_disco,
  181:                             mod_inbox,
  182:                             mod_global_distrib,
  183:                             mod_global_distrib_connections,
  184:                             mod_global_distrib_connections_endpoints,
  185:                             mod_global_distrib_connections_advertised_endpoints,
  186:                             mod_global_distrib_connections_tls,
  187:                             mod_global_distrib_redis,
  188:                             mod_global_distrib_cache,
  189:                             mod_global_distrib_bounce,
  190:                             mod_event_pusher_sns,
  191:                             mod_event_pusher_push,
  192:                             mod_event_pusher_http,
  193:                             mod_event_pusher_rabbit,
  194:                             mod_extdisco,
  195:                             mod_http_upload,
  196:                             mod_http_upload_s3,
  197:                             mod_jingle_sip,
  198:                             mod_keystore,
  199:                             mod_keystore_keys,
  200:                             mod_last,
  201:                             mod_mam,
  202:                             mod_mam_pm,
  203:                             mod_mam_muc,
  204:                             mod_muc,
  205:                             mod_muc_default_room,
  206:                             mod_muc_default_room_affiliations,
  207:                             mod_muc_log,
  208:                             mod_muc_log_top_link,
  209:                             mod_muc_light,
  210:                             mod_muc_light_config_schema,
  211:                             mod_offline,
  212:                             mod_offline_chatmarkers,
  213:                             mod_ping,
  214:                             mod_privacy,
  215:                             mod_private,
  216:                             mod_pubsub,
  217:                             mod_pubsub_pep_mapping,
  218:                             mod_pubsub_default_node_config,
  219:                             mod_push_service_mongoosepush,
  220:                             mod_register,
  221:                             mod_roster,
  222:                             mod_shared_roster_ldap,
  223:                             mod_sic,
  224:                             mod_smart_markers,
  225:                             mod_stream_management,
  226:                             mod_stream_management_stale_h,
  227:                             mod_time,
  228:                             mod_vcard,
  229:                             mod_vcard_ldap_uids,
  230:                             mod_vcard_ldap_vcard_map,
  231:                             mod_vcard_ldap_search_fields,
  232:                             mod_vcard_ldap_search_reported,
  233:                             mod_version,
  234:                             modules_without_config,
  235:                             incorrect_module]},
  236:      {services, [parallel], [service_domain_db,
  237:                              service_mongoose_system_metrics]},
  238:      {instrumentation, [parallel], [instrumentation,
  239:                                     instrumentation_log]},
  240:      {logs, [], log_cases()}
  241:     ].
  242: 
  243: init_per_suite(Config) ->
  244:     {ok, _} = application:ensure_all_started(jid),
  245:     create_files(Config),
  246:     Config.
  247: 
  248: end_per_suite(_Config) ->
  249:     ok.
  250: 
  251: init_per_group(dynamic_domains, Config) ->
  252:     meck:new(ejabberd_auth_http, [passthrough, no_link]),
  253:     meck:new(mod_test, [non_strict, no_link]),
  254:     meck:expect(ejabberd_auth_http, supported_features, fun() -> [] end),
  255:     meck:expect(mod_test, supported_features, fun() -> [] end),
  256:     Config;
  257: init_per_group(logs, _Config) ->
  258:     log_helper:set_up();
  259: init_per_group(_, Config) ->
  260:     Config.
  261: 
  262: end_per_group(dynamic_domains, _Config) ->
  263:     meck:unload();
  264: end_per_group(logs, _Config) ->
  265:     log_helper:tear_down();
  266: end_per_group(_, _Config) ->
  267:     ok.
  268: 
  269: init_per_testcase(CaseName, Config) ->
  270:     case lists:member(CaseName, log_cases()) of
  271:         true -> log_helper:subscribe();
  272:         false -> ok
  273:     end,
  274:     Config.
  275: 
  276: end_per_testcase(CaseName, _Config) ->
  277:     case lists:member(CaseName, log_cases()) of
  278:         true -> log_helper:unsubscribe();
  279:         false -> ok
  280:     end.
  281: 
  282: log_cases() ->
  283:     [no_warning_about_subdomain_patterns,
  284:      no_warning_for_resolvable_domain].
  285: 
  286: sample_pgsql(Config) ->
  287:     test_config_file(Config,  "mongooseim-pgsql").
  288: 
  289: miscellaneous(Config) ->
  290:     test_config_file(Config,  "miscellaneous").
  291: 
  292: s2s(Config) ->
  293:     test_config_file(Config,  "s2s_only").
  294: 
  295: modules(Config) ->
  296:     test_config_file(Config,  "modules").
  297: 
  298: outgoing_pools(Config) ->
  299:     test_config_file(Config,  "outgoing_pools").
  300: 
  301: host_types_file(Config) ->
  302:     test_config_file(Config, "host_types").
  303: 
  304: supported_features(_Config) ->
  305:     Gen = #{<<"general">> => #{<<"host_types">> => [<<"type1">>, <<"type2">>]}},
  306:     Auth = #{<<"auth">> => #{<<"internal">> => #{}}},
  307:     Mod = #{<<"modules">> => #{<<"mod_amp">> => #{}}},
  308:     ?cfg([{auth, <<"type1">>}, methods], [internal], maps:merge(Gen, Auth)),
  309:     ?cfg([{auth, <<"type1">>}, methods], [internal],
  310:          Gen#{<<"host_config">> => [Auth#{<<"host_type">> => <<"type1">>}]}),
  311:     ?cfg([{modules, <<"type1">>}, mod_amp], #{}, maps:merge(Gen, Mod)),
  312:     ?cfg([{modules, <<"type1">>}, mod_amp], #{},
  313:           Gen#{<<"host_config">> => [Mod#{<<"host_type">> => <<"type1">>}]}).
  314: 
  315: unsupported_features(_Config) ->
  316:     % ejabberd_auth_http and mod_test are mocked and they don't support dynamic domains
  317:     Gen = #{<<"general">> => #{<<"host_types">> => [<<"type1">>, <<"type2">>]}},
  318:     Auth = #{<<"auth">> => #{<<"http">> => #{}}},
  319:     Mod = #{<<"modules">> => #{<<"mod_test">> => #{}}},
  320:     ?err([#{reason := dynamic_domains_not_supported,
  321:             unsupported_auth_methods := [http],
  322:             unsupported_modules := []}],
  323:          maps:merge(Gen, Auth)),
  324:     ?err([#{reason := dynamic_domains_not_supported,
  325:             unsupported_auth_methods := [http],
  326:             unsupported_modules := []}],
  327:          Gen#{<<"host_config">> => [Auth#{<<"host_type">> => <<"type1">>}]}),
  328:     ?err([#{reason := dynamic_domains_not_supported,
  329:             unsupported_auth_methods := [],
  330:             unsupported_modules := [mod_test]}],
  331:          maps:merge(Gen, Mod)),
  332:     ?err([#{reason := dynamic_domains_not_supported,
  333:             unsupported_auth_methods := [],
  334:             unsupported_modules := [mod_test]}],
  335:          Gen#{<<"host_config">> => [Mod#{<<"host_type">> => <<"type1">>}]}).
  336: 
  337: %% tests: general
  338: loglevel(_Config) ->
  339:     ?cfg(loglevel, warning, #{}), % default
  340:     ?cfg(loglevel, debug, #{<<"general">> => #{<<"loglevel">> => <<"debug">>}}),
  341:     ?err(#{<<"general">> => #{<<"loglevel">> => <<"bebug">>}}),
  342:     %% make sure non-host options are not accepted in host_config
  343:     ?err(host_config(#{<<"general">> => #{<<"loglevel">> => <<"debug">>}})).
  344: 
  345: hosts(_Config) ->
  346:     ?cfg(hosts, [], % default
  347:          #{<<"general">> => #{<<"host_types">> => [<<"type1">>]}, without => [<<"hosts">>]}),
  348:     ?cfg(hosts, [<<"host1">>],
  349:          #{<<"general">> => #{<<"hosts">> => [<<"host1">>]}}),
  350:     ?cfg(hosts, [<<"host1">>, <<"host2">>],
  351:          #{<<"general">> => #{<<"hosts">> => [<<"host1">>, <<"host2">>]}}),
  352:     ?err(#{<<"general">> => #{<<"hosts">> => [<<"what is this?">>]}}),
  353:     ?err(#{<<"general">> => #{<<"hosts">> => [<<>>]}}),
  354:     ?err(#{<<"general">> => #{<<"hosts">> => [<<"host1">>, <<"host1">>]}}),
  355:     %% at least one host or host_type must be provided
  356:     ?err(#{<<"general">> => #{}, without => [<<"hosts">>]}),
  357:     ?err(#{<<"general">> => #{<<"hosts">> => []}}),
  358:     ?err(#{<<"general">> => #{<<"host_types">> => []}, without => [<<"hosts">>]}),
  359:     ?err(#{<<"general">> => #{<<"hosts">> => [], <<"host_types">> => []}}).
  360: 
  361: host_types(_Config) ->
  362:     ?cfg(host_types, [], #{}), % default
  363:     ?cfg([{host_types, [<<"type 1">>]},
  364:           {hosts, []}],
  365:          #{<<"general">> => #{<<"host_types">> => [<<"type 1">>]}, without => [<<"hosts">>]}),
  366:     ?cfg([{host_types, [<<"type 1">>, <<"type 2">>]},
  367:           {hosts, []}],
  368:          #{<<"general">> => #{<<"host_types">> => [<<"type 1">>, <<"type 2">>],
  369:                               <<"hosts">> => []}}),
  370:     ?err(#{<<"general">> => #{<<"host_types">> => [<<>>]}}),
  371:     ?err(#{<<"general">> => #{<<"host_types">> => [<<"type1">>, <<"type1">>]}}),
  372:     %% either hosts and host_types cannot have the same values
  373:     ?err(#{<<"general">> => #{<<"host_types">> => [<<"type1">>],
  374:                               <<"hosts">> => [<<"type1">>]}}).
  375: 
  376: default_server_domain(_Config) ->
  377:     ?cfg(default_server_domain, <<"host1">>,
  378:          #{<<"general">> => #{<<"default_server_domain">> => <<"host1">>}}),
  379:     ?err(#{<<"general">> => #{<<"default_server_domain">> => <<"what is this?">>}}),
  380:     ?err(#{<<"general">> => #{<<"default_server_domain">> => <<>>}}),
  381:     %% default_server_domain must be provided
  382:     ?err(#{without => [<<"default_server_domain">>]}).
  383: 
  384: registration_timeout(_Config) ->
  385:     ?cfg(registration_timeout, 600, #{}), % default
  386:     ?cfg(registration_timeout, infinity,
  387:          #{<<"general">> => #{<<"registration_timeout">> => <<"infinity">>}}),
  388:     ?cfg(registration_timeout, 300,
  389:          #{<<"general">> => #{<<"registration_timeout">> => 300}}),
  390:     ?err(#{<<"general">> => #{<<"registration_timeout">> => 0}}).
  391: 
  392: language(_Config) ->
  393:     ?cfg(language, <<"en">>, #{}), % default
  394:     ?cfg(language, <<"pl">>, #{<<"general">> => #{<<"language">> => <<"pl">>}}),
  395:     ?err(#{<<"general">> => #{<<"language">> => <<>>}}).
  396: 
  397: all_metrics_are_global(_Config) ->
  398:     ?cfg(all_metrics_are_global, false, #{}), % default
  399:     ?cfg(all_metrics_are_global, true, #{<<"general">> => #{<<"all_metrics_are_global">> => true}}),
  400:     ?err(#{<<"general">> => #{<<"all_metrics_are_global">> => <<"true">>}}).
  401: 
  402: sm_backend(_Config) ->
  403:     ?cfg(sm_backend, mnesia, #{}), % default
  404:     ?cfg(sm_backend, mnesia, #{<<"general">> => #{<<"sm_backend">> => <<"mnesia">>}}),
  405:     ?cfg(sm_backend, cets, #{<<"general">> => #{<<"sm_backend">> => <<"cets">>}}),
  406:     ?cfg(sm_backend, redis, #{<<"general">> => #{<<"sm_backend">> => <<"redis">>}}),
  407:     ?err(#{<<"general">> => #{<<"sm_backend">> => <<"amnesia">>}}).
  408: 
  409: component_backend(_Config) ->
  410:     ?cfg(component_backend, mnesia, #{}), % default
  411:     ?cfg(component_backend, mnesia, #{<<"general">> => #{<<"component_backend">> => <<"mnesia">>}}),
  412:     ?cfg(component_backend, cets, #{<<"general">> => #{<<"component_backend">> => <<"cets">>}}),
  413:     ?err(#{<<"general">> => #{<<"component_backend">> => <<"amnesia">>}}).
  414: 
  415: s2s_backend(_Config) ->
  416:     ?cfg(s2s_backend, mnesia, #{}), % default
  417:     ?cfg(s2s_backend, mnesia, #{<<"general">> => #{<<"s2s_backend">> => <<"mnesia">>}}),
  418:     ?err(#{<<"general">> => #{<<"s2s_backend">> => <<"redis">>}}),
  419:     ?err(#{<<"general">> => #{<<"s2s_backend">> => <<"amnesia">>}}).
  420: 
  421: max_fsm_queue(_Config) ->
  422:     ?cfg(max_fsm_queue, 100, #{<<"general">> => #{<<"max_fsm_queue">> => 100}}),
  423:     ?err(#{<<"general">> => #{<<"max_fsm_queue">> => -10}}).
  424: 
  425: http_server_name(_Config) ->
  426:     ?cfg(http_server_name, "my server",
  427:          #{<<"general">> => #{<<"http_server_name">> => <<"my server">>}}),
  428:     ?err(#{<<"general">> => #{<<"http_server_name">> => #{}}}).
  429: 
  430: rdbms_server_type(_Config) ->
  431:     ?cfg(rdbms_server_type, generic, #{}), % default
  432:     ?cfg(rdbms_server_type, mssql, #{<<"general">> => #{<<"rdbms_server_type">> => <<"mssql">>}}),
  433:     ?cfg(rdbms_server_type, pgsql, #{<<"general">> => #{<<"rdbms_server_type">> => <<"pgsql">>}}),
  434:     ?err(#{<<"general">> => #{<<"rdbms_server_type">> => <<"nosql">>}}).
  435: 
  436: route_subdomains(_Config) ->
  437:     ?cfgh(route_subdomains, s2s, #{<<"general">> => #{<<"route_subdomains">> => <<"s2s">>}}),
  438:     ?errh(#{<<"general">> => #{<<"route_subdomains">> => <<"c2s">>}}).
  439: 
  440: routing_modules(_Config) ->
  441:     ?cfg(routing_modules, mongoose_router:default_routing_modules(), #{}), % default
  442:     ?cfg(routing_modules,
  443:          xmpp_router:expand_routing_modules([mongoose_router_global, mongoose_router_localdomain]),
  444:          #{<<"general">> => #{<<"routing_modules">> => [<<"mongoose_router_global">>,
  445:                                                         <<"mongoose_router_localdomain">>]}}),
  446:     ?err(#{<<"general">> => #{<<"routing_modules">> => [<<"moongoose_router_global">>]}}).
  447: 
  448: replaced_wait_timeout(_Config) ->
  449:     ?cfg({replaced_wait_timeout, ?HOST}, 2000, #{}), % global default
  450:     ?cfgh(replaced_wait_timeout, 1000, #{<<"general">> => #{<<"replaced_wait_timeout">> => 1000}}),
  451:     ?errh(#{<<"general">> => #{<<"replaced_wait_timeout">> => 0}}).
  452: 
  453: hide_service_name(_Config) ->
  454:     ?cfg(hide_service_name, false, #{}), % default
  455:     ?cfg(hide_service_name, true, #{<<"general">> => #{<<"hide_service_name">> => true}}),
  456:     ?err(#{<<"general">> => #{<<"hide_service_name">> => []}}).
  457: 
  458: domain_certfile(_Config) ->
  459:     DomCert = #{<<"domain">> => <<"myxmpp.com">>,
  460:                 <<"certfile">> => <<"priv/cert.pem">>},
  461:     ?cfg(domain_certfile, #{<<"myxmpp.com">> => "priv/cert.pem"},
  462:          #{<<"general">> => #{<<"domain_certfile">> => [DomCert]}}),
  463:     ?err([#{reason := invalid_filename}],
  464:          #{<<"general">> => #{<<"domain_certfile">> =>
  465:                                   [DomCert#{<<"certfile">> => <<"missing.pem">>}]}}),
  466:     [?err(#{<<"general">> => #{<<"domain_certfile">> => [maps:without([K], DomCert)]}})
  467:      || K <- maps:keys(DomCert)],
  468:     [?err(#{<<"general">> => #{<<"domain_certfile">> => [DomCert#{K := <<>>}]}})
  469:      || K <- maps:keys(DomCert)],
  470:     ?err(#{<<"general">> => #{<<"domain_certfile">> => [DomCert, DomCert]}}).
  471: 
  472: %% tests: listen
  473: 
  474: listen_duplicate(_Config) ->
  475:     ?cfg(listen, [listener(c2s, #{port => 5222}),
  476:                   listener(c2s, #{port => 5223})],
  477:          #{<<"listen">> => #{<<"c2s">> => [#{<<"port">> => 5222, <<"ip_address">> => <<"0">>},
  478:                                            #{<<"port">> => 5223}]}}),
  479:     ?err([#{reason := duplicate_listeners,
  480:             duplicates := [{5222, {0, 0, 0, 0}, tcp}]}],
  481:          #{<<"listen">> => #{<<"c2s">> => [#{<<"port">> => 5222, <<"ip_address">> => <<"0">>},
  482:                                            #{<<"port">> => 5222}]}}),
  483:     ?err([#{reason := duplicate_listeners,
  484:             duplicates := [{5222, {0, 0, 0, 0}, tcp}]}],
  485:          #{<<"listen">> => #{<<"c2s">> => [#{<<"port">> => 5222, <<"ip_address">> => <<"0">>}],
  486:                              <<"s2s">> => [#{<<"port">> => 5222}]}}).
  487: 
  488: listen_c2s(_Config) ->
  489:     T = fun(Opts) -> listen_raw(c2s, maps:merge(#{<<"port">> => 5222}, Opts)) end,
  490:     P = [listen, 1],
  491:     ?cfg(P, config([listen, c2s], #{port => 5222}), T(#{})),
  492:     test_listen(P, T),
  493:     test_listen_xmpp(P, T),
  494:     ?cfg(P ++ [access], rule1, T(#{<<"access">> => <<"rule1">>})),
  495:     ?cfg(P ++ [shaper], c2s_shaper, T(#{<<"shaper">> => <<"c2s_shaper">>})),
  496:     ?cfg(P ++ [reuse_port], true, T(#{<<"reuse_port">> => true})),
  497:     ?cfg(P ++ [backwards_compatible_session], true, T(#{<<"backwards_compatible_session">> => true})),
  498:     ?cfg(P ++ [max_connections], 1000, T(#{<<"max_connections">> => 1000})),
  499:     ?cfg(P ++ [allowed_auth_methods], [rdbms, http],
  500:          T(#{<<"allowed_auth_methods">> => [<<"rdbms">>, <<"http">>]})),
  501:     ?err(T(#{<<"access">> => <<>>})),
  502:     ?err(T(#{<<"shaper">> => <<>>})),
  503:     ?err(T(#{<<"reuse_port">> => 0})),
  504:     ?err(T(#{<<"backwards_compatible_session">> => 0})),
  505:     ?err(T(#{<<"max_connections">> => 0})),
  506:     ?err(T(#{<<"allowed_auth_methods">> => [<<"bad_method">>]})),
  507:     ?err(T(#{<<"allowed_auth_methods">> => [<<"rdbms">>, <<"rdbms">>]})).
  508: 
  509: listen_c2s_fast_tls(_Config) ->
  510:     T = fun(Opts) -> listen_raw(c2s, #{<<"port">> => 5222,
  511:                                        <<"tls">> => Opts}) end,
  512:     P = [listen, 1, tls],
  513:     ?cfg(P, default_c2s_tls(fast_tls), T(#{})),
  514:     test_fast_tls_server(P, T),
  515:     ?cfg(P ++ [mode], tls, T(#{<<"mode">> => <<"tls">>})),
  516:     ?err(T(#{<<"mode">> => <<"stopttls">>})),
  517:     ?err(T(#{<<"module">> => <<"slow_tls">>})).
  518: 
  519: listen_c2s_just_tls(_Config) ->
  520:     T = fun(Opts) -> listen_raw(c2s, #{<<"port">> => 5222,
  521:                                        <<"tls">> => Opts#{<<"module">> => <<"just_tls">>}}) end,
  522:     P = [listen, 1, tls],
  523:     M = tls_ca_raw(),
  524:     ?cfg(P, maps:merge(default_c2s_tls(just_tls), tls_ca()), T(M)),
  525:     test_just_tls_server(P, T),
  526:     ?cfg(P ++ [mode], tls, T(M#{<<"mode">> => <<"tls">>})),
  527:     ?cfg(P ++ [disconnect_on_failure], false, T(M#{<<"disconnect_on_failure">> => false})),
  528:     ?cfg(P ++ [crl_files], ["priv/cert.pem"], % note: this is not a real CRL file
  529:          T(M#{<<"crl_files">> => [<<"priv/cert.pem">>]})),
  530:     ?err(T(M#{<<"mode">> => <<"stopttls">>})),
  531:     ?err(T(M#{<<"disconnect_on_failure">> => <<"sometimes">>})),
  532:     ?err(T(M#{<<"dhfile">> => <<"no_such_file.pem">>})),
  533:     ?err(T(M#{<<"crl_files">> => [<<"no_such_file.crl">>]})).
  534: 
  535: listen_s2s(_Config) ->
  536:     T = fun(Opts) -> listen_raw(s2s, maps:merge(#{<<"port">> => 5269}, Opts)) end,
  537:     P = [listen, 1],
  538:     ?cfg(P, config([listen, s2s], #{port => 5269}), T(#{})),
  539:     test_listen(P, T),
  540:     test_listen_xmpp(P, T),
  541:     ?cfg(P ++ [shaper], s2s_shaper, T(#{<<"shaper">> => <<"s2s_shaper">>})),
  542:     ?err(T(#{<<"shaper">> => <<>>})).
  543: 
  544: listen_s2s_tls(_Config) ->
  545:     T = fun(Opts) -> listen_raw(s2s, #{<<"port">> => 5269, <<"tls">> => Opts}) end,
  546:     P = [listen, 1, tls],
  547:     ?cfg(P, default_config([listen, s2s, tls]), T(#{})),
  548:     test_fast_tls_server(P, T).
  549: 
  550: listen_service(_Config) ->
  551:     T = fun(Opts) -> listen_raw(service, maps:merge(#{<<"port">> => 8888,
  552:                                                       <<"password">> => <<"secret">>}, Opts))
  553:         end,
  554:     P = [listen, 1],
  555:     ?cfg(P, config([listen, service], #{port => 8888, password => "secret"}), T(#{})),
  556:     test_listen(P, T),
  557:     test_listen_xmpp(P, T),
  558:     ?cfg(P ++ [access], rule1, T(#{<<"access">> => <<"rule1">>})),
  559:     ?cfg(P ++ [shaper_rule], fast, T(#{<<"shaper_rule">> => <<"fast">>})),
  560:     ?cfg(P ++ [check_from], false, T(#{<<"check_from">> => false})),
  561:     ?cfg(P ++ [hidden_components], true, T(#{<<"hidden_components">> => true})),
  562:     ?cfg(P ++ [conflict_behaviour], kick_old, T(#{<<"conflict_behaviour">> => <<"kick_old">>})),
  563:     ?cfg(P ++ [max_fsm_queue], 1000, T(#{<<"max_fsm_queue">> => 1000})),
  564:     ?err(T(#{<<"access">> => <<>>})),
  565:     ?err(T(#{<<"shaper_rule">> => <<>>})),
  566:     ?err(T(#{<<"check_from">> => 1})),
  567:     ?err(T(#{<<"hidden_components">> => <<"yes">>})),
  568:     ?err(T(#{<<"conflict_behaviour">> => <<"kill_server">>})),
  569:     ?err(T(#{<<"password">> => <<>>})),
  570:     ?err(T(#{<<"password">> => undefined})),
  571:     ?err(T(#{<<"max_fsm_queue">> => 0})).
  572: 
  573: listen_http(_Config) ->
  574:     T = fun(Opts) -> listen_raw(http, maps:merge(#{<<"port">> => 5280}, Opts)) end,
  575:     P = [listen, 1],
  576:     ?cfg(P, config([listen, http], #{port => 5280}), T(#{})),
  577:     test_listen(P, T).
  578: 
  579: listen_http_tls(_Config) ->
  580:     T = fun(Opts) -> listen_raw(http, #{<<"port">> => 5280, <<"tls">> => Opts}) end,
  581:     P = [listen, 1, tls],
  582:     test_just_tls_server(P, T),
  583:     ?cfg(P, config([listen, http, tls], tls_ca()), T(tls_ca_raw())).
  584: 
  585: listen_http_transport(_Config) ->
  586:     T = fun(Opts) -> listen_raw(http, #{<<"port">> => 5280, <<"transport">> => Opts}) end,
  587:     P = [listen, 1, transport],
  588:     ?cfg(P ++ [num_acceptors], 10, T(#{<<"num_acceptors">> => 10})),
  589:     ?cfg(P ++ [max_connections], 1024, T(#{<<"max_connections">> => 1024})),
  590:     ?err(T(#{<<"num_acceptors">> => 0})),
  591:     ?err(T(#{<<"max_connections">> => -1})).
  592: 
  593: listen_http_protocol(_Config) ->
  594:     T = fun(Opts) -> listen_raw(http, #{<<"port">> => 5280, <<"protocol">> => Opts}) end,
  595:     P = [listen, 1, protocol],
  596:     ?cfg(P ++ [compress], true, T(#{<<"compress">> => true})),
  597:     ?err(T(#{<<"compress">> => 1})).
  598: 
  599: listen_http_handlers_invalid(_Config) ->
  600:     T = fun(Opts) -> listen_raw(http, #{<<"port">> => 5280, <<"handlers">> => Opts}) end,
  601:     ?err(T(#{<<"mod_bosch">> => [#{<<"host">> => <<"dishwasher">>,
  602:                                    <<"path">> => <<"/cutlery">>}]})).
  603: 
  604: listen_http_handlers_bosh(_Config) ->
  605:     test_listen_http_handler(mod_bosh).
  606: 
  607: listen_http_handlers_websockets(_Config) ->
  608:     {P, T} = test_listen_http_handler(mod_websockets),
  609:     ?cfg(P ++ [timeout], 30000, T(#{<<"timeout">> => 30000})),
  610:     ?cfg(P ++ [ping_rate], 20, T(#{<<"ping_rate">> => 20})),
  611:     ?cfg(P ++ [max_stanza_size], 10000, T(#{<<"max_stanza_size">> => 10000})),
  612:     ?err(T(#{<<"timeout">> => -1})),
  613:     ?err(T(#{<<"ping_rate">> => 0})),
  614:     ?err(T(#{<<"max_stanza_size">> => 0})).
  615: 
  616: listen_http_handlers_client_api(_Config) ->
  617:     {P, T} = test_listen_http_handler(mongoose_client_api),
  618:     ?cfg(P ++ [handlers], [messages],
  619:          T(#{<<"handlers">> => [<<"messages">>]})),
  620:     ?cfg(P ++ [docs], false, T(#{<<"docs">> => false})),
  621:     ?err(T(#{<<"handlers">> => [<<"invalid">>]})),
  622:     ?err(T(#{<<"docs">> => <<"maybe">>})).
  623: 
  624: listen_http_handlers_admin_api(_Config) ->
  625:     {P, T} = test_listen_http_handler(mongoose_admin_api),
  626:     ?cfg(P ++ [handlers], [muc, inbox],
  627:          T(#{<<"handlers">> => [<<"muc">>, <<"inbox">>]})),
  628:     ?err(T(#{<<"handlers">> => [<<"invalid">>]})),
  629:     test_listen_http_handler_creds(P, T).
  630: 
  631: listen_http_handlers_graphql(_Config) ->
  632:     T = fun graphql_handler_raw/1,
  633:     {P, _} = test_listen_http_handler(mongoose_graphql_handler, T),
  634:     test_listen_http_handler_creds(P, T),
  635:     ?cfg(P ++ [allowed_categories], [<<"muc">>, <<"inbox">>],
  636:          T(#{<<"allowed_categories">> => [<<"muc">>, <<"inbox">>]})),
  637:     ?cfg(P ++ [sse_idle_timeout], 3600000, T(#{})),
  638:     ?err(T(#{<<"allowed_categories">> => [<<"invalid">>]})),
  639:     ?err(T(#{<<"schema_endpoint">> => <<"wrong_endpoint">>})),
  640:     ?err(T(#{<<"sse_idle_timeout">> => 0})),
  641:     ?err(http_handler_raw(mongoose_graphql_handler, #{})).
  642: 
  643: test_listen_http_handler_creds(P, T) ->
  644:     CredsRaw = #{<<"username">> => <<"user">>, <<"password">> => <<"pass">>},
  645:     ?cfg(P ++ [username], <<"user">>, T(CredsRaw)),
  646:     ?cfg(P ++ [password], <<"pass">>, T(CredsRaw)),
  647:     %% Both username and password required. Or none.
  648:     [?err(T(maps:remove(Key, CredsRaw))) || Key <- maps:keys(CredsRaw)],
  649:     ?err(CredsRaw#{<<"username">> => 1}),
  650:     ?err(CredsRaw#{<<"password">> => 1}).
  651: 
  652: test_listen_http_handler(Module) ->
  653:     T = fun(Opts) -> http_handler_raw(Module, Opts) end,
  654:     test_listen_http_handler(Module, T).
  655: 
  656: test_listen_http_handler(Module, T) ->
  657:     P = [listen, 1, handlers, 1],
  658:     ?cfg(P, config([listen, http, handlers, Module], #{host => "localhost", path => "/api"}),
  659:          T(#{})),
  660:     ?cfg(P ++ [host], '_', T(#{<<"host">> => <<"_">>})),
  661:     ?cfg(P ++ [path], "/my-path", T(#{<<"path">> => <<"/my-path">>})),
  662:     ?err(T(#{<<"host">> => <<>>})),
  663:     ?err(T(#{<<"host">> => undefined})),
  664:     ?err(T(#{<<"path">> => 12})),
  665:     ?err(T(#{<<"path">> => undefined})),
  666:     {P, T}.
  667: 
  668: test_listen(P, T) ->
  669:     ?cfg(P ++ [ip_address], "192.168.1.16", T(#{<<"ip_address">> => <<"192.168.1.16">>})),
  670:     ?cfg(P ++ [ip_tuple], {192, 168, 1, 16}, T(#{<<"ip_address">> => <<"192.168.1.16">>})),
  671:     ?cfg(P ++ [ip_version], 4, T(#{<<"ip_address">> => <<"192.168.1.16">>})),
  672:     ?cfg(P ++ [ip_address], "2001:db8:3:4:5:6:7:8",
  673:          T(#{<<"ip_address">> => <<"2001:db8:3:4:5:6:7:8">>})),
  674:     ?cfg(P ++ [ip_tuple], {8193, 3512, 3, 4, 5, 6, 7, 8},
  675:          T(#{<<"ip_address">> => <<"2001:db8:3:4:5:6:7:8">>})),
  676:     ?cfg(P ++ [ip_version], 6,
  677:          T(#{<<"ip_address">> => <<"2001:db8:3:4:5:6:7:8">>})),
  678:     ?cfg(P ++ [ip_version], 4, T(#{<<"ip_version">> => 4})),
  679:     ?cfg(P ++ [ip_version], 6, T(#{<<"ip_version">> => 6})),
  680:     ?cfg(P ++ [ip_address], "::", T(#{<<"ip_version">> => 6})),
  681:     ?cfg(P ++ [ip_tuple], {0, 0, 0, 0, 0, 0, 0, 0}, T(#{<<"ip_version">> => 6})),
  682:     ?cfg(P ++ [proto], tcp, T(#{<<"proto">> => <<"tcp">>})),
  683:     ?err(T(#{<<"ip_address">> => <<"192.168.1.999">>})),
  684:     ?err(T(#{<<"port">> => <<"5222">>})),
  685:     ?err(T(#{<<"port">> => 522222})),
  686:     ?err(T(#{<<"port">> => undefined})),
  687:     ?err(T(#{<<"ip_version">> => 7})),
  688:     ?err(T(#{<<"proto">> => <<"udp">>})). % only TCP is accepted
  689: 
  690: test_listen_xmpp(P, T) ->
  691:     ?cfg(P ++ [backlog], 10, T(#{<<"backlog">> => 10})),
  692:     ?cfg(P ++ [proxy_protocol], true, T(#{<<"proxy_protocol">> => true})),
  693:     ?cfg(P ++ [hibernate_after], 10, T(#{<<"hibernate_after">> => 10})),
  694:     ?cfg(P ++ [max_stanza_size], 10000, T(#{<<"max_stanza_size">> => 10000})),
  695:     ?cfg(P ++ [max_stanza_size], 0, T(#{<<"max_stanza_size">> => <<"infinity">>})),
  696:     ?cfg(P ++ [num_acceptors], 100, T(#{<<"num_acceptors">> => 100})),
  697:     ?err(T(#{<<"backlog">> => -10})),
  698:     ?err(T(#{<<"proxy_protocol">> => <<"awesome">>})),
  699:     ?err(T(#{<<"hibernate_after">> => -10})),
  700:     ?err(T(#{<<"max_stanza_size">> => <<"unlimited">>})),
  701:     ?err(T(#{<<"num_acceptors">> => 0})).
  702: 
  703: %% tests: auth
  704: 
  705: auth_methods(_Config) ->
  706:     ?cfg([{auth, ?HOST}, methods], [], #{}), % global default
  707:     ?cfgh([auth, methods], [], #{<<"auth">> => #{}}), % default
  708:     ?cfgh([auth, methods], [internal, rdbms], % default alphabetical order
  709:           #{<<"auth">> => #{<<"internal">> => #{},
  710:                             <<"rdbms">> => #{}}}),
  711:     ?cfgh([auth, methods], [rdbms, internal], % specified order
  712:           #{<<"auth">> => #{<<"internal">> => #{},
  713:                             <<"rdbms">> => #{},
  714:                             <<"methods">> => [<<"rdbms">>, <<"internal">>]}}),
  715:     ?cfgh([auth, methods], [internal], % only one of the defined methods is enabled
  716:           #{<<"auth">> => #{<<"internal">> => #{},
  717:                             <<"rdbms">> => #{},
  718:                             <<"methods">> => [<<"internal">>]}}),
  719:     ?errh(#{<<"auth">> => #{<<"rdbms">> => <<"enabled">>}}),
  720:     ?errh(#{<<"auth">> => #{<<"supernatural">> => #{}}}),
  721:     ?errh(#{<<"auth">> => #{<<"methods">> => [<<"rdbms">>]}}).
  722: 
  723: auth_password(_Config) ->
  724:     Defaults = #{format => scram, scram_iterations => 10000},
  725:     ?cfg([{auth, ?HOST}, password], Defaults, #{}), % global default
  726:     ?cfgh([auth, password], Defaults, #{<<"auth">> => #{}}), % default
  727:     ?cfgh([auth, password], Defaults, #{<<"auth">> => #{<<"password">> => #{}}}), % default
  728:     ?cfgh([auth, password, format], plain,
  729:           #{<<"auth">> => #{<<"password">> => #{<<"format">> => <<"plain">>}}}),
  730:     ?errh(#{<<"auth">> => #{<<"password">> => #{<<"format">> => <<"plane">>}}}),
  731:     ?cfgh([auth, password, hash], [sha, sha256],
  732:           #{<<"auth">> => #{<<"password">> => #{<<"hash">> => [<<"sha">>, <<"sha256">>]}}}),
  733:     ?errh(#{<<"auth">> => #{<<"password">> => #{<<"hash">> => [<<"sha1234">>]}}}),
  734:     ?errh(#{<<"auth">> => #{<<"password">> => #{<<"harsh">> => [<<"sha">>]}}}),
  735:     ?cfgh([auth, password, scram_iterations], 1000,
  736:           #{<<"auth">> => #{<<"password">> => #{<<"scram_iterations">> => 1000}}}),
  737:     ?errh(#{<<"auth">> => #{<<"password">> => #{<<"scram_iterations">> => false}}}).
  738: 
  739: auth_sasl_external(_Config) ->
  740:     ?cfg([{auth, ?HOST}, sasl_external], [standard], #{}), % global default
  741:     ?cfgh([auth, sasl_external], [standard], #{<<"auth">> => #{}}), % default
  742:     ?cfgh([auth, sasl_external], [standard,
  743:                                   common_name,
  744:                                   {mod, cyrsasl_external_verification}],
  745:           #{<<"auth">> => #{<<"sasl_external">> =>
  746:                                 [<<"standard">>,
  747:                                  <<"common_name">>,
  748:                                  <<"cyrsasl_external_verification">>]}}),
  749:     ?errh(#{<<"auth">> => #{<<"sasl_external">> => [<<"unknown">>]}}).
  750: 
  751: auth_sasl_mechanisms(_Config) ->
  752:     Default = cyrsasl:default_modules(),
  753:     ?cfg([{auth, ?HOST}, sasl_mechanisms], Default, #{}), % global default
  754:     ?cfg([{auth, ?HOST}, sasl_mechanisms], Default, #{<<"auth">> => #{}}), % default
  755:     ?cfgh([auth, sasl_mechanisms], [cyrsasl_external, cyrsasl_scram],
  756:           #{<<"auth">> => #{<<"sasl_mechanisms">> => [<<"external">>, <<"scram">>]}}),
  757:     ?errh(#{<<"auth">> => #{<<"sasl_mechanisms">> => [<<"none">>]}}).
  758: 
  759: max_users_per_domain(_Config) ->
  760:     ?cfg([{auth, ?HOST}, max_users_per_domain], infinity, #{}), % global default
  761:     ?cfgh([auth, max_users_per_domain], 1000, #{<<"auth">> =>
  762:                                                 #{<<"max_users_per_domain">> => 1000}}),
  763:     ?errh(#{<<"auth">> => #{<<"max_users_per_domain">> => 0}}).
  764: 
  765: auth_allow_multiple_connections(_Config) ->
  766:     ?cfgh([auth, anonymous, allow_multiple_connections], true,
  767:           auth_raw(<<"anonymous">>, #{<<"allow_multiple_connections">> => true})),
  768:     ?errh(auth_raw(<<"anonymous">>, #{<<"allow_multiple_connections">> => <<"yes">>})).
  769: 
  770: auth_anonymous_protocol(_Config) ->
  771:     ?cfgh([auth, anonymous, protocol], login_anon,
  772:           auth_raw(<<"anonymous">>, #{<<"protocol">> => <<"login_anon">>})),
  773:     ?errh(auth_raw(<<"anonymous">>, #{<<"protocol">> => <<"none">>})).
  774: 
  775: auth_ldap_pool(_Config) ->
  776:     ?cfgh([auth, ldap, pool_tag], default, auth_ldap_raw(#{})), % default
  777:     ?cfgh([auth, ldap, pool_tag], ldap_pool,
  778:           auth_ldap_raw(#{<<"pool_tag">> => <<"ldap_pool">>})),
  779:     ?errh(auth_ldap_raw(#{<<"pool_tag">> => <<>>})).
  780: 
  781: auth_ldap_bind_pool(_Config) ->
  782:     ?cfgh([auth, ldap, bind_pool_tag], bind, auth_ldap_raw(#{})), % default
  783:     ?cfgh([auth, ldap, bind_pool_tag], ldap_bind_pool,
  784:           auth_ldap_raw(#{<<"bind_pool_tag">> => <<"ldap_bind_pool">>})),
  785:     ?errh(auth_ldap_raw(#{<<"bind_pool_tag">> => true})).
  786: 
  787: auth_ldap_base(_Config) ->
  788:     ?cfgh([auth, ldap, base], <<>>, auth_ldap_raw(#{})), % default
  789:     ?cfgh([auth, ldap, base], <<"ou=Users,dc=example,dc=com">>,
  790:           auth_ldap_raw(#{<<"base">> => <<"ou=Users,dc=example,dc=com">>})),
  791:     ?errh(auth_ldap_raw(#{<<"base">> => 10})).
  792: 
  793: auth_ldap_uids(_Config) ->
  794:     ?cfgh([auth, ldap, uids], [{<<"uid">>, <<"%u">>}], auth_ldap_raw(#{})), % default
  795:     ?cfgh([auth, ldap, uids], [{<<"uid1">>, <<"user=%u">>}],
  796:           auth_ldap_raw(#{<<"uids">> => [#{<<"attr">> => <<"uid1">>,
  797:                                            <<"format">> => <<"user=%u">>}]})),
  798:     ?cfgh([auth, ldap, uids], [<<"uid1">>],
  799:           auth_ldap_raw(#{<<"uids">> => [#{<<"attr">> => <<"uid1">>}]})),
  800:     ?errh(auth_ldap_raw(#{<<"uids">> => [#{<<"format">> => <<"user=%u">>}]})).
  801: 
  802: auth_ldap_filter(_Config) ->
  803:     ?cfgh([auth, ldap, filter], <<>>, auth_ldap_raw(#{})), % default
  804:     ?cfgh([auth, ldap, filter], <<"(objectClass=inetOrgPerson)">>,
  805:           auth_ldap_raw(#{<<"filter">> => <<"(objectClass=inetOrgPerson)">>})),
  806:     ?errh(auth_ldap_raw(#{<<"filter">> => 10})).
  807: 
  808: auth_ldap_dn_filter(_Config) ->
  809:     ?cfgh([auth, ldap, dn_filter], {undefined, []}, auth_ldap_raw(#{})), % default
  810:     ?cfgh([auth, ldap, dn_filter], {<<"(user=%u@%d)">>, []},
  811:           auth_ldap_raw(#{<<"dn_filter">> => #{<<"filter">> => <<"(user=%u@%d)">>}})),
  812:     Pattern = <<"(&(name=%s)(owner=%D)(user=%u@%d))">>,
  813:     ?cfgh([auth, ldap, dn_filter], {Pattern, [<<"sn">>]},
  814:           auth_ldap_raw(#{<<"dn_filter">> => #{<<"filter">> => Pattern,
  815:                                                <<"attributes">> => [<<"sn">>]}})),
  816:     ?errh(auth_ldap_raw(#{<<"dn_filter">> => #{<<"attributes">> => [<<"sn">>]}})),
  817:     ?errh(auth_ldap_raw(#{<<"dn_filter">> => #{<<"filter">> => 12}})),
  818:     ?errh(auth_ldap_raw(#{<<"dn_filter">> => #{<<"filter">> => Pattern,
  819:                                                <<"attributes">> => <<"sn">>}})).
  820: 
  821: auth_ldap_local_filter(_Config) ->
  822:     ?cfgh([auth, ldap, local_filter], undefined, auth_ldap_raw(#{})), % default
  823:     Filter = #{<<"operation">> => <<"equal">>,
  824:                <<"attribute">> => <<"accountStatus">>,
  825:                <<"values">> => [<<"enabled">>]},
  826:     ?cfgh([auth, ldap, local_filter], {equal, {"accountStatus", ["enabled"]}},
  827:           auth_ldap_raw(#{<<"local_filter">> => Filter})),
  828:     [?errh(auth_ldap_raw(#{<<"local_filter">> => maps:remove(K, Filter)})) ||
  829:         K <- maps:keys(Filter)],
  830:     ?errh(auth_ldap_raw(#{<<"local_filter">> => Filter#{<<"operation">> := <<"lt">>}})),
  831:     ?errh(auth_ldap_raw(#{<<"local_filter">> => Filter#{<<"attribute">> := <<>>}})),
  832:     ?errh(auth_ldap_raw(#{<<"local_filter">> => Filter#{<<"values">> := []}})).
  833: 
  834: auth_ldap_deref(_Config) ->
  835:     ?cfgh([auth, ldap, deref], never, auth_ldap_raw(#{})), % default
  836:     ?cfgh([auth, ldap, deref], always, auth_ldap_raw(#{<<"deref">> => <<"always">>})),
  837:     ?errh(auth_ldap_raw(#{<<"deref">> => <<"sometimes">>})).
  838: 
  839: auth_external(_Config) ->
  840:     RequiredOpts = #{<<"program">> => <<"/usr/bin/auth">>},
  841:     Config = #{program => "/usr/bin/auth",
  842:                instances => 1}, % default
  843:     ?cfgh([auth, external], Config,
  844:           auth_raw(<<"external">>, RequiredOpts)),
  845:     ?cfgh([auth, external, instances], 2,
  846:           auth_raw(<<"external">>, RequiredOpts#{<<"instances">> => 2})),
  847:     ?errh(auth_raw(<<"external">>, #{<<"program">> => <<>>})),
  848:     ?errh(auth_raw(<<"external">>, #{<<"instances">> => 2})),
  849:     ?errh(auth_raw(<<"external">>, RequiredOpts#{<<"instances">> => 0})).
  850: 
  851: auth_http_basic_auth(_Config) ->
  852:     ?cfgh([auth, http, basic_auth], "admin:admin123",
  853:           auth_raw(<<"http">>, #{<<"basic_auth">> => <<"admin:admin123">>})),
  854:     ?errh(auth_raw(<<"http">>, #{<<"basic_auth">> => true})).
  855: 
  856: auth_jwt(_Config) ->
  857:     Opts = #{<<"secret">> => #{<<"value">> => <<"secret123">>},
  858:              <<"algorithm">> => <<"HS512">>,
  859:              <<"username_key">> => <<"user">>}, % tested together as all options are required
  860:     Config = #{algorithm => <<"HS512">>,
  861:                secret => {value, <<"secret123">>},
  862:                username_key => user},
  863:     ?cfgh([auth, jwt], Config,
  864:           auth_raw(<<"jwt">>, Opts)),
  865:     ?cfgh([auth, jwt, secret], {file, "priv/jwt_secret"},
  866:           auth_raw(<<"jwt">>, Opts#{<<"secret">> := #{<<"file">> => <<"priv/jwt_secret">>}})),
  867:     ?cfgh([auth, jwt, secret], {env, "SECRET"},
  868:           auth_raw(<<"jwt">>, Opts#{<<"secret">> := #{<<"env">> => <<"SECRET">>}})),
  869:     ?errh(auth_raw(<<"jwt">>, Opts#{<<"secret">> := #{<<"value">> => 123}})),
  870:     ?errh(auth_raw(<<"jwt">>, Opts#{<<"secret">> := #{<<"file">> => <<>>}})),
  871:     ?errh(auth_raw(<<"jwt">>, Opts#{<<"secret">> := #{<<"env">> => <<>>}})),
  872:     ?errh(auth_raw(<<"jwt">>, Opts#{<<"secret">> := #{<<"file">> => <<"/jwt_secret">>,
  873:                                                       <<"env">> => <<"SECRET">>}})),
  874:     ?errh(auth_raw(<<"jwt">>, Opts#{<<"algorithm">> := <<"bruteforce">>})),
  875:     ?errh(auth_raw(<<"jwt">>, Opts#{<<"username_key">> := <<>>})),
  876:     [?errh(auth_raw(<<"jwt">>, maps:without([K], Opts))) || K <- maps:keys(Opts)].
  877: 
  878: auth_rdbms_users_number_estimate(_Config) ->
  879:     ?cfgh([auth, rdbms, users_number_estimate], false, auth_raw(<<"rdbms">>, #{})), % default
  880:     ?cfgh([auth, rdbms, users_number_estimate], true,
  881:           auth_raw(<<"rdbms">>, #{<<"users_number_estimate">> => true})),
  882:     ?errh(auth_raw(<<"rdbms">>, #{<<"users_number_estimate">> => 1200})).
  883: 
  884: auth_dummy(_Config) ->
  885:     ?cfgh([auth, dummy], #{base_time => 50, variance => 450}, auth_raw(<<"dummy">>, #{})), % default
  886:     ?cfgh([auth, dummy, base_time], 0, auth_raw(<<"dummy">>, #{<<"base_time">> => 0})),
  887:     ?cfgh([auth, dummy, variance], 10, auth_raw(<<"dummy">>, #{<<"variance">> => 10})),
  888:     ?errh(auth_raw(<<"dummy">>, #{<<"base_time">> => -5})),
  889:     ?errh(auth_raw(<<"dummy">>, #{<<"variance">> => 0})).
  890: 
  891: %% tests: outgoing_pools
  892: 
  893: pool_basics(_Config) ->
  894:     P = [outgoing_pools, 1],
  895:     Required = #{<<"connection">> => #{<<"host">> => <<"http://localhost">>}},
  896:     ?cfg(P ++ [type], http, pool_raw(<<"http">>, <<"default">>, Required)),
  897:     ?cfg(P ++ [tag], default, pool_raw(<<"http">>, <<"default">>, Required)),
  898:     ?cfg(host_opts([{P ++ [tag], default}]),
  899:          host_config(pool_raw(<<"http">>, <<"default">>, Required))),
  900:     ?err(pool_raw(<<"swimming_pool">>, <<"default">>, Required)),
  901:     ?err(pool_raw(<<"http">>, 1000, Required)).
  902: 
  903: pool_scope(_Config) ->
  904:     P = [outgoing_pools, 1, scope],
  905:     Required = #{<<"connection">> => #{<<"host">> => <<"http://localhost">>}},
  906:     T = fun(Opts) -> pool_raw(<<"http">>, <<"default">>, maps:merge(Required, Opts)) end,
  907:     ?cfg(P, host_type, T(#{<<"scope">> => <<"host">>})),
  908:     ?cfg(P, host_type, T(#{<<"scope">> => <<"host_type">>})),
  909:     ?err(T(#{<<"scope">> => <<"whatever">>})),
  910:     ?err(host_config(T(#{<<"scope">> => <<"global">>}))). %% scope is not allowed in host_config
  911: 
  912: pool_rdbms(_Config) ->
  913:     test_pool_opts(rdbms, #{<<"connection">> => raw_sql_opts(pgsql)}).
  914: 
  915: pool_rdbms_connection_odbc(_Config) ->
  916:     P = [outgoing_pools, 1, conn_opts],
  917:     Required = #{<<"driver">> => <<"odbc">>, <<"settings">> => <<"DSN=mydb">>},
  918:     T = fun(Opts) -> pool_conn_raw(<<"rdbms">>, Opts) end,
  919:     test_pool_rdbms_connection_common_opts(P, T, Required),
  920:     ?cfg(P, config([outgoing_pools, rdbms, default, conn_opts],
  921:                    #{driver => odbc, settings => "DSN=mydb"}), T(Required)),
  922:     ?err(T(Required#{<<"settings">> => true})),
  923:     [?err(T(maps:remove(K, Required))) || K <- maps:keys(Required)].
  924: 
  925: pool_rdbms_connection_pgsql(_Config) ->
  926:     P = [outgoing_pools, 1, conn_opts],
  927:     T = fun(Opts) -> pool_conn_raw(<<"rdbms">>, Opts) end,
  928:     Required = raw_sql_opts(pgsql),
  929:     test_pool_rdbms_connection_common_opts(P, T, Required),
  930:     test_pool_rdbms_connection_sql_opts(P, T, Required, sql_opts(pgsql, 5432)).
  931: 
  932: pool_rdbms_connection_tls_pgsql(_Config) ->
  933:     P = [outgoing_pools, 1, conn_opts, tls],
  934:     Required = raw_sql_opts(pgsql),
  935:     T = fun(Opts) -> pool_conn_raw(<<"rdbms">>, Required#{<<"tls">> => Opts}) end,
  936:     M = tls_ca_raw(),
  937:     ?cfg(P, config([outgoing_pools, rdbms, default, conn_opts, tls], (tls_ca())#{required => false}),
  938:          T(M)),
  939:     ?cfg(P ++ [required], true, T(M#{<<"required">> => true})),
  940:     ?err(T(M#{<<"required">> => <<"maybe">>})),
  941:     test_just_tls_client(P, T).
  942: 
  943: pool_rdbms_connection_mysql(_Config) ->
  944:     P = [outgoing_pools, 1, conn_opts],
  945:     T = fun(Opts) -> pool_conn_raw(<<"rdbms">>, Opts) end,
  946:     Required = raw_sql_opts(mysql),
  947:     test_pool_rdbms_connection_common_opts(P, T, Required),
  948:     test_pool_rdbms_connection_sql_opts(P, T, Required, sql_opts(mysql, 3306)).
  949: 
  950: pool_rdbms_connection_tls_mysql(_Config) ->
  951:     P = [outgoing_pools, 1, conn_opts, tls],
  952:     Required = raw_sql_opts(mysql),
  953:     T = fun(Opts) -> pool_conn_raw(<<"rdbms">>, Required#{<<"tls">> => Opts}) end,
  954:     M = tls_ca_raw(),
  955:     ?cfg(P, config([outgoing_pools, rdbms, default, conn_opts, tls], tls_ca()), T(M)),
  956:     ?err(T(M#{<<"required">> => true})), % only for pgsql
  957:     test_just_tls_client(P, T).
  958: 
  959: test_pool_rdbms_connection_sql_opts(P, T, Required, Expected) ->
  960:     ?cfg(P, config([outgoing_pools, rdbms, default, conn_opts], Expected), T(Required)),
  961:     ?cfg(P ++ [port], 1234, T(Required#{<<"port">> => 1234})),
  962:     ?err(T(Required#{<<"host">> => <<>>})),
  963:     ?err(T(Required#{<<"port">> => -1})),
  964:     ?err(T(Required#{<<"database">> => <<>>})),
  965:     ?err(T(Required#{<<"username">> => <<>>})),
  966:     ?err(T(Required#{<<"password">> => <<>>})).
  967: 
  968: test_pool_rdbms_connection_common_opts(P, T, Required) ->
  969:     ?cfg(P ++ [query_timeout], 100, T(Required#{<<"query_timeout">> => 100})),
  970:     ?cfg(P ++ [keepalive_interval], 100, T(Required#{<<"keepalive_interval">> => 100})),
  971:     ?cfg(P ++ [max_start_interval], 200, T(Required#{<<"max_start_interval">> => 200})),
  972:     ?err(T(Required#{<<"query_timeout">> => -1})),
  973:     ?err(T(Required#{<<"keepalive_interval">> => 0})),
  974:     ?err(T(Required#{<<"max_start_interval">> => 0})),
  975:     [?err(T(maps:remove(K, Required))) || K <- maps:keys(Required)].
  976: 
  977: raw_sql_opts(Driver) ->
  978:     #{<<"driver">> => atom_to_binary(Driver),
  979:       <<"host">> => <<"localhost">>,
  980:       <<"database">> => <<"db">>,
  981:       <<"username">> => <<"dbuser">>,
  982:       <<"password">> => <<"secret">>}.
  983: 
  984: sql_opts(Driver, Port) ->
  985:     #{driver => Driver,
  986:       host => "localhost",
  987:       port => Port,
  988:       database => "db",
  989:       username => "dbuser",
  990:       password => "secret"}.
  991: 
  992: pool_http(_Config) ->
  993:     test_pool_opts(http, #{<<"connection">> => #{<<"host">> => <<"https://localhost:8443">>}}).
  994: 
  995: pool_http_connection(_Config) ->
  996:     P = [outgoing_pools, 1, conn_opts],
  997:     T = fun(Opts) -> pool_conn_raw(<<"http">>, Opts) end,
  998:     Required = #{<<"host">> => <<"https://localhost:8443">>},
  999:     ?cfg(P, config([outgoing_pools, http, default, conn_opts], #{host => "https://localhost:8443"}),
 1000:          T(Required)),
 1001:     ?cfg(P ++ [path_prefix], <<"/my_path/">>, T(Required#{<<"path_prefix">> => <<"/my_path/">>})),
 1002:     ?cfg(P ++ [request_timeout], 999, T(Required#{<<"request_timeout">> => 999})),
 1003:     ?err(T(#{})),
 1004:     ?err(T(#{<<"host">> => <<>>})),
 1005:     ?err(T(Required#{<<"path_prefix">> => <<>>})),
 1006:     ?err(T(Required#{<<"request_timeout">> => -1000})).
 1007: 
 1008: pool_http_connection_tls(_Config) ->
 1009:     P = [outgoing_pools, 1, conn_opts, tls],
 1010:     T = fun(Opts) -> pool_conn_raw(<<"http">>, #{<<"host">> => <<"http://localhost">>,
 1011:                                                  <<"tls">> => Opts}) end,
 1012:     ?cfg(P, config([outgoing_pools, http, default, conn_opts, tls], tls_ca()), T(tls_ca_raw())),
 1013:     test_just_tls_client(P, T).
 1014: 
 1015: pool_redis(_Config) ->
 1016:     test_pool_opts(redis, #{}).
 1017: 
 1018: pool_redis_connection(_Config) ->
 1019:     P = [outgoing_pools, 1, conn_opts],
 1020:     T = fun(Opts) -> pool_conn_raw(<<"redis">>, Opts) end,
 1021:     ?cfg(P, default_config([outgoing_pools, redis, default, conn_opts]), T(#{})),
 1022:     ?cfg(P ++ [host], "my_host", T(#{<<"host">> => <<"my_host">>})),
 1023:     ?cfg(P ++ [port], 9999, T(#{<<"port">> => 9999})),
 1024:     ?cfg(P ++ [database], 1, T(#{<<"database">> => 1})),
 1025:     ?cfg(P ++ [password], "password1", T(#{<<"password">> => <<"password1">>})),
 1026:     ?err(T(#{<<"host">> => 8443})),
 1027:     ?err(T(#{<<"port">> => 666666})),
 1028:     ?err(T(#{<<"database">> => -1})),
 1029:     ?err(T(#{<<"password">> => 0})).
 1030: 
 1031: pool_cassandra(_Config) ->
 1032:     test_pool_opts(cassandra, #{<<"connection">> => #{}}).
 1033: 
 1034: pool_cassandra_connection(_Config) ->
 1035:     P = [outgoing_pools, 1, conn_opts],
 1036:     T = fun(Opts) -> pool_conn_raw(<<"cassandra">>, Opts) end,
 1037:     ?cfg(P, default_config([outgoing_pools, cassandra, default, conn_opts]), T(#{})),
 1038:     ?cfg(P ++ [keyspace], big_mongooseim, T(#{<<"keyspace">> => <<"big_mongooseim">>})),
 1039:     ?err(T(#{<<"keyspace">> => <<>>})).
 1040: 
 1041: pool_cassandra_connection_auth_plain(_Config) ->
 1042:     P = [outgoing_pools, 1, conn_opts, auth, plain],
 1043:     T = fun(Opts) -> pool_conn_raw(<<"cassandra">>, #{<<"auth">> => #{<<"plain">> => Opts}}) end,
 1044:     Required = #{<<"username">> => <<"user">>, <<"password">> => <<"pass">>},
 1045:     ?cfg(P, #{username => <<"user">>, password => <<"pass">>}, T(Required)),
 1046:     [?err(T(maps:remove(K, Required))) || K <- maps:keys(Required)],
 1047:     [?err(T(Required#{K => false})) || K <- maps:keys(Required)].
 1048: 
 1049: pool_cassandra_connection_servers(_Config) ->
 1050:     P = [outgoing_pools, 1, conn_opts, servers],
 1051:     T = fun(Servers) -> pool_conn_raw(<<"cassandra">>, #{<<"servers">> => Servers}) end,
 1052:     Required = #{<<"host">> => <<"example.com">>},
 1053:     ?cfg(P, [#{host => "example.com", port => 9042}, % default port
 1054:              #{host => "example.com", port => 9043}],
 1055:          T([Required, Required#{<<"port">> => 9043}])),
 1056:     ?err(T([Required, Required#{<<"port">> => 9042}])), % same port for both servers
 1057:     ?err(T([#{}])), % missing host
 1058:     ?err(T([])). % no servers
 1059: 
 1060: pool_cassandra_connection_tls(_Config) ->
 1061:     P = [outgoing_pools, 1, conn_opts, tls],
 1062:     T = fun(Opts) -> pool_conn_raw(<<"cassandra">>, #{<<"tls">> => Opts}) end,
 1063:     ?cfg(P, config([outgoing_pools, cassandra, default, conn_opts, tls], tls_ca()), T(tls_ca_raw())),
 1064:     test_just_tls_client(P, T).
 1065: 
 1066: pool_elastic(_Config) ->
 1067:     test_pool_opts(elastic, #{<<"connection">> => #{}}).
 1068: 
 1069: pool_elastic_connection(_Config) ->
 1070:     P = [outgoing_pools, 1, conn_opts],
 1071:     T = fun(Opts) -> pool_conn_raw(<<"elastic">>, Opts) end,
 1072:     ?cfg(P, default_config([outgoing_pools, elastic, default, conn_opts]), T(#{})),
 1073:     ?cfg(P ++ [host], <<"my_host">>, T(#{<<"host">> => <<"my_host">>})),
 1074:     ?cfg(P ++ [port], 9999, T(#{<<"port">> => 9999})),
 1075:     ?err(T(#{<<"host">> => <<>>})),
 1076:     ?err(T(#{<<"port">> => 123456})).
 1077: 
 1078: pool_rabbit(_Config) ->
 1079:     test_pool_opts(rabbit, #{<<"connection">> => #{}}).
 1080: 
 1081: pool_rabbit_connection(_Config) ->
 1082:     P = [outgoing_pools, 1, conn_opts],
 1083:     T = fun(Opts) -> pool_conn_raw(<<"rabbit">>, Opts) end,
 1084:     ?cfg(P, default_config([outgoing_pools, rabbit, default, conn_opts]), T(#{})),
 1085:     ?cfg(P ++ [host], "my_host", T(#{<<"host">> => <<"my_host">>})),
 1086:     ?cfg(P ++ [port], 9999, T(#{<<"port">> => 9999})),
 1087:     ?cfg(P ++ [username], <<"user">>, T(#{<<"username">> => <<"user">>})),
 1088:     ?cfg(P ++ [password], <<"pass">>, T(#{<<"password">> => <<"pass">>})),
 1089:     ?cfg(P ++ [confirms_enabled], true, T(#{<<"confirms_enabled">> => true})),
 1090:     ?cfg(P ++ [max_worker_queue_len], 100, T(#{<<"max_worker_queue_len">> => 100})),
 1091:     ?err(T(#{<<"host">> => <<>>})),
 1092:     ?err(T(#{<<"port">> => 123456})),
 1093:     ?err(T(#{<<"username">> => <<>>})),
 1094:     ?err(T(#{<<"password">> => <<>>})),
 1095:     ?err(T(#{<<"confirms_enabled">> => <<"yes">>})),
 1096:     ?err(T(#{<<"max_worker_queue_len">> => -1})).
 1097: 
 1098: pool_ldap(_Config) ->
 1099:     test_pool_opts(ldap, #{<<"connection">> => #{}}).
 1100: 
 1101: pool_ldap_connection(_Config) ->
 1102:     P = [outgoing_pools, 1, conn_opts],
 1103:     T = fun(Opts) -> pool_conn_raw(<<"ldap">>, Opts) end,
 1104:     ?cfg(P, default_config([outgoing_pools, ldap, default, conn_opts]), T(#{})),
 1105:     ?cfg(P ++ [servers], ["server1.example.com", "server2.example.com"],
 1106:          T(#{<<"servers">> => [<<"server1.example.com">>, <<"server2.example.com">>]})),
 1107:     ?cfg(P ++ [port], 999, T(#{<<"port">> => 999})),
 1108:     ?cfg(P ++ [root_dn], <<"my_rootdn">>, T(#{<<"root_dn">> => <<"my_rootdn">>})),
 1109:     ?cfg(P ++ [password], <<"pass">>, T(#{<<"password">> => <<"pass">>})),
 1110:     ?cfg(P ++ [connect_interval], 5000, T(#{<<"connect_interval">> => 5000})),
 1111:     ?cfg(P ++ [port], 636, T(#{<<"tls">> => tls_ca_raw()})), % default TLS port is different
 1112:     ?err(T(#{<<"servers">> => [<<"server1.example.com">>, <<"server1.example.com">>]})),
 1113:     ?err(T(#{<<"servers">> => []})),
 1114:     ?err(T(#{<<"port">> => 123456})),
 1115:     ?err(T(#{<<"root_dn">> => 1})),
 1116:     ?err(T(#{<<"password">> => true})),
 1117:     ?err(T(#{<<"connect_interval">> => <<"infinity">>})).
 1118: 
 1119: pool_ldap_connection_tls(_Config) ->
 1120:     P = [outgoing_pools, 1, conn_opts, tls],
 1121:     T = fun(Opts) -> pool_conn_raw(<<"ldap">>, #{<<"tls">> => Opts}) end,
 1122:     ?cfg(P, config([outgoing_pools, ldap, default, conn_opts, tls], tls_ca()), T(tls_ca_raw())),
 1123:     test_just_tls_client(P, T).
 1124: 
 1125: test_pool_opts(Type, Required) ->
 1126:     P = [outgoing_pools, 1, opts],
 1127:     T = fun(Opts) -> pool_raw(atom_to_binary(Type), <<"default">>, Opts) end,
 1128:     ?cfg(P, default_config([outgoing_pools, Type, default, opts]), T(Required)),
 1129:     ?cfg(P ++ [workers], 11, T(Required#{<<"workers">> => 11})),
 1130:     ?cfg(P ++ [strategy], random_worker, T(Required#{<<"strategy">> => <<"random_worker">>})),
 1131:     ?cfg(P ++ [call_timeout], 999, T(Required#{<<"call_timeout">> => 999})),
 1132:     ?err(T(Required#{<<"workers">> => 0})),
 1133:     ?err(T(Required#{<<"strategy">> => <<"worst_worker">>})),
 1134:     ?err(T(Required#{<<"call_timeout">> => 0})).
 1135: 
 1136: test_just_tls_client(P, T) ->
 1137:     test_just_tls_common(P, T),
 1138:     test_just_tls_client_sni(P, T),
 1139:     M = tls_ca_raw(),
 1140:     ?err(T(M#{<<"dhfile">> => <<"priv/dh.pem">>})). % server-only
 1141: 
 1142: test_just_tls_server(P, T) ->
 1143:     test_just_tls_common(P, T),
 1144:     M = tls_ca_raw(),
 1145:     ?cfg(P ++ [dhfile], "priv/dh.pem", T(M#{<<"dhfile">> => <<"priv/dh.pem">>})),
 1146:     ?err(T(M#{<<"dhfile">> => <<"no_such_file.pem">>})).
 1147: 
 1148: test_just_tls_common(P, T) ->
 1149:     ?cfg(P ++ [verify_mode], none, T(#{<<"verify_mode">> => <<"none">>})),
 1150:     M = tls_ca_raw(),
 1151:     ?cfg(P ++ [cacertfile], "priv/ca.pem", T(M)),
 1152:     ?cfg(P ++ [certfile], "priv/cert.pem", T(M#{<<"certfile">> => <<"priv/cert.pem">>})),
 1153:     ?cfg(P ++ [ciphers], "TLS_AES_256_GCM_SHA384",
 1154:          T(M#{<<"ciphers">> => <<"TLS_AES_256_GCM_SHA384">>})),
 1155:     ?cfg(P ++ [keyfile], "priv/dc1.pem", T(M#{<<"keyfile">> => <<"priv/dc1.pem">>})),
 1156:     ?cfg(P ++ [password], "secret", T(M#{<<"password">> => <<"secret">>})),
 1157:     ?cfg(P ++ [versions], ['tlsv1.2', 'tlsv1.3'],
 1158:          T(M#{<<"versions">> => [<<"tlsv1.2">>, <<"tlsv1.3">>]})),
 1159:     ?err([#{reason := missing_cacertfile}], T(#{})),
 1160:     ?err([#{reason := missing_cacertfile}], T(#{<<"verify_mode">> => <<"peer">>})),
 1161:     ?err([#{reason := missing_cacertfile}], T(#{<<"verify_mode">> => <<"selfsigned_peer">>})),
 1162:     ?err(T(#{<<"verify_mode">> => <<"whatever">>})),
 1163:     ?err(T(M#{<<"certfile">> => <<"no_such_file.pem">>})),
 1164:     ?err(T(M#{<<"cacertfile">> => <<"no_such_file.pem">>})),
 1165:     ?err(T(M#{<<"ciphers">> => [<<"TLS_AES_256_GCM_SHA384">>]})),
 1166:     ?err(T(M#{<<"keyfile">> => <<"no_such_file.pem">>})),
 1167:     ?err(T(M#{<<"password">> => false})),
 1168:     ?err(T(M#{<<"versions">> => <<"tlsv1.2">>})),
 1169:     ?err(T(M#{<<"protocol_options">> => [<<"nosslv2">>]})). % only for fast_tls
 1170: 
 1171: test_just_tls_client_sni(ParentP, ParentT) ->
 1172:     P = ParentP ++ [server_name_indication],
 1173:     M = tls_ca_raw(),
 1174:     T = fun(Opts) -> ParentT(M#{<<"server_name_indication">> => Opts}) end,
 1175:     ?cfg(P ++ [enabled], false, T(#{<<"enabled">> => false})),
 1176:     ?cfg(P ++ [host], "host.example.com", T(#{<<"host">> => <<"host.example.com">>})),
 1177:     ?cfg(P ++ [protocol], https, T(#{<<"protocol">> => <<"https">>})),
 1178:     ?err(T(#{<<"enabled">> => <<"maybe">>})),
 1179:     ?err(T(#{<<"host">> => <<>>})),
 1180:     ?err(T(#{<<"protocol">> => <<"http">>})).
 1181: 
 1182: test_fast_tls_server(P, T) ->
 1183:     ?cfg(P ++ [verify_mode], none, T(#{<<"verify_mode">> => <<"none">>})),
 1184:     ?cfg(P ++ [certfile], "priv/cert.pem", T(#{<<"certfile">> => <<"priv/cert.pem">>})),
 1185:     ?cfg(P ++ [cacertfile], "priv/ca.pem", T(tls_ca_raw())),
 1186:     ?cfg(P ++ [ciphers], "TLS_AES_256_GCM_SHA384",
 1187:          T(#{<<"ciphers">> => <<"TLS_AES_256_GCM_SHA384">>})),
 1188:     ?cfg(P ++ [dhfile], "priv/dh.pem", T(#{<<"dhfile">> => <<"priv/dh.pem">>})),
 1189:     ?cfg(P ++ [protocol_options], ["nosslv2"], T(#{<<"protocol_options">> => [<<"nosslv2">>]})),
 1190:     ?err(T(#{<<"verify_mode">> => <<"selfsigned_peer">>})), % value only for just_tls
 1191:     ?err(T(#{<<"crl_files">> => [<<"priv/cert.pem">>]})), % option only for just_tls
 1192:     ?err(T(#{<<"certfile">> => <<"no_such_file.pem">>})),
 1193:     ?err(T(#{<<"cacertfile">> => <<"no_such_file.pem">>})),
 1194:     ?err(T(#{<<"ciphers">> => [<<"TLS_AES_256_GCM_SHA384">>]})),
 1195:     ?err(T(#{<<"dhfile">> => <<"no_such_file.pem">>})),
 1196:     ?err(T(#{<<"keyfile">> => <<"priv/dc1.pem">>})), % option only for just_tls
 1197:     ?err(T(#{<<"password">> => <<"secret">>})), % option only for just_tls
 1198:     ?err(T(#{<<"versions">> => [<<"tlsv1.2">>]})), % option only for just_tls
 1199:     ?err(T(#{<<"protocol_options">> => [<<>>]})).
 1200: 
 1201: tls_ca() ->
 1202:     #{cacertfile => "priv/ca.pem"}.
 1203: 
 1204: tls_ca_raw() ->
 1205:     #{<<"cacertfile">> => <<"priv/ca.pem">>}.
 1206: 
 1207: %% tests: internal_databases
 1208: 
 1209: internal_database_cets(_Config) ->
 1210:     CetsEnabled = #{<<"internal_databases">> => #{<<"cets">> => #{}}},
 1211:     CetsFile = #{<<"internal_databases">> => #{<<"cets">> =>
 1212:         #{<<"backend">> => <<"file">>, <<"node_list_file">> => <<"/dev/null">>}}},
 1213:     %% No internal_databases section means only mnesia
 1214:     ?cfg([internal_databases], #{mnesia => #{}}, #{}), % default
 1215:     %% Empty internal_databases could be configured explicitly
 1216:     ?cfg([internal_databases], #{}, #{<<"internal_databases">> => #{}}),
 1217: 
 1218:     ?cfg([internal_databases, cets, backend], file,
 1219:          #{<<"internal_databases">> => #{<<"cets">> => #{<<"backend">> => <<"file">>}}}),
 1220:     ?cfg([internal_databases, cets, backend], rdbms,
 1221:          #{<<"internal_databases">> => #{<<"cets">> => #{<<"cluster_name">> => <<"test">>}}}),
 1222: 
 1223:     ?cfg([internal_databases, cets, cluster_name], mongooseim, CetsEnabled),
 1224:     ?cfg([internal_databases, cets, node_list_file], "/dev/null", CetsFile),
 1225:     %% If only mnesia section is defined, CETS section is not included
 1226:     ?cfg([internal_databases], #{mnesia => #{}},
 1227:          #{<<"internal_databases">> => #{<<"mnesia">> => #{}}}),
 1228:     ?err(#{<<"internal_databases">> => #{<<"cets">> => #{<<"backend">> => <<"mnesia">>}}}),
 1229:     ?err(#{<<"internal_databases">> => #{<<"cets">> => #{<<"cluster_name">> => 123}}}).
 1230: 
 1231: %% tests: shaper, acl, access
 1232: shaper(_Config) ->
 1233:     ?cfg([shaper, normal], #{max_rate => 1000},
 1234:          #{<<"shaper">> => #{<<"normal">> => #{<<"max_rate">> => 1000}}}),
 1235:     ?err(#{<<"shaper">> => #{<<"unlimited">> => #{<<"max_rate">> => <<"infinity">>}}}),
 1236:     ?err(#{<<"shaper">> => #{<<"fast">> => #{}}}).
 1237: 
 1238: acl(_Config) ->
 1239:     ?cfgh([acl, local], [#{match => all}],
 1240:           #{<<"acl">> => #{<<"local">> => [#{<<"match">> => <<"all">>}]}}),
 1241:     ?cfgh([acl, local], [#{match => any_hosted_domain}],
 1242:           #{<<"acl">> => #{<<"local">> => [#{<<"match">> => <<"any_hosted_domain">>}]}}),
 1243:     ?cfgh([acl, local], [#{match => current_domain,
 1244:                            user_regexp => <<>>}],
 1245:           #{<<"acl">> => #{<<"local">> => [#{<<"user_regexp">> => <<>>}]}}),
 1246:     ?cfgh([acl, alice], [#{match => current_domain,
 1247:                            user_regexp => <<"ali.*">>,
 1248:                            server_regexp => <<".*host">>}],
 1249:           #{<<"acl">> => #{<<"alice">> => [#{<<"user_regexp">> => <<"aLi.*">>,
 1250:                                              <<"server_regexp">> => <<".*HosT">>}]}}),
 1251:     ?cfgh([acl, alice], [#{match => current_domain,
 1252:                            user => <<"alice">>,
 1253:                            server => <<"localhost">>}],
 1254:           #{<<"acl">> => #{<<"alice">> => [#{<<"user">> => <<"alice">>,
 1255:                                              <<"server">> => <<"localhost">>}]}}),
 1256:     ?errh(#{<<"acl">> => #{<<"local">> => <<"everybody">>}}),
 1257:     ?errh(#{<<"acl">> => #{<<"local">> => [#{<<"match">> => <<"lit">>}]}}),
 1258:     ?errh(#{<<"acl">> => #{<<"alice">> => [#{<<"user_glob">> => <<"a*">>,
 1259:                                              <<"server_blog">> => <<"bloghost">>}]}}),
 1260:     ?errh([#{reason := incorrect_acl_condition_value}],
 1261:           #{<<"acl">> => #{<<"local">> => [#{<<"user">> => <<"@@@">>}]}}).
 1262: 
 1263: acl_merge_host_and_global(_Config) ->
 1264:     G = #{<<"acl">> => #{<<"admin">> => [#{<<"user">> => <<"george">>}]}},
 1265:     H1 = #{<<"acl">> => #{<<"admin">> => [#{<<"user">> => <<"henry">>}]}},
 1266:     H2 = #{<<"acl">> => #{<<"hostile">> => [#{<<"user">> => <<"hacker">>}]}},
 1267:     ?cfg([{{acl, global}, #{admin => [#{user => <<"george">>, match => current_domain}]}},
 1268:           {{acl, ?HOST}, #{admin => [#{user => <<"george">>, match => current_domain}]}}],
 1269:          maps:merge(G, host_config(G))),
 1270:     ?cfg([{{acl, global}, #{admin => [#{user => <<"george">>, match => current_domain}]}},
 1271:           {{acl, ?HOST}, #{admin => [#{user => <<"george">>, match => current_domain},
 1272:                                      #{user => <<"henry">>, match => current_domain}]}}],
 1273:          maps:merge(G, host_config(H1))),
 1274:     ?cfg([{{acl, global}, #{admin => [#{user => <<"george">>, match => current_domain}]}},
 1275:           {{acl, ?HOST}, #{admin => [#{user => <<"george">>, match => current_domain}],
 1276:                            hostile => [#{user => <<"hacker">>, match => current_domain}]}}],
 1277:          maps:merge(G, host_config(H2))).
 1278: 
 1279: access(_Config) ->
 1280:     ?cfgh([access, c2s], [#{acl => blocked, value => deny},
 1281:                           #{acl => all, value => allow}],
 1282:           access_raw(<<"c2s">>, [#{<<"acl">> => <<"blocked">>, <<"value">> => <<"deny">>},
 1283:                                  #{<<"acl">> => <<"all">>, <<"value">> => <<"allow">>}])),
 1284:     ?cfgh([access, max_user_sessions], [#{acl => all, value => 10}],
 1285:           access_raw(<<"max_user_sessions">>, [#{<<"acl">> => <<"all">>, <<"value">> => 10}])),
 1286:     ?errh(access_raw(<<"max_user_sessions">>, [#{<<"acl">> => <<"all">>}])),
 1287:     ?errh(access_raw(<<"max_user_sessions">>, [#{<<"value">> => 10}])),
 1288:     ?errh(access_raw(<<"max_user_sessions">>, [#{<<"acl">> => 10, <<"value">> => 10}])).
 1289: 
 1290: access_merge_host_and_global(_Config) ->
 1291:     G1 = access_raw(<<"c2s">>, [#{<<"acl">> => <<"good">>, <<"value">> => <<"allow">>}]),
 1292:     G2 = access_raw(<<"c2s">>, [#{<<"acl">> => <<"gangsters">>, <<"value">> => <<"deny">>},
 1293:                                 #{<<"acl">> => <<"all">>, <<"value">> => <<"allow">>}]),
 1294:     H1 = access_raw(<<"c2s">>, [#{<<"acl">> => <<"harmless">>, <<"value">> => <<"allow">>}]),
 1295:     H2 = access_raw(<<"s2s">>, [#{<<"acl">> => <<"harmless">>, <<"value">> => <<"allow">>}]),
 1296:     H3 = access_raw(<<"c2s">>, [#{<<"acl">> => <<"hackers">>, <<"value">> => <<"deny">>}]),
 1297:     ?cfg([{{access, global}, #{c2s => [#{acl => good, value => allow}]}},
 1298:           {{access, ?HOST}, #{c2s => [#{acl => good, value => allow}]}}],
 1299:          maps:merge(G1, host_config(G1))),
 1300:     ?cfg([{{access, global}, #{c2s => [#{acl => good, value => allow}]}},
 1301:           {{access, ?HOST}, #{c2s => [#{acl => good, value => allow},
 1302:                                       #{acl => harmless, value => allow}]}}],
 1303:          maps:merge(G1, host_config(H1))),
 1304:     ?cfg([{{access, global}, #{c2s => [#{acl => good, value => allow}]}},
 1305:           {{access, ?HOST}, #{c2s => [#{acl => good, value => allow}],
 1306:                               s2s => [#{acl => harmless, value => allow}]}}],
 1307:          maps:merge(G1, host_config(H2))),
 1308:     ?cfg([{{access, global}, #{c2s => [#{acl => gangsters, value => deny},
 1309:                                        #{acl => all, value => allow}]}},
 1310:           {{access, ?HOST}, #{c2s => [#{acl => gangsters, value => deny},
 1311:                                       #{acl => hackers, value => deny},
 1312:                                       #{acl => all, value => allow}]}}],
 1313:          maps:merge(G2, host_config(H3))).
 1314: 
 1315: %% tests: s2s
 1316: 
 1317: s2s_host_config(_Config) ->
 1318:     DefaultS2S = default_s2s(),
 1319:     EmptyHostConfig = host_config(#{<<"s2s">> => #{}}),
 1320:     ?cfg(host_key(s2s), DefaultS2S,
 1321:          EmptyHostConfig#{<<"s2s">> => #{<<"dns">> => #{<<"timeout">> => 5}}}),
 1322:     StartTLSHostConfig = host_config(#{<<"s2s">> => #{<<"use_starttls">> => <<"required">>}}),
 1323:     ?cfg(host_key(s2s), DefaultS2S#{use_starttls => required},
 1324:          StartTLSHostConfig#{<<"s2s">> => #{<<"dns">> => #{<<"timeout">> => 5}}}).
 1325: 
 1326: s2s_dns_timeout(_Config) ->
 1327:     ?cfgh([s2s, dns, timeout], 10, #{}), % default
 1328:     ?cfgh([s2s, dns, timeout], 5, #{<<"s2s">> => #{<<"dns">> => #{<<"timeout">> => 5}}}),
 1329:     ?errh(#{<<"s2s">> => #{<<"dns">> => #{<<"timeout">> => 0}}}).
 1330: 
 1331: s2s_dns_retries(_Config) ->
 1332:     ?cfgh([s2s, dns, retries], 2, #{}), % default
 1333:     ?cfgh([s2s, dns, retries], 1, #{<<"s2s">> => #{<<"dns">> => #{<<"retries">> => 1}}}),
 1334:     ?errh(#{<<"s2s">> => #{<<"dns">> => #{<<"retries">> => 0}}}).
 1335: 
 1336: s2s_outgoing_port(_Config) ->
 1337:     ?cfgh([s2s, outgoing, port], 5269, #{}), % default
 1338:     ?cfgh([s2s, outgoing, port], 5270, #{<<"s2s">> => #{<<"outgoing">> => #{<<"port">> => 5270}}}),
 1339:     ?errh(#{<<"s2s">> => #{<<"outgoing">> => #{<<"port">> => <<"http">>}}}).
 1340: 
 1341: s2s_outgoing_ip_versions(_Config) ->
 1342:     ?cfgh([s2s, outgoing, ip_versions], [4, 6], #{}), % default
 1343:     ?cfgh([s2s, outgoing, ip_versions], [6, 4],
 1344:          #{<<"s2s">> => #{<<"outgoing">> => #{<<"ip_versions">> => [6, 4]}}}),
 1345:     ?errh(#{<<"s2s">> => #{<<"outgoing">> => #{<<"ip_versions">> => []}}}),
 1346:     ?errh(#{<<"s2s">> => #{<<"outgoing">> => #{<<"ip_versions">> => [<<"http">>]}}}).
 1347: 
 1348: s2s_outgoing_timeout(_Config) ->
 1349:     ?cfgh([s2s, outgoing, connection_timeout], 10000, #{}), % default
 1350:     ?cfgh([s2s, outgoing, connection_timeout], 5000,
 1351:           #{<<"s2s">> => #{<<"outgoing">> => #{<<"connection_timeout">> => 5000}}}),
 1352:     ?cfgh([s2s, outgoing, connection_timeout], infinity,
 1353:           #{<<"s2s">> => #{<<"outgoing">> => #{<<"connection_timeout">> => <<"infinity">>}}}),
 1354:     ?errh(#{<<"s2s">> => #{<<"outgoing">> => #{<<"connection_timeout">> => 0}}}).
 1355: 
 1356: s2s_use_starttls(_Config) ->
 1357:     ?cfgh([s2s, use_starttls], false, #{}), % default
 1358:     ?cfgh([s2s, use_starttls], required, #{<<"s2s">> => #{<<"use_starttls">> => <<"required">>}}),
 1359:     ?errh(#{<<"s2s">> => #{<<"use_starttls">> => <<"unnecessary">>}}).
 1360: 
 1361: s2s_certfile(_Config) ->
 1362:     ?cfgh([s2s, certfile], "priv/server.pem",  #{<<"s2s">> => #{<<"certfile">> => <<"priv/server.pem">>}}),
 1363:     ?errh([#{reason := invalid_filename}], #{<<"s2s">> => #{<<"certfile">> => <<"nofile.pem">>}}),
 1364:     ?errh(#{<<"s2s">> => #{<<"certfile">> => []}}).
 1365: 
 1366: s2s_default_policy(_Config) ->
 1367:     ?cfgh([s2s, default_policy], allow, #{}), % default
 1368:     ?cfgh([s2s, default_policy], deny, #{<<"s2s">> => #{<<"default_policy">> => <<"deny">>}}),
 1369:     ?errh(#{<<"s2s">> => #{<<"default_policy">> => <<"ask">>}}).
 1370: 
 1371: s2s_host_policy(_Config) ->
 1372:     Policy = #{<<"host">> => <<"host1">>,
 1373:                <<"policy">> => <<"allow">>},
 1374:     ?cfgh([s2s, host_policy], #{<<"host1">> => allow},
 1375:           #{<<"s2s">> => #{<<"host_policy">> => [Policy]}}),
 1376:     ?cfgh([s2s, host_policy], #{<<"host1">> => allow,
 1377:                                 <<"host2">> => deny},
 1378:           #{<<"s2s">> => #{<<"host_policy">> => [Policy, #{<<"host">> => <<"host2">>,
 1379:                                                            <<"policy">> => <<"deny">>}]}}),
 1380:     ?errh(#{<<"s2s">> => #{<<"host_policy">> => [maps:without([<<"host">>], Policy)]}}),
 1381:     ?errh(#{<<"s2s">> => #{<<"host_policy">> => [maps:without([<<"policy">>], Policy)]}}),
 1382:     ?errh(#{<<"s2s">> => #{<<"host_policy">> => [Policy#{<<"host">> => <<>>}]}}),
 1383:     ?errh(#{<<"s2s">> => #{<<"host_policy">> => [Policy#{<<"policy">> => <<"huh">>}]}}),
 1384:     ?errh(#{<<"s2s">> => #{<<"host_policy">> => [Policy,
 1385:                                                  Policy#{<<"policy">> => <<"deny">>}]}}).
 1386: 
 1387: s2s_address(_Config) ->
 1388:     Addr = #{<<"host">> => <<"host1">>,
 1389:              <<"ip_address">> => <<"192.168.1.2">>,
 1390:              <<"port">> => 5321},
 1391:     ?cfgh([s2s, address], #{<<"host1">> => #{ip_address => "192.168.1.2", port => 5321}},
 1392:           #{<<"s2s">> => #{<<"address">> => [Addr]}}),
 1393:     ?cfgh([s2s, address], #{<<"host1">> => #{ip_address => "192.168.1.2"}},
 1394:           #{<<"s2s">> => #{<<"address">> => [maps:without([<<"port">>], Addr)]}}),
 1395:     ?errh(#{<<"s2s">> => #{<<"address">> => [maps:without([<<"host">>], Addr)]}}),
 1396:     ?errh(#{<<"s2s">> => #{<<"address">> => [maps:without([<<"ip_address">>], Addr)]}}),
 1397:     ?errh(#{<<"s2s">> => #{<<"address">> => [Addr#{<<"host">> => <<>>}]}}),
 1398:     ?errh(#{<<"s2s">> => #{<<"address">> => [Addr#{<<"ip_address">> => <<"host2">>}]}}),
 1399:     ?errh(#{<<"s2s">> => #{<<"address">> => [Addr#{<<"port">> => <<"seaport">>}]}}),
 1400:     ?errh(#{<<"s2s">> => #{<<"address">> => [Addr, maps:remove(<<"port">>, Addr)]}}).
 1401: 
 1402: s2s_ciphers(_Config) ->
 1403:     ?cfgh([s2s, ciphers], mongoose_tls:default_ciphers(), #{}), % default
 1404:     ?cfgh([s2s, ciphers], "TLSv1.2",
 1405:           #{<<"s2s">> => #{<<"ciphers">> => <<"TLSv1.2">>}}),
 1406:     ?errh(#{<<"s2s">> => #{<<"ciphers">> => [<<"cipher1">>, <<"cipher2">>]}}).
 1407: 
 1408: s2s_shared(_Config) ->
 1409:     ?cfgh([s2s, shared], <<"secret">>, #{<<"s2s">> => #{<<"shared">> => <<"secret">>}}),
 1410:     ?errh(#{<<"s2s">> => #{<<"shared">> => 536837}}).
 1411: 
 1412: s2s_max_retry_delay(_Config) ->
 1413:     ?cfgh([s2s, max_retry_delay], 120, #{<<"s2s">> => #{<<"max_retry_delay">> => 120}}),
 1414:     ?errh(#{<<"s2s">> => #{<<"max_retry_delay">> => 0}}).
 1415: 
 1416: %% modules
 1417: 
 1418: mod_adhoc(_Config) ->
 1419:     check_module_defaults(mod_adhoc),
 1420:     check_iqdisc(mod_adhoc),
 1421:     P = [modules, mod_adhoc],
 1422:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_adhoc">> => #{K => V}}} end,
 1423:     %% report_commands_node is boolean
 1424:     ?cfgh(P ++ [report_commands_node], true, T(<<"report_commands_node">>, true)),
 1425:     ?cfgh(P ++ [report_commands_node], false, T(<<"report_commands_node">>, false)),
 1426:     %% not boolean
 1427:     ?errh(T(<<"report_commands_node">>, <<"hello">>)).
 1428: 
 1429: mod_auth_token(_Config) ->
 1430:     check_module_defaults(mod_auth_token),
 1431:     check_iqdisc(mod_auth_token),
 1432:     P = [modules, mod_auth_token],
 1433:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_auth_token">> => #{K => V}}} end,
 1434:     ?cfgh(P ++ [backend], rdbms, T(<<"backend">>, <<"rdbms">>)),
 1435:     ?cfgh(P ++ [validity_period, access], #{unit => minutes, value => 13},
 1436:           T(<<"validity_period">>,
 1437:             #{<<"access">> => #{<<"value">> => 13, <<"unit">> => <<"minutes">>}})),
 1438:     ?cfgh(P ++ [validity_period, refresh], #{unit => days, value => 31},
 1439:           T(<<"validity_period">>,
 1440:             #{<<"refresh">> => #{<<"value">> => 31, <<"unit">> => <<"days">>}})),
 1441:     ?errh(T(<<"backend">>, <<"nosql">>)),
 1442:     ?errh(T(<<"validity_period">>,
 1443:             #{<<"access">> => #{<<"value">> => -1, <<"unit">> => <<"minutes">>}})),
 1444:     ?errh(T(<<"validity_period">>,
 1445:             #{<<"access">> => #{<<"value">> => 10, <<"unit">> => <<"centuries">>}})),
 1446:     ?errh(T(<<"validity_period">>, #{<<"access">> => #{<<"value">> => 10}})),
 1447:     ?errh(T(<<"validity_period">>, #{<<"access">> => #{<<"unit">> => <<"days">>}})).
 1448: 
 1449: mod_blocking(_Config) ->
 1450:     test_privacy_opts(mod_blocking).
 1451: 
 1452: mod_bosh(_Config) ->
 1453:     check_module_defaults(mod_bosh),
 1454:     P = [modules, mod_bosh],
 1455:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_bosh">> => #{K => V}}} end,
 1456:     ?cfgh(P ++ [backend], mnesia, T(<<"backend">>, <<"mnesia">>)),
 1457:     ?cfgh(P ++ [inactivity], 10, T(<<"inactivity">>, 10)),
 1458:     ?cfgh(P ++ [inactivity], infinity, T(<<"inactivity">>, <<"infinity">>)),
 1459:     ?cfgh(P ++ [max_wait], 10, T(<<"max_wait">>, 10)),
 1460:     ?cfgh(P ++ [max_wait], infinity, T(<<"max_wait">>, <<"infinity">>)),
 1461:     ?cfgh(P ++ [server_acks], true, T(<<"server_acks">>, true)),
 1462:     ?cfgh(P ++ [server_acks], false, T(<<"server_acks">>, false)),
 1463:     ?cfgh(P ++ [max_pause], 10, T(<<"max_pause">>, 10)),
 1464:     ?errh(T(<<"backend">>, <<"nodejs">>)),
 1465:     ?errh(T(<<"inactivity">>, 0)),
 1466:     ?errh(T(<<"inactivity">>, <<"10">>)),
 1467:     ?errh(T(<<"inactivity">>, <<"inactivity">>)),
 1468:     ?errh(T(<<"max_wait">>, <<"10">>)),
 1469:     ?errh(T(<<"max_wait">>, 0)),
 1470:     ?errh(T(<<"server_acks">>, -1)),
 1471:     ?errh(T(<<"maxpause">>, 0)).
 1472: 
 1473: mod_caps(_Config) ->
 1474:     check_module_defaults(mod_caps),
 1475:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_caps">> => #{K => V}}} end,
 1476:     P = [modules, mod_caps],
 1477:     ?cfgh(P ++ [cache_size], 10, T(<<"cache_size">>, 10)),
 1478:     ?cfgh(P ++ [cache_life_time], 10, T(<<"cache_life_time">>, 10)),
 1479:     ?cfgh(P ++ [backend], mnesia, T(<<"backend">>, <<"mnesia">>)),
 1480:     ?errh(T(<<"cache_size">>, 0)),
 1481:     ?errh(T(<<"cache_size">>, <<"infinity">>)),
 1482:     ?errh(T(<<"cache_life_time">>, 0)),
 1483:     ?errh(T(<<"cache_life_time">>, <<"infinity">>)).
 1484: 
 1485: mod_cache_users(_Config) ->
 1486:     check_module_defaults(mod_cache_users),
 1487:     P = [modules, mod_cache_users],
 1488:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_cache_users">> => #{K => V}}} end,
 1489:     ?cfgh(P ++ [time_to_live], 8600, T(<<"time_to_live">>, 8600)),
 1490:     ?cfgh(P ++ [time_to_live], infinity, T(<<"time_to_live">>, <<"infinity">>)),
 1491:     ?cfgh(P ++ [number_of_segments], 10, T(<<"number_of_segments">>, 10)),
 1492:     ?cfgh(P ++ [strategy], fifo, T(<<"strategy">>, <<"fifo">>)),
 1493:     ?errh(T(<<"time_to_live">>, 0)),
 1494:     ?errh(T(<<"strategy">>, <<"lifo">>)),
 1495:     ?errh(T(<<"number_of_segments">>, 0)),
 1496:     ?errh(T(<<"number_of_segments">>, <<"infinity">>)).
 1497: 
 1498: mod_carboncopy(_Config) ->
 1499:     check_iqdisc(mod_carboncopy).
 1500: 
 1501: mod_csi(_Config) ->
 1502:     check_module_defaults(mod_csi),
 1503:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_csi">> => #{K => V}}} end,
 1504:     P = [modules, mod_csi],
 1505:     ?cfgh(P ++ [buffer_max], 10, T(<<"buffer_max">>, 10)),
 1506:     ?cfgh(P ++ [buffer_max], infinity, T(<<"buffer_max">>, <<"infinity">>)),
 1507:     ?errh(T(<<"buffer_max">>, -1)).
 1508: 
 1509: mod_disco(_Config) ->
 1510:     check_module_defaults(mod_disco),
 1511:     check_iqdisc(mod_disco),
 1512:     P = [modules, mod_disco],
 1513:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_disco">> => #{K => V}}} end,
 1514:     ?cfgh(P ++ [users_can_see_hidden_services], true,
 1515:           T(<<"users_can_see_hidden_services">>, true)),
 1516:     ?cfgh(P ++ [users_can_see_hidden_services], false,
 1517:           T(<<"users_can_see_hidden_services">>, false)),
 1518:     %% extra_domains are binaries
 1519:     ?cfgh(P ++ [extra_domains], [<<"localhost">>, <<"erlang-solutions.com">>],
 1520:           T(<<"extra_domains">>, [<<"localhost">>, <<"erlang-solutions.com">>])),
 1521:     ?cfgh(P ++ [extra_domains], [],
 1522:           T(<<"extra_domains">>, [])),
 1523:     Info = #{<<"name">> => <<"abuse-address">>,
 1524:              <<"urls">> => [<<"admin@example.com">>]},
 1525:     SpiritUrls = [<<"spirit1@localhost">>, <<"spirit2@localhost">>],
 1526:     ?cfgh(P ++ [server_info],
 1527:           [#{name => <<"abuse-address">>, urls => [<<"admin@example.com">>]},
 1528:            #{name => <<"friendly-spirits">>, urls => SpiritUrls, modules => [mod_muc, mod_disco]}],
 1529:           T(<<"server_info">>, [Info, #{<<"name">> => <<"friendly-spirits">>,
 1530:                                         <<"urls">> => SpiritUrls,
 1531:                                         <<"modules">> => [<<"mod_muc">>, <<"mod_disco">>]}])),
 1532:     ?errh(T(<<"users_can_see_hidden_services">>, 1)),
 1533:     ?errh(T(<<"users_can_see_hidden_services">>, <<"true">>)),
 1534:     ?errh(T(<<"extra_domains">>, [<<"user@localhost">>])),
 1535:     ?errh(T(<<"extra_domains">>, [1])),
 1536:     ?errh(T(<<"extra_domains">>, <<"domains domains domains">>)),
 1537:     ?errh(T(<<"server_info">>, [Info#{<<"name">> => 1}])),
 1538:     ?errh(T(<<"server_info">>, [Info#{<<"name">> => <<"">>}])),
 1539:     ?errh(T(<<"server_info">>, [Info#{<<"modules">> => <<"roll">>}])),
 1540:     ?errh(T(<<"server_info">>, [Info#{<<"modules">> => [<<"meow_meow_meow">>]}])),
 1541:     ?errh(T(<<"server_info">>, [Info#{<<"urls">> => [1]}])),
 1542:     ?errh(T(<<"server_info">>, [Info#{<<"urls">> => [<<"">>]}])),
 1543:     ?errh(T(<<"server_info">>, [maps:remove(<<"name">>, Info)])),
 1544:     ?errh(T(<<"server_info">>, [maps:remove(<<"urls">>, Info)])).
 1545: 
 1546: mod_extdisco(_Config) ->
 1547:     check_module_defaults(mod_extdisco),
 1548:     check_iqdisc(mod_extdisco),
 1549:     P = [modules, mod_extdisco, service],
 1550:     T = fun(Opts) -> #{<<"modules">> =>
 1551:                            #{<<"mod_extdisco">> =>
 1552:                                  #{<<"service">> => [Opts]}}}
 1553:         end,
 1554:     RequiredOpts = #{<<"type">> => <<"stun">>, <<"host">> => <<"stun1">>},
 1555:     Service = #{type => stun, host => <<"stun1">>},
 1556:     ?cfgh(P, [Service], T(RequiredOpts)),
 1557:     ?cfgh(P, [Service#{port => 3478}], T(RequiredOpts#{<<"port">> => 3478})),
 1558:     ?cfgh(P, [Service#{transport => <<"udp">>}], T(RequiredOpts#{<<"transport">> => <<"udp">>})),
 1559:     ?cfgh(P, [Service#{username => <<"user">>}], T(RequiredOpts#{<<"username">> => <<"user">>})),
 1560:     ?cfgh(P, [Service#{password => <<"pass">>}], T(RequiredOpts#{<<"password">> => <<"pass">>})),
 1561:     [?errh(T(maps:remove(Key, RequiredOpts))) || Key <- maps:keys(RequiredOpts)],
 1562:     [?errh(T(RequiredOpts#{Key => 1})) || Key <- maps:keys(RequiredOpts)],
 1563:     ?errh(T(RequiredOpts#{<<"type">> => <<>>})),
 1564:     ?errh(T(RequiredOpts#{<<"host">> => <<>>})),
 1565:     ?errh(T(RequiredOpts#{<<"port">> => -1})),
 1566:     ?errh(T(RequiredOpts#{<<"transport">> => <<>>})),
 1567:     ?errh(T(RequiredOpts#{<<"username">> => <<>>})),
 1568:     ?errh(T(RequiredOpts#{<<"password">> => <<>>})).
 1569: 
 1570: mod_inbox(_Config) ->
 1571:     check_module_defaults(mod_inbox),
 1572:     check_iqdisc(mod_inbox),
 1573:     P = [modules, mod_inbox],
 1574:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_inbox">> => Opts}} end,
 1575:     ChatMarkers = [<<"displayed">>, <<"received">>, <<"acknowledged">>],
 1576:     ?cfgh(P ++ [backend], rdbms, T(#{<<"backend">> => <<"rdbms">>})),
 1577:     ?cfgh(P ++ [async_writer], #{pool_size => 8}, T(#{<<"async_writer">> => #{<<"pool_size">> => 8}})),
 1578:     ?cfgh(P ++ [reset_markers], ChatMarkers, T(#{<<"reset_markers">> => ChatMarkers})),
 1579:     ?cfgh(P ++ [groupchat], [muc, muclight], T(#{<<"groupchat">> => [<<"muc">>, <<"muclight">>]})),
 1580:     ?cfgh(P ++ [boxes],
 1581:           [<<"inbox">>, <<"archive">>, <<"bin">>, <<"favourites">>, <<"spam">>],
 1582:           T(#{<<"boxes">> => [<<"favourites">>, <<"spam">>]})),
 1583:     ?cfgh(P ++ [bin_ttl], 30, T(#{<<"bin_ttl">> => 30})),
 1584:     ?cfgh(P ++ [bin_clean_after], 43200000, T(#{<<"bin_clean_after">> => 12})),
 1585:     ?cfgh(P ++ [aff_changes], true, T(#{<<"aff_changes">> => true})),
 1586:     ?cfgh(P ++ [delete_domain_limit], 1000, T(#{<<"delete_domain_limit">> => 1000})),
 1587:     ?cfgh(P ++ [remove_on_kicked], false, T(#{<<"remove_on_kicked">> => false})),
 1588:     ?cfgh(P ++ [max_result_limit], infinity, T(#{<<"max_result_limit">> => <<"infinity">>})),
 1589:     ?cfgh(P ++ [max_result_limit], 100, T(#{<<"max_result_limit">> => 100})),
 1590:     ?errh(T(#{<<"backend">> => <<"nodejs">>})),
 1591:     ?errh(T(#{<<"pool_size">> => -1})),
 1592:     ?errh(T(#{<<"reset_markers">> => 1})),
 1593:     ?errh(T(#{<<"reset_markers">> => [<<"destroyed">>]})),
 1594:     ?errh(T(#{<<"groupchat">> => [<<"test">>]})),
 1595:     ?errh(T(#{<<"boxes">> => [<<"archive">>]})),
 1596:     ?errh(T(#{<<"boxes">> => [<<"duplicate">>, <<"duplicate">>]})),
 1597:     ?errh(T(#{<<"boxes">> => <<"test">>})),
 1598:     ?errh(T(#{<<"bin_ttl">> => true})),
 1599:     ?errh(T(#{<<"bin_clean_after">> => -1})),
 1600:     ?errh(T(#{<<"aff_changes">> => 1})),
 1601:     ?errh(T(#{<<"delete_domain_limit">> => []})),
 1602:     ?errh(T(#{<<"remove_on_kicked">> => 1})),
 1603:     ?errh(T(#{<<"max_result_limit">> => 0})),
 1604:     ?errh(T(#{<<"max_result_limit">> => -1})).
 1605: 
 1606: mod_global_distrib(_Config) ->
 1607:     P = [modules, mod_global_distrib],
 1608:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_global_distrib">> => Opts}} end,
 1609:     RequiredOpts = global_distrib_required_opts(),
 1610:     ExpectedCfg = mod_config(mod_global_distrib, global_distrib_expected_config()),
 1611:     ?cfgh(P, ExpectedCfg, T(RequiredOpts)),
 1612:     ?cfgh(P ++ [message_ttl], 42,
 1613:           T(RequiredOpts#{<<"message_ttl">> => 42})),
 1614:     ?cfgh(P ++ [hosts_refresh_interval], 100,
 1615:           T(RequiredOpts#{<<"hosts_refresh_interval">> => 100})),
 1616:     [?errh(T(maps:remove(Key, RequiredOpts))) || Key <- maps:keys(RequiredOpts)],
 1617:     ?errh(T(RequiredOpts#{<<"global_host">> => <<>>})),
 1618:     ?errh(T(RequiredOpts#{<<"local_host">> => <<>>})),
 1619:     ?errh(T(RequiredOpts#{<<"local_host">> => <<"nohost">>})), % passed to 'endpoints', not resolved
 1620:     ?errh(T(RequiredOpts#{<<"message_ttl">> => -1})),
 1621:     ?errh(T(RequiredOpts#{<<"hosts_refresh_interval">> => -1})).
 1622: 
 1623: mod_global_distrib_connections(_Config) ->
 1624:     RequiredOpts = global_distrib_required_opts(),
 1625:     P = [modules, mod_global_distrib, connections],
 1626:     T = fun(Opts) -> #{<<"modules">> =>
 1627:                            #{<<"mod_global_distrib">> =>
 1628:                                  RequiredOpts#{<<"connections">> => Opts}}}
 1629:         end,
 1630:     ?cfgh(P, global_distrib_expected_connections(), T(#{})),
 1631:     ?cfgh(P ++ [connections_per_endpoint], 22,
 1632:           T(#{<<"connections_per_endpoint">> => 22})),
 1633:     ?cfgh(P ++ [endpoint_refresh_interval], 120,
 1634:           T(#{<<"endpoint_refresh_interval">> => 120})),
 1635:     ?cfgh(P ++ [endpoint_refresh_interval_when_empty], 5,
 1636:           T(#{<<"endpoint_refresh_interval_when_empty">> => 5})),
 1637:     ?cfgh(P ++ [disabled_gc_interval], 60,
 1638:           T(#{<<"disabled_gc_interval">> => 60})),
 1639:     ?errh(T(#{<<"connections_per_endpoint">> => -1})),
 1640:     ?errh(T(#{<<"endpoint_refresh_interval">> => 0})),
 1641:     ?errh(T(#{<<"endpoint_refresh_interval_when_empty">> => 0})),
 1642:     ?errh(T(#{<<"disabled_gc_interval">> => 0})).
 1643: 
 1644: mod_global_distrib_connections_endpoints(_Config) ->
 1645:     check_mod_global_distrib_endpoint_opts(<<"endpoints">>),
 1646:     RequiredModOpts = global_distrib_required_opts(),
 1647:     P = [modules, mod_global_distrib, connections],
 1648:     T = fun(Opts) -> #{<<"modules">> =>
 1649:                            #{<<"mod_global_distrib">> =>
 1650:                                  RequiredModOpts#{<<"connections">> => #{<<"endpoints">> => Opts}}}}
 1651:         end,
 1652: 
 1653:     %% 'enpoints' propagate to 'advertised_endpoints' and 'resolved_endpoints'
 1654:     ?cfgh([{P ++ [endpoints], [{"172.16.0.2", 5555}]},
 1655:            {P ++ [advertised_endpoints], [{"172.16.0.2", 5555}]},
 1656:            {P ++ [resolved_endpoints], [{{172, 16, 0, 2}, 5555}]}],
 1657:           T([#{<<"host">> => <<"172.16.0.2">>, <<"port">> => 5555}])),
 1658: 
 1659:     ?cfgh([{P ++ [endpoints], [{"localhost", 15555}]},
 1660:            {P ++ [advertised_endpoints], [{"localhost", 15555}]},
 1661:            {P ++ [resolved_endpoints], [{{0, 0, 0, 0, 0, 0, 0, 1}, 15555},
 1662:                                         {{127, 0, 0, 1}, 15555}]
 1663:            }],
 1664:           T([#{<<"host">> => <<"localhost">>, <<"port">> => 15555}])),
 1665:     ?errh(T([#{<<"host">> => <<"172.16.0.299">>, <<"port">> => 5555}])),
 1666:     ?errh(T([#{<<"host">> => <<"nohost">>, <<"port">> => 15555}])).
 1667: 
 1668: mod_global_distrib_connections_advertised_endpoints(_Config) ->
 1669:     check_mod_global_distrib_endpoint_opts(<<"advertised_endpoints">>).
 1670: 
 1671: check_mod_global_distrib_endpoint_opts(OptKey) ->
 1672:     RequiredModOpts = global_distrib_required_opts(),
 1673:     P = [modules, mod_global_distrib, connections, binary_to_atom(OptKey)],
 1674:     T = fun(Opts) -> #{<<"modules">> =>
 1675:                            #{<<"mod_global_distrib">> =>
 1676:                                  RequiredModOpts#{<<"connections">> => #{OptKey => Opts}}}}
 1677:         end,
 1678:     RequiredOpts = #{<<"host">> => <<"172.16.0.2">>, <<"port">> => 5678},
 1679:     ?cfgh(P, [{"172.16.0.2", 5678}], T([RequiredOpts])),
 1680:     [?errh(T(maps:remove(Key, RequiredOpts))) || Key <- maps:keys(RequiredOpts)],
 1681:     ?errh(T([RequiredOpts#{<<"host">> => <<>>}])),
 1682:     ?errh(T([RequiredOpts#{<<"port">> => -1}])).
 1683: 
 1684: mod_global_distrib_connections_tls(_Config) ->
 1685:     RequiredModOpts = global_distrib_required_opts(),
 1686:     P = host_key([modules, mod_global_distrib, connections, tls]),
 1687:     T = fun(Opts) -> #{<<"modules">> =>
 1688:                            #{<<"mod_global_distrib">> =>
 1689:                                  RequiredModOpts#{<<"connections">> => #{<<"tls">> => Opts}}}}
 1690:         end,
 1691:     % Does not test host_config, but other tests do that,
 1692:     % and this module should be enabled globally anyway
 1693:     test_fast_tls_server(P, T).
 1694: 
 1695: mod_global_distrib_redis(_Config) ->
 1696:     RequiredModOpts = global_distrib_required_opts(),
 1697:     P = [modules, mod_global_distrib, redis],
 1698:     T = fun(Opts) -> #{<<"modules">> =>
 1699:                            #{<<"mod_global_distrib">> =>
 1700:                                  RequiredModOpts#{<<"redis">> => Opts}}}
 1701:         end,
 1702:     ?cfgh(P, default_config(P), T(#{})),
 1703:     ?cfgh(P ++ [pool], global_distrib, T(#{<<"pool">> => <<"global_distrib">>})),
 1704:     ?cfgh(P ++ [expire_after], 120, T(#{<<"expire_after">> => 120})),
 1705:     ?cfgh(P ++ [refresh_after], 60, T(#{<<"refresh_after">> => 60})),
 1706:     ?errh(T(#{<<"pool">> => <<"">>})),
 1707:     ?errh(T(#{<<"expire_after">> => 0})),
 1708:     ?errh(T(#{<<"refresh_after">> => -1})).
 1709: 
 1710: mod_global_distrib_cache(_Config) ->
 1711:     RequiredModOpts = global_distrib_required_opts(),
 1712:     P = [modules, mod_global_distrib, cache],
 1713:     T = fun(Opts) -> #{<<"modules">> =>
 1714:                            #{<<"mod_global_distrib">> =>
 1715:                                  RequiredModOpts#{<<"cache">> => Opts}}}
 1716:         end,
 1717:     ?cfgh(P, default_config(P), T(#{})),
 1718:     ?cfgh(P ++ [cache_missed], false, T(#{<<"cache_missed">> => false})),
 1719:     ?cfgh(P ++ [domain_lifetime_seconds], 60, T(#{<<"domain_lifetime_seconds">> => 60})),
 1720:     ?cfgh(P ++ [jid_lifetime_seconds], 30, T(#{<<"jid_lifetime_seconds">> => 30})),
 1721:     ?cfgh(P ++ [max_jids], 9999, T(#{<<"max_jids">> => 9999})),
 1722:     ?errh(T(#{<<"cache_missed">> => <<"yes">>})),
 1723:     ?errh(T(#{<<"domain_lifetime_seconds">> => -1})),
 1724:     ?errh(T(#{<<"jid_lifetime_seconds">> => -1})),
 1725:     ?errh(T(#{<<"max_jids">> => -1})).
 1726: 
 1727: mod_global_distrib_bounce(_Config) ->
 1728:     RequiredModOpts = global_distrib_required_opts(),
 1729:     P = [modules, mod_global_distrib, bounce],
 1730:     T = fun(Opts) -> #{<<"modules">> =>
 1731:                            #{<<"mod_global_distrib">> =>
 1732:                                  RequiredModOpts#{<<"bounce">> => Opts}}}
 1733:         end,
 1734:     ?cfgh(P, default_config(P), T(#{})),
 1735:     ?cfgh(P ++ [enabled], false, T(#{<<"enabled">> => false})),
 1736:     ?cfgh(P ++ [resend_after_ms], 300, T(#{<<"resend_after_ms">> => 300})),
 1737:     ?cfgh(P ++ [max_retries], 3, T(#{<<"max_retries">> => 3})),
 1738:     ?errh(T(#{<<"enabled">> => <<"">>})),
 1739:     ?errh(T(#{<<"resend_after_ms">> => -1})),
 1740:     ?errh(T(#{<<"max_retries">> => -1})).
 1741: 
 1742: global_distrib_required_opts() ->
 1743:     #{<<"global_host">> => <<"global.example.com">>,
 1744:       <<"local_host">> => <<"localhost">>}.
 1745: 
 1746: global_distrib_expected_config() ->
 1747:     #{global_host => <<"global.example.com">>,
 1748:       local_host => <<"localhost">>,
 1749:       connections => global_distrib_expected_connections()}.
 1750: 
 1751: global_distrib_expected_connections() ->
 1752:     config([modules, mod_global_distrib, connections],
 1753:            #{endpoints => [{"localhost", 5555}],
 1754:              advertised_endpoints => [{"localhost", 5555}],
 1755:              resolved_endpoints => [{{0, 0, 0, 0, 0, 0, 0, 1}, 5555},
 1756:                                     {{127, 0, 0, 1}, 5555}]}).
 1757: 
 1758: mod_event_pusher_sns(_Config) ->
 1759:     RequiredOpts = #{<<"sns_host">> => <<"sns.eu-west-1.amazonaws.com">>,
 1760:                      <<"region">> => <<"eu-west-1">>,
 1761:                      <<"access_key_id">> => <<"AKIAIOSFODNN7EXAMPLE">>,
 1762:                      <<"secret_access_key">> => <<"KEY">>,
 1763:                      <<"account_id">> => <<"123456789012">>},
 1764:     ExpectedCfg = #{sns_host => "sns.eu-west-1.amazonaws.com",
 1765:                     region => "eu-west-1",
 1766:                     access_key_id => "AKIAIOSFODNN7EXAMPLE",
 1767:                     secret_access_key => "KEY",
 1768:                     account_id => "123456789012"},
 1769:     P = [modules, mod_event_pusher, sns],
 1770:     T = fun(Opts) -> #{<<"modules">> =>
 1771:                            #{<<"mod_event_pusher">> => #{<<"sns">> => Opts}}}
 1772:         end,
 1773:     ?cfgh(P, config(P, ExpectedCfg), T(RequiredOpts)),
 1774:     ?cfgh(P ++ [presence_updates_topic], "pres",
 1775:           T(RequiredOpts#{<<"presence_updates_topic">> => <<"pres">>})),
 1776:     ?cfgh(P ++ [pm_messages_topic], "pm",
 1777:           T(RequiredOpts#{<<"pm_messages_topic">> => <<"pm">>})),
 1778:     ?cfgh(P ++ [muc_messages_topic], "muc",
 1779:           T(RequiredOpts#{<<"muc_messages_topic">> => <<"muc">>})),
 1780:     ?cfgh(P ++ [plugin_module], mod_event_pusher_sns_defaults,
 1781:           T(RequiredOpts#{<<"plugin_module">> => <<"mod_event_pusher_sns_defaults">>})),
 1782:     ?cfgh(P ++ [pool_size], 10,
 1783:           T(RequiredOpts#{<<"pool_size">> => 10})),
 1784:     ?cfgh(P ++ [publish_retry_count], 1,
 1785:           T(RequiredOpts#{<<"publish_retry_count">> => 1})),
 1786:     ?cfgh(P ++ [publish_retry_time_ms], 100,
 1787:           T(RequiredOpts#{<<"publish_retry_time_ms">> => 100})),
 1788:     [?errh(T(maps:remove(Key, RequiredOpts))) || Key <- maps:keys(RequiredOpts)],
 1789:     [?errh(T(RequiredOpts#{Key => 1})) || Key <- maps:keys(RequiredOpts)],
 1790:     ?errh(T(RequiredOpts#{<<"presence_updates_topic">> => #{}})),
 1791:     ?errh(T(RequiredOpts#{<<"pm_messages_topic">> => true})),
 1792:     ?errh(T(RequiredOpts#{<<"muc_messages_topic">> => [1, 2]})),
 1793:     ?errh(T(RequiredOpts#{<<"plugin_module">> => <<"plug_and_play">>})),
 1794:     ?errh(T(RequiredOpts#{<<"pool_size">> => 0})),
 1795:     ?errh(T(RequiredOpts#{<<"publish_retry_count">> => -1})),
 1796:     ?errh(T(RequiredOpts#{<<"publish_retry_time_ms">> => -1})).
 1797: 
 1798: mod_event_pusher_push(_Config) ->
 1799:     P = [modules, mod_event_pusher, push],
 1800:     T = fun(Opts) -> #{<<"modules">> =>
 1801:                            #{<<"mod_event_pusher">> => #{<<"push">> => Opts}}}
 1802:         end,
 1803:     ?cfgh(P, default_config(P), T(#{})),
 1804:     check_iqdisc(P, T),
 1805:     test_wpool(P ++ [wpool], fun(Opts) -> T(#{<<"wpool">> => Opts}) end),
 1806:     ?cfgh(P ++ [backend], rdbms, T(#{<<"backend">> => <<"rdbms">>})),
 1807:     ?cfgh(P ++ [plugin_module], mod_event_pusher_push_plugin_enhanced,
 1808:           T(#{<<"plugin_module">> => <<"mod_event_pusher_push_plugin_enhanced">>})),
 1809:     ?cfgh(P ++ [virtual_pubsub_hosts], [{fqdn, <<"host1">>}, {fqdn, <<"host2">>}],
 1810:           T(#{<<"virtual_pubsub_hosts">> => [<<"host1">>, <<"host2">>]})),
 1811:     ?cfgh(P ++ [virtual_pubsub_hosts], [{prefix, <<"pubsub.">>}, {prefix, <<"pub-sub.">>}],
 1812:           T(#{<<"virtual_pubsub_hosts">> => [<<"pubsub.@HOST@">>, <<"pub-sub.@HOST@">>]})),
 1813:     ?errh(T(#{<<"backend">> => <<"redis">>})),
 1814:     ?errh(T(#{<<"wpool">> => true})),
 1815:     ?errh(T(#{<<"plugin_module">> => <<"wow_cool_but_missing">>})),
 1816:     ?errh(T(#{<<"plugin_module">> => 1})),
 1817:     ?errh(T(#{<<"virtual_pubsub_hosts">> => [<<"host with whitespace">>]})),
 1818:     ?errh(T(#{<<"virtual_pubsub_hosts">> => [<<"invalid.sub@HOST@">>]})),
 1819:     ?errh(T(#{<<"virtual_pubsub_hosts">> => [<<"invalid.sub.@HOST@.as.well">>]})).
 1820: 
 1821: test_wpool(P, T) ->
 1822:     ?cfgh(P, default_config(P), T(#{})),
 1823:     ?cfgh(P ++ [workers], 200, T(#{<<"workers">> => 200})),
 1824:     ?cfgh(P ++ [strategy], random_worker, T(#{<<"strategy">> => <<"random_worker">>})),
 1825:     ?cfgh(P ++ [call_timeout], 1000, T(#{<<"call_timeout">> => 1000})),
 1826:     ?errh(T(#{<<"workers">> => 0})),
 1827:     ?errh(T(#{<<"strategy">> => <<"worst_worker">>})),
 1828:     ?errh(T(#{<<"workers">> => 0})).
 1829: 
 1830: mod_event_pusher_http(_Config) ->
 1831:     P = [modules, mod_event_pusher, http, handlers],
 1832:     T = fun(Handlers) -> #{<<"modules">> =>
 1833:                            #{<<"mod_event_pusher">> =>
 1834:                                  #{<<"http">> => #{<<"handlers">> => Handlers}}}}
 1835:         end,
 1836:     DefaultHandler = mod_event_pusher_http_handler(),
 1837:     ?cfgh(P, [DefaultHandler], T([#{}])),
 1838:     ?cfgh(P, [DefaultHandler#{pool_name => my_pool}],
 1839:           T([#{<<"pool_name">> => <<"my_pool">>}])),
 1840:     ?cfgh(P, [DefaultHandler#{path => <<"notifications">>}],
 1841:           T([#{<<"path">> => <<"/notifications">>}])),
 1842:     ?cfgh(P, [DefaultHandler#{callback_module => mod_event_pusher_http}], % existing module
 1843:           T([#{<<"callback_module">> => <<"mod_event_pusher_http">>}])),
 1844:     ?cfgh(P, [DefaultHandler, DefaultHandler#{pool_name => my_pool}], % two handlers
 1845:           T([#{}, #{<<"pool_name">> => <<"my_pool">>}])),
 1846:     ?errh(T([#{<<"pool_name">> => <<>>}])),
 1847:     ?errh(T([#{<<"path">> => true}])),
 1848:     ?errh(T([#{<<"callback_module">> => <<"wow_cool_but_missing">>}])),
 1849:     ?errh(T([#{<<"callback_module">> => 1}])),
 1850:     ?errh(T([#{}, #{}])). % handlers have to be unique
 1851: 
 1852: mod_event_pusher_rabbit(_Config) ->
 1853:     P = [modules, mod_event_pusher, rabbit],
 1854:     T = fun(Key, Opts) -> #{<<"modules">> =>
 1855:                                 #{<<"mod_event_pusher">> =>
 1856:                                       #{<<"rabbit">> => #{Key => Opts}}}}
 1857:         end,
 1858:     test_event_pusher_rabbit_exchange(P ++ [presence_exchange],
 1859:                                       fun(Opts) -> T(<<"presence_exchange">>, Opts) end),
 1860:     test_event_pusher_rabbit_msg_exchange(P ++ [chat_msg_exchange],
 1861:                                           fun(Opts) -> T(<<"chat_msg_exchange">>, Opts) end),
 1862:     test_event_pusher_rabbit_msg_exchange(P ++ [groupchat_msg_exchange],
 1863:                                           fun(Opts) -> T(<<"groupchat_msg_exchange">>, Opts) end).
 1864: 
 1865: test_event_pusher_rabbit_msg_exchange(P, T) ->
 1866:     test_event_pusher_rabbit_exchange(P, T),
 1867:     ?cfgh(P, default_config(P), T(#{})),
 1868:     ?cfgh(P ++ [sent_topic], <<"outgoing">>, T(#{<<"sent_topic">> => <<"outgoing">>})),
 1869:     ?cfgh(P ++ [recv_topic], <<"incoming">>, T(#{<<"recv_topic">> => <<"incoming">>})),
 1870:     ?errh(T(#{<<"sent_topic">> => <<>>})),
 1871:     ?errh(T(#{<<"recv_topic">> => <<>>})).
 1872: 
 1873: test_event_pusher_rabbit_exchange(P, T) ->
 1874:     ?cfgh(P, default_config(P), T(#{})),
 1875:     ?cfgh(P ++ [name], <<"notifications">>, T(#{<<"name">> => <<"notifications">>})),
 1876:     ?cfgh(P ++ [type], <<"direct">>, T(#{<<"type">> => <<"direct">>})),
 1877:     ?errh(T(#{<<"name">> => <<>>})),
 1878:     ?errh(T(#{<<"type">> => <<>>})).
 1879: 
 1880: mod_http_upload(_Config) ->
 1881:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_http_upload">> => Opts}} end,
 1882:     P = [modules, mod_http_upload],
 1883:     RequiredOpts = #{<<"s3">> => http_upload_s3_required_opts()},
 1884:     S3Cfg = http_upload_s3_expected_cfg(),
 1885:     ?cfgh(P, mod_config(mod_http_upload,
 1886:         #{host => <<"upload.@HOST@">>,
 1887:           s3 => config_parser_helper:config([modules, mod_http_upload, s3], S3Cfg)
 1888:          }),
 1889:         T(RequiredOpts)),
 1890:     ?cfgh(P ++ [s3], S3Cfg#{add_acl => false}, T(RequiredOpts)),
 1891:     ?cfgh(P ++ [host], {prefix, <<"upload.">>},
 1892:           T(RequiredOpts#{<<"host">> => <<"upload.@HOST@">>})),
 1893:     ?cfgh(P ++ [host], {fqdn, <<"upload.test">>},
 1894:           T(RequiredOpts#{<<"host">> => <<"upload.test">>})),
 1895:     ?cfgh(P ++ [backend], s3,
 1896:           T(RequiredOpts#{<<"backend">> => <<"s3">>})),
 1897:     ?cfgh(P ++ [expiration_time], 666,
 1898:           T(RequiredOpts#{<<"expiration_time">> => 666})),
 1899:     ?cfgh(P ++ [token_bytes], 32,
 1900:           T(RequiredOpts#{<<"token_bytes">> => 32})),
 1901:     ?cfgh(P ++ [max_file_size], 42,
 1902:           T(RequiredOpts#{<<"max_file_size">> => 42})),
 1903:     ?errh(T(#{})), %% missing 's3'
 1904:     ?errh(T(RequiredOpts#{<<"backend">> => <<"">>})),
 1905:     ?errh(T(RequiredOpts#{<<"expiration_time">> => 0})),
 1906:     ?errh(T(RequiredOpts#{<<"token_bytes">> => 0})),
 1907:     ?errh(T(RequiredOpts#{<<"max_file_size">> => 0})),
 1908:     ?errh(T(RequiredOpts#{<<"host">> => <<"is this a host? no.">>})),
 1909:     ?errh(T(RequiredOpts#{<<"host">> => <<"invalid-.com">>})),
 1910:     ?errh(T(RequiredOpts#{<<"host">> => <<"-invalid.com">>})),
 1911:     ?errh(T(RequiredOpts#{<<"host">> => [<<"valid.@HOST@">>]})),
 1912:     ?errh(T(RequiredOpts#{<<"host">> => <<"invalid.sub@HOST@">>})),
 1913:     ?errh(T(RequiredOpts#{<<"host">> => <<"invalid.sub.@HOST@.as.well">>})),
 1914:     ?errh(T(RequiredOpts#{<<"host">> => [<<"not.supported.any.more.@HOSTS@">>]})),
 1915:     check_iqdisc(mod_http_upload, RequiredOpts).
 1916: 
 1917: mod_http_upload_s3(_Config) ->
 1918:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_http_upload">> =>
 1919:                                               #{<<"s3">> => Opts}}} end,
 1920:     RequiredOpts = http_upload_s3_required_opts(),
 1921:     ExpectedCfg = http_upload_s3_expected_cfg(),
 1922:     P = [modules, mod_http_upload, s3],
 1923:     ?cfgh(P, ExpectedCfg#{add_acl => false}, T(RequiredOpts)),
 1924:     ?cfgh(P ++ [add_acl], true, T(RequiredOpts#{<<"add_acl">> => true})),
 1925:     [?errh(T(maps:remove(Key, RequiredOpts))) || Key <- maps:keys(RequiredOpts)],
 1926:     ?errh(T(RequiredOpts#{<<"bucket_url">> => <<>>})),
 1927:     ?errh(T(RequiredOpts#{<<"region">> => true})),
 1928:     ?errh(T(RequiredOpts#{<<"access_key_id">> => []})),
 1929:     ?errh(T(RequiredOpts#{<<"secret_access_key">> => 3})),
 1930:     ?errh(T(RequiredOpts#{<<"add_acl">> => <<"true">>})).
 1931: 
 1932: http_upload_s3_required_opts() ->
 1933:     #{<<"bucket_url">> => <<"https://s3-eu-west-1.amazonaws.com/mybucket">>,
 1934:       <<"region">> => <<"antarctica-1">>,
 1935:       <<"access_key_id">> => <<"PLEASE">>,
 1936:       <<"secret_access_key">> => <<"ILOVEU">>}.
 1937: 
 1938: http_upload_s3_expected_cfg() ->
 1939:     #{access_key_id => <<"PLEASE">>,
 1940:       bucket_url => <<"https://s3-eu-west-1.amazonaws.com/mybucket">>,
 1941:       region => <<"antarctica-1">>,
 1942:       secret_access_key => <<"ILOVEU">>}.
 1943: 
 1944: mod_jingle_sip(_Config) ->
 1945:     check_module_defaults(mod_jingle_sip),
 1946:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_jingle_sip">> => Opts}} end,
 1947:     P = [modules, mod_jingle_sip],
 1948:     ?cfgh(P ++ [backend], mnesia, T(#{<<"backend">> => <<"mnesia">>})),
 1949:     ?cfgh(P ++ [proxy_host], "proxxxy.com",
 1950:           T(#{<<"proxy_host">> => <<"proxxxy.com">>})),
 1951:     ?cfgh(P ++ [proxy_port], 5601,
 1952:           T(#{<<"proxy_port">> => 5601})),
 1953:     ?cfgh(P ++ [listen_port], 5602,
 1954:           T(#{<<"listen_port">> => 5602})),
 1955:     ?cfgh(P ++ [local_host], "localhost",
 1956:           T(#{<<"local_host">> => <<"localhost">>})),
 1957:     ?cfgh(P ++ [sdp_origin], "127.0.0.1",
 1958:           T(#{<<"sdp_origin">> => <<"127.0.0.1">>})),
 1959:     ?cfgh(P ++ [transport], "tcp",
 1960:           T(#{<<"transport">> => <<"tcp">>})),
 1961:     ?cfgh(P ++ [username_to_phone], [{<<"2000006168">>, <<"+919177074440">>}],
 1962:           T(#{<<"username_to_phone">> => [#{<<"username">> => <<"2000006168">>,
 1963:                                             <<"phone">> => <<"+919177074440">>}]})),
 1964:     ?errh(T(#{<<"backend">> => <<"amnesia">>})),
 1965:     ?errh(T(#{<<"proxy_host">> => 1})),
 1966:     ?errh(T(#{<<"proxy_port">> => 1000000})),
 1967:     ?errh(T(#{<<"listen_port">> => -1})),
 1968:     ?errh(T(#{<<"local_host">> => <<>>})),
 1969:     ?errh(T(#{<<"sdp_origin">> => <<"abc">>})).
 1970: 
 1971: mod_keystore(_Config) ->
 1972:     check_module_defaults(mod_keystore),
 1973:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_keystore">> => Opts}} end,
 1974:     P = [modules, mod_keystore],
 1975:     ?cfgh(P ++ [ram_key_size], 1024,
 1976:           T(#{<<"ram_key_size">> => 1024})),
 1977:     ?errh(T(#{<<"ram_key_size">> => -1})).
 1978: 
 1979: mod_keystore_keys(_Config) ->
 1980:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_keystore">> =>
 1981:                                               #{<<"keys">> => Opts}}}
 1982:         end,
 1983:     P = [modules, mod_keystore, keys],
 1984:     RequiredOpts = #{<<"name">> => <<"access_secret">>,
 1985:                      <<"type">> => <<"ram">>},
 1986:     ?cfgh(P ++ [access_secret], ram,
 1987:           T([RequiredOpts])),
 1988:     ?cfgh(P ++ [access_secret], {file, "priv/access_psk"},
 1989:           T([RequiredOpts#{<<"type">> => <<"file">>,
 1990:                            <<"path">> => <<"priv/access_psk">>}])),
 1991:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 1992:     ?errh(T([RequiredOpts#{<<"name">> => <<>>}])),
 1993:     ?errh(T([RequiredOpts#{<<"type">> => <<"rampampam">>}])),
 1994:     ?errh(T([RequiredOpts#{<<"type">> => <<"file">>}])),
 1995:     ?errh(T([RequiredOpts#{<<"type">> => <<"file">>,
 1996:                            <<"path">> => <<"does/not/exists">>}])),
 1997:     ?errh(T([#{<<"name">> => <<"same_name_twice">>,
 1998:                <<"type">> => <<"ram">>},
 1999:              #{<<"name">> => <<"same_name_twice">>,
 2000:                <<"type">> => <<"file">>,
 2001:                <<"path">> => <<"priv/access_psk">>}])).
 2002: 
 2003: mod_last(_Config) ->
 2004:     check_iqdisc(mod_last),
 2005:     check_module_defaults(mod_last),
 2006:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_last">> => Opts}} end,
 2007:     P = [modules, mod_last],
 2008:     ?cfgh(P ++ [backend], mnesia, T(#{<<"backend">> => <<"mnesia">>})),
 2009:     ?cfgh(P ++ [backend], rdbms, T(#{<<"backend">> => <<"rdbms">>})),
 2010:     ?errh(T(#{<<"backend">> => <<"frontend">>})).
 2011: 
 2012: mod_mam(_Config) ->
 2013:     check_module_defaults(mod_mam),
 2014:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_mam">> => Opts}} end,
 2015:     P = [modules, mod_mam],
 2016:     test_cache_config(P ++ [cache], fun(Opts) -> T(#{<<"cache">> => Opts}) end),
 2017:     test_mod_mam(P, T).
 2018: 
 2019: mod_mam_pm(_Config) ->
 2020:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_mam">> => #{<<"pm">> => Opts}}} end,
 2021:     P = [modules, mod_mam, pm],
 2022:     test_mod_mam(P, T),
 2023:     ?cfgh(P, default_config(P), T(#{})),
 2024:     ?cfgh(P ++ [archive_groupchats], true, T(#{<<"archive_groupchats">> => true})),
 2025:     ?cfgh(P ++ [same_mam_id_for_peers], true, T(#{<<"same_mam_id_for_peers">> => true})),
 2026:     ?errh(T(#{<<"host">> => <<"muc.@HOST@">>})), % muc-only
 2027:     ?errh(T(#{<<"archive_groupchats">> => <<"not really">>})),
 2028:     ?errh(T(#{<<"same_mam_id_for_peers">> => <<"not really">>})).
 2029: 
 2030: mod_mam_muc(_Config) ->
 2031:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_mam">> => #{<<"muc">> => Opts}}} end,
 2032:     P = [modules, mod_mam, muc],
 2033:     test_mod_mam(P, T),
 2034:     ?cfgh(P, default_config(P), T(#{})),
 2035:     ?cfgh(P ++ [host], {prefix, <<"muc.">>}, T(#{<<"host">> => <<"muc.@HOST@">>})),
 2036:     ?cfgh(P ++ [host], {fqdn, <<"muc.test">>}, T(#{<<"host">> => <<"muc.test">>})),
 2037:     ?errh(T(#{<<"host">> => <<"is this a host? no.">>})),
 2038:     ?errh(T(#{<<"host">> => [<<"valid.@HOST@">>]})),
 2039:     ?errh(T(#{<<"host">> => <<"invalid.sub@HOST@">>})),
 2040:     ?errh(T(#{<<"host">> => <<"invalid.sub.@HOST@.as.well">>})),
 2041:     ?errh(T(#{<<"archive_groupchats">> => true})), % pm-only
 2042:     ?errh(T(#{<<"same_mam_id_for_peers">> => true})). % pm-only
 2043: 
 2044: test_mod_mam(P, T) ->
 2045:     test_async_writer(P, T),
 2046:     ?cfgh(P ++ [backend], rdbms,
 2047:           T(#{<<"backend">> => <<"rdbms">>})),
 2048:     ?cfgh(P ++ [no_stanzaid_element], true,
 2049:           T(#{<<"no_stanzaid_element">> => true})),
 2050:     ?cfgh(P ++ [is_archivable_message], mod_mam_utils,
 2051:           T(#{<<"is_archivable_message">> => <<"mod_mam_utils">>})),
 2052:     ?cfgh(P ++ [archive_chat_markers], true,
 2053:           T(#{<<"archive_chat_markers">> => true})),
 2054:     ?cfgh(P ++ [message_retraction], false,
 2055:           T(#{<<"message_retraction">> => false})),
 2056:     ?cfgh(P ++ [user_prefs_store], rdbms,
 2057:           T(#{<<"user_prefs_store">> => <<"rdbms">>})),
 2058:     ?cfgh(P ++ [full_text_search], false,
 2059:           T(#{<<"full_text_search">> => false})),
 2060:     ?cfgh(P ++ [cache_users], false,
 2061:           T(#{<<"cache_users">> => false})),
 2062:     ?cfgh(P ++ [delete_domain_limit], 1000,
 2063:           T(#{<<"delete_domain_limit">> => 1000})),
 2064:     ?cfgh(P ++ [default_result_limit], 100,
 2065:           T(#{<<"default_result_limit">> => 100})),
 2066:     ?cfgh(P ++ [max_result_limit], 1000,
 2067:           T(#{<<"max_result_limit">> => 1000})),
 2068:     ?cfgh(P ++ [enforce_simple_queries], true,
 2069:           T(#{<<"enforce_simple_queries">> => true})),
 2070:     ?cfgh(P ++ [db_jid_format], mam_jid_rfc,
 2071:           T(#{<<"db_jid_format">> => <<"mam_jid_rfc">>})),
 2072:     ?cfgh(P ++ [db_message_format], mam_message_xml,
 2073:           T(#{<<"db_message_format">> => <<"mam_message_xml">>})),
 2074:     ?cfgh(P ++ [extra_fin_element], mod_mam_utils,
 2075:           T(#{<<"extra_fin_element">> => <<"mod_mam_utils">>})),
 2076:     ?cfgh(P ++ [extra_lookup_params], mod_mam_utils,
 2077:           T(#{<<"extra_lookup_params">> => <<"mod_mam_utils">>})),
 2078:     ?errh(T(#{<<"backend">> => <<"notepad">>})),
 2079:     ?errh(T(#{<<"no_stanzaid_element">> => <<"true">>})),
 2080:     ?errh(T(#{<<"is_archivable_message">> => <<"mod_mam_fake">>})),
 2081:     ?errh(T(#{<<"archive_chat_markers">> => <<"maybe">>})),
 2082:     ?errh(T(#{<<"message_retraction">> => 1})),
 2083:     ?errh(T(#{<<"user_prefs_store">> => <<"textfile">>})),
 2084:     ?errh(T(#{<<"full_text_search">> => <<"disabled">>})),
 2085:     ?errh(T(#{<<"cache_users">> => []})),
 2086:     ?errh(T(#{<<"delete_domain_limit">> => []})),
 2087:     ?errh(T(#{<<"default_result_limit">> => -1})),
 2088:     ?errh(T(#{<<"max_result_limit">> => -2})),
 2089:     ?errh(T(#{<<"enforce_simple_queries">> => -2})),
 2090:     ?errh(T(#{<<"db_jid_format">> => <<"not_a_module">>})),
 2091:     ?errh(T(#{<<"db_message_format">> => <<"not_a_module">>})),
 2092:     ?errh(T(#{<<"extra_fin_element">> => <<"bad_module">>})),
 2093:     ?errh(T(#{<<"extra_lookup_params">> => <<"bad_module">>})).
 2094: 
 2095: test_cache_config(P, T) ->
 2096:     ?cfgh(P ++ [module], internal, T(#{<<"module">> => <<"internal">>})),
 2097:     ?cfgh(P ++ [time_to_live], 8600, T(#{<<"time_to_live">> => 8600})),
 2098:     ?cfgh(P ++ [time_to_live], infinity, T(#{<<"time_to_live">> => <<"infinity">>})),
 2099:     ?cfgh(P ++ [number_of_segments], 10, T(#{<<"number_of_segments">> => 10})),
 2100:     ?cfgh(P ++ [strategy], fifo, T(#{<<"strategy">> => <<"fifo">>})),
 2101:     ?errh(T(#{<<"module">> => <<"mod_wrong_cache">>})),
 2102:     ?errh(T(#{<<"time_to_live">> => 0})),
 2103:     ?errh(T(#{<<"strategy">> => <<"lifo">>})),
 2104:     ?errh(T(#{<<"number_of_segments">> => 0})),
 2105:     ?errh(T(#{<<"number_of_segments">> => <<"infinity">>})),
 2106:     ?errh(T(#{<<"cache">> => []})).
 2107: 
 2108: test_async_writer(ParentP, ParentT) ->
 2109:     P = ParentP ++ [async_writer],
 2110:     T = fun(Opts) -> ParentT(#{<<"async_writer">> => Opts}) end,
 2111:     ?cfgh(P ++ [flush_interval], 1500, T(#{<<"flush_interval">> => 1500})),
 2112:     ?cfgh(P ++ [batch_size], 1500, T(#{<<"batch_size">> => 1500})),
 2113:     ?cfgh(P ++ [pool_size], 1500, T(#{<<"pool_size">> => 1500})),
 2114:     ?cfgh(P ++ [enabled], false, T(#{<<"enabled">> => false})),
 2115:     ?errh(T(#{<<"flush_interval">> => -1})),
 2116:     ?errh(T(#{<<"batch_size">> => -1})),
 2117:     ?errh(T(#{<<"pool_size">> => -1})),
 2118:     ?errh(T(#{<<"enabled">> => <<"wrong">>})).
 2119: 
 2120: mod_muc(_Config) ->
 2121:     check_module_defaults(mod_muc),
 2122:     P = [modules, mod_muc],
 2123:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_muc">> => #{K => V}}} end,
 2124:     ?cfgh(P ++ [host], {prefix, <<"conference.">>},
 2125:           T(<<"host">>, <<"conference.@HOST@">>)),
 2126:     ?cfgh(P ++ [host], {fqdn, <<"conference.test">>},
 2127:           T(<<"host">>, <<"conference.test">>)),
 2128:     ?cfgh(P ++ [backend], mnesia, T(<<"backend">>, <<"mnesia">>)),
 2129:     ?cfgh(P ++ [access], all, T(<<"access">>, <<"all">>)),
 2130:     ?cfgh(P ++ [access_create], admin, T(<<"access_create">>, <<"admin">>)),
 2131:     ?cfgh(P ++ [access_admin], none, T(<<"access_admin">>, <<"none">>)),
 2132:     ?cfgh(P ++ [access_persistent], all, T(<<"access_persistent">>, <<"all">>)),
 2133:     ?cfgh(P ++ [history_size], 20, T(<<"history_size">>, 20)),
 2134:     ?cfgh(P ++ [room_shaper], muc_room_shaper,
 2135:           T(<<"room_shaper">>, <<"muc_room_shaper">>)),
 2136:     ?cfgh(P ++ [max_room_id], infinity, T(<<"max_room_id">>, <<"infinity">>)),
 2137:     ?cfgh(P ++ [max_room_name], 30, T(<<"max_room_name">>, 30)),
 2138:     ?cfgh(P ++ [max_room_desc], 0, T(<<"max_room_desc">>, 0)),
 2139:     ?cfgh(P ++ [min_message_interval], 10, T(<<"min_message_interval">>, 10)),
 2140:     ?cfgh(P ++ [min_presence_interval], 0, T(<<"min_presence_interval">>, 0)),
 2141:     ?cfgh(P ++ [max_users], 30, T(<<"max_users">>, 30)),
 2142:     ?cfgh(P ++ [max_users_admin_threshold], 2,
 2143:           T(<<"max_users_admin_threshold">>, 2)),
 2144:     ?cfgh(P ++ [user_message_shaper], muc_msg_shaper,
 2145:           T(<<"user_message_shaper">>, <<"muc_msg_shaper">>)),
 2146:     ?cfgh(P ++ [user_presence_shaper], muc_pres_shaper,
 2147:           T(<<"user_presence_shaper">>, <<"muc_pres_shaper">>)),
 2148:     ?cfgh(P ++ [max_user_conferences], 10, T(<<"max_user_conferences">>, 10)),
 2149:     ?cfgh(P ++ [http_auth_pool], external_auth,
 2150:           T(<<"http_auth_pool">>, <<"external_auth">>)),
 2151:     ?cfgh(P ++ [load_permanent_rooms_at_startup], true,
 2152:           T(<<"load_permanent_rooms_at_startup">>, true)),
 2153:     ?cfgh(P ++ [hibernate_timeout], infinity,
 2154:           T(<<"hibernate_timeout">>, <<"infinity">>)),
 2155:     ?cfgh(P ++ [hibernated_room_check_interval], 5000,
 2156:           T(<<"hibernated_room_check_interval">>, 5000)),
 2157:     ?cfgh(P ++ [hibernated_room_timeout], 0,
 2158:           T(<<"hibernated_room_timeout">>, 0)),
 2159:     ?errh(T(<<"host">>, <<>>)),
 2160:     ?errh(T(<<"host">>, <<"is this a host? no.">>)),
 2161:     ?errh(T(<<"host">>, [<<"valid.@HOST@">>])),
 2162:     ?errh(T(<<"host">>, <<"invalid.sub@HOST@">>)),
 2163:     ?errh(T(<<"host">>, <<"invalid.sub.@HOST@.as.well">>)),
 2164:     ?errh(T(<<"backend">>, <<"amnesia">>)),
 2165:     ?errh(T(<<"access">>, <<>>)),
 2166:     ?errh(T(<<"access_create">>, 1)),
 2167:     ?errh(T(<<"access_admin">>, [])),
 2168:     ?errh(T(<<"access_persistent">>, true)),
 2169:     ?errh(T(<<"history_size">>, <<"20">>)),
 2170:     ?errh(T(<<"room_shaper">>, <<>>)),
 2171:     ?errh(T(<<"max_room_id">>, #{})),
 2172:     ?errh(T(<<"max_room_name">>, <<"infinite!">>)),
 2173:     ?errh(T(<<"max_room_desc">>, -1)),
 2174:     ?errh(T(<<"min_message_interval">>, -10)),
 2175:     ?errh(T(<<"min_presence_interval">>, <<"infinity">>)),
 2176:     ?errh(T(<<"max_users">>, 0)),
 2177:     ?errh(T(<<"max_users_admin_threshold">>, 0)),
 2178:     ?errh(T(<<"user_message_shaper">>, [])),
 2179:     ?errh(T(<<"user_presence_shaper">>, <<>>)),
 2180:     ?errh(T(<<"max_user_conferences">>, -1)),
 2181:     ?errh(T(<<"http_auth_pool">>, <<>>)),
 2182:     ?errh(T(<<"load_permanent_rooms_at_startup">>, <<"true">>)),
 2183:     ?errh(T(<<"hibernate_timeout">>, <<"really big">>)),
 2184:     ?errh(T(<<"hibernated_room_check_interval">>, -1)),
 2185:     ?errh(T(<<"hibernated_room_timeout">>, false)).
 2186: 
 2187: mod_muc_default_room(_Config) ->
 2188:     P = [modules, mod_muc, default_room],
 2189:     T = fun(K, V) ->
 2190:                 M = #{<<"mod_muc">> => #{<<"default_room">> => #{K => V}}},
 2191:                 #{<<"modules">> => M}
 2192:         end,
 2193:     ?cfgh(P ++ [title], <<"living room">>, T(<<"title">>, <<"living room">>)),
 2194:     ?cfgh(P ++ [description], <<"a room that is alive">>,
 2195:           T(<<"description">>, <<"a room that is alive">>)),
 2196:     ?cfgh(P ++ [allow_change_subj], true, T(<<"allow_change_subj">>, true)),
 2197:     ?cfgh(P ++ [allow_query_users], false, T(<<"allow_query_users">>, false)),
 2198:     ?cfgh(P ++ [allow_private_messages], true,
 2199:           T(<<"allow_private_messages">>, true)),
 2200:     ?cfgh(P ++ [allow_visitor_status], false,
 2201:           T(<<"allow_visitor_status">>, false)),
 2202:     ?cfgh(P ++ [allow_visitor_nickchange], true,
 2203:           T(<<"allow_visitor_nickchange">>, true)),
 2204:     ?cfgh(P ++ [public], false, T(<<"public">>, false)),
 2205:     ?cfgh(P ++ [public_list], true, T(<<"public_list">>, true)),
 2206:     ?cfgh(P ++ [persistent], true, T(<<"persistent">>, true)),
 2207:     ?cfgh(P ++ [moderated], false, T(<<"moderated">>, false)),
 2208:     ?cfgh(P ++ [members_by_default], true, T(<<"members_by_default">>, true)),
 2209:     ?cfgh(P ++ [members_only], false, T(<<"members_only">>, false)),
 2210:     ?cfgh(P ++ [allow_user_invites], true, T(<<"allow_user_invites">>, true)),
 2211:     ?cfgh(P ++ [allow_multiple_sessions], false,
 2212:           T(<<"allow_multiple_sessions">>, false)),
 2213:     ?cfgh(P ++ [password_protected], true, T(<<"password_protected">>, true)),
 2214:     ?cfgh(P ++ [password], <<"secret">>, T(<<"password">>, <<"secret">>)),
 2215:     ?cfgh(P ++ [anonymous], true, T(<<"anonymous">>, true)),
 2216:     ?cfgh(P ++ [max_users],  100, T(<<"max_users">>, 100)),
 2217:     ?cfgh(P ++ [logging], false, T(<<"logging">>, false)),
 2218:     ?cfgh(P ++ [maygetmemberlist], [moderator],
 2219:           T(<<"maygetmemberlist">>, [<<"moderator">>])),
 2220:     ?cfgh(P ++ [subject], <<"Lambda days">>, T(<<"subject">>, <<"Lambda days">>)),
 2221:     ?cfgh(P ++ [subject_author], <<"Alice">>, T(<<"subject_author">>, <<"Alice">>)),
 2222:     ?errh(T(<<"title">>, true)),
 2223:     ?errh(T(<<"description">>, 1)),
 2224:     ?errh(T(<<"allow_change_subj">>, <<"true">>)),
 2225:     ?errh(T(<<"allow_query_users">>, <<>>)),
 2226:     ?errh(T(<<"allow_private_messages">>, 1)),
 2227:     ?errh(T(<<"allow_visitor_status">>, [])),
 2228:     ?errh(T(<<"allow_visitor_nickchange">>, #{})),
 2229:     ?errh(T(<<"public">>, 0)),
 2230:     ?errh(T(<<"public_list">>, [false])),
 2231:     ?errh(T(<<"persistent">>, 1)),
 2232:     ?errh(T(<<"moderated">>, <<"yes">>)),
 2233:     ?errh(T(<<"members_by_default">>, 0)),
 2234:     ?errh(T(<<"members_only">>, [true])),
 2235:     ?errh(T(<<"allow_user_invites">>, <<>>)),
 2236:     ?errh(T(<<"allow_multiple_sessions">>, [])),
 2237:     ?errh(T(<<"password_protected">>, #{})),
 2238:     ?errh(T(<<"password">>, false)),
 2239:     ?errh(T(<<"anonymous">>, <<"maybe">>)),
 2240:     ?errh(T(<<"max_users">>, 0)),
 2241:     ?errh(T(<<"logging">>, [true, false])),
 2242:     ?errh(T(<<"maygetmemberlist">>, <<"moderator">>)),
 2243:     ?errh(T(<<"maygetmemberlist">>, [<<>>])),
 2244:     ?errh(T(<<"subject">>, [<<"subjective">>])),
 2245:     ?errh(T(<<"subject_author">>, 1)).
 2246: 
 2247: mod_muc_default_room_affiliations(_Config) ->
 2248:     P = [modules, mod_muc, default_room, affiliations],
 2249:     T = fun(V) ->
 2250:                 M = #{<<"mod_muc">> => #{<<"default_room">> => #{<<"affiliations">> => V}}},
 2251:                 #{<<"modules">> => M}
 2252:         end,
 2253:     RequiredOpts = #{<<"user">> => <<"alice">>,
 2254:                      <<"server">> => <<"localhost">>,
 2255:                      <<"resource">> => <<"phone">>,
 2256:                      <<"affiliation">> => <<"moderator">>},
 2257:     ExpectedCfg = {{<<"alice">>, <<"localhost">>, <<"phone">>}, moderator},
 2258:     ?cfgh(P, [], T([])),
 2259:     ?cfgh(P, [ExpectedCfg], T([RequiredOpts])),
 2260:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 2261:     ?errh(T([RequiredOpts#{<<"user">> := <<>>}])),
 2262:     ?errh(T([RequiredOpts#{<<"server">> := <<"domain? not really!">>}])),
 2263:     ?errh(T([RequiredOpts#{<<"resource">> := false}])),
 2264:     ?errh(T([RequiredOpts#{<<"affiliation">> := <<>>}])).
 2265: 
 2266: mod_muc_log(_Config) ->
 2267:     check_module_defaults(mod_muc_log),
 2268:     P = [modules, mod_muc_log],
 2269:     T = fun(K, V) -> #{<<"modules">> => #{<<"mod_muc_log">> => #{K => V}}} end,
 2270:     ?cfgh(P ++ [outdir], "www/muc", T(<<"outdir">>, <<"www/muc">>)),
 2271:     ?cfgh(P ++ [access_log], muc_admin, T(<<"access_log">>, <<"muc_admin">>)),
 2272:     ?cfgh(P ++ [dirtype], subdirs, T(<<"dirtype">>, <<"subdirs">>)),
 2273:     ?cfgh(P ++ [dirname], room_name, T(<<"dirname">>, <<"room_name">>)),
 2274:     ?cfgh(P ++ [file_format], html, T(<<"file_format">>, <<"html">>)),
 2275:     ?cfgh(P ++ [css_file], <<"path/to/css_file">>,
 2276:           T(<<"css_file">>, <<"path/to/css_file">>)),
 2277:     ?cfgh(P ++ [timezone], local, T(<<"timezone">>, <<"local">>)),
 2278:     ?cfgh(P ++ [spam_prevention], false, T(<<"spam_prevention">>, false)),
 2279:     ?errh(T(<<"outdir">>, <<"does/not/exist">>)),
 2280:     ?errh(T(<<"access_log">>, 1)),
 2281:     ?errh(T(<<"dirtype">>, <<"imaginary">>)),
 2282:     ?errh(T(<<"dirname">>, <<"dyrektory">>)),
 2283:     ?errh(T(<<"file_format">>, <<"none">>)),
 2284:     ?errh(T(<<"css_file">>, <<>>)),
 2285:     ?errh(T(<<"timezone">>, <<"yes">>)),
 2286:     ?errh(T(<<"spam_prevention">>, <<"spam and eggs and spam">>)).
 2287: 
 2288: mod_muc_log_top_link(_Config) ->
 2289:     P = [modules, mod_muc_log, top_link],
 2290:     T = fun(V) ->
 2291:                 M = #{<<"mod_muc_log">> => #{<<"top_link">> => V}},
 2292:                 #{<<"modules">> => M}
 2293:         end,
 2294:     RequiredOpts = #{<<"target">> => <<"https://esl.github.io/MongooseDocs/">>,
 2295:                      <<"text">> => <<"Docs">>},
 2296:     ExpectedCfg = {"https://esl.github.io/MongooseDocs/", "Docs"},
 2297:     ?cfgh(P, ExpectedCfg, T(RequiredOpts)),
 2298:     [?errh(T(maps:remove(K, RequiredOpts))) || K <- maps:keys(RequiredOpts)],
 2299:     ?errh(T(RequiredOpts#{<<"target">> => true})),
 2300:     ?errh(T(RequiredOpts#{<<"text">> => <<"">>})).
 2301: 
 2302: mod_muc_light(_Config) ->
 2303:     check_module_defaults(mod_muc_light),
 2304:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_muc_light">> => Opts}} end,
 2305:     P = [modules, mod_muc_light],
 2306:     test_cache_config(P ++ [cache_affs], fun(Opts) -> T(#{<<"cache_affs">> => Opts}) end),
 2307:     ?cfgh(P ++ [backend], mnesia,
 2308:           T(#{<<"backend">> => <<"mnesia">>})),
 2309:     ?cfgh(P ++ [host], {prefix, <<"muclight.">>},
 2310:           T(#{<<"host">> => <<"muclight.@HOST@">>})),
 2311:     ?cfgh(P ++ [host], {fqdn, <<"muclight.test">>},
 2312:           T(#{<<"host">> => <<"muclight.test">>})),
 2313:     ?cfgh(P ++ [equal_occupants], true,
 2314:           T(#{<<"equal_occupants">> => true})),
 2315:     ?cfgh(P ++ [legacy_mode], false,
 2316:           T(#{<<"legacy_mode">> => false})),
 2317:     ?cfgh(P ++ [rooms_per_user], 100,
 2318:           T(#{<<"rooms_per_user">> => 100})),
 2319:     ?cfgh(P ++ [blocking], false,
 2320:           T(#{<<"blocking">> => false})),
 2321:     ?cfgh(P ++ [all_can_configure], true,
 2322:           T(#{<<"all_can_configure">> => true})),
 2323:     ?cfgh(P ++ [all_can_invite], false,
 2324:           T(#{<<"all_can_invite">> => false})),
 2325:     ?cfgh(P ++ [max_occupants], infinity,
 2326:           T(#{<<"max_occupants">> => <<"infinity">>})),
 2327:     ?cfgh(P ++ [rooms_per_page], 10,
 2328:           T(#{<<"rooms_per_page">> => 10})),
 2329:     ?cfgh(P ++ [rooms_in_rosters], true,
 2330:           T(#{<<"rooms_in_rosters">> => true})),
 2331:     ?errh(T(#{<<"backend">> => <<"frontend">>})),
 2332:     ?errh(T(#{<<"host">> => <<"what is a domain?!">>})),
 2333:     ?errh(T(#{<<"host">> => <<"invalid..com">>})),
 2334:     ?errh(T(#{<<"host">> => [<<"valid.@HOST@">>]})),
 2335:     ?errh(T(#{<<"host">> => <<"invalid.sub@HOST@">>})),
 2336:     ?errh(T(#{<<"host">> => <<"invalid.sub.@HOST@.as.well">>})),
 2337:     ?errh(T(#{<<"host">> => <<"inv@lidsub.@HOST@">>})),
 2338:     ?errh(T(#{<<"equal_occupants">> => <<"true">>})),
 2339:     ?errh(T(#{<<"legacy_mode">> => 1234})),
 2340:     ?errh(T(#{<<"rooms_per_user">> => 0})),
 2341:     ?errh(T(#{<<"blocking">> => <<"true">>})),
 2342:     ?errh(T(#{<<"all_can_configure">> => []})),
 2343:     ?errh(T(#{<<"all_can_invite">> => #{}})),
 2344:     ?errh(T(#{<<"max_occupants">> => <<"seven">>})),
 2345:     ?errh(T(#{<<"rooms_per_page">> => false})),
 2346:     ?errh(T(#{<<"rooms_in_rosters">> => [1, 2, 3]})).
 2347: 
 2348: mod_muc_light_config_schema(_Config) ->
 2349:     T = fun(Opts) -> #{<<"modules">> =>
 2350:                            #{<<"mod_muc_light">> => #{<<"config_schema">> => Opts}}} end,
 2351:     P = [modules, mod_muc_light, config_schema],
 2352:     Field = #{<<"field">> => <<"my_field">>},
 2353:     ?cfgh(P, [], T([])),
 2354:     ?cfgh(P, [{<<"my_field">>, <<"My Room">>, my_field, binary}],
 2355:           T([Field#{<<"string_value">> => <<"My Room">>}])),
 2356:     ?cfgh(P, [{<<"my_field">>, 1, my_field, integer}],
 2357:           T([Field#{<<"integer_value">> => 1}])),
 2358:     ?cfgh(P, [{<<"my_field">>, 0.5, my_field, float}],
 2359:           T([Field#{<<"float_value">> => 0.5}])),
 2360:     ?cfgh(P, [{<<"my_field">>, 0, your_field, integer}],
 2361:           T([Field#{<<"integer_value">> => 0,
 2362:                     <<"internal_key">> => <<"your_field">>}])),
 2363:     ?cfgh(P, [{<<"żółć"/utf8>>, <<"Рентгеноэлектрокардиографический"/utf8>>, 'żółć', binary}],
 2364:           T([#{<<"field">> => <<"żółć"/utf8>>,
 2365:                <<"string_value">> => <<"Рентгеноэлектрокардиографический"/utf8>>}])),
 2366:     ?cfgh(P, [{<<"first">>, 1, first, integer}, % the config is u-key-sorted
 2367:               {<<"second">>, <<"two">>, second, binary}],
 2368:           T([#{<<"field">> => <<"second">>, <<"string_value">> => <<"two">>},
 2369:              #{<<"field">> => <<"second">>, <<"float_value">> => 2.0},
 2370:              #{<<"field">> => <<"first">>, <<"integer_value">> => 1}])),
 2371:     ?errh(T([#{<<"string_value">> => <<"My Room">>}])),
 2372:     ?errh(T([#{<<"field">> => <<>>,
 2373:                <<"string_value">> => <<"My Room">>}])),
 2374:     ?errh(T([Field#{<<"string_value">> => 0}])),
 2375:     ?errh(T([Field#{<<"integer_value">> => 1.5}])),
 2376:     ?errh(T([Field#{<<"float_value">> => 1}])),
 2377:     ?errh(T([Field#{<<"integer_value">> => 0,
 2378:                     <<"string_value">> => <<"My Room">>}])),
 2379:     ?errh(T([Field#{<<"integer_value">> => 0,
 2380:                     <<"internal_key">> => <<>>}])).
 2381: 
 2382: mod_offline(_Config) ->
 2383:     check_module_defaults(mod_offline),
 2384:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_offline">> => Opts}} end,
 2385:     P = [modules, mod_offline],
 2386:     ?cfgh(P ++ [access_max_user_messages], custom_max_user_offline_messages,
 2387:           T(#{<<"access_max_user_messages">> => <<"custom_max_user_offline_messages">>})),
 2388:     ?cfgh(P ++ [backend], rdbms,
 2389:           T(#{<<"backend">> => <<"rdbms">>})),
 2390:     ?errh(T(#{<<"access_max_user_messages">> => 1})),
 2391:     ?errh(T(#{<<"backend">> => <<"frontend">>})).
 2392: 
 2393: mod_offline_chatmarkers(_Config) ->
 2394:     check_module_defaults(mod_offline_chatmarkers),
 2395:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_offline_chatmarkers">> => Opts}} end,
 2396:     P = [modules, mod_offline_chatmarkers],
 2397:     ?cfgh(P ++ [backend], rdbms,
 2398:           T(#{<<"backend">> => <<"rdbms">>})),
 2399:     ?cfgh(P ++ [store_groupchat_messages], true,
 2400:           T(#{<<"store_groupchat_messages">> => true})),
 2401:     ?errh(T(#{<<"store_groupchat_messages">> => 1})),
 2402:     ?errh(T(#{<<"backend">> => <<"frontend">>})).
 2403: 
 2404: mod_ping(_Config) ->
 2405:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_ping">> => Opts}} end,
 2406:     P = [modules, mod_ping],
 2407:     check_iqdisc(mod_ping),
 2408:     check_module_defaults(mod_ping),
 2409:     ?cfgh(P ++ [send_pings], true,
 2410:           T(#{<<"send_pings">> => true})),
 2411:     ?cfgh(P ++ [ping_interval], timer:seconds(10),
 2412:           T(#{<<"ping_interval">> => 10})),
 2413:     ?cfgh(P ++ [timeout_action], kill,
 2414:           T(#{<<"timeout_action">> => <<"kill">>})),
 2415:     ?cfgh(P ++ [ping_req_timeout], timer:seconds(20),
 2416:           T(#{<<"ping_req_timeout">> => 20})),
 2417:     ?errh(T(#{<<"send_pings">> => 1})),
 2418:     ?errh(T(#{<<"ping_interval">> => 0})),
 2419:     ?errh(T(#{<<"timeout_action">> => <<"kill_them_all">>})),
 2420:     ?errh(T(#{<<"ping_req_timeout">> => 0})).
 2421: 
 2422: mod_privacy(_Config) ->
 2423:     test_privacy_opts(mod_privacy).
 2424: 
 2425: test_privacy_opts(Module) ->
 2426:     T = fun(Opts) -> #{<<"modules">> => #{atom_to_binary(Module) => Opts}} end,
 2427:     P = [modules, Module],
 2428:     check_module_defaults(Module),
 2429:     ?cfgh(P ++ [backend], mnesia,
 2430:           T(#{<<"backend">> => <<"mnesia">>})),
 2431:     ?errh(T(#{<<"backend">> => <<"mongoddt">>})).
 2432: 
 2433: mod_private(_Config) ->
 2434:     check_iqdisc(mod_private),
 2435:     check_module_defaults(mod_private),
 2436:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_private">> => Opts}} end,
 2437:     P = [modules, mod_private],
 2438:     ?cfgh(P ++ [backend], rdbms, T(#{<<"backend">> => <<"rdbms">>})),
 2439:     ?errh(T(#{<<"backend">> => <<"mssql">>})).
 2440: 
 2441: mod_pubsub(_Config) ->
 2442:     check_iqdisc(mod_pubsub),
 2443:     check_module_defaults(mod_pubsub),
 2444:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_pubsub">> => Opts}} end,
 2445:     P = [modules, mod_pubsub],
 2446:     ?cfgh(P ++ [host], {prefix, <<"pubsub.">>},
 2447:           T(#{<<"host">> => <<"pubsub.@HOST@">>})),
 2448:     ?cfgh(P ++ [host], {fqdn, <<"pubsub.test">>},
 2449:           T(#{<<"host">> => <<"pubsub.test">>})),
 2450:     ?cfgh(P ++ [backend], rdbms,
 2451:           T(#{<<"backend">> => <<"rdbms">>})),
 2452:     ?cfgh(P ++ [access_createnode], all,
 2453:           T(#{<<"access_createnode">> => <<"all">>})),
 2454:     ?cfgh(P ++ [max_items_node], 20,
 2455:           T(#{<<"max_items_node">> => 20})),
 2456:     ?cfgh(P ++ [max_subscriptions_node], 30,
 2457:           T(#{<<"max_subscriptions_node">> => 30})),
 2458:     ?cfgh(P ++ [nodetree], nodetree_tree,
 2459:           T(#{<<"nodetree">> => <<"tree">>})),
 2460:     ?cfgh(P ++ [ignore_pep_from_offline], false,
 2461:           T(#{<<"ignore_pep_from_offline">> => false})),
 2462:     ?cfgh(P ++ [last_item_cache], rdbms,
 2463:           T(#{<<"last_item_cache">> => <<"rdbms">>})),
 2464:     ?cfgh(P ++ [plugins], [<<"flat">>, <<"dag">>],
 2465:           T(#{<<"plugins">> => [<<"flat">>, <<"dag">>]})),
 2466:     ?cfgh(P ++ [item_publisher], true,
 2467:           T(#{<<"item_publisher">> => true})),
 2468:     ?cfgh(P ++ [sync_broadcast], false,
 2469:           T(#{<<"sync_broadcast">> => false})),
 2470:     test_wpool(P ++ [wpool], fun(Opts) -> T(#{<<"wpool">> => Opts}) end),
 2471:     ?errh(T(#{<<"host">> => <<"">>})),
 2472:     ?errh(T(#{<<"host">> => <<"is this a host? no.">>})),
 2473:     ?errh(T(#{<<"host">> => <<"invalid domain.com">>})),
 2474:     ?errh(T(#{<<"host">> => <<"inv@lid.com">>})),
 2475:     ?errh(T(#{<<"host">> => [<<"valid.@HOST@">>]})),
 2476:     ?errh(T(#{<<"host">> => <<"invalid.sub@HOST@">>})),
 2477:     ?errh(T(#{<<"host">> => <<"invalid.sub.@HOST@.as.well">>})),
 2478:     ?errh(T(#{<<"backend">> => <<"amnesia">>})),
 2479:     ?errh(T(#{<<"access_createnode">> => <<"">>})),
 2480:     ?errh(T(#{<<"max_items_node">> => -1})),
 2481:     ?errh(T(#{<<"max_subscriptions_node">> => 3.1415})),
 2482:     ?errh(T(#{<<"nodetree">> => <<"christmas_tree">>})),
 2483:     ?errh(T(#{<<"ignore_pep_from_offline">> => <<"maybe">>})),
 2484:     ?errh(T(#{<<"last_item_cache">> => false})),
 2485:     ?errh(T(#{<<"plugins">> => [<<"deep">>]})),
 2486:     ?errh(T(#{<<"item_publisher">> => 1})),
 2487:     ?errh(T(#{<<"sync_broadcast">> => []})).
 2488: 
 2489: mod_pubsub_pep_mapping(_Config) ->
 2490:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_pubsub">> =>
 2491:                                               #{<<"pep_mapping">> => Opts}}} end,
 2492:     P = [modules, mod_pubsub, pep_mapping],
 2493:     RequiredOpts = #{<<"namespace">> => <<"urn:xmpp:microblog:0">>,
 2494:                      <<"node">> => <<"mb">>},
 2495:     ?cfgh(P ++ [<<"urn:xmpp:microblog:0">>], <<"mb">>,
 2496:           T([RequiredOpts])),
 2497:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 2498:     [?errh(T([RequiredOpts#{Key => <<>>}])) || Key <- maps:keys(RequiredOpts)].
 2499: 
 2500: mod_pubsub_default_node_config(_Config) ->
 2501:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_pubsub">> =>
 2502:                                               #{<<"default_node_config">> => Opts}}} end,
 2503:     P = [modules, mod_pubsub, default_node_config],
 2504:     ?cfgh(P, [{access_model, open}],
 2505:           T(#{<<"access_model">> => <<"open">>})),
 2506:     ?cfgh(P, [{deliver_notifications, true}],
 2507:           T(#{<<"deliver_notifications">> => true})),
 2508:     ?cfgh(P, [{deliver_payloads, false}],
 2509:           T(#{<<"deliver_payloads">> => false})),
 2510:     ?cfgh(P, [{max_items, 1000}],
 2511:           T(#{<<"max_items">> => 1000})),
 2512:     ?cfgh(P, [{max_payload_size, 1000}],
 2513:           T(#{<<"max_payload_size">> => 1000})),
 2514:     ?cfgh(P, [{node_type, dag}],
 2515:           T(#{<<"node_type">> => <<"dag">>})),
 2516:     ?cfgh(P, [{notification_type, headline}],
 2517:           T(#{<<"notification_type">> => <<"headline">>})),
 2518:     ?cfgh(P, [{notify_config, true}],
 2519:           T(#{<<"notify_config">> => true})),
 2520:     ?cfgh(P, [{notify_delete, false}],
 2521:           T(#{<<"notify_delete">> => false})),
 2522:     ?cfgh(P, [{notify_retract, true}],
 2523:           T(#{<<"notify_retract">> => true})),
 2524:     ?cfgh(P, [{persist_items, false}],
 2525:           T(#{<<"persist_items">> => false})),
 2526:     ?cfgh(P, [{presence_based_delivery, true}],
 2527:           T(#{<<"presence_based_delivery">> => true})),
 2528:     ?cfgh(P, [{publish_model, open}],
 2529:           T(#{<<"publish_model">> => <<"open">>})),
 2530:     ?cfgh(P, [{purge_offline, false}],
 2531:           T(#{<<"purge_offline">> => false})),
 2532:     ?cfgh(P, [{roster_groups_allowed, [<<"friends">>]}],
 2533:           T(#{<<"roster_groups_allowed">> => [<<"friends">>]})),
 2534:     ?cfgh(P, [{send_last_published_item, on_sub_and_presence}],
 2535:           T(#{<<"send_last_published_item">> => <<"on_sub_and_presence">>})),
 2536:     ?cfgh(P, [{subscribe, true}],
 2537:           T(#{<<"subscribe">> => true})),
 2538:     ?errh(T(#{<<"access_model">> => <<>>})),
 2539:     ?errh(T(#{<<"deliver_notifications">> => <<"yes">>})),
 2540:     ?errh(T(#{<<"deliver_payloads">> => 0})),
 2541:     ?errh(T(#{<<"max_items">> => -1})),
 2542:     ?errh(T(#{<<"max_payload_size">> => -1})),
 2543:     ?errh(T(#{<<"node_type">> => [<<"dag">>]})),
 2544:     ?errh(T(#{<<"notification_type">> => <<>>})),
 2545:     ?errh(T(#{<<"notify_config">> => <<"false">>})),
 2546:     ?errh(T(#{<<"notify_delete">> => [true]})),
 2547:     ?errh(T(#{<<"notify_retract">> => #{}})),
 2548:     ?errh(T(#{<<"persist_items">> => 1})),
 2549:     ?errh(T(#{<<"presence_based_delivery">> => []})),
 2550:     ?errh(T(#{<<"publish_model">> => <<"">>})),
 2551:     ?errh(T(#{<<"purge_offline">> => 1})),
 2552:     ?errh(T(#{<<"roster_groups_allowed">> => [<<>>]})),
 2553:     ?errh(T(#{<<"send_last_published_item">> => <<>>})),
 2554:     ?errh(T(#{<<"subscribe">> => <<"never">>})).
 2555: 
 2556: mod_push_service_mongoosepush(_Config) ->
 2557:     check_module_defaults(mod_push_service_mongoosepush),
 2558:     P = [modules, mod_push_service_mongoosepush],
 2559:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_push_service_mongoosepush">> => Opts}} end,
 2560:     ?cfgh(P ++ [pool_name], test_pool,
 2561:           T(#{<<"pool_name">> => <<"test_pool">>})),
 2562:     ?cfgh(P ++ [api_version], <<"v2">>,
 2563:           T(#{<<"api_version">> => <<"v2">>})),
 2564:     ?cfgh(P ++ [max_http_connections], 999,
 2565:           T(#{<<"max_http_connections">> => 999})),
 2566:     ?errh(T(#{<<"pool_name">> => 1})),
 2567:     ?errh(T(#{<<"api_version">> => <<"v4">>})),
 2568:     ?errh(T(#{<<"max_http_connections">> => -1})).
 2569: 
 2570: mod_register(_Config) ->
 2571:     check_module_defaults(mod_register),
 2572:     check_iqdisc(mod_register),
 2573:     P = [modules, mod_register],
 2574:     ?cfgh(P ++ [access], register,
 2575:           ip_access_register(<<"127.0.0.1">>)),
 2576:     ?cfgh(P ++ [ip_access], [{allow, "127.0.0.0/8"},
 2577:                              {deny, "0.0.0.0"}],
 2578:           ip_access_register(<<"0.0.0.0">>)),
 2579:     ?cfgh(P ++ [ip_access], [{allow, "127.0.0.0/8"},
 2580:                              {deny, "0.0.0.4"}],
 2581:           ip_access_register(<<"0.0.0.4">>)),
 2582:     ?cfgh(P ++ [ip_access], [{allow, "127.0.0.0/8"},
 2583:                              {deny, "::1"}],
 2584:           ip_access_register(<<"::1">>)),
 2585:     ?cfgh(P ++ [ip_access], [{allow, "127.0.0.0/8"},
 2586:                              {deny, "::1/128"}],
 2587:           ip_access_register(<<"::1/128">>)),
 2588:     ?errh(invalid_ip_access_register()),
 2589:     ?errh(invalid_ip_access_register_ipv6()),
 2590:     ?errh(ip_access_register(<<"hello">>)),
 2591:     ?errh(ip_access_register(<<"0.d">>)),
 2592:     ?cfgh(P ++ [welcome_message], {"Subject", "Body"},
 2593:           welcome_message()),
 2594:     %% List of jids
 2595:     ?cfgh(P ++ [registration_watchers], [<<"alice@bob">>, <<"ilovemongoose@help">>],
 2596:           registration_watchers([<<"alice@bob">>, <<"ilovemongoose@help">>])),
 2597:     ?errh(registration_watchers([<<"alice@bob">>, <<"jids@have@no@feelings!">>])),
 2598:     %% non-negative integer
 2599:     ?cfgh(P ++ [password_strength], 42,
 2600:           password_strength_register(42)),
 2601:     ?errh(password_strength_register(<<"42">>)),
 2602:     ?errh(password_strength_register(<<"strong">>)),
 2603:     ?errh(password_strength_register(-150)),
 2604:     ?errh(welcome_message(<<"Subject">>, 1)),
 2605:     ?errh(welcome_message(1, <<"Body">>)).
 2606: 
 2607: welcome_message() ->
 2608:     welcome_message(<<"Subject">>, <<"Body">>).
 2609: 
 2610: welcome_message(S, B) ->
 2611:     Opts = #{<<"welcome_message">> => #{<<"subject">> => S, <<"body">> => B}},
 2612:     #{<<"modules">> => #{<<"mod_register">> => Opts}}.
 2613: 
 2614: password_strength_register(Strength) ->
 2615:     Opts = #{<<"password_strength">> => Strength},
 2616:     #{<<"modules">> => #{<<"mod_register">> => Opts}}.
 2617: 
 2618: ip_access_register(Ip) ->
 2619:     Opts = #{<<"access">> => <<"register">>,
 2620:              <<"ip_access">> =>
 2621:                  [#{<<"address">> => <<"127.0.0.0/8">>, <<"policy">> => <<"allow">>},
 2622:                   #{<<"address">> => Ip, <<"policy">> => <<"deny">>}]},
 2623:     #{<<"modules">> => #{<<"mod_register">> => Opts}}.
 2624: 
 2625: invalid_ip_access_register() ->
 2626:     Opts = #{<<"access">> => <<"register">>,
 2627:              <<"ip_access">> =>
 2628:                  [#{<<"address">> => <<"127.0.0.0/8">>, <<"policy">> => <<"allawww">>},
 2629:                   #{<<"address">> => <<"8.8.8.8">>, <<"policy">> => <<"denyh">>}]},
 2630:     #{<<"modules">> => #{<<"mod_register">> => Opts}}.
 2631: 
 2632: invalid_ip_access_register_ipv6() ->
 2633:     Opts = #{<<"access">> => <<"register">>,
 2634:              <<"ip_access">> =>
 2635:                  [#{<<"address">> => <<"::1/129">>, <<"policy">> => <<"allow">>}]},
 2636:     #{<<"modules">> => #{<<"mod_register">> => Opts}}.
 2637: 
 2638: registration_watchers(JidBins) ->
 2639:     Opts = #{<<"registration_watchers">> => JidBins},
 2640:     #{<<"modules">> => #{<<"mod_register">> => Opts}}.
 2641: 
 2642: mod_roster(_Config) ->
 2643:     check_iqdisc(mod_roster),
 2644:     check_module_defaults(mod_roster),
 2645:     P = [modules, mod_roster],
 2646:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_roster">> => Opts}} end,
 2647:     ?cfgh(P ++ [versioning],  true,
 2648:           T(#{<<"versioning">> => true})),
 2649:     ?cfgh(P ++ [store_current_id], true,
 2650:           T(#{<<"store_current_id">> => true})),
 2651:     ?cfgh(P ++ [backend], rdbms,
 2652:           T(#{<<"backend">> => <<"rdbms">>})),
 2653:     ?errh(T(#{<<"versioning">> => 1})),
 2654:     ?errh(T(#{<<"store_current_id">> => 1})),
 2655:     ?errh(T(#{<<"backend">> => 1})),
 2656:     ?errh(T(#{<<"backend">> => <<"iloveyou">>})).
 2657: 
 2658: mod_shared_roster_ldap(_Config) ->
 2659:     check_module_defaults(mod_shared_roster_ldap),
 2660:     P = [modules, mod_shared_roster_ldap],
 2661:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_shared_roster_ldap">> => Opts}} end,
 2662:     ?cfgh(P ++ [pool_tag], my_tag,
 2663:           T(#{<<"pool_tag">> => <<"my_tag">>})),
 2664:     ?cfgh(P ++ [base], <<"string">>,
 2665:           T(#{<<"base">> => <<"string">>})),
 2666:     ?cfgh(P ++ [deref], always,
 2667:           T(#{<<"deref">> => <<"always">>})),
 2668:     %% Options: attributes
 2669:     ?cfgh(P ++ [groupattr], <<"cn">>,
 2670:           T(#{<<"groupattr">> => <<"cn">>})),
 2671:     ?cfgh(P ++ [groupdesc], <<"default">>,
 2672:           T(#{<<"groupdesc">> => <<"default">>})),
 2673:     ?cfgh(P ++ [userdesc], <<"cn">>,
 2674:           T(#{<<"userdesc">> => <<"cn">>})),
 2675:     ?cfgh(P ++ [useruid], <<"cn">>,
 2676:           T(#{<<"useruid">> => <<"cn">>})),
 2677:     ?cfgh(P ++ [memberattr], <<"memberUid">>,
 2678:           T(#{<<"memberattr">> => <<"memberUid">>})),
 2679:     ?cfgh(P ++ [memberattr_format], <<"%u">>,
 2680:           T(#{<<"memberattr_format">> => <<"%u">>})),
 2681:     ?cfgh(P ++ [memberattr_format_re], <<"">>,
 2682:           T(#{<<"memberattr_format_re">> => <<"">>})),
 2683:     %% Options: parameters
 2684:     ?cfgh(P ++ [auth_check], true,
 2685:           T(#{<<"auth_check">> => true})),
 2686:     ?cfgh(P ++ [user_cache_validity], 300,
 2687:           T(#{<<"user_cache_validity">> => 300})),
 2688:     ?cfgh(P ++ [group_cache_validity], 300,
 2689:           T(#{<<"group_cache_validity">> => 300})),
 2690:     ?cfgh(P ++ [user_cache_size], 300,
 2691:           T(#{<<"user_cache_size">> => 300})),
 2692:     ?cfgh(P ++ [group_cache_size], 300,
 2693:           T(#{<<"group_cache_size">> => 300})),
 2694:     %% Options: LDAP filters
 2695:     ?cfgh(P ++ [rfilter], <<"rfilter_test">>,
 2696:           T(#{<<"rfilter">> => <<"rfilter_test">>})),
 2697:     ?cfgh(P ++ [gfilter], <<"gfilter_test">>,
 2698:           T(#{<<"gfilter">> => <<"gfilter_test">>})),
 2699:     ?cfgh(P ++ [ufilter], <<"ufilter_test">>,
 2700:           T(#{<<"ufilter">> => <<"ufilter_test">>})),
 2701:     ?cfgh(P ++ [filter], <<"filter_test">>,
 2702:           T(#{<<"filter">> => <<"filter_test">>})),
 2703:     ?errh(T(#{<<"pool_tag">> => 1})),
 2704:     ?errh(T(#{<<"base">> => 1})),
 2705:     ?errh(T(#{<<"deref">> => 1})),
 2706:     %% Options: attributes
 2707:     ?errh(T(#{<<"groupattr">> => 1})),
 2708:     ?errh(T(#{<<"groupdesc">> => 1})),
 2709:     ?errh(T(#{<<"userdesc">> => 1})),
 2710:     ?errh(T(#{<<"useruid">> => 1})),
 2711:     ?errh(T(#{<<"memberattr">> => 1})),
 2712:     ?errh(T(#{<<"memberattr_format">> => 1})),
 2713:     ?errh(T(#{<<"memberattr_format_re">> => 1})),
 2714:     %% Options: parameters
 2715:     ?errh(T(#{<<"auth_check">> => 1})),
 2716:     ?errh(T(#{<<"user_cache_validity">> => -1})),
 2717:     ?errh(T(#{<<"group_cache_validity">> => -1})),
 2718:     ?errh(T(#{<<"user_cache_size">> => -1})),
 2719:     ?errh(T(#{<<"group_cache_size">> => -1})),
 2720:     %% Options: LDAP filters
 2721:     ?errh(T(#{<<"rfilter">> => 1})),
 2722:     ?errh(T(#{<<"gfilter">> => 1})),
 2723:     ?errh(T(#{<<"ufilter">> => 1})),
 2724:     ?errh(T(#{<<"filter">> => 1})).
 2725: 
 2726: mod_sic(_Config) ->
 2727:     check_module_defaults(mod_sic),
 2728:     check_iqdisc(mod_sic).
 2729: 
 2730: mod_smart_markers(_Config) ->
 2731:     check_module_defaults(mod_smart_markers),
 2732:     check_iqdisc(mod_smart_markers),
 2733:     P = [modules, mod_smart_markers],
 2734:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_smart_markers">> => Opts}} end,
 2735:     ?cfgh(P ++ [backend], rdbms, T(#{<<"backend">> => <<"rdbms">>})),
 2736:     ?cfgh(P ++ [keep_private], true, T(#{<<"keep_private">> => true})),
 2737:     ?cfgh(P ++ [async_writer], #{pool_size => 8}, T(#{<<"async_writer">> => #{<<"pool_size">> => 8}})),
 2738:     ?errh(T(#{<<"backend">> => <<"nodejs">>})),
 2739:     ?errh(T(#{<<"keep_private">> => 1})).
 2740: 
 2741: mod_stream_management(_Config) ->
 2742:     check_module_defaults(mod_stream_management),
 2743:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_stream_management">> => Opts}} end,
 2744:     P = [modules, mod_stream_management],
 2745:     ?cfgh(P ++ [buffer_max], no_buffer, T(#{<<"buffer">> => false})),
 2746:     ?cfgh(P ++ [buffer_max], 10,  T(#{<<"buffer_max">> => 10})),
 2747:     ?cfgh(P ++ [ack_freq], never, T(#{<<"ack">> => false})),
 2748:     ?cfgh(P ++ [ack_freq], 1, T(#{<<"ack_freq">> => 1})),
 2749:     ?cfgh(P ++ [resume_timeout], 999, T(#{<<"resume_timeout">> => 999})),
 2750: 
 2751:     ?errh(T(#{<<"buffer">> => 0})),
 2752:     ?errh(T(#{<<"buffer_max">> => -1})),
 2753:     ?errh(T(#{<<"ack">> => <<"false">>})),
 2754:     ?errh(T(#{<<"ack_freq">> => 0})),
 2755:     ?errh(T(#{<<"resume_timeout">> => true})),
 2756:     ?errh(T(#{<<"backend">> => <<"iloveyou">>})).
 2757: 
 2758: mod_stream_management_stale_h(_Config) ->
 2759:     P = [modules, mod_stream_management, stale_h],
 2760:     T = fun(Opts) -> #{<<"modules">> =>
 2761:                            #{<<"mod_stream_management">> => #{<<"stale_h">> => Opts}}} end,
 2762:     ?cfgh(P ++ [enabled], true, T(#{<<"enabled">> => true})),
 2763:     ?cfgh(P ++ [repeat_after], 999, T(#{<<"repeat_after">> => 999})),
 2764:     ?cfgh(P ++ [geriatric], 999, T(#{<<"geriatric">> => 999})),
 2765:     ?cfgh(P, config_parser_helper:default_config(P), T(#{})),
 2766: 
 2767:     ?errh(T(#{<<"enabled">> => <<"true">>})),
 2768:     ?errh(T(#{<<"repeat_after">> => -1})),
 2769:     ?errh(T(#{<<"geriatric">> => <<"one">>})).
 2770: 
 2771: mod_time(_Config) ->
 2772:     check_iqdisc(mod_time),
 2773:     check_module_defaults(mod_time).
 2774: 
 2775: mod_vcard(_Config) ->
 2776:     check_module_defaults(mod_vcard),
 2777:     check_iqdisc(mod_vcard),
 2778:     P = [modules, mod_vcard],
 2779:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_vcard">> => Opts}} end,
 2780:     ?cfgh(P ++ [iqdisc], one_queue,
 2781:           T(#{<<"iqdisc">> => #{<<"type">> => <<"one_queue">>}})),
 2782:     ?cfgh(P ++ [host], {prefix, <<"vjud.">>},
 2783:           T(#{<<"host">> => <<"vjud.@HOST@">>})),
 2784:     ?cfgh(P ++ [host], {fqdn, <<"vjud.test">>},
 2785:           T(#{<<"host">> => <<"vjud.test">>})),
 2786:     ?cfgh(P ++ [search], true,
 2787:           T(#{<<"search">> => true})),
 2788:     ?cfgh(P ++ [backend], mnesia,
 2789:           T(#{<<"backend">> => <<"mnesia">>})),
 2790:     ?cfgh(P ++ [matches], infinity,
 2791:           T(#{<<"matches">> => <<"infinity">>})),
 2792:     %% ldap
 2793:     ?cfgh(P ++ [ldap], config_parser_helper:default_config(P ++ [ldap]),
 2794:           T(#{<<"backend">> => <<"ldap">>})),
 2795:     ?cfgh(P ++ [ldap, pool_tag], my_tag,
 2796:           T(#{<<"backend">> => <<"ldap">>, <<"ldap">> => #{<<"pool_tag">> => <<"my_tag">>}})),
 2797:     ?cfgh(P ++ [ldap, base], <<"ou=Users,dc=ejd,dc=com">>,
 2798:           T(#{<<"backend">> => <<"ldap">>, <<"ldap">> => #{<<"base">> => <<"ou=Users,dc=ejd,dc=com">>}})),
 2799:     ?cfgh(P ++ [ldap, filter], <<"(&(objectClass=shadowAccount)(memberOf=Jabber Users))">>,
 2800:           T(#{<<"backend">> => <<"ldap">>, <<"ldap">> => #{<<"filter">> => <<"(&(objectClass=shadowAccount)(memberOf=Jabber Users))">>}})),
 2801:     ?cfgh(P ++ [ldap, deref], always,
 2802:           T(#{<<"backend">> => <<"ldap">>, <<"ldap">> => #{<<"deref">> => <<"always">>}})),
 2803:     ?cfgh(P ++ [ldap, search_operator], 'or',
 2804:           T(#{<<"backend">> => <<"ldap">>, <<"ldap">> => #{<<"search_operator">> => <<"or">>}})),
 2805:     ?cfgh(P ++ [ldap, binary_search_fields], [<<"PHOTO">>],
 2806:           T(#{<<"backend">> => <<"ldap">>, <<"ldap">> => #{<<"binary_search_fields">> => [<<"PHOTO">>]}})),
 2807:     ?errh(T(#{<<"host">> => 1})),
 2808:     ?errh(T(#{<<"host">> => <<" ">>})),
 2809:     ?errh(T(#{<<"host">> => <<"is this a host? no.">>})),
 2810:     ?errh(T(#{<<"host">> => [<<"valid.@HOST@">>]})),
 2811:     ?errh(T(#{<<"host">> => <<"invalid.sub@HOST@">>})),
 2812:     ?errh(T(#{<<"host">> => <<"invalid.sub.@HOST@.as.well">>})),
 2813:     ?errh(T(#{<<"search">> => 1})),
 2814:     ?errh(T(#{<<"backend">> => <<"mememesia">>})),
 2815:     ?errh(T(#{<<"matches">> => -1})),
 2816:     %% ldap
 2817:     ?errh(T(#{<<"ldap_pool_tag">> => -1})),
 2818:     ?errh(T(#{<<"ldap_base">> => -1})),
 2819:     ?errh(T(#{<<"ldap_field">> => -1})),
 2820:     ?errh(T(#{<<"ldap_deref">> => <<"nevernever">>})),
 2821:     ?errh(T(#{<<"ldap_search_operator">> => <<"more">>})),
 2822:     ?errh(T(#{<<"ldap_binary_search_fields">> => [1]})).
 2823: 
 2824: mod_vcard_ldap_uids(_Config) ->
 2825:     P = [modules, mod_vcard, ldap, uids],
 2826:     T = fun(Opts) -> #{<<"modules">> =>
 2827:                            #{<<"mod_vcard">> => #{<<"backend">> => <<"ldap">>,
 2828:                                                   <<"ldap">> => #{<<"uids">> => Opts}}}} end,
 2829:     RequiredOpts = #{<<"attr">> => <<"name">>},
 2830:     ExpectedCfg = <<"name">>,
 2831:     ?cfgh(P, [], T([])),
 2832:     ?cfgh(P, [ExpectedCfg], T([RequiredOpts])),
 2833:     ?cfgh(P, [{<<"name">>, <<"%u@mail.example.org">>}],
 2834:           T([RequiredOpts#{<<"format">> => <<"%u@mail.example.org">>}])),
 2835:     ?cfgh(P, [{<<"name">>, <<"%u@mail.example.org">>}, ExpectedCfg],
 2836:           T([RequiredOpts#{<<"format">> => <<"%u@mail.example.org">>}, RequiredOpts])),
 2837:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 2838:     ?errh(T(RequiredOpts#{<<"attr">> := 1})),
 2839:     ?errh(T(RequiredOpts#{<<"format">> => true})).
 2840: 
 2841: mod_vcard_ldap_vcard_map(_Config) ->
 2842:     P = [modules, mod_vcard, ldap, vcard_map],
 2843:     T = fun(Opts) -> #{<<"modules">> =>
 2844:                            #{<<"mod_vcard">> => #{<<"backend">> => <<"ldap">>,
 2845:                                                   <<"ldap">> => #{<<"vcard_map">> => Opts}}}} end,
 2846:     RequiredOpts = #{<<"vcard_field">> => <<"FAMILY">>,
 2847:                      <<"ldap_pattern">> => <<"%s">>,
 2848:                      <<"ldap_field">> => <<"sn">>},
 2849:     ExpectedCfg = {<<"FAMILY">>, <<"%s">>, [<<"sn">>]},
 2850:     ?cfgh(P, [], T([])),
 2851:     ?cfgh(P, [ExpectedCfg], T([RequiredOpts])),
 2852:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 2853:     ?errh(T(RequiredOpts#{<<"vcard_field">> := false})),
 2854:     ?errh(T(RequiredOpts#{<<"ldap_pattern">> := false})),
 2855:     ?errh(T(RequiredOpts#{<<"ldap_field">> := -1})).
 2856: 
 2857: mod_vcard_ldap_search_fields(_Config) ->
 2858:     P = [modules, mod_vcard, ldap, search_fields],
 2859:     T = fun(Opts) -> #{<<"modules">> =>
 2860:                            #{<<"mod_vcard">> => #{<<"backend">> => <<"ldap">>,
 2861:                                                   <<"ldap">> => #{<<"search_fields">> => Opts}}}} end,
 2862:     RequiredOpts = #{<<"search_field">> => <<"Full Name">>,
 2863:                      <<"ldap_field">> => <<"cn">>},
 2864:     ExpectedCfg = {<<"Full Name">>, <<"cn">>},
 2865:     ?cfgh(P, [], T([])),
 2866:     ?cfgh(P, [ExpectedCfg], T([RequiredOpts])),
 2867:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 2868:     ?errh(T(RequiredOpts#{<<"search_field">> := false})),
 2869:     ?errh(T(RequiredOpts#{<<"ldap_field">> := -1})).
 2870: 
 2871: mod_vcard_ldap_search_reported(_Config) ->
 2872:     P = [modules, mod_vcard, ldap, search_reported],
 2873:     T = fun(Opts) -> #{<<"modules">> =>
 2874:                            #{<<"mod_vcard">> => #{<<"backend">> => <<"ldap">>,
 2875:                                                   <<"ldap">> => #{<<"search_reported">> => Opts}}}} end,
 2876:     RequiredOpts = #{<<"search_field">> => <<"Full Name">>,
 2877:                      <<"vcard_field">> => <<"FN">>},
 2878:     ExpectedCfg = {<<"Full Name">>, <<"FN">>},
 2879:     ?cfgh(P, [], T([])),
 2880:     ?cfgh(P, [ExpectedCfg], T([RequiredOpts])),
 2881:     [?errh(T([maps:remove(Key, RequiredOpts)])) || Key <- maps:keys(RequiredOpts)],
 2882:     ?errh(T(RequiredOpts#{<<"search_field">> := false})),
 2883:     ?errh(T(RequiredOpts#{<<"vcard_field">> := -1})).
 2884: 
 2885: mod_version(_Config) ->
 2886:     check_module_defaults(mod_version),
 2887:     check_iqdisc(mod_version),
 2888:     P = [modules, mod_version],
 2889:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_version">> => Opts}} end,
 2890:     ?cfgh(P ++ [os_info], true, T(#{<<"os_info">> => true})),
 2891:     ?errh(T(#{<<"os_info">> => 1})).
 2892: 
 2893: modules_without_config(_Config) ->
 2894:     ?cfgh([modules, mod_amp], #{}, #{<<"modules">> => #{<<"mod_amp">> => #{}}}),
 2895:     ?errh(#{<<"modules">> => #{<<"mod_wrong">> => #{}}}).
 2896: 
 2897: incorrect_module(_Config) ->
 2898:     ?errh(#{<<"modules">> => #{<<"mod_incorrect">> => #{}}}).
 2899: 
 2900: %% Services
 2901: 
 2902: service_domain_db(_Config) ->
 2903:     P = [services, service_domain_db],
 2904:     T = fun(Opts) -> #{<<"services">> => #{<<"service_domain_db">> => Opts}} end,
 2905:     ?cfg(P, default_config(P), T(#{})),
 2906:     ?cfg(P ++ [event_cleaning_interval], 1000, T(#{<<"event_cleaning_interval">> => 1000})),
 2907:     ?cfg(P ++ [event_max_age], 5000, T(#{<<"event_max_age">> => 5000})),
 2908:     ?cfg(P ++ [db_pool], my_pool, T(#{<<"db_pool">> => <<"my_pool">>})),
 2909:     ?err(T(#{<<"event_cleaning_interval">> => 0})),
 2910:     ?err(T(#{<<"event_max_age">> => 0})),
 2911:     ?err(T(#{<<"db_pool">> => 10})).
 2912: 
 2913: service_mongoose_system_metrics(_Config) ->
 2914:     P = [services, service_mongoose_system_metrics],
 2915:     T = fun(Opts) -> #{<<"services">> => #{<<"service_mongoose_system_metrics">> => Opts}} end,
 2916:     ?cfg(P, default_config(P), T(#{})),
 2917:     ?cfg(P ++ [initial_report], 5000, T(#{<<"initial_report">> => 5000})),
 2918:     ?cfg(P ++ [periodic_report], 5000, T(#{<<"periodic_report">> => 5000})),
 2919:     ?cfg(P ++ [tracking_id], #{id => "G-12345678", secret => "Secret"},
 2920:          T(#{<<"tracking_id">> => #{<<"id">> => <<"G-12345678">>, <<"secret">> => <<"Secret">>}})),
 2921:     ?cfg(P ++ [report], true, T(#{<<"report">> => true})),
 2922:     ?err(T(#{<<"initial_report">> => <<"forever">>})),
 2923:     ?err(T(#{<<"periodic_report">> => <<"forever">>})),
 2924:     ?err(T(#{<<"initial_report">> => -1})),
 2925:     ?err(T(#{<<"periodic_report">> => -1})),
 2926:     ?err(T(#{<<"tracking_id">> => #{<<"id">> => "G-12345678"}})),
 2927:     ?err(T(#{<<"tracking_id">> => #{<<"secret">> => "Secret"}})),
 2928:     ?err(T(#{<<"tracking_id">> => #{<<"secret">> => 666, <<"id">> => 666}})),
 2929:     ?err(T(#{<<"report">> => <<"maybe">>})).
 2930: 
 2931: %% Instrumentation
 2932: 
 2933: instrumentation(_Config) ->
 2934:     P = [instrumentation],
 2935:     T = fun(Opts) -> #{<<"instrumentation">> => Opts} end,
 2936:     ?cfg(P, #{}, T(#{})),
 2937:     ?cfg(P, #{prometheus => #{}}, T(#{<<"prometheus">> => #{}})),
 2938:     ?cfg(P, #{exometer => #{}}, T(#{<<"exometer">> => #{}})),
 2939:     ?err(T(#{<<"prometheus">> => #{<<"fire">> => 1}})),
 2940:     ?err(T(#{<<"bad_module">> => #{}})).
 2941: 
 2942: instrumentation_log(_Config) ->
 2943:     P = [instrumentation, log],
 2944:     T = fun(Opts) -> #{<<"instrumentation">> => #{<<"log">> => Opts}} end,
 2945:     ?cfg(P, default_config(P), T(#{})),
 2946:     ?cfg(P ++ [level], info, T(#{<<"level">> => <<"info">>})),
 2947:     ?err(T(#{<<"level">> => <<"insane">>})).
 2948: 
 2949: %% Logs
 2950: 
 2951: no_warning_about_subdomain_patterns(_Config) ->
 2952:     check_module_defaults(mod_vcard),
 2953:     check_iqdisc(mod_vcard),
 2954:     P = [modules, mod_vcard],
 2955:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_vcard">> => Opts}} end,
 2956:     ?cfgh(P ++ [host], {prefix, <<"vjud.">>},
 2957:           T(#{<<"host">> => <<"vjud.@HOST@">>})),
 2958:     ?assertNoLog(warning, #{what := cfg_validate_domain}),
 2959: 
 2960:     ?cfgh(P ++ [host], {fqdn, <<"vjud.test">>},
 2961:           T(#{<<"host">> => <<"vjud.test">>})),
 2962:     ?assertLog(warning, #{what := cfg_validate_domain, reason := nxdomain, domain := "vjud.test"}).
 2963: 
 2964: no_warning_for_resolvable_domain(_Config) ->
 2965:     T = fun(Opts) -> #{<<"modules">> => #{<<"mod_http_upload">> => Opts}} end,
 2966:     P = [modules, mod_http_upload],
 2967:     RequiredOpts = #{<<"s3">> => http_upload_s3_required_opts()},
 2968:     ?cfgh(P ++ [host], {fqdn, <<"example.org">>},
 2969:           T(RequiredOpts#{<<"host">> => <<"example.org">>})),
 2970:     ?assertNoLog(_, #{what := cfg_validate_domain}),
 2971: 
 2972:     ?cfgh(P ++ [host], {fqdn, <<"something.invalid">>},
 2973:           T(RequiredOpts#{<<"host">> => <<"something.invalid">>})),
 2974:     ?assertLog(warning, #{what := cfg_validate_domain, reason := nxdomain,
 2975:                           domain := "something.invalid"}).
 2976: 
 2977: %% Helpers for module tests
 2978: 
 2979: check_iqdisc(Module) ->
 2980:     P = [modules, Module],
 2981:     T = fun(Opts) -> #{<<"modules">> => #{atom_to_binary(Module) => Opts}} end,
 2982:     check_iqdisc(P, T).
 2983: 
 2984: check_iqdisc(Module, RequiredOpts) when is_map(RequiredOpts) ->
 2985:     P = [modules, Module],
 2986:     T = fun(Opts) ->
 2987:                 #{<<"modules">> => #{atom_to_binary(Module) => maps:merge(RequiredOpts, Opts)}}
 2988:         end,
 2989:     check_iqdisc(P, T);
 2990: check_iqdisc(ParentP, ParentT) when is_function(ParentT, 1) ->
 2991:     P = ParentP ++ [iqdisc],
 2992:     T = fun(Opts) -> ParentT(#{<<"iqdisc">> => Opts}) end,
 2993:     ?cfgh(P, {queues, 10}, T(#{<<"type">> => <<"queues">>, <<"workers">> => 10})),
 2994:     ?cfgh(P, parallel, T(#{<<"type">> => <<"parallel">>})),
 2995:     ?cfgh(P, one_queue, T(#{<<"type">> => <<"one_queue">>})),
 2996:     ?cfgh(P, no_queue, T(#{<<"type">> => <<"no_queue">>})),
 2997:     ?errh(T(#{<<"type">> => <<"one_queue_and_a_half">>})),
 2998:     ?errh(T(#{<<"type">> => <<"queues">>, <<"workers">> => 0})),
 2999:     ?errh(T(#{<<"type">> => <<"no_queue">>, <<"workers">> => 10})),
 3000:     ?errh(T(#{<<"workers">> => 10})).
 3001: 
 3002: check_module_defaults(Mod) ->
 3003:     ExpectedCfg = default_mod_config(Mod),
 3004:     ?cfgh([modules, Mod], ExpectedCfg, #{<<"modules">> => #{atom_to_binary(Mod) => #{}}}).
 3005: 
 3006: %% helpers for 'listen' tests
 3007: 
 3008: listener(Type, Opts) ->
 3009:     config([listen, Type], Opts).
 3010: 
 3011: graphql_handler_raw(Opts) ->
 3012:     http_handler_raw(mongoose_graphql_handler,
 3013:                      maps:merge(#{<<"schema_endpoint">> => <<"admin">>}, Opts)).
 3014: 
 3015: http_handler_raw(Type, Opts) ->
 3016:     MergedOpts = maps:merge(#{<<"host">> => <<"localhost">>, <<"path">> => <<"/api">>}, Opts),
 3017:     listen_raw(http, #{<<"port">> => 5280,
 3018:                        <<"handlers">> => #{atom_to_binary(Type) => [remove_undefined(MergedOpts)]}}
 3019:               ).
 3020: 
 3021: listen_raw(Type, Opts) ->
 3022:     #{<<"listen">> => #{atom_to_binary(Type) => [remove_undefined(Opts)]}}.
 3023: 
 3024: remove_undefined(M) ->
 3025:     maps:filter(fun(_, V) -> V =/= undefined end, M).
 3026: 
 3027: %% helpers for 'auth' tests
 3028: 
 3029: auth_ldap_raw(Opts) ->
 3030:     auth_raw(<<"ldap">>, Opts).
 3031: 
 3032: auth_raw(Method, Opts) ->
 3033:     #{<<"auth">> => #{Method => Opts}}.
 3034: 
 3035: %% helpers for 'pool' tests
 3036: 
 3037: pool_raw(Type, Tag, Opts) ->
 3038:     #{<<"outgoing_pools">> => #{Type => #{Tag => Opts}}}.
 3039: 
 3040: pool_conn_raw(Type, Opts) ->
 3041:     #{<<"outgoing_pools">> => #{Type => #{<<"default">> => #{<<"connection">> => Opts}}}}.
 3042: 
 3043: %% helpers for 'access' tests
 3044: 
 3045: access_raw(RuleName, RuleSpec) ->
 3046:     #{<<"access">> => #{RuleName => RuleSpec}}.
 3047: 
 3048: %% helpers for 'host_config' tests
 3049: 
 3050: host_config(Config) ->
 3051:     #{<<"host_config">> => [Config#{<<"host_type">> => ?HOST}]}.
 3052: 
 3053: %% helpers for parsing
 3054: 
 3055: -spec parse(map()) -> [mongoose_config_parser_toml:config()].
 3056: parse(M0) ->
 3057:     %% As 'hosts' (or 'host_types') and 'default_server_domain' options are mandatory,
 3058:     %% this function inserts them with dummy values if they are missing.
 3059:     %% To prevent the insertion, add a 'without' option to the map, e.g. without => [<<"hosts">>]
 3060:     %% The resulting map is then passed to the TOML config parser.
 3061:     M = maybe_insert_dummy_domain(M0),
 3062:     mongoose_config_parser:get_opts(mongoose_config_parser_toml:process(M)).
 3063: 
 3064: maybe_insert_dummy_domain(M) ->
 3065:     DummyGenM = #{<<"default_server_domain">> => ?HOST,
 3066:                   <<"hosts">> => [?HOST]},
 3067:     {FilteredGenM, RawConfig} = case maps:take(without, M) of
 3068:                                     {Keys, Cfg} -> {maps:without(Keys, DummyGenM), Cfg};
 3069:                                     error -> {DummyGenM, M}
 3070:                                 end,
 3071:     OldGenM = maps:get(<<"general">>, RawConfig, #{}),
 3072:     NewGenM = maps:merge(FilteredGenM, OldGenM),
 3073:     RawConfig#{<<"general">> => NewGenM}.
 3074: 
 3075: %% helpers for testing individual options
 3076: 
 3077: -spec host_opts([{key_prefix(), mongoose_config:value()}]) ->
 3078:           [{mongoose_config:key() | mongoose_config:key_path(), mongoose_config:value()}].
 3079: host_opts(ExpectedOptions) ->
 3080:     lists:map(fun({Key, Value}) -> {host_key(Key), Value} end, ExpectedOptions).
 3081: 
 3082: %% @doc Build full per-host config key for host-or-global options
 3083: -spec host_key(top_level_key_prefix()) -> mongoose_config:key();
 3084:               (key_path_prefix()) -> mongoose_config:key_path().
 3085: host_key([TopKey | Rest]) when is_atom(TopKey) ->
 3086:     [{TopKey, ?HOST} | Rest];
 3087: host_key(Key) when is_atom(Key) ->
 3088:     {Key, ?HOST}.
 3089: 
 3090: -spec assert_options([{mongoose_config:key() | mongoose_config:key_path(), mongoose_config:value()}],
 3091:                      [mongoose_config_parser_toml:config()]) -> any().
 3092: assert_options(ExpectedOptions, Config) ->
 3093:     lists:foreach(fun({Key, Value}) -> assert_option(Key, Value, Config) end, ExpectedOptions).
 3094: 
 3095: -spec assert_option(mongoose_config:key() | mongoose_config:key_path(), mongoose_config:value(),
 3096:                     [mongoose_config_parser_toml:config()]) -> any().
 3097: assert_option(KeyPath, Value, Config) when is_list(KeyPath) ->
 3098:     compare_nodes(KeyPath, Value, get_config_value(KeyPath, Config));
 3099: assert_option(Key, Value, Config) ->
 3100:     assert_option([Key], Value, Config).
 3101: 
 3102: -spec get_config_value(mongoose_config:key_path(), [mongoose_config_parser_toml:config()]) ->
 3103:           mongoose_config:value().
 3104: get_config_value([TopKey | Rest], Config) ->
 3105:     case lists:keyfind(TopKey, 1, Config) of
 3106:         false -> ct:fail({"option not found", TopKey, Config});
 3107:         {_, TopValue} -> lists:foldl(fun get_value/2, TopValue, Rest)
 3108:     end.
 3109: 
 3110: get_value(Index, List) when is_integer(Index), Index > 0, is_list(List) ->
 3111:     lists:nth(Index, List);
 3112: get_value(Key, Map) when not is_integer(Key), is_map(Map) ->
 3113:     maps:get(Key, Map).
 3114: 
 3115: %% helpers for file tests
 3116: 
 3117: test_config_file(Config, File) ->
 3118:     OptionsPath = ejabberd_helper:data(Config, File ++ ".options"),
 3119:     ExpectedOpts = config_parser_helper:options(File),
 3120: 
 3121:     TOMLPath = ejabberd_helper:data(Config, File ++ ".toml"),
 3122:     TOMLOpts = mongoose_config_parser:parse_file(TOMLPath),
 3123: 
 3124:     %% Save the parsed TOML options
 3125:     %% - for debugging
 3126:     %% - to update tests after a config change - always check the diff!
 3127:     save_opts(OptionsPath ++ ".parsed", TOMLOpts),
 3128:     compare_config(ExpectedOpts, TOMLOpts).
 3129: 
 3130: save_opts(Path, Opts) ->
 3131:     FormattedOpts = [io_lib:format("~p.~n", [Opt]) || Opt <- lists:sort(Opts)],
 3132:     file:write_file(Path, FormattedOpts).
 3133: 
 3134: compare_config(C1, C2) ->
 3135:     compare_unordered_lists(C1, C2, fun handle_config_option/2).
 3136: 
 3137: handle_config_option({K1, V1}, {K2, V2}) ->
 3138:     ?eq(K1, K2),
 3139:     compare_nodes([K1], V1, V2);
 3140: handle_config_option(Opt1, Opt2) ->
 3141:     ?eq(Opt1, Opt2).
 3142: 
 3143: %% Comparisons for config options that have paths (top-level or nested in maps)
 3144: 
 3145: -spec compare_nodes(mongoose_config:key_path(), mongoose_config:value(), mongoose_config:value()) ->
 3146:           any().
 3147: compare_nodes([listen] = P, V1, V2) ->
 3148:     compare_ordered_lists_of_nodes(P, V1, V2);
 3149: compare_nodes([listen, I, handlers] = P, V1, V2) when is_integer(I) ->
 3150:     compare_ordered_lists_of_nodes(P, V1, V2);
 3151: compare_nodes([outgoing_pools] = P, V1, V2) ->
 3152:     compare_ordered_lists_of_nodes(P, V1, V2);
 3153: compare_nodes(Node, V1, V2) when is_map(V1), is_map(V2) ->
 3154:     compare_maps(V1, V2, fun({K1, MV1}, {K2, MV2}) ->
 3155:                                  ?eq(K1, K2),
 3156:                                  compare_nodes(Node ++ [K1], MV1, MV2)
 3157:                          end);
 3158: compare_nodes(Node, V1, V2) ->
 3159:     ?eq({Node, V1}, {Node, V2}).
 3160: 
 3161: compare_ordered_lists_of_nodes(Path, L1, L2) when length(L1) =:= length(L2) ->
 3162:     lists:foreach(fun({I, V1, V2}) -> compare_nodes(Path ++ [I], V1, V2) end,
 3163:                   lists:zip3(lists:seq(1, length(L1)), L1, L2)).
 3164: 
 3165: %% Generic assertions, use the 'F' handler for any custom cases
 3166: compare_unordered_lists(L1, L2) when is_list(L1), is_list(L2) ->
 3167:     compare_unordered_lists(L1, L2, fun(V1, V2) -> ?eq(V1, V2) end).
 3168: 
 3169: compare_unordered_lists(L1, L2, F) when is_list(L1), is_list(L2) ->
 3170:     SL1 = lists:sort(L1),
 3171:     SL2 = lists:sort(L2),
 3172:     compare_ordered_lists(SL1, SL2, F).
 3173: 
 3174: compare_ordered_lists([H1|T1], [H1|T2], F) ->
 3175:     compare_ordered_lists(T1, T2, F);
 3176: compare_ordered_lists([H1|T1] = L1, [H2|T2] = L2, F) ->
 3177:     try F(H1, H2)
 3178:     catch error:R:S ->
 3179:             ct:fail({"Failed to compare ordered lists", L1, L2, R, S})
 3180:     end,
 3181:     compare_ordered_lists(T1, T2, F);
 3182: compare_ordered_lists([], [], _) ->
 3183:     ok.
 3184: 
 3185: compare_maps(M1, M2) ->
 3186:     compare_maps(M1, M2, fun(V1, V2) -> ?eq(V1, V2) end).
 3187: 
 3188: compare_maps(M1, M2, F) ->
 3189:     compare_unordered_lists(maps:to_list(M1), maps:to_list(M2), F).
 3190: 
 3191: create_files(Config) ->
 3192:     %% The files must exist for validation to pass
 3193:     Root = small_path_helper:repo_dir(Config),
 3194:     file:make_dir("priv"),
 3195:     [ensure_copied(filename:join(Root, From), To) || {From, To} <- files_to_copy()],
 3196:     ok = file:write_file("priv/access_psk", ""),
 3197:     ok = file:write_file("priv/provision_psk", ""),
 3198:     ok = file:write_file("priv/jwt_secret", "secret123"),
 3199:     ok = filelib:ensure_dir("www/muc/dummy").
 3200: 
 3201: ensure_copied(From, To) ->
 3202:     case file:copy(From, To) of
 3203:         {ok, _} ->
 3204:             ok;
 3205:         Other ->
 3206:             error(#{what => ensure_copied_failed, from => From, to => To,
 3207:                     reason => Other})
 3208:     end.
 3209: 
 3210: files_to_copy() ->
 3211:     [{"tools/ssl/mongooseim/privkey.pem", "priv/dc1.pem"},
 3212:      {"tools/ssl/mongooseim/cert.pem", "priv/cert.pem"},
 3213:      {"tools/ssl/mongooseim/dh_server.pem", "priv/dh.pem"},
 3214:      {"tools/ssl/mongooseim/server.pem", "priv/server.pem"},
 3215:      {"tools/ssl/ca/cacert.pem", "priv/ca.pem"}].