Skip to content

Commit

Permalink
chore: no customized process function before validate
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh committed Aug 19, 2024
1 parent d2402db commit f26c028
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 56 deletions.
22 changes: 4 additions & 18 deletions pkg/remote/codec/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ type PayloadValidator interface {
// Validate validates the input payload with the attached checksum.
// Return pass if validation succeed, or return error.
Validate(ctx context.Context, expectedValue string, inboundPayload []byte) (pass bool, err error)

// ProcessBeforeValidate is used to do some preprocess before validate.
// For example, you can extract some value from ttheader and set to the rpcinfo, which may be useful for validation.
ProcessBeforeValidate(ctx context.Context, message remote.Message) (context.Context, error)
}

func getValidatorKey(ctx context.Context, p PayloadValidator) string {
Expand Down Expand Up @@ -79,12 +75,8 @@ func payloadChecksumValidate(ctx context.Context, pv PayloadValidator, in remote
rpcinfo.Record(ctx, message.RPCInfo(), stats.ChecksumValidateFinish, err)
}()

// this return ctx can only be used in Validate part since Decode has no return argument for context
ctx = fillRPCInfoBeforeValidate(ctx, message)
// before validate
ctx, err = pv.ProcessBeforeValidate(ctx, message)
if err != nil {
return err
}

// get key and value
key := getValidatorKey(ctx, pv)
Expand Down Expand Up @@ -172,7 +164,7 @@ func (p *crcPayloadValidator) Key(ctx context.Context) string {
}

func (p *crcPayloadValidator) Generate(ctx context.Context, outPayload []byte) (need bool, value string, err error) {
return true, getCRC32C([][]byte{outPayload}), nil
return true, getCRC32C(outPayload), nil
}

func (p *crcPayloadValidator) Validate(ctx context.Context, expectedValue string, inputPayload []byte) (pass bool, err error) {
Expand All @@ -186,10 +178,6 @@ func (p *crcPayloadValidator) Validate(ctx context.Context, expectedValue string
return true, nil
}

func (p *crcPayloadValidator) ProcessBeforeValidate(ctx context.Context, message remote.Message) (context.Context, error) {
return ctx, nil
}

// crc32cTable is used for crc32c check
var (
crc32cTable *crc32.Table
Expand All @@ -198,15 +186,13 @@ var (

// getCRC32C calculates the crc32c checksum of the input bytes.
// the checksum will be converted into big-endian format and encoded into hex string.
func getCRC32C(payload [][]byte) string {
func getCRC32C(payload []byte) string {
if crc32cTable == nil {
return ""
}
csb := make([]byte, Size32)
var checksum uint32
for i := 0; i < len(payload); i++ {
checksum = crc32.Update(checksum, crc32cTable, payload[i])
}
checksum = crc32.Update(checksum, crc32cTable, payload)
binary.BigEndian.PutUint32(csb, checksum)
return hex.EncodeToString(csb)
}
Expand Down
41 changes: 3 additions & 38 deletions pkg/remote/codec/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ type mockPayloadValidator struct {
}

const (
mockGenerateErrorKey = "mockGenerateError"
mockExceedLimitKey = "mockExceedLimit"
mockPreprocessKey = "preprocessSucceedKey"
mockPreprocessFailedKey = "preprocessFailed"
mockGenerateErrorKey = "mockGenerateError"
mockExceedLimitKey = "mockExceedLimit"
)

type preprocessStruct struct {
value string
}

func (m *mockPayloadValidator) Key(ctx context.Context) string {
return "mockValidator"
}
Expand All @@ -52,17 +46,6 @@ func (m *mockPayloadValidator) Validate(ctx context.Context, expectedValue strin
return value == expectedValue, nil
}

func (m *mockPayloadValidator) ProcessBeforeValidate(ctx context.Context, message remote.Message) (context.Context, error) {
if l := ctx.Value(mockPreprocessFailedKey); l != nil {
return ctx, errors.New("mockPreprocessFailed")
}
s := ctx.Value(mockPreprocessKey)
if s != nil {
s.(*preprocessStruct).value = "123"
}
return ctx, nil
}

func TestPayloadValidator(t *testing.T) {
p := &mockPayloadValidator{}
payload := make([]byte, 0)
Expand All @@ -71,14 +54,7 @@ func TestPayloadValidator(t *testing.T) {
test.Assert(t, err == nil, err)
test.Assert(t, need)

ctx := context.Background()
s := &preprocessStruct{value: "1"}
ctx = context.WithValue(ctx, mockPreprocessKey, s)
ctx, err = p.ProcessBeforeValidate(ctx, nil)
test.Assert(t, err == nil, err)
test.Assert(t, s.value == "123")

pass, err := p.Validate(ctx, value, payload)
pass, err := p.Validate(context.Background(), value, payload)
test.Assert(t, err == nil, err)
test.Assert(t, pass, true)
}
Expand Down Expand Up @@ -133,11 +109,8 @@ func TestPayloadChecksumValidate(t *testing.T) {
message := initClientRecvMsg()
message.TransInfo().PutTransStrInfo(sendMsg.TransInfo().TransStrInfo()) // put header strinfo
message.SetPayloadLen(len(payload))
s := &preprocessStruct{value: "1"}
ctx = context.WithValue(context.Background(), mockPreprocessKey, s)
err = payloadChecksumValidate(ctx, pv, in, message)
test.Assert(t, err == nil, err)
test.Assert(t, s.value == "123")

// validate failed, checksum validation error
in = remote.NewReaderBuffer(payload)
Expand All @@ -146,12 +119,4 @@ func TestPayloadChecksumValidate(t *testing.T) {
message.SetPayloadLen(len(payload))
err = payloadChecksumValidate(context.Background(), pv, in, message)
test.Assert(t, err != nil)

// validate failed, preprocess error
ctx = context.WithValue(context.Background(), mockPreprocessFailedKey, true)
s = &preprocessStruct{value: "1"}
ctx = context.WithValue(ctx, mockPreprocessKey, s)
err = payloadChecksumValidate(ctx, pv, in, message)
test.Assert(t, err != nil)
test.Assert(t, s.value == "1") // will not be modified
}

0 comments on commit f26c028

Please sign in to comment.