Skip to content

Commit

Permalink
Fix and chaincode label regexp and Backfill tests
Browse files Browse the repository at this point in the history
Ensure that some of the special cases are properly evaluated.

FAB-17340

Change-Id: If73c4d5e230221ac7240b17eef0876ca168dc174
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm authored and ale-linux committed Jan 9, 2020
1 parent 4549cfb commit 83dacb4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/chaincode/persistence/chaincode_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ type ChaincodePackageParser struct {
var (
// LabelRegexp is the regular expression controlling
// the allowed characters for the package label
LabelRegexp = regexp.MustCompile("^[a-zA-Z0-9]+([.+-_][a-zA-Z0-9]+)*$")
LabelRegexp = regexp.MustCompile(`^[[:alnum:]][[:alnum:]_.+-]*$`)
)

func validateLabel(label string) error {
Expand Down
56 changes: 56 additions & 0 deletions core/chaincode/persistence/label_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package persistence

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLabels(t *testing.T) {
tests := []struct {
label string
success bool
}{
{label: "", success: false},
{label: ".", success: false},
{label: "0", success: true},
{label: ":", success: false},
{label: "_", success: false},
{label: "a", success: true},
{label: "a#", success: false},
{label: "a$", success: false},
{label: "a%", success: false},
{label: "a-", success: true},
{label: "a++b", success: true},
{label: "a+b", success: true},
{label: "a+bb", success: true},
{label: "a--b", success: true},
{label: "a-b", success: true},
{label: "a-bb", success: true},
{label: "a::b", success: false},
{label: "a:b", success: false},
{label: "a__b", success: true},
{label: "a_b", success: true},
{label: "a_bb", success: true},
{label: "aa", success: true},
}

for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
err := validateLabel(tt.label)
if tt.success {
assert.NoError(t, err)
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.label)
}
})
}
}

0 comments on commit 83dacb4

Please sign in to comment.