+
Skip to content

return error with invalid flags #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var ErrIncorrectIDPosition = errors.New("incorrect ID position")
// ErrNoID returns when ID not found in the Claim.
var ErrNoID = errors.New("ID is not set")

// ErrInvalidSubjectPosition returns when subject position flags sets in invalid value.
var ErrInvalidSubjectPosition = errors.New("invalid subject position")

// ErrSlotOverflow means some ElemBytes overflows Q Field. And wraps the name
// of overflowed slot.
type ErrSlotOverflow struct {
Expand All @@ -69,6 +72,7 @@ func (e ErrSlotOverflow) Error() string {
return fmt.Sprintf("Slot %v not in field (too large)", e.Field)
}


type SlotName string

const (
Expand Down Expand Up @@ -324,14 +328,16 @@ func (c *Claim) GetSchemaHash() SchemaHash {
}

// GetIDPosition returns the position at which the ID is stored.
func (c *Claim) GetIDPosition() IDPosition {
func (c *Claim) GetIDPosition() (IDPosition, error) {
switch c.getSubject() {
case subjectFlagSelf:
return IDPositionNone, nil
case subjectFlagOtherIdenIndex:
return IDPositionIndex
return IDPositionIndex, nil
case subjectFlagOtherIdenValue:
return IDPositionValue
return IDPositionValue, nil
default:
return IDPositionNone
return 0, ErrInvalidSubjectPosition
}
}

Expand Down
87 changes: 87 additions & 0 deletions claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,90 @@ func TestSchemaHash_BigInt(t *testing.T) {
assert.Equal(t, exp, got)

}

func TestGetIDPosition(t *testing.T) {
tests := []struct {
name string
claim func(t *testing.T) *Claim
expectedPosition IDPosition
}{
{
name: "self claim",
claim: func(t *testing.T) *Claim {
c, err := NewClaim(SchemaHash{})
require.NoError(t, err)
return c
},
expectedPosition: IDPositionNone,
},
{
name: "subject stored in index",
claim: func(t *testing.T) *Claim {
c, err := NewClaim(SchemaHash{})
require.NoError(t, err)

var genesis [27]byte
genesis32bytes := hashBytes([]byte("genesistest"))
copy(genesis[:], genesis32bytes[:])

c.SetIndexID(NewID(TypeDefault, genesis))
return c
},
expectedPosition: IDPositionIndex,
},
{
name: "subject stored in value",
claim: func(t *testing.T) *Claim {
c, err := NewClaim(SchemaHash{})
require.NoError(t, err)

var genesis [27]byte
genesis32bytes := hashBytes([]byte("genesistest"))
copy(genesis[:], genesis32bytes[:])

c.SetValueID(NewID(TypeDefault, genesis))
return c
},
expectedPosition: IDPositionValue,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.claim(t)
position, err := c.GetIDPosition()
require.NoError(t, err)
require.Equal(t, tt.expectedPosition, position)
})
}
}

func TestGetIDPosition_ErrorCase(t *testing.T) {
tests := []struct {
name string
claim func(t *testing.T) *Claim
expectedPosition IDPosition
expectedError error
}{
{
name: "invalid position",
claim: func(t *testing.T) *Claim {
c, err := NewClaim(SchemaHash{})
require.NoError(t, err)
c.setSubject(_subjectFlagInvalid)
return c
},
expectedPosition: IDPositionNone,
expectedError: ErrInvalidSubjectPosition,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.claim(t)
position, err := c.GetIDPosition()
require.ErrorIs(t, err, tt.expectedError)
require.Equal(t, tt.expectedPosition, position)
})
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载