|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/cosmos/cosmos-sdk/types/address" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/tendermint/tendermint/crypto/ed25519" |
| 10 | + |
| 11 | + types "github.com/akash-network/akash-api/go/node/cert/v1beta3" |
| 12 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCertStateToPrefix(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + state types.Certificate_State |
| 19 | + expected []byte |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "valid certificate state", |
| 23 | + state: types.CertificateValid, |
| 24 | + expected: CertStateValidPrefix, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "revoked certificate state", |
| 28 | + state: types.CertificateRevoked, |
| 29 | + expected: CertStateRevokedPrefix, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + for _, tc := range tests { |
| 34 | + t.Run(tc.name, func(t *testing.T) { |
| 35 | + result := certStateToPrefix(tc.state) |
| 36 | + require.Equal(t, tc.expected, result) |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestCertStateToPrefixPanics(t *testing.T) { |
| 42 | + require.Panics(t, func() { |
| 43 | + certStateToPrefix(types.CertificateStateInvalid) |
| 44 | + }, "should panic for invalid certificate state") |
| 45 | +} |
| 46 | + |
| 47 | +func TestBuildCertPrefix(t *testing.T) { |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + state types.Certificate_State |
| 51 | + expected []byte |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "valid certificate state", |
| 55 | + state: types.CertificateValid, |
| 56 | + expected: append(CertPrefix, CertStateValidPrefix...), |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "revoked certificate state", |
| 60 | + state: types.CertificateRevoked, |
| 61 | + expected: append(CertPrefix, CertStateRevokedPrefix...), |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + result := buildCertPrefix(tc.state) |
| 68 | + require.Equal(t, tc.expected, result) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestBuildCertPrefixPanics(t *testing.T) { |
| 74 | + require.Panics(t, func() { |
| 75 | + buildCertPrefix(types.CertificateStateInvalid) |
| 76 | + }, "should panic for invalid certificate state") |
| 77 | +} |
| 78 | + |
| 79 | +func TestCertificateKey(t *testing.T) { |
| 80 | + owner := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) |
| 81 | + serial := big.NewInt(123) |
| 82 | + |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + state types.Certificate_State |
| 86 | + certID types.CertID |
| 87 | + expected []byte |
| 88 | + wantErr bool |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "valid certificate", |
| 92 | + state: types.CertificateValid, |
| 93 | + certID: types.CertID{ |
| 94 | + Owner: owner, |
| 95 | + Serial: *serial, |
| 96 | + }, |
| 97 | + wantErr: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "revoked certificate", |
| 101 | + state: types.CertificateRevoked, |
| 102 | + certID: types.CertID{ |
| 103 | + Owner: owner, |
| 104 | + Serial: *serial, |
| 105 | + }, |
| 106 | + wantErr: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "empty owner", |
| 110 | + state: types.CertificateValid, |
| 111 | + certID: types.CertID{ |
| 112 | + Owner: sdk.AccAddress{}, |
| 113 | + Serial: *serial, |
| 114 | + }, |
| 115 | + wantErr: true, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tc := range tests { |
| 120 | + t.Run(tc.name, func(t *testing.T) { |
| 121 | + result, err := CertificateKey(tc.state, tc.certID) |
| 122 | + if tc.wantErr { |
| 123 | + require.Error(t, err) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + require.NoError(t, err) |
| 128 | + require.NotEmpty(t, result) |
| 129 | + |
| 130 | + // Verify the key structure |
| 131 | + state, parsedID, err := ParseCertKey(result) |
| 132 | + require.NoError(t, err) |
| 133 | + require.Equal(t, tc.state, state) |
| 134 | + require.Equal(t, tc.certID.Owner, parsedID.Owner) |
| 135 | + require.Equal(t, tc.certID.Serial.String(), parsedID.Serial.String()) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestCertificateKeyRaw(t *testing.T) { |
| 141 | + owner := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) |
| 142 | + serial := big.NewInt(123) |
| 143 | + |
| 144 | + tests := []struct { |
| 145 | + name string |
| 146 | + state types.Certificate_State |
| 147 | + certID types.CertID |
| 148 | + expected []byte |
| 149 | + wantErr bool |
| 150 | + }{ |
| 151 | + { |
| 152 | + name: "valid key", |
| 153 | + state: types.CertificateValid, |
| 154 | + certID: types.CertID{ |
| 155 | + Owner: owner, |
| 156 | + Serial: *serial, |
| 157 | + }, |
| 158 | + expected: append(append(append(CertPrefix, CertStateValidPrefix...), address.MustLengthPrefix(owner.Bytes())...), mustSerialPrefix(serial.Bytes())...), |
| 159 | + wantErr: false, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "valid key 2", |
| 163 | + state: types.CertificateRevoked, |
| 164 | + certID: types.CertID{ |
| 165 | + Owner: owner, |
| 166 | + Serial: *serial, |
| 167 | + }, |
| 168 | + expected: append(append(append(CertPrefix, CertStateRevokedPrefix...), address.MustLengthPrefix(owner.Bytes())...), mustSerialPrefix(serial.Bytes())...), |
| 169 | + wantErr: false, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tc := range tests { |
| 174 | + t.Run(tc.name, func(t *testing.T) { |
| 175 | + result, err := CertificateKey(tc.state, tc.certID) |
| 176 | + if tc.wantErr { |
| 177 | + require.Error(t, err) |
| 178 | + return |
| 179 | + } |
| 180 | + |
| 181 | + require.NoError(t, err) |
| 182 | + require.NotEmpty(t, result) |
| 183 | + require.Equal(t, tc.expected, result) |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func TestMustCertificateKey(t *testing.T) { |
| 189 | + owner := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) |
| 190 | + serial := big.NewInt(123) |
| 191 | + validID := types.CertID{ |
| 192 | + Owner: owner, |
| 193 | + Serial: *serial, |
| 194 | + } |
| 195 | + |
| 196 | + t.Run("valid case", func(t *testing.T) { |
| 197 | + require.NotPanics(t, func() { |
| 198 | + key := MustCertificateKey(types.CertificateValid, validID) |
| 199 | + require.NotEmpty(t, key) |
| 200 | + }) |
| 201 | + }) |
| 202 | + |
| 203 | + t.Run("panic on invalid input", func(t *testing.T) { |
| 204 | + require.Panics(t, func() { |
| 205 | + MustCertificateKey(types.CertificateValid, types.CertID{}) |
| 206 | + }) |
| 207 | + }) |
| 208 | +} |
| 209 | + |
| 210 | +func TestParseCertKey(t *testing.T) { |
| 211 | + owner := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) |
| 212 | + serial := big.NewInt(123) |
| 213 | + certID := types.CertID{ |
| 214 | + Owner: owner, |
| 215 | + Serial: *serial, |
| 216 | + } |
| 217 | + |
| 218 | + tests := []struct { |
| 219 | + name string |
| 220 | + key []byte |
| 221 | + state types.Certificate_State |
| 222 | + certID types.CertID |
| 223 | + wantErr bool |
| 224 | + }{ |
| 225 | + { |
| 226 | + name: "valid key - valid state", |
| 227 | + key: MustCertificateKey(types.CertificateValid, certID), |
| 228 | + state: types.CertificateValid, |
| 229 | + certID: certID, |
| 230 | + wantErr: false, |
| 231 | + }, |
| 232 | + { |
| 233 | + name: "valid key - revoked state", |
| 234 | + key: MustCertificateKey(types.CertificateRevoked, certID), |
| 235 | + state: types.CertificateRevoked, |
| 236 | + certID: certID, |
| 237 | + wantErr: false, |
| 238 | + }, |
| 239 | + { |
| 240 | + name: "invalid key - too short", |
| 241 | + key: []byte{0x11}, |
| 242 | + wantErr: true, |
| 243 | + }, |
| 244 | + { |
| 245 | + name: "invalid key - wrong prefix", |
| 246 | + key: []byte{0x12, 0x01, 0x20}, |
| 247 | + wantErr: true, |
| 248 | + }, |
| 249 | + { |
| 250 | + name: "invalid key - invalid address length", |
| 251 | + key: append(append(CertPrefix, CertStateValidPrefix...), 0xFF), |
| 252 | + wantErr: true, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "invalid key - malformed address", |
| 256 | + key: append(append(CertPrefix, CertStateValidPrefix...), 0x01, 0x00), |
| 257 | + wantErr: true, |
| 258 | + }, |
| 259 | + { |
| 260 | + name: "invalid key - malformed address 2", |
| 261 | + key: append(append(CertPrefix, CertStateValidPrefix...), 0x13, 0x00), |
| 262 | + wantErr: true, |
| 263 | + }, |
| 264 | + { |
| 265 | + name: "invalid key - malformed address 2", |
| 266 | + key: append(append(CertPrefix, CertStateValidPrefix...), 0x04, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00), |
| 267 | + wantErr: true, |
| 268 | + }, |
| 269 | + } |
| 270 | + |
| 271 | + for _, tc := range tests { |
| 272 | + t.Run(tc.name, func(t *testing.T) { |
| 273 | + state, parsedID, err := ParseCertKey(tc.key) |
| 274 | + if tc.wantErr { |
| 275 | + require.Error(t, err) |
| 276 | + return |
| 277 | + } |
| 278 | + |
| 279 | + require.NoError(t, err) |
| 280 | + require.Equal(t, tc.state, state) |
| 281 | + require.Equal(t, tc.certID.Owner.String(), parsedID.Owner.String()) |
| 282 | + require.Equal(t, tc.certID.Serial.String(), parsedID.Serial.String()) |
| 283 | + }) |
| 284 | + } |
| 285 | +} |
0 commit comments