@@ -1014,128 +1014,134 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
1014
1014
cases := []struct {
1015
1015
decodeType string
1016
1016
inputValue * big.Int
1017
- err error
1017
+ unpackErr error
1018
+ packErr error
1018
1019
expectValue interface {}
1019
1020
}{
1020
1021
{
1021
1022
decodeType : "uint8" ,
1022
1023
inputValue : big .NewInt (math .MaxUint8 + 1 ),
1023
- err : errBadUint8 ,
1024
+ unpackErr : errBadUint8 ,
1024
1025
},
1025
1026
{
1026
1027
decodeType : "uint8" ,
1027
1028
inputValue : big .NewInt (math .MaxUint8 ),
1028
- err : nil ,
1029
+ unpackErr : nil ,
1029
1030
expectValue : uint8 (math .MaxUint8 ),
1030
1031
},
1031
1032
{
1032
1033
decodeType : "uint16" ,
1033
1034
inputValue : big .NewInt (math .MaxUint16 + 1 ),
1034
- err : errBadUint16 ,
1035
+ unpackErr : errBadUint16 ,
1035
1036
},
1036
1037
{
1037
1038
decodeType : "uint16" ,
1038
1039
inputValue : big .NewInt (math .MaxUint16 ),
1039
- err : nil ,
1040
+ unpackErr : nil ,
1040
1041
expectValue : uint16 (math .MaxUint16 ),
1041
1042
},
1042
1043
{
1043
1044
decodeType : "uint32" ,
1044
1045
inputValue : big .NewInt (math .MaxUint32 + 1 ),
1045
- err : errBadUint32 ,
1046
+ unpackErr : errBadUint32 ,
1046
1047
},
1047
1048
{
1048
1049
decodeType : "uint32" ,
1049
1050
inputValue : big .NewInt (math .MaxUint32 ),
1050
- err : nil ,
1051
+ unpackErr : nil ,
1051
1052
expectValue : uint32 (math .MaxUint32 ),
1052
1053
},
1053
1054
{
1054
1055
decodeType : "uint64" ,
1055
1056
inputValue : maxU64Plus1 ,
1056
- err : errBadUint64 ,
1057
+ unpackErr : errBadUint64 ,
1057
1058
},
1058
1059
{
1059
1060
decodeType : "uint64" ,
1060
1061
inputValue : maxU64 ,
1061
- err : nil ,
1062
+ unpackErr : nil ,
1062
1063
expectValue : uint64 (math .MaxUint64 ),
1063
1064
},
1064
1065
{
1065
1066
decodeType : "uint256" ,
1066
1067
inputValue : maxU64Plus1 ,
1067
- err : nil ,
1068
+ unpackErr : nil ,
1068
1069
expectValue : maxU64Plus1 ,
1069
1070
},
1070
1071
{
1071
1072
decodeType : "int8" ,
1072
1073
inputValue : big .NewInt (math .MaxInt8 + 1 ),
1073
- err : errBadInt8 ,
1074
+ unpackErr : errBadInt8 ,
1074
1075
},
1075
1076
{
1076
- decodeType : "int8" ,
1077
1077
inputValue : big .NewInt (math .MinInt8 - 1 ),
1078
- err : errBadInt8 ,
1078
+ packErr : errInvalidSign ,
1079
1079
},
1080
1080
{
1081
1081
decodeType : "int8" ,
1082
1082
inputValue : big .NewInt (math .MaxInt8 ),
1083
- err : nil ,
1083
+ unpackErr : nil ,
1084
1084
expectValue : int8 (math .MaxInt8 ),
1085
1085
},
1086
1086
{
1087
1087
decodeType : "int16" ,
1088
1088
inputValue : big .NewInt (math .MaxInt16 + 1 ),
1089
- err : errBadInt16 ,
1089
+ unpackErr : errBadInt16 ,
1090
1090
},
1091
1091
{
1092
- decodeType : "int16" ,
1093
1092
inputValue : big .NewInt (math .MinInt16 - 1 ),
1094
- err : errBadInt16 ,
1093
+ packErr : errInvalidSign ,
1095
1094
},
1096
1095
{
1097
1096
decodeType : "int16" ,
1098
1097
inputValue : big .NewInt (math .MaxInt16 ),
1099
- err : nil ,
1098
+ unpackErr : nil ,
1100
1099
expectValue : int16 (math .MaxInt16 ),
1101
1100
},
1102
1101
{
1103
1102
decodeType : "int32" ,
1104
1103
inputValue : big .NewInt (math .MaxInt32 + 1 ),
1105
- err : errBadInt32 ,
1104
+ unpackErr : errBadInt32 ,
1106
1105
},
1107
1106
{
1108
- decodeType : "int32" ,
1109
1107
inputValue : big .NewInt (math .MinInt32 - 1 ),
1110
- err : errBadInt32 ,
1108
+ packErr : errInvalidSign ,
1111
1109
},
1112
1110
{
1113
1111
decodeType : "int32" ,
1114
1112
inputValue : big .NewInt (math .MaxInt32 ),
1115
- err : nil ,
1113
+ unpackErr : nil ,
1116
1114
expectValue : int32 (math .MaxInt32 ),
1117
1115
},
1118
1116
{
1119
1117
decodeType : "int64" ,
1120
1118
inputValue : new (big.Int ).Add (big .NewInt (math .MaxInt64 ), big .NewInt (1 )),
1121
- err : errBadInt64 ,
1119
+ unpackErr : errBadInt64 ,
1122
1120
},
1123
1121
{
1124
- decodeType : "int64" ,
1125
1122
inputValue : new (big.Int ).Sub (big .NewInt (math .MinInt64 ), big .NewInt (1 )),
1126
- err : errBadInt64 ,
1123
+ packErr : errInvalidSign ,
1127
1124
},
1128
1125
{
1129
1126
decodeType : "int64" ,
1130
1127
inputValue : big .NewInt (math .MaxInt64 ),
1131
- err : nil ,
1128
+ unpackErr : nil ,
1132
1129
expectValue : int64 (math .MaxInt64 ),
1133
1130
},
1134
1131
}
1135
1132
for i , testCase := range cases {
1136
1133
packed , err := encodeABI .Pack (testCase .inputValue )
1137
- if err != nil {
1138
- panic (err )
1134
+ if testCase .packErr != nil {
1135
+ if err == nil {
1136
+ t .Fatalf ("expected packing of testcase input value to fail" )
1137
+ }
1138
+ if err != testCase .packErr {
1139
+ t .Fatalf ("expected error '%v', got '%v'" , testCase .packErr , err )
1140
+ }
1141
+ continue
1142
+ }
1143
+ if err != nil && err != testCase .packErr {
1144
+ panic (fmt .Errorf ("unexpected error packing test-case input: %v" , err ))
1139
1145
}
1140
1146
ty , err := NewType (testCase .decodeType , "" , nil )
1141
1147
if err != nil {
@@ -1145,8 +1151,8 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
1145
1151
{Type : ty },
1146
1152
}
1147
1153
decoded , err := decodeABI .Unpack (packed )
1148
- if err != testCase .err {
1149
- t .Fatalf ("Expected error %v, actual error %v. case %d" , testCase .err , err , i )
1154
+ if err != testCase .unpackErr {
1155
+ t .Fatalf ("Expected error %v, actual error %v. case %d" , testCase .unpackErr , err , i )
1150
1156
}
1151
1157
if err != nil {
1152
1158
continue
0 commit comments