github.com/Finschia/finschia-sdk@v0.48.1/x/token/genesis_test.go (about) 1 package token_test 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" 10 sdk "github.com/Finschia/finschia-sdk/types" 11 "github.com/Finschia/finschia-sdk/x/token" 12 ) 13 14 func TestValidateGenesis(t *testing.T) { 15 addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) 16 testCases := map[string]struct { 17 gs *token.GenesisState 18 valid bool 19 }{ 20 "default genesis": { 21 token.DefaultGenesisState(), 22 true, 23 }, 24 "invalid class nonce": { 25 &token.GenesisState{ 26 ClassState: &token.ClassGenesisState{ 27 Nonce: sdk.NewUint(math.MaxUint64).Incr(), 28 }, 29 }, 30 false, 31 }, 32 "balances of invalid contract id": { 33 &token.GenesisState{ 34 Balances: []token.ContractBalances{ 35 { 36 Balances: []token.Balance{ 37 { 38 Address: addr.String(), 39 Amount: sdk.OneInt(), 40 }, 41 }, 42 }, 43 }, 44 }, 45 false, 46 }, 47 "empty tokens in a balance": { 48 &token.GenesisState{ 49 Balances: []token.ContractBalances{ 50 { 51 ContractId: "deadbeef", 52 }, 53 }, 54 }, 55 false, 56 }, 57 "invalid address in a balance": { 58 &token.GenesisState{ 59 Balances: []token.ContractBalances{ 60 { 61 ContractId: "deadbeef", 62 Balances: []token.Balance{ 63 { 64 Amount: sdk.OneInt(), 65 }, 66 }, 67 }, 68 }, 69 }, 70 false, 71 }, 72 "invalid amount in a balance": { 73 &token.GenesisState{ 74 Balances: []token.ContractBalances{ 75 { 76 ContractId: "deadbeef", 77 Balances: []token.Balance{ 78 { 79 Address: addr.String(), 80 Amount: sdk.ZeroInt(), 81 }, 82 }, 83 }, 84 }, 85 }, 86 false, 87 }, 88 "invalid id of class": { 89 &token.GenesisState{ 90 Classes: []token.Contract{{ 91 Name: "test", 92 Symbol: "TT", 93 }}, 94 }, 95 false, 96 }, 97 "invalid name of class": { 98 &token.GenesisState{ 99 Classes: []token.Contract{{ 100 Id: "deadbeef", 101 Name: string(make([]rune, 21)), 102 Symbol: "TT", 103 }}, 104 }, 105 false, 106 }, 107 "invalid symbol of class": { 108 &token.GenesisState{ 109 Classes: []token.Contract{{ 110 Id: "deadbeef", 111 Name: "test", 112 Symbol: "tt", 113 }}, 114 }, 115 false, 116 }, 117 "invalid image uri of class": { 118 &token.GenesisState{ 119 Classes: []token.Contract{{ 120 Id: "deadbeef", 121 Name: "test", 122 Symbol: "TT", 123 Uri: string(make([]rune, 1001)), 124 }}, 125 }, 126 false, 127 }, 128 "invalid meta of class": { 129 &token.GenesisState{ 130 Classes: []token.Contract{{ 131 Id: "deadbeef", 132 Name: "test", 133 Symbol: "TT", 134 Meta: string(make([]rune, 1001)), 135 }}, 136 }, 137 false, 138 }, 139 "invalid decimals of class": { 140 &token.GenesisState{ 141 Classes: []token.Contract{{ 142 Id: "deadbeef", 143 Name: "test", 144 Symbol: "TT", 145 Decimals: -1, 146 }}, 147 }, 148 false, 149 }, 150 "grants of invalid contract id": { 151 &token.GenesisState{ 152 Grants: []token.ContractGrants{{ 153 Grants: []token.Grant{{ 154 Grantee: addr.String(), 155 Permission: token.PermissionMint, 156 }}, 157 }}, 158 }, 159 false, 160 }, 161 "empty grants": { 162 &token.GenesisState{ 163 Grants: []token.ContractGrants{{ 164 ContractId: "deadbeef", 165 }}, 166 }, 167 false, 168 }, 169 "invalid grantee of grant": { 170 &token.GenesisState{ 171 Grants: []token.ContractGrants{{ 172 ContractId: "deadbeef", 173 Grants: []token.Grant{{ 174 Permission: token.PermissionMint, 175 }}, 176 }}, 177 }, 178 false, 179 }, 180 "invalid action of grant": { 181 &token.GenesisState{ 182 Grants: []token.ContractGrants{{ 183 ContractId: "deadbeef", 184 Grants: []token.Grant{{ 185 Grantee: addr.String(), 186 }}, 187 }}, 188 }, 189 false, 190 }, 191 "authorizations of invalid contract id": { 192 &token.GenesisState{ 193 Authorizations: []token.ContractAuthorizations{{ 194 Authorizations: []token.Authorization{{ 195 Holder: addr.String(), 196 Operator: addr.String(), 197 }}, 198 }}, 199 }, 200 false, 201 }, 202 "empty authorizations": { 203 &token.GenesisState{ 204 Authorizations: []token.ContractAuthorizations{{ 205 ContractId: "deadbeef", 206 }}, 207 }, 208 false, 209 }, 210 "invalid holder of authorization": { 211 &token.GenesisState{ 212 Authorizations: []token.ContractAuthorizations{{ 213 ContractId: "deadbeef", 214 Authorizations: []token.Authorization{{ 215 Operator: addr.String(), 216 }}, 217 }}, 218 }, 219 false, 220 }, 221 "invalid operator of authorization": { 222 &token.GenesisState{ 223 Authorizations: []token.ContractAuthorizations{{ 224 ContractId: "deadbeef", 225 Authorizations: []token.Authorization{{ 226 Holder: addr.String(), 227 }}, 228 }}, 229 }, 230 false, 231 }, 232 } 233 234 for name, tc := range testCases { 235 t.Run(name, func(t *testing.T) { 236 err := token.ValidateGenesis(*tc.gs) 237 if !tc.valid { 238 require.Error(t, err) 239 return 240 } 241 require.NoError(t, err) 242 }) 243 } 244 }