|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(server_named_queue_prefix_prop_SUITE). |
| 9 | + |
| 10 | +-compile(export_all). |
| 11 | + |
| 12 | +-include_lib("common_test/include/ct.hrl"). |
| 13 | +-include_lib("proper/include/proper.hrl"). |
| 14 | + |
| 15 | +-define(NUM_TESTS, 1000). |
| 16 | +-define(MAX_PREFIX_LEN, 64). |
| 17 | + |
| 18 | +all() -> |
| 19 | + [ |
| 20 | + prop_valid_prefix_returned_verbatim, |
| 21 | + prop_too_long_prefix_yields_error, |
| 22 | + prop_no_valid_prefix_yields_default, |
| 23 | + prop_generated_name_starts_with_prefix, |
| 24 | + prop_generated_name_within_amqp_limit, |
| 25 | + prop_different_guids_yield_different_names, |
| 26 | + prop_different_prefixes_yield_different_names |
| 27 | + ]. |
| 28 | + |
| 29 | +init_per_suite(Config) -> |
| 30 | + rabbit_ct_helpers:log_environment(), |
| 31 | + rabbit_ct_helpers:run_setup_steps(Config). |
| 32 | + |
| 33 | +end_per_suite(Config) -> |
| 34 | + rabbit_ct_helpers:run_teardown_steps(Config). |
| 35 | + |
| 36 | +init_per_testcase(Testcase, Config) -> |
| 37 | + rabbit_ct_helpers:testcase_started(Config, Testcase). |
| 38 | + |
| 39 | +%% A non-empty prefix of at most 64 bytes is returned as {ok, Prefix}. |
| 40 | +prop_valid_prefix_returned_verbatim(Config) -> |
| 41 | + rabbit_ct_proper_helpers:run_proper( |
| 42 | + fun() -> prop_valid_prefix(Config) end, [], ?NUM_TESTS). |
| 43 | + |
| 44 | +prop_valid_prefix(_Config) -> |
| 45 | + ?FORALL(Prefix, valid_prefix_gen(), |
| 46 | + begin |
| 47 | + Args = [{<<"x-name-prefix">>, longstr, Prefix}], |
| 48 | + rabbit_amqqueue:server_named_queue_prefix(Args) =:= {ok, Prefix} |
| 49 | + end). |
| 50 | + |
| 51 | +%% A prefix longer than 64 bytes yields {error, {prefix_too_long, Prefix}}. |
| 52 | +prop_too_long_prefix_yields_error(Config) -> |
| 53 | + rabbit_ct_proper_helpers:run_proper( |
| 54 | + fun() -> prop_too_long_prefix(Config) end, [], ?NUM_TESTS). |
| 55 | + |
| 56 | +prop_too_long_prefix(_Config) -> |
| 57 | + ?FORALL(Prefix, too_long_prefix_gen(), |
| 58 | + begin |
| 59 | + Args = [{<<"x-name-prefix">>, longstr, Prefix}], |
| 60 | + rabbit_amqqueue:server_named_queue_prefix(Args) =:= |
| 61 | + {error, {prefix_too_long, Prefix}} |
| 62 | + end). |
| 63 | + |
| 64 | +%% Absent, empty, or wrong-type `x-name-prefix` yields {ok, <<"amq.gen">>}. |
| 65 | +prop_no_valid_prefix_yields_default(Config) -> |
| 66 | + rabbit_ct_proper_helpers:run_proper( |
| 67 | + fun() -> prop_default_fallback(Config) end, [], ?NUM_TESTS). |
| 68 | + |
| 69 | +prop_default_fallback(_Config) -> |
| 70 | + ?FORALL(Args, invalid_prefix_args_gen(), |
| 71 | + rabbit_amqqueue:server_named_queue_prefix(Args) =:= {ok, <<"amq.gen">>}). |
| 72 | + |
| 73 | +%% rabbit_guid:binary/2 produces "prefix-<base64url>". |
| 74 | +prop_generated_name_starts_with_prefix(Config) -> |
| 75 | + rabbit_ct_proper_helpers:run_proper( |
| 76 | + fun() -> prop_name_starts_with_prefix(Config) end, [], ?NUM_TESTS). |
| 77 | + |
| 78 | +prop_name_starts_with_prefix(_Config) -> |
| 79 | + ?FORALL({Prefix, Guid}, {valid_prefix_gen(), binary(16)}, |
| 80 | + begin |
| 81 | + Generated = rabbit_guid:binary(Guid, Prefix), |
| 82 | + ExpectedHead = <<Prefix/binary, "-">>, |
| 83 | + HeadLen = byte_size(ExpectedHead), |
| 84 | + byte_size(Generated) >= HeadLen andalso |
| 85 | + binary:part(Generated, 0, HeadLen) =:= ExpectedHead |
| 86 | + end). |
| 87 | + |
| 88 | +%% The generated name fits within the 255-byte AMQP shortstr limit. |
| 89 | +prop_generated_name_within_amqp_limit(Config) -> |
| 90 | + rabbit_ct_proper_helpers:run_proper( |
| 91 | + fun() -> prop_name_within_limit(Config) end, [], ?NUM_TESTS). |
| 92 | + |
| 93 | +prop_name_within_limit(_Config) -> |
| 94 | + ?FORALL({Prefix, Guid}, {valid_prefix_gen(), binary(16)}, |
| 95 | + byte_size(rabbit_guid:binary(Guid, Prefix)) =< 255). |
| 96 | + |
| 97 | +%% Two distinct GUIDs with the same prefix produce different names. |
| 98 | +prop_different_guids_yield_different_names(Config) -> |
| 99 | + rabbit_ct_proper_helpers:run_proper( |
| 100 | + fun() -> prop_unique_names(Config) end, [], ?NUM_TESTS). |
| 101 | + |
| 102 | +prop_unique_names(_Config) -> |
| 103 | + ?FORALL({Prefix, G1, G2}, {valid_prefix_gen(), binary(16), binary(16)}, |
| 104 | + G1 =:= G2 orelse |
| 105 | + rabbit_guid:binary(G1, Prefix) =/= rabbit_guid:binary(G2, Prefix)). |
| 106 | + |
| 107 | +%% Two distinct prefixes with the same GUID produce different names. |
| 108 | +prop_different_prefixes_yield_different_names(Config) -> |
| 109 | + rabbit_ct_proper_helpers:run_proper( |
| 110 | + fun() -> prop_unique_prefixes(Config) end, [], ?NUM_TESTS). |
| 111 | + |
| 112 | +prop_unique_prefixes(_Config) -> |
| 113 | + ?FORALL({P1, P2, Guid}, {valid_prefix_gen(), valid_prefix_gen(), binary(16)}, |
| 114 | + P1 =:= P2 orelse |
| 115 | + rabbit_guid:binary(Guid, P1) =/= rabbit_guid:binary(Guid, P2)). |
| 116 | + |
| 117 | +%% ------------------------------------------------------------------- |
| 118 | +%% Generators |
| 119 | +%% ------------------------------------------------------------------- |
| 120 | + |
| 121 | +valid_prefix_gen() -> |
| 122 | + ?SUCHTHAT(P, non_empty(binary()), byte_size(P) =< ?MAX_PREFIX_LEN). |
| 123 | + |
| 124 | +too_long_prefix_gen() -> |
| 125 | + ?LET(Extra, binary(), |
| 126 | + <<(binary:copy(<<"x">>, ?MAX_PREFIX_LEN + 1))/binary, Extra/binary>>). |
| 127 | + |
| 128 | +invalid_prefix_args_gen() -> |
| 129 | + oneof([ |
| 130 | + %% no `x-name-prefix` at all |
| 131 | + list(unrelated_arg_gen()), |
| 132 | + %% empty binary |
| 133 | + ?LET(Extra, list(unrelated_arg_gen()), |
| 134 | + [{<<"x-name-prefix">>, longstr, <<>>} | Extra]), |
| 135 | + %% non-longstr type |
| 136 | + ?LET({Extra, Type}, |
| 137 | + {list(unrelated_arg_gen()), |
| 138 | + oneof([signedint, bool, double, timestamp, byte, short, long, |
| 139 | + float, binary, void])}, |
| 140 | + [{<<"x-name-prefix">>, Type, 0} | Extra]) |
| 141 | + ]). |
| 142 | + |
| 143 | +unrelated_arg_gen() -> |
| 144 | + ?LET(Key, oneof([<<"x-queue-type">>, <<"x-max-length">>, <<"x-expires">>]), |
| 145 | + {Key, longstr, <<"classic">>}). |
0 commit comments