From cc6e7542785eb9c9a41f5666e11ecb08777e0025 Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Thu, 12 May 2022 18:27:55 +0300 Subject: [PATCH] retunr error with invalid flags --- claim.go | 14 ++++++--- claim_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/claim.go b/claim.go index c2d716a..2c78cc3 100644 --- a/claim.go +++ b/claim.go @@ -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 { @@ -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 ( @@ -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 } } diff --git a/claim_test.go b/claim_test.go index 95d8ad9..d1a7cd8 100644 --- a/claim_test.go +++ b/claim_test.go @@ -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) + }) + } +}