forked from MEDIGO/bcrypt-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
71 lines (63 loc) · 1.51 KB
/
main_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
package main
import (
"flag"
"fmt"
"os"
"testing"
ca "github.com/juamedgod/cliassert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
)
func Test_main(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expectedCost := DefaultCost
args := []string{}
if tt.cost != 0 {
expectedCost = tt.cost
args = append(args, fmt.Sprintf("--cost=%d", tt.cost))
}
res := runTool(os.Args[0], args, tt.password)
if tt.wantErr {
if res.Success() {
t.Errorf("the command was expected to fail but succeeded")
} else if tt.expectedErr != nil {
res.AssertErrorMatch(t, tt.expectedErr)
}
} else {
if !res.Success() {
t.Errorf("the command was expected to success but failed with %q", res.Stderr())
} else {
stdout := []byte(res.Stdout())
require.NoError(t, bcrypt.CompareHashAndPassword(stdout, []byte(tt.password)))
c, err := bcrypt.Cost(stdout)
require.NoError(t, err)
assert.Equal(t, c, expectedCost)
}
}
})
}
}
func TestMain(m *testing.M) {
if os.Getenv("BE_TOOL") == "1" {
main()
os.Exit(0)
return
}
flag.Parse()
c := m.Run()
os.Exit(c)
}
func runTool(bin string, args []string, stdin string) ca.CmdResult {
cmd := ca.NewCommand()
if stdin != "" {
cmd.SetStdin(stdin)
}
os.Setenv("BE_TOOL", "1")
defer os.Unsetenv("BE_TOOL")
return cmd.Exec(bin, args...)
}
func RunTool(args ...string) ca.CmdResult {
return runTool(os.Args[0], args, "")
}