Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueleresca/7984e9b877fc249bc2d2ceca1803dae8 to your computer and use it in GitHub Desktop.
Save samueleresca/7984e9b877fc249bc2d2ceca1803dae8 to your computer and use it in GitHub Desktop.
package v3rpc
import (
"context"
"testing"
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
txn "go.etcd.io/etcd/server/v3/etcdserver/txn"
"go.etcd.io/etcd/server/v3/lease"
betesting "go.etcd.io/etcd/server/v3/storage/backend/testing"
"go.etcd.io/etcd/server/v3/storage/mvcc"
"go.uber.org/zap/zaptest"
)
// FuzzTxnRangeRequest represents the fuzz test
func FuzzTxnRangeRequest(f *testing.F) {
testcases := []pb.RangeRequest{
{
Key: []byte{2},
RangeEnd: []byte{2},
Limit: 3,
Revision: 3,
SortOrder: 2,
SortTarget: 2,
},
}
for _, tc := range testcases {
soValue := pb.RangeRequest_SortOrder_value[tc.SortOrder.String()]
soTarget := pb.RangeRequest_SortTarget_value[tc.SortTarget.String()]
// Add the seed corpus
f.Add(tc.Key, tc.RangeEnd, tc.Limit, tc.Revision, soValue, soTarget)
}
// Definition of the fuzz target, the parameters assume a value based on the mutation of the seed corpus
f.Fuzz(func(t *testing.T,
key []byte,
rangeEnd []byte,
limit int64,
revision int64,
sortOrder int32,
sortTarget int32,
) {
fuzzRequest := &pb.RangeRequest {
Key: key,
RangeEnd: rangeEnd,
Limit: limit,
SortOrder: pb.RangeRequest_SortOrder(sortOrder),
SortTarget: pb.RangeRequest_SortTarget(sortTarget),
}
verifyCheck(t, func() error {
return checkRangeRequest(fuzzRequest)
})
execTransaction(t, &pb.RequestOp {
Request: &pb.RequestOp_RequestRange {
RequestRange: fuzzRequest,
},
})
})
}
func verifyCheck(t *testing.T, check func() error) {
errCheck := check()
if errCheck != nil {
t.Skip("Validation not passing. Skipping the apply.")
}
}
func execTransaction(t *testing.T, req *pb.RequestOp) {
b, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, b)
s := mvcc.NewStore(zaptest.NewLogger(t), b, &lease.FakeLessor{}, mvcc.StoreConfig{})
defer s.Close()
// setup cancelled context
ctx, cancel := context.WithCancel(context.TODO())
cancel()
request := &pb.TxnRequest{
Success: []*pb.RequestOp{req},
}
_, _, err := txn.Txn(ctx, zaptest.NewLogger(t), request, false, s, &lease.FakeLessor{})
if err != nil {
t.Skipf("Application erroring. %s", err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment