@@ -14,7 +14,6 @@ import (
1414 "os"
1515 "path"
1616 "path/filepath"
17- "regexp"
1817 "testing"
1918
2019 "github.com/cloudflare/cfssl/log"
@@ -27,25 +26,15 @@ import (
2726)
2827
2928const (
30- initYaml = "i.yaml"
3129 startYaml = "s.yaml"
3230 ldapTestDir = "ldapTestDir"
3331)
3432
3533var (
3634 longUserName = util .RandomString (1025 )
37- )
38-
39- var (
4035 longFileName = util .RandomString (261 )
4136)
4237
43- // Create a config element in unexpected format
44- var badSyntaxYaml = "bad.yaml"
45-
46- // Unsupported file type
47- var unsupportedFileType = "config.txt"
48-
4938type TestData struct {
5039 input []string // input
5140 expected string // expected result
@@ -69,19 +58,6 @@ func checkTest(in *TestData, t *testing.T) {
6958 }
7059}
7160
72- // errorTest validates error cases
73- func errorTest (in * TestData , t * testing.T ) {
74- err := RunMain (in .input )
75- if err != nil {
76- matched , _ := regexp .MatchString (in .expected , err .Error ())
77- if ! matched {
78- t .Errorf ("FAILED:\n \t in: %v;\n \t out: %v;\n \t expected: %v\n " , in .input , err .Error (), in .expected )
79- }
80- } else {
81- t .Errorf ("FAILED:\n \t in: %v;\n \t out: <nil>\n \t expected: %v\n " , in .input , in .expected )
82- }
83- }
84-
8561func TestMain (m * testing.M ) {
8662 os .Setenv ("FABRIC_CA_SERVER_OPERATIONS_LISTENADDRESS" , "localhost:0" )
8763 defer os .Unsetenv ("FABRIC_CA_SERVER_OPERATIONS_LISTENADDRESS" )
@@ -99,9 +75,22 @@ func TestNoArguments(t *testing.T) {
9975
10076func TestErrors (t * testing.T ) {
10177 os .Unsetenv (homeEnvVar )
78+ initYaml := "i.yaml"
79+ // Create a config element in unexpected format
80+ badSyntaxYaml := "bad.yaml"
81+ // Unsupported file type
82+ unsupportedFileType := "config.txt"
10283 _ = ioutil .WriteFile (badSyntaxYaml , []byte ("signing: true\n " ), 0644 )
103-
104- errorCases := []TestData {
84+ defer func () {
85+ os .Remove (badSyntaxYaml )
86+ os .Remove (unsupportedFileType )
87+ os .Remove (startYaml )
88+ }()
89+
90+ tests := []struct {
91+ cmd []string
92+ expected string
93+ }{
10594 {[]string {cmdName , "init" , "-c" , initYaml }, "option is required" },
10695 {[]string {cmdName , "init" , "-c" , initYaml , "-n" , "acme.com" , "-b" , "user::" }, "Failed to read" },
10796 {[]string {cmdName , "init" , "-b" , "user:pass" , "-n" , "acme.com" , "ca.key" }, "Unrecognized arguments found" },
@@ -118,20 +107,45 @@ func TestErrors(t *testing.T) {
118107 {[]string {cmdName , "start" , "-c" , startYaml , "-b" , "user:pass" , "ca.key" }, "Unrecognized arguments found" },
119108 }
120109
121- for _ , e := range errorCases {
122- errorTest (& e , t )
123- _ = os .Remove (initYaml )
110+ for _ , tt := range tests {
111+ tt := tt
112+ t .Run (tt .expected , func (t * testing.T ) {
113+ err := RunMain (tt .cmd )
114+ assert .Error (t , err , tt .expected )
115+ os .Remove (initYaml )
116+ })
124117 }
125118}
126119
127120func TestOneTimePass (t * testing.T ) {
128- testDir := "oneTimePass"
129- os .RemoveAll (testDir )
130- defer os .RemoveAll (testDir )
131- // Test with "-b" option
132- err := RunMain ([]string {cmdName , "init" , "-b" , "admin:adminpw" , "--registry.maxenrollments" , "1" , "-H" , testDir })
133- if err != nil {
134- t .Fatalf ("Failed to init server with one time passwords: %s" , err )
121+ tests := []struct {
122+ testName string
123+ cmd []string
124+ }{
125+ {
126+ testName : "When identity has user and pass" ,
127+ cmd : []string {cmdName , "init" , "-b" , "admin:adminpw" , "--registry.maxenrollments" , "1" , "-H" , os .TempDir ()},
128+ },
129+ {
130+ testName : "When identity has user and passfile" ,
131+ cmd : []string {cmdName , "init" , "-b" , "admin:adminpw" , "-f" , "pwFile" , "--registry.maxenrollments" , "1" , "-H" , os .TempDir ()},
132+ },
133+ {
134+ testName : "When identity has user, pass, and passfile" ,
135+ cmd : []string {cmdName , "init" , "-b" , "admin:adminpw" , "-f" , "pwFile" , "--registry.maxenrollments" , "1" , "-H" , os .TempDir ()},
136+ },
137+ }
138+
139+ for _ , tt := range tests {
140+ t .Run (tt .testName , func (t * testing.T ) {
141+ defer os .Remove (filepath .Join (os .TempDir (), "pwFile" ))
142+
143+ err := ioutil .WriteFile (filepath .Join (os .TempDir (), "pwFile" ), []byte ("mypassword\n " ), 0666 )
144+ assert .NoError (t , err , "Failed to create passfile" )
145+
146+ err = RunMain (tt .cmd )
147+ assert .NoError (t , err , "Failed to init server with one time passwords" )
148+ })
135149 }
136150}
137151
@@ -426,11 +440,8 @@ func checkConfigAndDBLoc(t *testing.T, args TestData, cfgFile string, dsFile str
426440func TestClean (t * testing.T ) {
427441 defYaml := util .GetDefaultConfigFile (cmdName )
428442 os .Remove (defYaml )
429- os .Remove (initYaml )
430443 os .Remove (startYaml )
431- os .Remove (badSyntaxYaml )
432444 os .Remove (fmt .Sprintf ("/tmp/%s.yaml" , longFileName ))
433- os .Remove (unsupportedFileType )
434445 os .Remove ("ca-key.pem" )
435446 os .Remove ("ca-cert.pem" )
436447 os .Remove ("IssuerSecretKey" )
0 commit comments