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