Commit bb90ac40 authored by Daisuke IIZUKA's avatar Daisuke IIZUKA Committed by Gari Singh
Browse files

[FAB-11046] Increase test coverage for cauthdsl



Increase test coverage for commom/cauthdsl

Change-Id: I18d55c737606e882fa3d4a3e4beda0a9237c0090
Signed-off-by: default avatarDaisuke IIZUKA <daisuke.iizuka.ag@hitachi.com>
parent 89eb2cbe
......@@ -13,6 +13,7 @@ import (
"time"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
mb "github.com/hyperledger/fabric/protos/msp"
......@@ -273,3 +274,34 @@ func TestSignedByMspPeer(t *testing.T) {
assert.Equal(t, role.MspIdentifier, "A")
assert.Equal(t, role.Role, mb.MSPRole_PEER)
}
func TestReturnNil(t *testing.T) {
policy := Envelope(And(SignedBy(-1), SignedBy(-2)), signers)
spe, err := compile(policy.Rule, policy.Identities, &mockDeserializer{})
assert.Nil(t, spe)
assert.EqualError(t, err, "identity index out of range, requested -1, but identies length is 2")
}
func TestDeserializeIdentityError(t *testing.T) {
// Prepare
policy := Envelope(SignedBy(0), signers)
spe, err := compile(policy.Rule, policy.Identities, &mockDeserializer{fail: errors.New("myError")})
assert.NoError(t, err)
// Let logger output to buf
var buf bytes.Buffer
backend := logging.NewLogBackend(&buf, "", 0)
cauthdslLogger.SetBackend(logging.AddModuleLevel(backend))
defer func() {
flogging.Reset()
}()
// Call
signedData, used := toSignedData([][]byte{nil}, [][]byte{nil}, [][]byte{nil})
ret := spe(signedData, used)
// Check result (ret and log)
assert.False(t, ret)
assert.Contains(t, buf.String(), "Principal deserialization failure (myError) for identity ")
}
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/
package cauthdsl
......@@ -103,3 +93,28 @@ func TestRejectOnUnknown(t *testing.T) {
err = policy.Evaluate([]*cb.SignedData{})
assert.Error(t, err, "Should have errored evaluating the default policy")
}
func TestNewPolicyErrorCase(t *testing.T) {
provider := NewPolicyProvider(nil)
pol1, msg1, err1 := provider.NewPolicy([]byte{0})
assert.Nil(t, pol1)
assert.Nil(t, msg1)
assert.EqualError(t, err1, "Error unmarshaling to SignaturePolicy: proto: common.SignaturePolicyEnvelope: illegal tag 0 (wire type 0)")
sigPolicy2 := &cb.SignaturePolicyEnvelope{Version: -1}
data2 := marshalOrPanic(sigPolicy2)
pol2, msg2, err2 := provider.NewPolicy(data2)
assert.Nil(t, pol2)
assert.Nil(t, msg2)
assert.EqualError(t, err2, "This evaluator only understands messages of version 0, but version was -1")
pol3, msg3, err3 := provider.NewPolicy([]byte{})
assert.Nil(t, pol3)
assert.Nil(t, msg3)
assert.EqualError(t, err3, "Empty policy element")
var pol4 *policy = nil
err4 := pol4.Evaluate([]*cb.SignedData{})
assert.EqualError(t, err4, "No such policy")
}
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/
package cauthdsl
......@@ -228,10 +218,75 @@ func TestMSPIDWIthSpecialChars(t *testing.T) {
}
func TestBadStringsNoPanic(t *testing.T) {
_, err := FromString("OR('A.member', 'Bmember')")
assert.Error(t, err)
_, err = FromString("OR('A.member', Bmember)")
assert.Error(t, err)
_, err := FromString("OR('A.member', Bmember)") // error after 1st Evaluate()
assert.EqualError(t, err, "unrecognized token 'Bmember' in policy string")
_, err = FromString("OR('A.member', 'Bmember')") // error after 2nd Evalute()
assert.EqualError(t, err, "unrecognized token 'Bmember' in policy string")
_, err = FromString(`OR('A.member', '\'Bmember\'')`) // error after 3rd Evalute()
assert.EqualError(t, err, "unrecognized token 'Bmember' in policy string")
}
func TestOutOfNumIsString(t *testing.T) {
p1, err := FromString("OutOf('1', 'A.member', 'B.member')")
assert.NoError(t, err)
principals := make([]*msp.MSPPrincipal, 0)
principals = append(principals, &msp.MSPPrincipal{
PrincipalClassification: msp.MSPPrincipal_ROLE,
Principal: utils.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: "A"})})
principals = append(principals, &msp.MSPPrincipal{
PrincipalClassification: msp.MSPPrincipal_ROLE,
Principal: utils.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: "B"})})
p2 := &common.SignaturePolicyEnvelope{
Version: 0,
Rule: NOutOf(1, []*common.SignaturePolicy{SignedBy(0), SignedBy(1)}),
Identities: principals,
}
assert.Equal(t, p1, p2)
}
func TestOutOfErrorCase(t *testing.T) {
p1, err1 := FromString("") // 1st NewEvaluableExpressionWithFunctions() returns an error
assert.Nil(t, p1)
assert.EqualError(t, err1, "Unexpected end of expression")
p2, err2 := FromString("OutOf(1)") // outof() if len(args)<2
assert.Nil(t, p2)
assert.EqualError(t, err2, "Expected at least two arguments to NOutOf. Given 1")
p3, err3 := FromString("OutOf(true, 'A.member')") // outof() }else{. 1st arg is non of float, int, string
assert.Nil(t, p3)
assert.EqualError(t, err3, "Unexpected type bool")
p4, err4 := FromString("OutOf(1, 2)") // oufof() switch default. 2nd arg is not string.
assert.Nil(t, p4)
assert.EqualError(t, err4, "Unexpected type float64")
p5, err5 := FromString("OutOf(1, 'true')") // firstPass() switch default
assert.Nil(t, p5)
assert.EqualError(t, err5, "Unexpected type bool")
p6, err6 := FromString(`OutOf('\'\\\'A\\\'\'', 'B.member')`) // secondPass() switch args[1].(type) default
assert.Nil(t, p6)
assert.EqualError(t, err6, "Unrecognized type, expected a number, got string")
p7, err7 := FromString(`OutOf(1, '\'1\'')`) // secondPass() switch args[1].(type) default
assert.Nil(t, p7)
assert.EqualError(t, err7, "Unrecognized type, expected a principal or a policy, got float64")
p8, err8 := FromString(`''`) // 2nd NewEvaluateExpressionWithFunction() returns an error
assert.Nil(t, p8)
assert.EqualError(t, err8, "Unexpected end of expression")
p9, err9 := FromString(`'\'\''`) // 3rd NewEvaluateExpressionWithFunction() returns an error
assert.Nil(t, p9)
assert.EqualError(t, err9, "Unexpected end of expression")
}
func TestBadStringBeforeFAB11404_ThisCanDeleteAfterFAB11404HasMerged(t *testing.T) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment