github.com/zcqzcg/fabric-ca@v2.0.0-alpha.0.20200416163940-d878ee6db75a+incompatible/lib/metadata/version_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 metadata_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/hyperledger/fabric-ca/lib/metadata"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestVersion(t *testing.T) {
    27  	// Positive test cases
    28  	cmpVersion(t, "1.1.1-xxxx", "1.1.1-yyy-zzz", 0)
    29  	cmpVersion(t, "1.0.0", "1.1.0", 1)
    30  	cmpVersion(t, "1.1.0", "1.1.0.0.0", 0)
    31  	cmpVersion(t, "1.5.0.0.0", "1.5", 0)
    32  	cmpVersion(t, "1.0.0", "1.0.0.1", 1)
    33  	cmpVersion(t, "1.1.0", "1.0.0", -1)
    34  	cmpVersion(t, "1.0.0.0.1", "1.0", -1)
    35  	cmpLevels(t, "1.0.0", 0, 0, 0)
    36  	cmpLevels(t, "1.0.4", 0, 0, 0)
    37  	cmpLevels(t, "1.1.0", 1, 1, 1)
    38  	cmpLevels(t, "1.1.1", 1, 1, 1)
    39  	cmpLevels(t, "1.2.1", 1, 1, 1)
    40  	// Negative test cases
    41  	_, err := metadata.CmpVersion("1.x.2.0", "1.7.8")
    42  	if err == nil {
    43  		t.Error("Expecting error at 1.x.2.0")
    44  	}
    45  	_, err = metadata.CmpVersion("1.2.0", "x.1.7.8")
    46  	if err == nil {
    47  		t.Error("Expecting error at x.1.7.8")
    48  	}
    49  }
    50  
    51  func cmpVersion(t *testing.T, v1, v2 string, expectedResult int) {
    52  	result, err := metadata.CmpVersion(v1, v2)
    53  	if err != nil {
    54  		t.Fatalf("Failed comparing versions: %s", err)
    55  	}
    56  	assert.Equal(t, expectedResult, result)
    57  }
    58  
    59  func cmpLevels(t *testing.T, version string, identity, affiliation, certificate int) {
    60  	levels, err := metadata.GetLevels(version)
    61  	if err != nil {
    62  		t.Fatalf("GetLevels failed: %s", err)
    63  	}
    64  	assert.Equal(t, levels.Identity, identity)
    65  	assert.Equal(t, levels.Affiliation, affiliation)
    66  	assert.Equal(t, levels.Certificate, certificate)
    67  }
    68  
    69  func TestGetVersionInfo(t *testing.T) {
    70  	info := metadata.GetVersionInfo("fabric-ca-client")
    71  	assert.Contains(t, info, "Version: "+metadata.Version)
    72  
    73  	metadata.Version = "1.0.0"
    74  	info = metadata.GetVersionInfo("fabric-ca-client")
    75  	assert.Contains(t, info, "Version: 1.0.0")
    76  }
    77  
    78  func TestGetVersion(t *testing.T) {
    79  	info := metadata.GetVersion()
    80  
    81  	metadata.Version = "1.0.0"
    82  	info = metadata.GetVersion()
    83  	assert.Contains(t, info, "1.0.0")
    84  }