File: test_compatibility.py

package info (click to toggle)
zxcvbn-python 4.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,880 kB
  • sloc: python: 2,222; makefile: 3
file content (79 lines) | stat: -rw-r--r-- 2,439 bytes parent folder | download
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
import json
from zxcvbn import zxcvbn
import sys, getopt
from decimal import Decimal

MAX_NUMBER_FOR_ACCURACY = 1000000000000
MIN_NUMBER_FOR_ACCURACY = 1

def update_console_status(total):
    """ iterate from 0 to total and show progress in console """
    total = int(total)
    try:
        range_total = xrange(total)
        range_100_min_total = xrange(100-total)
    except NameError:
        range_total = range(total)
        range_100_min_total = range(100-total)

    str_progress = "%d%% ["%total + "".join(['#' for s in range_total]) + "".join([' ' for s in range_100_min_total]) + "] \r"
    sys.stdout.write(str_progress)
    sys.stdout.flush()

def main(argv):

    verbose = False
    tests_file = ""

    try:
        tests_file = argv[0]
        opts, args = getopt.getopt(argv[1:],"v")
    except getopt.GetoptError:
        print (""""test_compatibility <path/to/tests.json> [options]
            options:
            -v: verbose""")
    for opt, arg in opts:
        if opt == '-v':
            verbose = True

    with open(tests_file) as json_data:
        d = json.load(json_data)

    number_of_passwords = len(d)
    scores_collision = 0
    guesses_collision = 0
    refresh_rate = number_of_passwords // 100

    i = 0
    for js_zxcvbn_score in d:
        if i%refresh_rate== 0:
            update_console_status(i*100/number_of_passwords)
        i += 1
        py_zxcvbn_scroe = dict()
        py_zxcvbn_scroe_full = zxcvbn(js_zxcvbn_score['password'])
        py_zxcvbn_scroe["password"] = py_zxcvbn_scroe_full["password"]
        py_zxcvbn_scroe["guesses"] = py_zxcvbn_scroe_full["guesses"]
        py_zxcvbn_scroe["score"] = py_zxcvbn_scroe_full["score"]

        if (abs(py_zxcvbn_scroe["guesses"] - Decimal(js_zxcvbn_score["guesses"])) > MIN_NUMBER_FOR_ACCURACY and
           py_zxcvbn_scroe["guesses"] < MAX_NUMBER_FOR_ACCURACY):
            guesses_collision += 1
            if verbose:
                print ("""\033[91m==========================================
expected:
%s
results:
%s\033[00m""" % (js_zxcvbn_score, py_zxcvbn_scroe))

        if py_zxcvbn_scroe["score"] != js_zxcvbn_score["score"]:
            scores_collision += 1

    if (guesses_collision or scores_collision):
        print ("""\033[91mFailed!
guesses_collision:%d
guesses_score:%d""" % (guesses_collision, scores_collision))
    else:
        print ("\033[92mPassed!")

if __name__ == "__main__":
    main(sys.argv[1:])