github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/types/msg_test.go (about) 1 package types 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/suite" 7 8 "github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1" 9 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 10 11 "github.com/ethereum/go-ethereum/common" 12 "github.com/ethereum/go-ethereum/crypto" 13 ) 14 15 type MsgsTestSuite struct { 16 suite.Suite 17 contract common.Address 18 deployer sdk.AccAddress 19 deployerStr string 20 withdrawerStr string 21 } 22 23 func TestMsgsTestSuite(t *testing.T) { 24 suite.Run(t, new(MsgsTestSuite)) 25 } 26 27 func (suite *MsgsTestSuite) SetupTest() { 28 deployer := ethsecp256k1.GenerateAddress() 29 suite.contract = crypto.CreateAddress(deployer, 1) 30 suite.deployer = sdk.AccAddress(deployer.Bytes()) 31 suite.deployerStr = suite.deployer.String() 32 suite.withdrawerStr = sdk.AccAddress(ethsecp256k1.GenerateAddress().Bytes()).String() 33 } 34 35 func (suite *MsgsTestSuite) TestMsgRegisterFeeSplitGetters() { 36 msgInvalid := MsgRegisterFeeSplit{} 37 msg := NewMsgRegisterFeeSplit( 38 suite.contract, 39 suite.deployer, 40 suite.deployer, 41 []uint64{1}, 42 ) 43 suite.Require().Equal(RouterKey, msg.Route()) 44 suite.Require().Equal(TypeMsgRegisterFeeSplit, msg.Type()) 45 suite.Require().NotNil(msgInvalid.GetSignBytes()) 46 suite.Require().NotNil(msg.GetSigners()) 47 } 48 49 func (suite *MsgsTestSuite) TestMsgRegisterFeeSplitNew() { 50 testCases := []struct { 51 msg string 52 contract string 53 deployer string 54 withdraw string 55 nonces []uint64 56 expectPass bool 57 }{ 58 { 59 "pass", 60 suite.contract.String(), 61 suite.deployerStr, 62 suite.withdrawerStr, 63 []uint64{1}, 64 true, 65 }, 66 { 67 "pass - empty withdrawer address", 68 suite.contract.String(), 69 suite.deployerStr, 70 "", 71 []uint64{1}, 72 true, 73 }, 74 { 75 "pass - same withdrawer and deployer address", 76 suite.contract.String(), 77 suite.deployerStr, 78 suite.deployerStr, 79 []uint64{1}, 80 true, 81 }, 82 { 83 "invalid contract address", 84 "", 85 suite.deployerStr, 86 suite.withdrawerStr, 87 []uint64{1}, 88 false, 89 }, 90 { 91 "must not be zero: invalid contract address", 92 "0x0000000000000000000000000000000000000000", 93 suite.deployerStr, 94 suite.withdrawerStr, 95 []uint64{1}, 96 false, 97 }, 98 { 99 "invalid deployer address", 100 suite.contract.String(), 101 "", 102 suite.withdrawerStr, 103 []uint64{1}, 104 false, 105 }, 106 { 107 "invalid withdraw address", 108 suite.contract.String(), 109 suite.deployerStr, 110 "withdraw", 111 []uint64{1}, 112 false, 113 }, 114 { 115 "invalid withdraw address", 116 suite.contract.String(), 117 suite.deployerStr, 118 "0x0123456789", 119 []uint64{1}, 120 false, 121 }, 122 { 123 "invalid nonces", 124 suite.contract.String(), 125 suite.deployerStr, 126 suite.withdrawerStr, 127 []uint64{}, 128 false, 129 }, 130 { 131 "invalid nonces - array length must be less than 20", 132 suite.contract.String(), 133 suite.deployerStr, 134 suite.withdrawerStr, 135 []uint64{1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 136 false, 137 }, 138 } 139 140 for i, tc := range testCases { 141 tx := MsgRegisterFeeSplit{ 142 ContractAddress: tc.contract, 143 DeployerAddress: tc.deployer, 144 WithdrawerAddress: tc.withdraw, 145 Nonces: tc.nonces, 146 } 147 err := tx.ValidateBasic() 148 149 if tc.expectPass { 150 suite.Require().NoError(err, "valid test %d failed: %s", i, tc.msg) 151 } else { 152 suite.Require().Error(err, "invalid test %d passed: %s", i, tc.msg) 153 suite.Require().Contains(err.Error(), tc.msg) 154 } 155 } 156 } 157 158 func (suite *MsgsTestSuite) TestMsgCancelFeeSplitGetters() { 159 msgInvalid := MsgCancelFeeSplit{} 160 msg := NewMsgCancelFeeSplit( 161 suite.contract, 162 sdk.AccAddress(suite.deployer.Bytes()), 163 ) 164 suite.Require().Equal(RouterKey, msg.Route()) 165 suite.Require().Equal(TypeMsgCancelFeeSplit, msg.Type()) 166 suite.Require().NotNil(msgInvalid.GetSignBytes()) 167 suite.Require().NotNil(msg.GetSigners()) 168 } 169 170 func (suite *MsgsTestSuite) TestMsgCancelFeeSplitNew() { 171 testCases := []struct { 172 msg string 173 contract string 174 deployer string 175 expectPass bool 176 }{ 177 { 178 "msg cancel contract fee - pass", 179 suite.contract.String(), 180 suite.deployerStr, 181 true, 182 }, 183 { 184 "invalid contract address", 185 "", 186 suite.deployerStr, 187 false, 188 }, 189 { 190 "must not be zero: invalid contract address", 191 "0x0000000000000000000000000000000000000000", 192 suite.deployerStr, 193 false, 194 }, 195 { 196 "invalid deployer address", 197 suite.contract.String(), 198 "", 199 false, 200 }, 201 } 202 203 for i, tc := range testCases { 204 tx := MsgCancelFeeSplit{ 205 ContractAddress: tc.contract, 206 DeployerAddress: tc.deployer, 207 } 208 err := tx.ValidateBasic() 209 210 if tc.expectPass { 211 suite.Require().NoError(err, "valid test %d failed: %s, %v", i, tc.msg) 212 } else { 213 suite.Require().Error(err, "invalid test %d passed: %s, %v", i, tc.msg) 214 suite.Require().Contains(err.Error(), tc.msg) 215 } 216 } 217 } 218 219 func (suite *MsgsTestSuite) TestMsgUpdateFeeSplitGetters() { 220 msgInvalid := MsgUpdateFeeSplit{} 221 msg := NewMsgUpdateFeeSplit( 222 suite.contract, 223 sdk.AccAddress(suite.deployer.Bytes()), 224 sdk.AccAddress(suite.deployer.Bytes()), 225 ) 226 suite.Require().Equal(RouterKey, msg.Route()) 227 suite.Require().Equal(TypeMsgUpdateFeeSplit, msg.Type()) 228 suite.Require().NotNil(msgInvalid.GetSignBytes()) 229 suite.Require().NotNil(msg.GetSigners()) 230 } 231 232 func (suite *MsgsTestSuite) TestMsgUpdateFeeSplitNew() { 233 withdrawerStr := sdk.AccAddress(ethsecp256k1.GenerateAddress().Bytes()).String() 234 testCases := []struct { 235 msg string 236 contract string 237 deployer string 238 withdraw string 239 expectPass bool 240 }{ 241 { 242 "msg update fee - pass", 243 suite.contract.String(), 244 suite.deployerStr, 245 withdrawerStr, 246 true, 247 }, 248 { 249 "invalid contract address", 250 "", 251 suite.deployerStr, 252 withdrawerStr, 253 false, 254 }, 255 { 256 "must not be zero: invalid contract address", 257 "0x0000000000000000000000000000000000000000", 258 suite.deployerStr, 259 withdrawerStr, 260 false, 261 }, 262 { 263 "invalid deployer address", 264 suite.contract.String(), 265 "", 266 suite.deployerStr, 267 false, 268 }, 269 { 270 "invalid withdraw address", 271 suite.contract.String(), 272 suite.deployerStr, 273 "withdraw", 274 false, 275 }, 276 { 277 "change fee withdrawer to deployer - pass", 278 suite.contract.String(), 279 suite.deployerStr, 280 suite.deployerStr, 281 true, 282 }, 283 } 284 285 for i, tc := range testCases { 286 tx := MsgUpdateFeeSplit{ 287 ContractAddress: tc.contract, 288 DeployerAddress: tc.deployer, 289 WithdrawerAddress: tc.withdraw, 290 } 291 err := tx.ValidateBasic() 292 293 if tc.expectPass { 294 suite.Require().NoError(err, "valid test %d failed: %s, %v", i, tc.msg) 295 } else { 296 suite.Require().Error(err, "invalid test %d passed: %s, %v", i, tc.msg) 297 suite.Require().Contains(err.Error(), tc.msg) 298 } 299 } 300 }