src/archive/tar/fuzz_test.go GO 81 lines View on github.com → Search inside
1// Copyright 2021 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package tar67import (8	"bytes"9	"io"10	"testing"11)1213func FuzzReader(f *testing.F) {14	b := bytes.NewBuffer(nil)15	w := NewWriter(b)16	inp := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")17	err := w.WriteHeader(&Header{18		Name: "lorem.txt",19		Mode: 0600,20		Size: int64(len(inp)),21	})22	if err != nil {23		f.Fatalf("failed to create writer: %s", err)24	}25	_, err = w.Write(inp)26	if err != nil {27		f.Fatalf("failed to write file to archive: %s", err)28	}29	if err := w.Close(); err != nil {30		f.Fatalf("failed to write archive: %s", err)31	}32	f.Add(b.Bytes())3334	f.Fuzz(func(t *testing.T, b []byte) {35		r := NewReader(bytes.NewReader(b))36		type file struct {37			header  *Header38			content []byte39		}40		files := []file{}41		for {42			hdr, err := r.Next()43			if err == io.EOF {44				break45			}46			if err != nil {47				return48			}49			buf := bytes.NewBuffer(nil)50			if _, err := io.Copy(buf, r); err != nil {51				continue52			}53			files = append(files, file{header: hdr, content: buf.Bytes()})54		}5556		// If we were unable to read anything out of the archive don't57		// bother trying to roundtrip it.58		if len(files) == 0 {59			return60		}6162		out := bytes.NewBuffer(nil)63		w := NewWriter(out)64		for _, f := range files {65			if err := w.WriteHeader(f.header); err != nil {66				t.Fatalf("unable to write previously parsed header: %s", err)67			}68			if _, err := w.Write(f.content); err != nil {69				t.Fatalf("unable to write previously parsed content: %s", err)70			}71		}72		if err := w.Close(); err != nil {73			t.Fatalf("Unable to write archive: %s", err)74		}7576		// TODO: We may want to check if the archive roundtrips. This would require77		// taking into account addition of the two zero trailer blocks that Writer.Close78		// appends.79	})80}

Code quality findings 1

Ensure errors are handled or logged
warning correctness unhandled-error
if err != nil {

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.