Skip to content

Bug: forceOccurrenceOfEachType does not behave as expected #8

Open
@jshergal

Description

@jshergal

When setting forceOccurrenceOfEachType to true the expectation would be that the resultant random string will contain at least 1 character from each of the specified character classes. Due to the current implementation, this behavior is not guaranteed. It is "likely" given a small set of character classes and a long enough string, but it does not guarantee it.

On a related note, the current unit tests around this functionality do not expose this flaw as they only use a single regular expression to verify that the strings are of the expected length and contain only the characters from the specified classes, but they do not check to ensure that there is at least 1 character from each class within the resultant string. The unit tests should be updated to additionally check for a single occurrence of each character class within the string. For example, to ensure there is at least 1 lowercase letter:
Regex.IsMatch(randomString, @"[a-z]");

As an example of the bug, I made a small update to the test ValidateForceOccurrenceForAlphaNumericLowercaseWithCustomSymbols() to check for all character classes. Two of the resulting strings failed the test:
"ik^omn@$xyk$&&hc@%$a"
"*@lz#@g@a^#wkh^p^%h!"

The expectation with forceOccurrenceOfEachType would be that those strings would also contain a digit, but they did not. Out of 1000 strings these were the only two that did not, but it shows that while it is highly likely to receive a string that follows the setting, it is not guaranteed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions