|
| 1 | +package aptos_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + aptoscap "github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/chain-capabilities/aptos" |
| 9 | + typesaptos "github.com/smartcontractkit/chainlink-common/pkg/types/chains/aptos" |
| 10 | +) |
| 11 | + |
| 12 | +func TestConvertViewPayloadFromProto_ConvertsNestedVectorStructAndGenericTags(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + payload, err := aptoscap.ConvertViewPayloadFromProto(&aptoscap.ViewPayload{ |
| 16 | + Module: &aptoscap.ModuleID{Address: []byte{0x01}, Name: "coin"}, |
| 17 | + Function: "name", |
| 18 | + ArgTypes: []*aptoscap.TypeTag{ |
| 19 | + { |
| 20 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_VECTOR, |
| 21 | + Value: &aptoscap.TypeTag_Vector{Vector: &aptoscap.VectorTag{ |
| 22 | + ElementType: &aptoscap.TypeTag{ |
| 23 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_STRUCT, |
| 24 | + Value: &aptoscap.TypeTag_Struct{Struct: &aptoscap.StructTag{ |
| 25 | + Address: []byte{0x02}, |
| 26 | + Module: "aptos_coin", |
| 27 | + Name: "Coin", |
| 28 | + TypeParams: []*aptoscap.TypeTag{ |
| 29 | + { |
| 30 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_GENERIC, |
| 31 | + Value: &aptoscap.TypeTag_Generic{Generic: &aptoscap.GenericTag{Index: 7}}, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }}, |
| 35 | + }, |
| 36 | + }}, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | + require.NoError(t, err) |
| 41 | + require.NotNil(t, payload) |
| 42 | + require.Equal(t, "name", payload.Function) |
| 43 | + require.Len(t, payload.ArgTypes, 1) |
| 44 | + |
| 45 | + vectorTag, ok := payload.ArgTypes[0].Value.(typesaptos.VectorTag) |
| 46 | + require.True(t, ok) |
| 47 | + structTag, ok := vectorTag.ElementType.Value.(typesaptos.StructTag) |
| 48 | + require.True(t, ok) |
| 49 | + require.Equal(t, "aptos_coin", structTag.Module) |
| 50 | + require.Equal(t, "Coin", structTag.Name) |
| 51 | + require.Len(t, structTag.TypeParams, 1) |
| 52 | + genericTag, ok := structTag.TypeParams[0].Value.(typesaptos.GenericTag) |
| 53 | + require.True(t, ok) |
| 54 | + require.EqualValues(t, 7, genericTag.Index) |
| 55 | +} |
| 56 | + |
| 57 | +func TestConvertViewPayloadFromProto_RejectsInvalidPayloadInputs(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + testCases := []struct { |
| 61 | + name string |
| 62 | + payload *aptoscap.ViewPayload |
| 63 | + wantErr string |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "missing payload", |
| 67 | + payload: nil, |
| 68 | + wantErr: "payload is required", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "missing module", |
| 72 | + payload: &aptoscap.ViewPayload{Function: "name"}, |
| 73 | + wantErr: "payload.module is required", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "missing module address", |
| 77 | + payload: &aptoscap.ViewPayload{ |
| 78 | + Module: &aptoscap.ModuleID{Name: "coin"}, |
| 79 | + Function: "name", |
| 80 | + }, |
| 81 | + wantErr: "payload.module.address is required", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "missing function", |
| 85 | + payload: &aptoscap.ViewPayload{ |
| 86 | + Module: &aptoscap.ModuleID{Address: []byte{0x01}, Name: "coin"}, |
| 87 | + }, |
| 88 | + wantErr: "payload.function is required", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "missing module name", |
| 92 | + payload: &aptoscap.ViewPayload{ |
| 93 | + Module: &aptoscap.ModuleID{Address: []byte{0x01}}, |
| 94 | + Function: "name", |
| 95 | + }, |
| 96 | + wantErr: "payload.module.name is required", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "oversized module address", |
| 100 | + payload: &aptoscap.ViewPayload{ |
| 101 | + Module: &aptoscap.ModuleID{Address: make([]byte, typesaptos.AccountAddressLength+1), Name: "coin"}, |
| 102 | + Function: "name", |
| 103 | + }, |
| 104 | + wantErr: "module address too long", |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tc := range testCases { |
| 109 | + tc := tc |
| 110 | + t.Run(tc.name, func(t *testing.T) { |
| 111 | + t.Parallel() |
| 112 | + |
| 113 | + _, err := aptoscap.ConvertViewPayloadFromProto(tc.payload) |
| 114 | + require.ErrorContains(t, err, tc.wantErr) |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestConvertTypeTagFromProto_RejectsInvalidInput(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + testCases := []struct { |
| 123 | + name string |
| 124 | + tag *aptoscap.TypeTag |
| 125 | + wantErr string |
| 126 | + }{ |
| 127 | + { |
| 128 | + name: "nil type tag", |
| 129 | + tag: nil, |
| 130 | + wantErr: "type tag is nil", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "unsupported kind", |
| 134 | + tag: &aptoscap.TypeTag{Kind: aptoscap.TypeTagKind(255)}, |
| 135 | + wantErr: "unsupported type tag kind", |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "missing struct address", |
| 139 | + tag: &aptoscap.TypeTag{ |
| 140 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_STRUCT, |
| 141 | + Value: &aptoscap.TypeTag_Struct{Struct: &aptoscap.StructTag{ |
| 142 | + Module: "coin", |
| 143 | + Name: "Coin", |
| 144 | + }}, |
| 145 | + }, |
| 146 | + wantErr: "struct address is required", |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "missing struct module", |
| 150 | + tag: &aptoscap.TypeTag{ |
| 151 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_STRUCT, |
| 152 | + Value: &aptoscap.TypeTag_Struct{Struct: &aptoscap.StructTag{ |
| 153 | + Address: []byte{0x01}, |
| 154 | + Name: "Coin", |
| 155 | + }}, |
| 156 | + }, |
| 157 | + wantErr: "struct module is required", |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "missing struct name", |
| 161 | + tag: &aptoscap.TypeTag{ |
| 162 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_STRUCT, |
| 163 | + Value: &aptoscap.TypeTag_Struct{Struct: &aptoscap.StructTag{ |
| 164 | + Address: []byte{0x01}, |
| 165 | + Module: "coin", |
| 166 | + }}, |
| 167 | + }, |
| 168 | + wantErr: "struct name is required", |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "oversized struct address", |
| 172 | + tag: &aptoscap.TypeTag{ |
| 173 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_STRUCT, |
| 174 | + Value: &aptoscap.TypeTag_Struct{Struct: &aptoscap.StructTag{ |
| 175 | + Address: make([]byte, typesaptos.AccountAddressLength+1), |
| 176 | + Module: "coin", |
| 177 | + Name: "Coin", |
| 178 | + }}, |
| 179 | + }, |
| 180 | + wantErr: "struct address too long", |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "invalid vector element type", |
| 184 | + tag: &aptoscap.TypeTag{ |
| 185 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_VECTOR, |
| 186 | + Value: &aptoscap.TypeTag_Vector{Vector: &aptoscap.VectorTag{ |
| 187 | + ElementType: &aptoscap.TypeTag{ |
| 188 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_STRUCT, |
| 189 | + Value: &aptoscap.TypeTag_Struct{Struct: &aptoscap.StructTag{ |
| 190 | + Address: make([]byte, typesaptos.AccountAddressLength+1), |
| 191 | + Module: "coin", |
| 192 | + Name: "Coin", |
| 193 | + }}, |
| 194 | + }, |
| 195 | + }}, |
| 196 | + }, |
| 197 | + wantErr: "invalid vector element type: struct address too long", |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "generic index out of range", |
| 201 | + tag: &aptoscap.TypeTag{ |
| 202 | + Kind: aptoscap.TypeTagKind_TYPE_TAG_KIND_GENERIC, |
| 203 | + Value: &aptoscap.TypeTag_Generic{Generic: &aptoscap.GenericTag{Index: 1 << 16}}, |
| 204 | + }, |
| 205 | + wantErr: "generic type index out of range", |
| 206 | + }, |
| 207 | + } |
| 208 | + |
| 209 | + for _, tc := range testCases { |
| 210 | + tc := tc |
| 211 | + t.Run(tc.name, func(t *testing.T) { |
| 212 | + t.Parallel() |
| 213 | + |
| 214 | + _, err := aptoscap.ConvertTypeTagFromProto(tc.tag) |
| 215 | + require.ErrorContains(t, err, tc.wantErr) |
| 216 | + }) |
| 217 | + } |
| 218 | +} |
0 commit comments