github.com/bestbeforetoday/fabric-ca@v2.0.0-alpha+incompatible/util/args_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util_test
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric-ca/util"
    24  )
    25  
    26  func TestGetCommandLineOptValue(t *testing.T) {
    27  	testGetCommandLineOptValue(t,
    28  		[]string{"fabric-ca", "client", "enroll", "-config", "myconfig.json"},
    29  		"-config",
    30  		true,
    31  		"myconfig.json",
    32  		[]string{"fabric-ca", "client", "enroll"})
    33  	testGetCommandLineOptValue(t,
    34  		[]string{"fabric-ca", "client", "-config", "myconfig.json", "enroll"},
    35  		"-config",
    36  		true,
    37  		"myconfig.json",
    38  		[]string{"fabric-ca", "client", "enroll"})
    39  	testGetCommandLineOptValue(t,
    40  		[]string{"fabric-ca", "client", "-config", "myconfig.json", "enroll"},
    41  		"-config",
    42  		false,
    43  		"myconfig.json",
    44  		[]string{"fabric-ca", "client", "-config", "myconfig.json", "enroll"})
    45  	testGetCommandLineOptValue(t,
    46  		[]string{"fabric-ca", "client", "-config", "myconfig.json", "enroll"},
    47  		"-config2",
    48  		true,
    49  		"",
    50  		[]string{"fabric-ca", "client", "-config", "myconfig.json", "enroll"})
    51  }
    52  
    53  func TestSetDefaultServerPort(t *testing.T) {
    54  	testSetDefaultServerPort(t,
    55  		[]string{"fabric-ca", "client", "enroll"},
    56  		[]string{"fabric-ca", "client", "enroll", "-port", "7054"})
    57  	testSetDefaultServerPort(t,
    58  		[]string{"fabric-ca", "client", "enroll", "-port", "1234"},
    59  		[]string{"fabric-ca", "client", "enroll", "-port", "1234"})
    60  }
    61  
    62  func TestOpts(t *testing.T) {
    63  	testOpt(t, "-protocol", "protocol", "protocol")
    64  	testOpt(t, "-protocol", "", "http")
    65  	testOpt(t, "-address", "addr", "addr")
    66  	testOpt(t, "-address", "", "localhost")
    67  	testOpt(t, "-port", "port", "port")
    68  	testOpt(t, "-port", "", "7054")
    69  }
    70  
    71  func testGetCommandLineOptValue(t *testing.T,
    72  	args []string, opt string, remove bool, expectedVal string, expectedArgs []string) {
    73  
    74  	saveArgs := os.Args
    75  	os.Args = args
    76  	val := util.GetCommandLineOptValue(opt, remove)
    77  	if val != expectedVal {
    78  		t.Errorf("val was '%s' but expected '%s'", val, expectedVal)
    79  	}
    80  	compareArgs(t, os.Args, expectedArgs)
    81  	os.Args = saveArgs
    82  }
    83  
    84  func testSetDefaultServerPort(t *testing.T, inputArgs []string, expectedOutputArgs []string) {
    85  	saveArgs := os.Args
    86  	os.Args = inputArgs
    87  	util.SetDefaultServerPort()
    88  	compareArgs(t, os.Args, expectedOutputArgs)
    89  	os.Args = saveArgs
    90  }
    91  
    92  func testOpt(t *testing.T, opt, val, expectedVal string) {
    93  	saveArgs := os.Args
    94  	if val != "" {
    95  		os.Args = []string{"fabric-ca", "client", "enroll", opt, val}
    96  	} else {
    97  		os.Args = []string{"fabric-ca", "client", "enroll"}
    98  	}
    99  	switch opt {
   100  	case "-protocol":
   101  		val = util.GetServerProtocol()
   102  	case "-address":
   103  		val = util.GetServerAddr()
   104  	case "-port":
   105  		val = util.GetServerPort()
   106  	default:
   107  		panic("bad opt value")
   108  	}
   109  	if val != expectedVal {
   110  		t.Errorf("val was '%s' but expected '%s' for option '%s'", val, expectedVal, opt)
   111  	}
   112  	os.Args = saveArgs
   113  }
   114  
   115  func compareArgs(t *testing.T, args, expectedArgs []string) {
   116  	if len(args) == len(expectedArgs) {
   117  		for i, arg := range args {
   118  			if arg != expectedArgs[i] {
   119  				t.Errorf("args were '%+v' but expected '%+v'", args, expectedArgs)
   120  				return
   121  			}
   122  		}
   123  	} else {
   124  		t.Errorf("args were '%+v' but expected '%+v'", args, expectedArgs)
   125  	}
   126  
   127  }