yellow.git


yellow

~ opinionated web scanning pipeline for pentesters in a hurry
git clone https://github.com/djnnvx/yellow.git
log | files | refs | archive | README | LICENSE

io_test.go (1690B)

      1 package core
      2 
      3 import (
      4 	"errors"
      5 	"os"
      6 	"path/filepath"
      7 	"strings"
      8 	"testing"
      9 )
     10 
     11 func TestWriteLines(t *testing.T) {
     12 	tests := []struct {
     13 		name  string
     14 		lines []string
     15 		want  string
     16 	}{
     17 		{name: "normal", lines: []string{"a", "b", "c"}, want: "a\nb\nc\n"},
     18 		{name: "single", lines: []string{"only"}, want: "only\n"},
     19 		{name: "empty", lines: nil, want: ""},
     20 	}
     21 
     22 	for _, tt := range tests {
     23 		t.Run(tt.name, func(t *testing.T) {
     24 			path := filepath.Join(t.TempDir(), "out.txt")
     25 			if err := WriteLines(path, tt.lines); err != nil {
     26 				t.Fatalf("WriteLines: %v", err)
     27 			}
     28 			got, err := os.ReadFile(path)
     29 			if err != nil {
     30 				t.Fatalf("read back: %v", err)
     31 			}
     32 			if string(got) != tt.want {
     33 				t.Errorf("got %q, want %q", string(got), tt.want)
     34 			}
     35 		})
     36 	}
     37 }
     38 
     39 func TestSaveStreamSuccess(t *testing.T) {
     40 	path := filepath.Join(t.TempDir(), "out.txt")
     41 	body := "hello world"
     42 
     43 	n, err := SaveStream(path, strings.NewReader(body))
     44 	if err != nil {
     45 		t.Fatalf("SaveStream: %v", err)
     46 	}
     47 	if n != int64(len(body)) {
     48 		t.Errorf("byte count: got %d, want %d", n, len(body))
     49 	}
     50 	got, err := os.ReadFile(path)
     51 	if err != nil {
     52 		t.Fatalf("read back: %v", err)
     53 	}
     54 	if string(got) != body {
     55 		t.Errorf("got %q, want %q", string(got), body)
     56 	}
     57 }
     58 
     59 type errReader struct{}
     60 
     61 func (errReader) Read([]byte) (int, error) { return 0, errors.New("boom") }
     62 
     63 func TestSaveStreamRemovesPartialFileOnError(t *testing.T) {
     64 	path := filepath.Join(t.TempDir(), "out.txt")
     65 
     66 	if _, err := SaveStream(path, errReader{}); err == nil {
     67 		t.Fatal("SaveStream: want error, got nil")
     68 	}
     69 	if _, err := os.Stat(path); !os.IsNotExist(err) {
     70 		t.Errorf("partial file left behind: stat err = %v, want not-exist", err)
     71 	}
     72 }