1// Copyright 2021 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15 16package wal 17 18import ( 19 "bytes" 20 "io/ioutil" 21 "os" 22 23 "go.etcd.io/etcd/raft/v3/raftpb" 24 "go.etcd.io/etcd/server/v3/storage/wal/walpb" 25 "go.uber.org/zap" 26) 27 28func FuzzWalCreate(data []byte) int { 29 p, err := ioutil.TempDir("/tmp", "waltest") 30 if err != nil { 31 return -1 32 } 33 defer os.RemoveAll(p) 34 w, err := Create(zap.NewExample(), p, data) 35 if err != nil { 36 return 0 37 } 38 if err = w.SaveSnapshot(walpb.Snapshot{}); err != nil { 39 return 0 40 } 41 if err = w.Save(raftpb.HardState{}, []raftpb.Entry{{Index: 0}}); err != nil { 42 return 0 43 } 44 w.Close() 45 neww, err := Open(zap.NewExample(), p, walpb.Snapshot{}) 46 if err != nil { 47 return 0 48 } 49 defer neww.Close() 50 metadata, _, _, err := neww.ReadAll() 51 if err != nil { 52 return 0 53 } 54 if !bytes.Equal(data, metadata) { 55 panic("data and metadata are not equal, but they should be") 56 } 57 return 1 58} 59