freezer_utils_test.go 1.9 KB

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