|
| 1 | +package util |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2020 Crunchy Data Solutions, Inc. |
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import ( |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + "unicode" |
| 22 | +) |
| 23 | + |
| 24 | +func TestGeneratePassword(t *testing.T) { |
| 25 | + // different lengths |
| 26 | + for _, length := range []int{1, 2, 3, 5, 20} { |
| 27 | + password, err := GeneratePassword(length) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("expected no error, got %v", err) |
| 30 | + } |
| 31 | + if expected, actual := length, len(password); expected != actual { |
| 32 | + t.Fatalf("expected length %v, got %v", expected, actual) |
| 33 | + } |
| 34 | + if i := strings.IndexFunc(password, unicode.IsPrint); i > 0 { |
| 35 | + t.Fatalf("expected only printable characters, got %q in %q", password[i], password) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // random contents |
| 40 | + previous := []string{} |
| 41 | + |
| 42 | + for i := 0; i < 10; i++ { |
| 43 | + password, err := GeneratePassword(5) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("expected no error, got %v", err) |
| 46 | + } |
| 47 | + if i := strings.IndexFunc(password, unicode.IsPrint); i > 0 { |
| 48 | + t.Fatalf("expected only printable characters, got %q in %q", password[i], password) |
| 49 | + } |
| 50 | + |
| 51 | + for i := range previous { |
| 52 | + if password == previous[i] { |
| 53 | + t.Fatalf("expected passwords to not repeat, got %q after %q", password, previous) |
| 54 | + } |
| 55 | + } |
| 56 | + previous = append(previous, password) |
| 57 | + } |
| 58 | +} |
0 commit comments