Skip to content

Commit

Permalink
Updated spatial matcher tests and fixed the implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
trichards57 committed Feb 9, 2018
1 parent 00e4bb0 commit fd9af54
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
32 changes: 0 additions & 32 deletions zxcvbn-core-test/Matcher/SpacialMatcherTests.cs

This file was deleted.

63 changes: 63 additions & 0 deletions zxcvbn-core-test/Matcher/SpatialMatcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Linq;
using FluentAssertions;
using Xunit;
using Zxcvbn.Matcher;
using Zxcvbn.Matcher.Matches;

namespace Zxcvbn.Tests.Matcher
{
public class SpatialMatcherTests
{
private readonly SpatialMatcher _matcher = new SpatialMatcher();

[Theory, InlineData(""), InlineData("/"), InlineData("qw"), InlineData("*/")]
public void DoesNotmatch1And2CharacterPatterns(string password)
{
var matches = _matcher.MatchPassword(password);

matches.Should().BeEmpty();
}

[Theory, InlineData("12345", "qwerty", 1, 0), InlineData("@WSX", "qwerty", 1, 4), InlineData("6tfGHJ", "qwerty", 2, 3), InlineData("hGFd", "qwerty", 1, 2),
InlineData("/;p09876yhn", "qwerty", 3, 0), InlineData("Xdr%", "qwerty", 1, 2), InlineData("159-", "keypad", 1, 0),
InlineData("*84", "keypad", 1, 0), InlineData("/8520", "keypad", 1, 0), InlineData("369", "keypad", 1, 0), InlineData("/963.", "mac_keypad", 1, 0),
InlineData("*-632.0214", "mac_keypad", 9, 0), InlineData("aoEP%yIxkjq:", "dvorak", 4, 5), InlineData(";qoaOQ:Aoq;a", "dvorak", 11, 4)]
public void MatchesKeyboardPattern(string pattern, string keyboard, int turns, int shifts)
{
var matches = _matcher.MatchPassword(pattern).OfType<SpatialMatch>().ToList();

matches.Should().NotBeEmpty();
matches.Should().ContainSingle(s => s.Graph == keyboard);

var match = matches.Single(m => m.Graph == keyboard);

match.Turns.Should().Be(turns);
match.ShiftedCount.Should().Be(shifts);
match.i.Should().Be(0);
match.j.Should().Be(pattern.Length - 1);
match.Pattern.Should().Be("spatial");
}

// [Fact]
// public void MultipleSequencesMultipleKeyboards()
// {
// var sm = new SpatialMatcher();

// var res = sm.MatchPassword("plko14569852pyfdb").ToList();
// res.Count.Should().Be(6);
// }

// [Fact]
// public void SingleSequence()
// {
// var sm = new SpatialMatcher();

// var res = sm.MatchPassword("qwert").ToList();
// res.Count.Should().Be(1);

// res[0].i.Should().Be(0);
// res[0].j.Should().Be(4);
// res[0].Token.Should().Be("qwert");
// }
}
}
11 changes: 8 additions & 3 deletions zxcvbn-core/Matcher/SpatialMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ private static IEnumerable<Match> SpatialMatch(SpatialGraph graph, string passwo
var i = 0;
while (i < password.Length - 1)
{
int turns = 0, shiftedCount = 0;
var lastDirection = -1;
var turns = 0;
var shiftedCount = 0;
int? lastDirection = null;

var j = i + 1;

Expand All @@ -105,9 +106,13 @@ private static IEnumerable<Match> SpatialMatch(SpatialGraph graph, string passwo
if (j < password.Length)
{
var curChar = password[j].ToString();
foreach (var adjacent in adjacents.Where(a => a != null))
foreach (var adjacent in adjacents)
{
currentDirection++;

if (adjacent == null)
continue;

if (adjacent.Contains(curChar))
{
found = true;
Expand Down

0 comments on commit fd9af54

Please sign in to comment.