Debian Codegraph @

punch_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright (c) 2014 The fileutil authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build amd64 amd64p32 arm64 arm64be ppc64 ppc64le mips64 mips64le mips64p32 mips64p32le sparc64
6
7 package fileutil // import "modernc.org/fileutil"
8
9 import (
10 "io/ioutil"
11 "os"
12 "testing"
13 )
14
15 func TestPunch(t *testing.T) {
16 file, err := ioutil.TempFile("", "punchhole-")
17 if err != nil {
18 t.Error(err)
19 }
20 defer os.Remove(file.Name())
21 defer file.Close()
22 buf := make([]byte, 10<<20)
23 for i := range buf {
24 buf[i] = byte(1 + (i+1)&0xfe)
25 }
26 if _, err = file.Write(buf); err != nil {
27 t.Errorf("error writing to the temp file: %v", err)
28 t.FailNow()
29 }
30 if err = file.Sync(); err != nil {
31 t.Logf("error syncing %q: %v", file.Name(), err)
32 }
33 for i, j := range []int{1, 31, 1 << 10} {
34 if err = PunchHole(file, int64(j), int64(j)); err != nil {
35 t.Errorf("%d. error punching at %d, size %d: %v", i, j, j, err)
36 continue
37 }
38 // read back, with 1-1 bytes overlaid
39 n, err := file.ReadAt(buf[:j+2], int64(j-1))
40 if err != nil {
41 t.Errorf("%d. error reading file: %v", i, err)
42 continue
43 }
44 buf = buf[:n]
45 if buf[0] == 0 {
46 t.Errorf("%d. file at %d has been overwritten with 0!", i, j-1)
47 }
48 if buf[n-1] == 0 {
49 t.Errorf("%d. file at %d has been overwritten with 0!", i, j-1+n)
50 }
51 if !hasPunchHole {
52 continue
53 }
54
55 for k, v := range buf[1 : n-1] {
56 if v != 0 {
57 t.Errorf("%d. error reading file at %d got %d, want 0.", i, k, v)
58 }
59 }
60 }
61 }