-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdecode_test.go
95 lines (80 loc) · 2.8 KB
/
jdecode_test.go
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
package junosdecode_test
import (
"errors"
"fmt"
"math/rand"
"testing"
"github.com/jeremmfr/junosdecode"
)
const (
junWordCoded = "$9$1HFIyKXxdsgJ-VH.Pfn6lKMXdsZUi5Qnikfz"
junWordDecoded = "testPassWord"
)
// TestDecode decode example password.
func TestDecodePassword(t *testing.T) {
t.Parallel()
passwordDecoded, err := junosdecode.Decode(junWordCoded)
if err != nil {
t.Errorf("error on decode %v", err)
}
if passwordDecoded != junWordDecoded {
t.Errorf("decode password failed")
}
}
// TestDecodeBadEncoded try decode encrypted secret but with missing or extra character.
func TestDecodeBadEncoded(t *testing.T) {
t.Parallel()
// remove last character
if passwordDecoded, err := junosdecode.Decode(junWordCoded[:len(junWordCoded)-1]); err == nil {
t.Errorf("missing character in junWordCoded not detected, passwordDecoded: %s", passwordDecoded)
}
// add last character
if passwordDecoded, err := junosdecode.Decode(junWordCoded + string(rune(rand.Intn(26)))); err == nil { //nolint: gosec
t.Errorf("extra character in junWordCoded not detected, passwordDecoded: %s", passwordDecoded)
}
// add two last characters
if passwordDecoded, err := junosdecode.Decode(
junWordCoded + string(rune(rand.Intn(26))) + string(rune(rand.Intn(26)))); err == nil { //nolint: gosec
t.Errorf("extra characters in junWordCoded not detected, passwordDecoded: %s", passwordDecoded)
}
}
func TestDecodeEmptyEncoded(t *testing.T) {
t.Parallel()
// empty input
if passwordDecoded, err := junosdecode.Decode(""); err == nil {
t.Errorf("empty input not detected, passwordDecoded: %s", passwordDecoded)
} else if !errors.Is(err, junosdecode.ErrEmptySecret) {
t.Errorf("got unexpected error: %s", err)
}
}
func TestDecodeNotEnoughCharsEncoded(t *testing.T) {
t.Parallel()
// MagicPrefix input
if passwordDecoded, err := junosdecode.Decode(junosdecode.MagicPrefix); err == nil {
t.Errorf("MagicPrefix input not detected, passwordDecoded: %s", passwordDecoded)
} else if !errors.Is(err, junosdecode.ErrNotEnoughChars) {
t.Errorf("got unexpected error: %s", err)
}
// MagicPrefix and 2 chars input
if passwordDecoded, err := junosdecode.Decode(junosdecode.MagicPrefix + "!!"); err == nil {
t.Errorf("MagicPrefix + 2 chars input not detected, passwordDecoded: %s", passwordDecoded)
} else if !errors.Is(err, junosdecode.ErrNotEnoughChars) {
t.Errorf("got unexpected error: %s", err)
}
}
func TestDecodeParrallel(t *testing.T) {
t.Parallel()
for i, v := range []string{junWordCoded, junWordCoded, junWordCoded} {
word := v
t.Run(fmt.Sprintf("Loop number %d", i), func(t *testing.T) {
t.Parallel()
output, err := junosdecode.Decode(word)
if err != nil {
t.Errorf("got unexpected error: %s", err)
}
if output != junWordDecoded {
t.Errorf("got unexpected decoded word: %s", output)
}
})
}
}