/usr/share/gocode/src/github.com/lxc/lxd/shared/util_test.go is in golang-github-lxc-lxd-dev 2.0.2-0ubuntu1~16.04.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | package shared
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestFileCopy(t *testing.T) {
helloWorld := []byte("hello world\n")
source, err := ioutil.TempFile("", "")
if err != nil {
t.Error(err)
return
}
defer os.Remove(source.Name())
if err := WriteAll(source, helloWorld); err != nil {
source.Close()
t.Error(err)
return
}
source.Close()
dest, err := ioutil.TempFile("", "")
defer os.Remove(dest.Name())
if err != nil {
t.Error(err)
return
}
dest.Close()
if err := FileCopy(source.Name(), dest.Name()); err != nil {
t.Error(err)
return
}
dest2, err := os.Open(dest.Name())
if err != nil {
t.Error(err)
return
}
content, err := ioutil.ReadAll(dest2)
if err != nil {
t.Error(err)
return
}
if string(content) != string(helloWorld) {
t.Error("content mismatch: ", string(content), "!=", string(helloWorld))
return
}
}
func TestReadLastNLines(t *testing.T) {
source, err := ioutil.TempFile("", "")
if err != nil {
t.Error(err)
return
}
defer os.Remove(source.Name())
for i := 0; i < 50; i++ {
fmt.Fprintf(source, "%d\n", i)
}
lines, err := ReadLastNLines(source, 100)
if err != nil {
t.Error(err)
return
}
split := strings.Split(lines, "\n")
for i := 0; i < 50; i++ {
if fmt.Sprintf("%d", i) != split[i] {
t.Error(fmt.Sprintf("got %s expected %d", split[i], i))
return
}
}
source.Seek(0, 0)
for i := 0; i < 150; i++ {
fmt.Fprintf(source, "%d\n", i)
}
lines, err = ReadLastNLines(source, 100)
if err != nil {
t.Error(err)
return
}
split = strings.Split(lines, "\n")
for i := 0; i < 100; i++ {
if fmt.Sprintf("%d", i+50) != split[i] {
t.Error(fmt.Sprintf("got %s expected %d", split[i], i))
return
}
}
}
|