github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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 "io/ioutil" 22 "os" 23 "testing" 24 ) 25 26 func TestCopyFrom(t *testing.T) { 27 var ( 28 content = []byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8} 29 prefix = []byte{0x9, 0xa, 0xb, 0xc, 0xd, 0xf} 30 ) 31 var cases = []struct { 32 src, dest string 33 offset uint64 34 writePrefix bool 35 }{ 36 {"foo", "bar", 0, false}, 37 {"foo", "bar", 1, false}, 38 {"foo", "bar", 8, false}, 39 {"foo", "foo", 0, false}, 40 {"foo", "foo", 1, false}, 41 {"foo", "foo", 8, false}, 42 {"foo", "bar", 0, true}, 43 {"foo", "bar", 1, true}, 44 {"foo", "bar", 8, true}, 45 } 46 for _, c := range cases { 47 ioutil.WriteFile(c.src, content, 0644) 48 49 if err := copyFrom(c.src, c.dest, c.offset, func(f *os.File) error { 50 if !c.writePrefix { 51 return nil 52 } 53 f.Write(prefix) 54 return nil 55 }); err != nil { 56 os.Remove(c.src) 57 t.Fatalf("Failed to copy %v", err) 58 } 59 60 blob, err := ioutil.ReadFile(c.dest) 61 if err != nil { 62 os.Remove(c.src) 63 os.Remove(c.dest) 64 t.Fatalf("Failed to read %v", err) 65 } 66 want := content[c.offset:] 67 if c.writePrefix { 68 want = append(prefix, want...) 69 } 70 if !bytes.Equal(blob, want) { 71 t.Fatal("Unexpected value") 72 } 73 os.Remove(c.src) 74 os.Remove(c.dest) 75 } 76 }