1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/zxcvbn-cpp/native-src/zxcvbn/matching.hpp"
#include <algorithm>
#include <string>
#include <string_view>
#include <vector>
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zxcvbn-cpp/native-src/zxcvbn/adjacency_graphs.hpp"
#include "third_party/zxcvbn-cpp/native-src/zxcvbn/common.hpp"
#include "third_party/zxcvbn-cpp/native-src/zxcvbn/frequency_lists.hpp"
using ::testing::ElementsAre;
using ::testing::IsEmpty;
namespace zxcvbn {
namespace {
struct Variation {
std::string password;
idx_t i;
idx_t j;
};
// takes a pattern and list of prefixes/suffixes
// returns a bunch of variants of that pattern embedded
// with each possible prefix/suffix combination, including no prefix/suffix
// returns a list of triplets [variant, i, j] where [i,j] is the start/end of
// the pattern, inclusive
std::vector<Variation> gen_pws(const std::string& pattern,
std::vector<std::string> prefixes,
std::vector<std::string> suffixes) {
if (std::find(prefixes.begin(), prefixes.end(), "") == prefixes.end())
prefixes.insert(prefixes.begin(), "");
if (std::find(suffixes.begin(), suffixes.end(), "") == suffixes.end())
suffixes.insert(suffixes.begin(), "");
std::vector<Variation> result;
for (const auto& prefix : prefixes) {
for (const auto& suffix : suffixes) {
result.push_back({prefix + pattern + suffix, prefix.size(),
prefix.size() + pattern.size() - 1});
}
}
return result;
}
struct ExpectedDictionaryMatch {
idx_t i;
idx_t j;
std::string token;
std::string matched_word;
rank_t rank;
bool l33t;
bool reversed;
std::unordered_map<std::string, std::string> sub;
};
bool operator==(const Match& lhs, const ExpectedDictionaryMatch& rhs) {
return lhs.i == rhs.i && lhs.j == rhs.j && lhs.token == rhs.token &&
lhs.get_pattern() == MatchPattern::DICTIONARY &&
lhs.get_dictionary().matched_word == rhs.matched_word &&
lhs.get_dictionary().rank == rhs.rank &&
lhs.get_dictionary().l33t == rhs.l33t &&
lhs.get_dictionary().reversed == rhs.reversed &&
lhs.get_dictionary().sub == rhs.sub;
}
struct ExpectedSpatialMatch {
idx_t i;
idx_t j;
std::string token;
GraphTag graph;
unsigned turns;
idx_t shifted_count;
};
bool operator==(const Match& lhs, const ExpectedSpatialMatch& rhs) {
return lhs.i == rhs.i && lhs.j == rhs.j && lhs.token == rhs.token &&
lhs.get_pattern() == MatchPattern::SPATIAL &&
lhs.get_spatial().graph == rhs.graph &&
lhs.get_spatial().turns == rhs.turns &&
lhs.get_spatial().shifted_count == rhs.shifted_count;
}
struct ExpectedSequenceMatch {
idx_t i;
idx_t j;
std::string token;
SequenceTag sequence_tag;
bool ascending;
};
bool operator==(const Match& lhs, const ExpectedSequenceMatch& rhs) {
return lhs.i == rhs.i && lhs.j == rhs.j && lhs.token == rhs.token &&
lhs.get_pattern() == MatchPattern::SEQUENCE &&
lhs.get_sequence().sequence_tag == rhs.sequence_tag &&
lhs.get_sequence().ascending == rhs.ascending;
}
struct ExpectedRepeatMatch {
idx_t i;
idx_t j;
std::string token;
std::string base_token;
};
bool operator==(const Match& lhs, const ExpectedRepeatMatch& rhs) {
return lhs.i == rhs.i && lhs.j == rhs.j && lhs.token == rhs.token &&
lhs.get_pattern() == MatchPattern::REPEAT &&
lhs.get_repeat().base_token == rhs.base_token;
}
struct ExpectedRegexMatch {
idx_t i;
idx_t j;
std::string token;
RegexTag regex_tag;
};
bool operator==(const Match& lhs, const ExpectedRegexMatch& rhs) {
return lhs.i == rhs.i && lhs.j == rhs.j && lhs.token == rhs.token &&
lhs.get_pattern() == MatchPattern::REGEX &&
lhs.get_regex().regex_tag == rhs.regex_tag;
}
struct ExpectedDateMatch {
idx_t i;
idx_t j;
std::string token;
std::string separator;
unsigned year;
unsigned month;
unsigned day;
};
bool operator==(const Match& lhs, const ExpectedDateMatch& rhs) {
return lhs.i == rhs.i && lhs.j == rhs.j && lhs.token == rhs.token &&
lhs.get_pattern() == MatchPattern::DATE &&
lhs.get_date().separator == rhs.separator &&
lhs.get_date().year == rhs.year && lhs.get_date().month == rhs.month &&
lhs.get_date().day == rhs.day;
}
} // namespace
TEST(ZxcvbnTest, DictionaryMatching) {
std::vector<std::vector<std::string_view>> test_dicts = {
{"motherboard", "mother", "board", "abcd", "cdef"},
{"z", "8", "99", "$", "asdf1234&*"},
};
{
// matches words that contain other words
std::string password = "motherboard";
RankedDicts test_dicts_processed(test_dicts);
std::vector<Match> matches =
dictionary_match(password, test_dicts_processed);
EXPECT_THAT(matches, ElementsAre(
ExpectedDictionaryMatch{
.i = 0,
.j = 5,
.token = "mother",
.matched_word = "mother",
.rank = 2,
},
ExpectedDictionaryMatch{
.i = 0,
.j = 10,
.token = "motherboard",
.matched_word = "motherboard",
.rank = 1,
},
ExpectedDictionaryMatch{
.i = 6,
.j = 10,
.token = "board",
.matched_word = "board",
.rank = 3,
}));
}
{
// matches multiple words when they overlap
std::string password = "abcdef";
std::vector<Match> matches =
dictionary_match(password, RankedDicts(test_dicts));
EXPECT_THAT(matches, ElementsAre(
ExpectedDictionaryMatch{
.i = 0,
.j = 3,
.token = "abcd",
.matched_word = "abcd",
.rank = 4,
},
ExpectedDictionaryMatch{
.i = 2,
.j = 5,
.token = "cdef",
.matched_word = "cdef",
.rank = 5,
}));
}
{
// ignores uppercasing
std::string password = "BoaRdZ";
std::vector<Match> matches =
dictionary_match(password, RankedDicts(test_dicts));
EXPECT_THAT(matches, ElementsAre(
ExpectedDictionaryMatch{
.i = 0,
.j = 4,
.token = "BoaRd",
.matched_word = "board",
.rank = 3,
},
ExpectedDictionaryMatch{
.i = 5,
.j = 5,
.token = "Z",
.matched_word = "z",
.rank = 1,
}));
}
{
// identifies words surrounded by non-words
std::string word = "asdf1234&*";
for (const auto& variation : gen_pws(word, {"q", "%%"}, {"%", "qq"})) {
std::vector<Match> matches =
dictionary_match(variation.password, RankedDicts(test_dicts));
EXPECT_THAT(matches, ElementsAre(ExpectedDictionaryMatch{
.i = variation.i,
.j = variation.j,
.token = word,
.matched_word = word,
.rank = 5,
}));
}
}
{
// matches against all words in provided dictionaries
for (const auto& test_dict : test_dicts) {
rank_t expected_rank = 0;
for (std::string_view ranked_word : test_dict) {
expected_rank++;
// skip words that contain others
if (ranked_word == "motherboard")
continue;
std::vector<Match> matches =
dictionary_match(std::string(ranked_word), RankedDicts(test_dicts));
EXPECT_THAT(matches, ElementsAre(ExpectedDictionaryMatch{
.i = 0,
.j = ranked_word.size() - 1,
.token = std::string(ranked_word),
.matched_word = std::string(ranked_word),
.rank = expected_rank,
}));
}
}
}
{
// default dictionaries
SetRankedDicts(RankedDicts({{"wow"}}));
std::vector<Match> matches =
dictionary_match("wow", default_ranked_dicts());
EXPECT_THAT(matches, ElementsAre(ExpectedDictionaryMatch{
.i = 0,
.j = 2,
.token = "wow",
.matched_word = "wow",
.rank = 1,
}));
}
}
TEST(ZxcvbnTest, ReverseDictionaryMatching) {
std::vector<std::vector<std::string_view>> test_dicts = {
{"123", "321", "456", "654"},
};
// matches against reversed words
std::string password = "0123456789";
std::vector<Match> matches =
reverse_dictionary_match(password, RankedDicts(test_dicts));
EXPECT_THAT(matches, ElementsAre(
ExpectedDictionaryMatch{
.i = 1,
.j = 3,
.token = "123",
.matched_word = "321",
.rank = 2,
.reversed = true,
},
ExpectedDictionaryMatch{
.i = 4,
.j = 6,
.token = "456",
.matched_word = "654",
.rank = 4,
.reversed = true,
}));
}
TEST(ZxcvbnTest, L33tMatching) {
std::vector<std::pair<std::string, std::vector<std::string>>> test_table = {
{"a", {"4", "@"}},
{"c", {"(", "{", "[", "<"}},
{"g", {"6", "9"}},
{"o", {"0"}},
};
{
// reduces l33t table to only the substitutions that a password might be
// employing
struct {
std::string pw;
std::unordered_map<std::string, std::vector<std::string>> expected;
} tests[] = {
{"", {}},
{"abcdefgo123578!#$&*)]}>", {}},
{"a", {}},
{"4", {{"a", {"4"}}}},
{"4@", {{"a", {"4", "@"}}}},
{"4({60",
{{"a", {"4"}}, {"c", {"(", "{"}}, {"g", {"6"}}, {"o", {"0"}}}},
};
for (const auto& test : tests) {
EXPECT_EQ(relevant_l33t_subtable(test.pw, test_table), test.expected);
}
}
{
// enumerates the different sets of l33t substitutions a password might be
// using
struct {
std::unordered_map<std::string, std::vector<std::string>> table;
std::vector<std::unordered_map<std::string, std::string>> subs;
} tests[] = {
{{}, {{}}},
{{{"a", {"@"}}}, {{{"@", "a"}}}},
{{{"a", {"@", "4"}}}, {{{"@", "a"}}, {{"4", "a"}}}},
{{{"a", {"@", "4"}}, {"c", {"("}}},
{{{"@", "a"}, {"(", "c"}}, {{"4", "a"}, {"(", "c"}}}},
};
for (const auto& test : tests) {
EXPECT_EQ(enumerate_l33t_subs(test.table), test.subs);
}
}
{
std::vector<std::vector<std::string_view>> dicts = {
{"aac", "password", "paassword", "asdf0"},
{"cgo"},
};
auto lm = [&](const std::string& password) {
return l33t_match(password, RankedDicts(dicts), test_table);
};
// doesn't match ""
EXPECT_THAT(lm(""), IsEmpty());
// doesn't match pure dictionary words
EXPECT_THAT(lm("password"), IsEmpty());
// matches against common l33t substitutions
struct {
std::string password;
std::string pattern;
std::string word;
rank_t rank;
idx_t i;
idx_t j;
std::unordered_map<std::string, std::string> sub;
} tests[] = {
{"p4ssword", "p4ssword", "password", 2, 0, 7, {{"4", "a"}}},
{"p@ssw0rd", "p@ssw0rd", "password", 2, 0, 7, {{"@", "a"}, {"0", "o"}}},
{"aSdfO{G0asDfO", "{G0", "cgo", 1, 5, 7, {{"{", "c"}, {"0", "o"}}},
};
for (const auto& test : tests) {
EXPECT_THAT(lm(test.password), ElementsAre(ExpectedDictionaryMatch{
.i = test.i,
.j = test.j,
.token = test.pattern,
.matched_word = test.word,
.rank = test.rank,
.l33t = true,
.sub = test.sub,
}));
}
// matches against overlapping l33t patterns
EXPECT_THAT(lm("@a(go{G0"), ElementsAre(
ExpectedDictionaryMatch{
.i = 0,
.j = 2,
.token = "@a(",
.matched_word = "aac",
.rank = 1,
.l33t = true,
.sub = {{"@", "a"}, {"(", "c"}},
},
ExpectedDictionaryMatch{
.i = 2,
.j = 4,
.token = "(go",
.matched_word = "cgo",
.rank = 1,
.l33t = true,
.sub = {{"(", "c"}},
},
ExpectedDictionaryMatch{
.i = 5,
.j = 7,
.token = "{G0",
.matched_word = "cgo",
.rank = 1,
.l33t = true,
.sub = {{"{", "c"}, {"0", "o"}},
}));
// doesn't match when multiple l33t substitutions are needed for the same
// letter
EXPECT_THAT(lm("p4@ssword"), IsEmpty());
// doesn't match single-character l33ted words
EXPECT_THAT(l33t_match("4 1 @", {}, {}), IsEmpty());
// doesn't match with subsets of possible l33t substitutions
EXPECT_THAT(lm("4sdf0"), IsEmpty());
}
}
TEST(ZxcvbnTest, SpatialMatching) {
// doesn't match 1- and 2-character spatial patterns
for (const std::string& password : {"", "/", "qw", "*/"}) {
EXPECT_THAT(spatial_match(password, {}), IsEmpty());
}
// for testing, make a subgraph that contains a single keyboard
Graphs test_graphs = {{GraphTag::QWERTY, graphs().at(GraphTag::QWERTY)}};
std::string pattern = "6tfGHJ";
std::vector<Match> matches =
spatial_match("rz!" + pattern + "%z", test_graphs);
EXPECT_THAT(matches, ElementsAre(ExpectedSpatialMatch{
.i = 3,
.j = 3 + pattern.size() - 1,
.token = pattern,
.graph = GraphTag::QWERTY,
.turns = 2,
.shifted_count = 3,
}));
struct {
std::string pattern;
GraphTag keyboard;
unsigned turns;
idx_t shifts;
} tests[] = {
{"12345", GraphTag::QWERTY, 1, 0},
{"@WSX", GraphTag::QWERTY, 1, 4},
{"6tfGHJ", GraphTag::QWERTY, 2, 3},
{"hGFd", GraphTag::QWERTY, 1, 2},
{"/;p09876yhn", GraphTag::QWERTY, 3, 0},
{"Xdr%", GraphTag::QWERTY, 1, 2},
{"159-", GraphTag::KEYPAD, 1, 0},
{"*84", GraphTag::KEYPAD, 1, 0},
{"/8520", GraphTag::KEYPAD, 1, 0},
{"369", GraphTag::KEYPAD, 1, 0},
{"/963.", GraphTag::MAC_KEYPAD, 1, 0},
{"*-632.0214", GraphTag::MAC_KEYPAD, 9, 0},
{"aoEP%yIxkjq:", GraphTag::DVORAK, 4, 5},
{";qoaOQ:Aoq;a", GraphTag::DVORAK, 11, 4},
};
for (const auto& test : tests) {
Graphs test_graphs = {{test.keyboard, graphs().at(test.keyboard)}};
std::vector<Match> matches = spatial_match(test.pattern, test_graphs);
EXPECT_THAT(matches, ElementsAre(ExpectedSpatialMatch{
.i = 0,
.j = test.pattern.size() - 1,
.token = test.pattern,
.graph = test.keyboard,
.turns = test.turns,
.shifted_count = test.shifts,
}));
}
}
TEST(ZxcvbnTest, SequenceMatching) {
// doesn't match 0- and 1-character sequences
for (const std::string& password : {"", "a", "1"}) {
EXPECT_THAT(sequence_match(password), IsEmpty());
}
{
// matches overlapping patterns
std::vector<Match> matches = sequence_match("abcbabc");
EXPECT_THAT(matches, ElementsAre(
ExpectedSequenceMatch{
.i = 0,
.j = 2,
.token = "abc",
.sequence_tag = SequenceTag::kLower,
.ascending = true,
},
ExpectedSequenceMatch{
.i = 2,
.j = 4,
.token = "cba",
.sequence_tag = SequenceTag::kLower,
.ascending = false,
},
ExpectedSequenceMatch{
.i = 4,
.j = 6,
.token = "abc",
.sequence_tag = SequenceTag::kLower,
.ascending = true,
}));
}
{
// matches embedded sequence patterns
std::string pattern = "jihg";
for (const auto& variation : gen_pws(pattern, {"!", "22"}, {"!", "22"})) {
std::vector<Match> matches = sequence_match(variation.password);
EXPECT_THAT(matches, ElementsAre(ExpectedSequenceMatch{
.i = variation.i,
.j = variation.j,
.token = pattern,
.sequence_tag = SequenceTag::kLower,
.ascending = false,
}));
}
}
{
struct {
std::string pattern;
SequenceTag sequence_tag;
bool ascending;
} tests[] = {
{"ABC", SequenceTag::kUpper, true},
{"CBA", SequenceTag::kUpper, false},
{"PQR", SequenceTag::kUpper, true},
{"RQP", SequenceTag::kUpper, false},
{"XYZ", SequenceTag::kUpper, true},
{"ZYX", SequenceTag::kUpper, false},
{"abcd", SequenceTag::kLower, true},
{"dcba", SequenceTag::kLower, false},
{"jihg", SequenceTag::kLower, false},
{"wxyz", SequenceTag::kLower, true},
{"zxvt", SequenceTag::kLower, false},
{"0369", SequenceTag::kDigits, true},
{"97531", SequenceTag::kDigits, false},
};
for (const auto& test : tests) {
std::vector<Match> matches = sequence_match(test.pattern);
EXPECT_THAT(matches, ElementsAre(ExpectedSequenceMatch{
.i = 0,
.j = test.pattern.size() - 1,
.token = test.pattern,
.sequence_tag = test.sequence_tag,
.ascending = test.ascending,
}));
}
}
}
TEST(ZxcvbnTest, RepeatMatching) {
// doesn't match 0- and 1-character repeat patterns
for (const std::string& password : {"", "#"}) {
EXPECT_THAT(repeat_match(password), IsEmpty());
}
{
// matches embedded repeat patterns
std::string pattern = "&&&&";
for (const auto& variation : gen_pws(pattern, {"@", "y4@"}, {"u", "u%7"})) {
std::vector<Match> matches = repeat_match(variation.password);
EXPECT_THAT(matches, ElementsAre(ExpectedRepeatMatch{
.i = variation.i,
.j = variation.j,
.token = pattern,
.base_token = "&",
}));
}
}
{
// matches repeats with base character
for (size_t length : {3, 12}) {
for (char chr : {'a', 'Z', '4', '&'}) {
std::string pattern(length, chr);
std::vector<Match> matches = repeat_match(pattern);
EXPECT_THAT(matches, ElementsAre(ExpectedRepeatMatch{
.i = 0,
.j = pattern.size() - 1,
.token = pattern,
.base_token = std::string(1, chr),
}));
}
}
}
{
// matches multiple adjacent repeats
std::vector<Match> matches = repeat_match("BBB1111aaaaa@@@@@@");
EXPECT_THAT(matches, ElementsAre(
ExpectedRepeatMatch{
.i = 0,
.j = 2,
.token = "BBB",
.base_token = "B",
},
ExpectedRepeatMatch{
.i = 3,
.j = 6,
.token = "1111",
.base_token = "1",
},
ExpectedRepeatMatch{
.i = 7,
.j = 11,
.token = "aaaaa",
.base_token = "a",
},
ExpectedRepeatMatch{
.i = 12,
.j = 17,
.token = "@@@@@@",
.base_token = "@",
}));
}
{
// matches multiple repeats with non-repeats in-between
std::vector<Match> matches =
repeat_match("2818BBBbzsdf1111@*&@!aaaaaEUDA@@@@@@1729");
EXPECT_THAT(matches, ElementsAre(
ExpectedRepeatMatch{
.i = 4,
.j = 6,
.token = "BBB",
.base_token = "B",
},
ExpectedRepeatMatch{
.i = 12,
.j = 15,
.token = "1111",
.base_token = "1",
},
ExpectedRepeatMatch{
.i = 21,
.j = 25,
.token = "aaaaa",
.base_token = "a",
},
ExpectedRepeatMatch{
.i = 30,
.j = 35,
.token = "@@@@@@",
.base_token = "@",
}));
}
{
// matches multi-character repeat pattern
std::string pattern = "abab";
std::vector<Match> matches = repeat_match(pattern);
EXPECT_THAT(matches, ElementsAre(ExpectedRepeatMatch{
.i = 0,
.j = pattern.size() - 1,
.token = pattern,
.base_token = "ab",
}));
}
{
// matches aabaab as a repeat instead of the aa prefix
std::string pattern = "aabaab";
std::vector<Match> matches = repeat_match(pattern);
EXPECT_THAT(matches, ElementsAre(ExpectedRepeatMatch{
.i = 0,
.j = pattern.size() - 1,
.token = pattern,
.base_token = "aab",
}));
}
{
// identifies ab as repeat string, even though abab is also repeated
std::string pattern = "abababab";
std::vector<Match> matches = repeat_match(pattern);
EXPECT_THAT(matches, ElementsAre(ExpectedRepeatMatch{
.i = 0,
.j = pattern.size() - 1,
.token = pattern,
.base_token = "ab",
}));
}
{
// identifies äö as repeat string, even though äöäö is also repeated.
// verifies that match.i and match.j operate in code point counts, and not
// in bytes.
std::string pattern = "\u00E4\u00F6\u00E4\u00F6\u00E4\u00F6\u00E4\u00F6";
std::vector<Match> matches = repeat_match(pattern);
EXPECT_THAT(matches, ElementsAre(ExpectedRepeatMatch{
.i = 0,
.j = 7,
.token = pattern,
.base_token = "\u00E4\u00F6",
}));
}
}
TEST(ZxcvbnTest, RegexMatching) {
struct {
std::string pattern;
RegexTag regex_tag;
} tests[] = {
{"1922", RegexTag::RECENT_YEAR},
{"2017", RegexTag::RECENT_YEAR},
};
for (const auto& test : tests) {
std::vector<Match> matches = regex_match(test.pattern, REGEXEN());
EXPECT_THAT(matches, ElementsAre(ExpectedRegexMatch{
.i = 0,
.j = test.pattern.size() - 1,
.token = test.pattern,
.regex_tag = test.regex_tag,
}));
}
}
TEST(ZxcvbnTest, DateMatching) {
// matches dates that use `sep` as a separator
for (const std::string& sep : {"", " ", "-", "/", "\\", "_", "."}) {
std::string password = "13" + sep + "2" + sep + "1921";
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 0,
.j = password.size() - 1,
.token = password,
.separator = sep,
.year = 1921,
.month = 2,
.day = 13,
}));
}
// matches dates with `order` format
for (std::string order : {"mdy", "dmy", "ymd", "ydm"}) {
order.replace(order.find('y'), 1, "88");
order.replace(order.find('m'), 1, "8");
order.replace(order.find('d'), 1, "8");
std::vector<Match> matches = date_match(order);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 0,
.j = order.size() - 1,
.token = order,
.separator = "",
.year = 1988,
.month = 8,
.day = 8,
}));
}
{
// matches the date with year closest to REFERENCE_YEAR when ambiguous
std::string password = "111504";
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 0,
.j = password.size() - 1,
.token = password,
.separator = "",
.year = 2004,
.month = 11,
.day = 15,
}));
}
{
struct {
unsigned day;
unsigned month;
unsigned year;
} tests[] = {
{1, 1, 1999},
{21, 8, 2000},
{19, 12, 2005},
{22, 11, 1551},
};
for (const auto& test : tests) {
std::string password = std::to_string(test.year) +
std::to_string(test.month) +
std::to_string(test.day);
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 0,
.j = password.size() - 1,
.token = password,
.separator = "",
.year = test.year,
.month = test.month,
.day = test.day,
}));
}
for (const auto& test : tests) {
std::string password = std::to_string(test.year) + "." +
std::to_string(test.month) + "." +
std::to_string(test.day);
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 0,
.j = password.size() - 1,
.token = password,
.separator = ".",
.year = test.year,
.month = test.month,
.day = test.day,
}));
}
}
{
// matches zero-padded dates
std::string password = "02/02/02";
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 0,
.j = password.size() - 1,
.token = password,
.separator = "/",
.year = 2002,
.month = 2,
.day = 2,
}));
}
{
// matches embedded dates
std::string pattern = "1/1/91";
for (const auto& variation : gen_pws(pattern, {"a", "ab"}, {"!"})) {
std::vector<Match> matches = date_match(variation.password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = variation.i,
.j = variation.j,
.token = pattern,
.separator = "/",
.year = 1991,
.month = 1,
.day = 1,
}));
}
}
{
// matches overlapping dates
std::string password = "12/20/1991.12.20";
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(
ExpectedDateMatch{
.i = 0,
.j = 9,
.token = "12/20/1991",
.separator = "/",
.year = 1991,
.month = 12,
.day = 20,
},
ExpectedDateMatch{
.i = 6,
.j = 15,
.token = "1991.12.20",
.separator = ".",
.year = 1991,
.month = 12,
.day = 20,
}));
}
{
// matches dates padded by non-ambiguous digits
std::string password = "912/20/919";
std::vector<Match> matches = date_match(password);
EXPECT_THAT(matches, ElementsAre(ExpectedDateMatch{
.i = 1,
.j = 8,
.token = "12/20/91",
.separator = "/",
.year = 1991,
.month = 12,
.day = 20,
}));
}
}
TEST(ZxcvbnTest, Omnimatch) {
EXPECT_THAT(omnimatch(""), IsEmpty());
SetRankedDicts(RankedDicts({{"rosebud", "maelstrom"}}));
std::string password = "r0sebudmaelstrom11/20/91aaaa";
std::vector<Match> matches = omnimatch(password);
struct {
MatchPattern pattern;
idx_t i;
idx_t j;
} tests[] = {
{MatchPattern::DICTIONARY, 0, 6},
{MatchPattern::DICTIONARY, 7, 15},
{MatchPattern::DATE, 16, 23},
{MatchPattern::REPEAT, 24, 27},
};
for (const auto& test : tests) {
bool included =
std::any_of(matches.begin(), matches.end(), [&test](const auto& match) {
return std::make_tuple(match.get_pattern(), match.i, match.j) ==
std::make_tuple(test.pattern, test.i, test.j);
});
EXPECT_TRUE(included);
}
}
} // namespace zxcvbn
|