github.com/theQRL/go-zond@v0.1.1/core/rawdb/freezer_utils_test.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"testing"
    23  )
    24  
    25  func TestCopyFrom(t *testing.T) {
    26  	var (
    27  		content = []byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}
    28  		prefix  = []byte{0x9, 0xa, 0xb, 0xc, 0xd, 0xf}
    29  	)
    30  	var cases = []struct {
    31  		src, dest   string
    32  		offset      uint64
    33  		writePrefix bool
    34  	}{
    35  		{"foo", "bar", 0, false},
    36  		{"foo", "bar", 1, false},
    37  		{"foo", "bar", 8, false},
    38  		{"foo", "foo", 0, false},
    39  		{"foo", "foo", 1, false},
    40  		{"foo", "foo", 8, false},
    41  		{"foo", "bar", 0, true},
    42  		{"foo", "bar", 1, true},
    43  		{"foo", "bar", 8, true},
    44  	}
    45  	for _, c := range cases {
    46  		os.WriteFile(c.src, content, 0600)
    47  
    48  		if err := copyFrom(c.src, c.dest, c.offset, func(f *os.File) error {
    49  			if !c.writePrefix {
    50  				return nil
    51  			}
    52  			f.Write(prefix)
    53  			return nil
    54  		}); err != nil {
    55  			os.Remove(c.src)
    56  			t.Fatalf("Failed to copy %v", err)
    57  		}
    58  
    59  		blob, err := os.ReadFile(c.dest)
    60  		if err != nil {
    61  			os.Remove(c.src)
    62  			os.Remove(c.dest)
    63  			t.Fatalf("Failed to read %v", err)
    64  		}
    65  		want := content[c.offset:]
    66  		if c.writePrefix {
    67  			want = append(prefix, want...)
    68  		}
    69  		if !bytes.Equal(blob, want) {
    70  			t.Fatal("Unexpected value")
    71  		}
    72  		os.Remove(c.src)
    73  		os.Remove(c.dest)
    74  	}
    75  }