|
5 | 5 | "math/big" |
6 | 6 | "testing" |
7 | 7 |
|
| 8 | + it "github.com/iden3/go-circuits/v2/testing" |
8 | 9 | core "github.com/iden3/go-iden3-core/v2" |
9 | 10 | "github.com/iden3/go-merkletree-sql/v2" |
10 | 11 | "github.com/stretchr/testify/assert" |
@@ -92,3 +93,88 @@ func TestStateJsonMarshallers(t *testing.T) { |
92 | 93 | require.NoError(t, err) |
93 | 94 | require.Equal(t, in, out) |
94 | 95 | } |
| 96 | + |
| 97 | +func TestVerifyCredentialSubjectID(t *testing.T) { |
| 98 | + tests := []struct { |
| 99 | + name string |
| 100 | + user *it.IdentityTest |
| 101 | + claim func(t *testing.T) *core.Claim |
| 102 | + nonceSubject *big.Int |
| 103 | + }{ |
| 104 | + { |
| 105 | + name: "Success. Same profileID with nonce", |
| 106 | + user: it.NewIdentity(t, userPK), |
| 107 | + nonceSubject: big.NewInt(10), |
| 108 | + claim: func(t *testing.T) *core.Claim { |
| 109 | + profileID, err := core.ProfileID(it.NewIdentity(t, userPK).ID, big.NewInt(10)) |
| 110 | + require.NoError(t, err) |
| 111 | + return it.DefaultUserClaim(t, profileID) |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "Success. Same genesis profileID", |
| 116 | + user: it.NewIdentity(t, userPK), |
| 117 | + nonceSubject: big.NewInt(0), |
| 118 | + claim: func(t *testing.T) *core.Claim { |
| 119 | + return it.DefaultUserClaim(t, it.NewIdentity(t, userPK).ID) |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + err := verifyCredentialSubjectID(tt.user.ID, *tt.claim(t), tt.nonceSubject) |
| 127 | + require.NoError(t, err) |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestVerifyCredentialSubjectID_Error(t *testing.T) { |
| 133 | + tests := []struct { |
| 134 | + name string |
| 135 | + userID *it.IdentityTest |
| 136 | + claim func(t *testing.T) *core.Claim |
| 137 | + nonceSubject *big.Int |
| 138 | + err string |
| 139 | + }{ |
| 140 | + { |
| 141 | + name: "Different userID on the claim", |
| 142 | + userID: it.NewIdentity(t, userPK), |
| 143 | + claim: func(t *testing.T) *core.Claim { |
| 144 | + // put another indentity to claim |
| 145 | + return it.DefaultUserClaim(t, it.NewIdentity(t, issuerPK).ID) |
| 146 | + }, |
| 147 | + nonceSubject: big.NewInt(10), |
| 148 | + err: ErrorUserProfileMismatch, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "Different nonceSubject on the claim", |
| 152 | + userID: it.NewIdentity(t, userPK), |
| 153 | + claim: func(t *testing.T) *core.Claim { |
| 154 | + profileID, err := core.ProfileID(it.NewIdentity(t, userPK).ID, big.NewInt(11)) |
| 155 | + require.NoError(t, err) |
| 156 | + return it.DefaultUserClaim(t, profileID) |
| 157 | + }, |
| 158 | + nonceSubject: big.NewInt(10), |
| 159 | + err: ErrorUserProfileMismatch, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "Different nonceSubject on the claim. Genesis profile", |
| 163 | + userID: it.NewIdentity(t, userPK), |
| 164 | + claim: func(t *testing.T) *core.Claim { |
| 165 | + profileID, err := core.ProfileID(it.NewIdentity(t, userPK).ID, big.NewInt(11)) |
| 166 | + require.NoError(t, err) |
| 167 | + return it.DefaultUserClaim(t, profileID) |
| 168 | + }, |
| 169 | + nonceSubject: big.NewInt(0), |
| 170 | + err: ErrorUserProfileMismatch, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + for _, tt := range tests { |
| 175 | + t.Run(tt.name, func(t *testing.T) { |
| 176 | + err := verifyCredentialSubjectID(tt.userID.ID, *tt.claim(t), tt.nonceSubject) |
| 177 | + require.EqualError(t, err, tt.err) |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
0 commit comments