src/Password.tests.ps1

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
Import-Module $PSScriptRoot/Password.psm1 -Force  # force code to be reloaded
Describe 'Rendder Password' {
    Context "GetConfiguredRules" {
        It "is empty when no rules in profile" {
            $PasswordProfile = @{}
            
            GetConfiguredRules $PasswordProfile | Should -Be @()
        }

        It "ignore disabled rules" {
            $PasswordProfile = @{
                Lowercase = $False
                Uppercase = $True
                Digits    = $False
                Symbols   = $True
            }
            
            GetConfiguredRules $PasswordProfile | Should -Be @("Uppercase", "Symbols")
        }

        It "preserve rules order" {
            $PasswordProfile = @{
                Lowercase = $True
                Uppercase = $True
                Digits    = $True
                Symbols   = $True
            }
            
            GetConfiguredRules $PasswordProfile | Should -Be @("Lowercase", "Uppercase", "Digits", "Symbols")
        }
    }

    Context "GetSetOfCharacters" {
        $CharacterSubsets = [ordered]@{
            Lowercase = "abcdefghijklmnopqrstuvwxyz"
            Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Digits    = "0123456789"
            Symbols   = "!`"#$%&'()*+,-./:;<=>?@[`\]^_``{|}~"
        }

        It "get set of characters without rule" {
            GetSetOfCharacters @() | Should -BeExactly "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!`"#$%&'()*+,-./:;<=>?@[\]^_``{|}~"
        }

        It "get set of characters with single rule: <rule>"-TestCases @(
            @{rule = "Lowercase" }
            @{rule = "Uppercase" }
            @{rule = "Digits" }
            @{rule = "Symbols" }
         ) {
            param($rule)
            GetSetOfCharacters @($rule) | Should -BeExactly $CharacterSubsets.$rule

        }

        It "get set of characters with several rules" {
            GetSetOfCharacters @("Lowercase", "Digits") | Should -BeExactly "abcdefghijklmnopqrstuvwxyz0123456789"
        }
    }

    Context "ConsumeEntropy" {
        $GeneratedPassword = ""
        # declaring BigInt as a string is mandatory, see https://stackoverflow.com/q/55366362/802365
        [BigInt]$EntropyAsInt = "99600400399777174105034830393873797761817714609490038944205586760025858632478"
        
        $SetOfCharacters = "abcdefghijklmnopqrstuvwxyz0123456789"
        $MaxLength = 12

        $GeneratedPassword, $RemainingEntropy = ConsumeEntropy $GeneratedPassword $EntropyAsInt $SetOfCharacters $MaxLength

        It "returns generated password value" {
            $GeneratedPassword | Should -BeExactly "gsrwvjl3d0sn"
        }

        It "returns the remaining entropy" {
            $RemainingEntropy | Should -Be "21019920789038193790619410818194537836313158091882651458040"
        }
    }

    Context "GetOneCharPerRule" {
        [BigInt]$Entropy = "21019920789038193790619410818194537836313158091882651458040"
        
        It "get one char per rule without rules" {
            GetOneCharPerRule $Entropy @() `
            | Should -BeExactly "", "21019920789038193790619410818194537836313158091882651458040"
        }

        It "get one char per rule with several rules" {
            GetOneCharPerRule $Entropy @("Lowercase", "Digits") `
            | Should -BeExactly "a0", "80845849188608437656228503146902068601204454199548659454"
        }
    }

    Context "InsertStringPseudoRandomly" {
        It "add new characters" {
            [BigInt]$Entropy = "80845849188608437656228503146902068601204454199548659454"
            InsertStringPseudoRandomly "gsrwvjl3d0sn" $Entropy "a0" | Should -BeExactly "gsrwvjl03d0asn"
        }
    }

    Context "RenderPassword" {
        $PasswordProfile = @{
            Site      = "example.org"
            Login     = "contact@example.org"
            Lowercase = $True
            Digits    = $True
            Length    = 14
            Counter   = 1
        }
        $MasterPassword = "password"
        $Entropy = CalcEntropy $PasswordProfile $MasterPassword
        It "is correct" {
            RenderPassword $Entropy $PasswordProfile | Should -BeExactly "gsrwvjl03d0asn"
        }
    }
}

Describe 'Password' {
    Context "CalcEntropy" {
        It "Computes entropy as a lower case hexadecimal string" {
            $PasswordProfile = @{
                Site      = "example.org"
                Login     = "contact@example.org"
                Counter   = 1
            }
            $MasterPassword = "password"

            CalcEntropy $PasswordProfile $MasterPassword | Should -BeExactly 'dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e'
        }
    }
    Context "Generate" {
        It 'with profile #1' {
            $PasswordProfile = @{
                Site      = "example.org"
                Login     = "contact@example.org"
                Lowercase = $True
                Uppercase = $True
                Digits    = $True
                Symbols   = $True
                Length    = 16
                Counter   = 1
            }
            $MasterPassword = "password"

            GeneratePassword $PasswordProfile $MasterPassword | Should -BeExactly "WHLpUL)e00[iHR+w"
        }

        It 'with profile #2' {
            $PasswordProfile = @{
                Site      = "example.org"
                Login     = "contact@example.org"
                Lowercase = $True
                Uppercase = $True
                Digits    = $True
                Symbols   = $false
                Length    = 14
                Counter   = 2
            }
            $MasterPassword = "password"
            
            GeneratePassword $PasswordProfile $MasterPassword | Should -BeExactly "MBAsB7b1Prt8Sl"
        }

        It 'with profile #3' {
            $PasswordProfile = @{
                Site      = "example.org"
                Login     = "contact@example.org"
                Lowercase = $False
                Uppercase = $False
                Digits    = $True
                Symbols   = $False
                Length    = 16
                Counter   = 1
            }
            $MasterPassword = "password"

            GeneratePassword $PasswordProfile $MasterPassword | Should -BeExactly "8742368585200667"
        }

        It 'with profile #4' {
            $PasswordProfile = @{
                Site      = "example.org"
                Login     = "contact@example.org"
                Lowercase = $True
                Uppercase = $True
                Digits    = $False
                Symbols   = $True
                Length    = 16
                Counter   = 1
            }
            $MasterPassword = "password"

            GeneratePassword $PasswordProfile $MasterPassword | Should -BeExactly "s>{F}RwkN/-fmM.X"
        }

        It 'with profile NRT 328' {
            $PasswordProfile = @{
                Site      = "site"
                Login     = "login"
                Lowercase = $True
                Uppercase = $True
                Digits    = $True
                Symbols   = $True
                Length    = 16
                Counter   = 10
            }
            $MasterPassword = "test"

            GeneratePassword $PasswordProfile $MasterPassword | Should -BeExactly "XFt0F*,r619:+}[."
        }
    }
}