File: ar_test.go

package info (click to toggle)
golang-pault-go-debian 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: makefile: 2
file content (79 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download
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
package deb_test

import (
	"io"
	"io/ioutil"
	"log"
	"os"
	"testing"

	"pault.ag/go/debian/deb"
)

/*
 *
 */

func isok(t *testing.T, err error) {
	if err != nil && err != io.EOF {
		log.Printf("Error! Error is not nil! %s\n", err)
		t.FailNow()
	}
}

func notok(t *testing.T, err error) {
	if err == nil {
		log.Printf("Error! Error is nil!\n")
		t.FailNow()
	}
}

func assert(t *testing.T, expr bool) {
	if !expr {
		log.Printf("Assertion failed!")
		t.FailNow()
	}
}

/*
 *
 */

// As discussed in testdata/README.md, `multi_archive.a` is taken from Blake
// Smith's ar project. Some of the data below follows.

func TestAr(t *testing.T) {
	file, err := os.Open("testdata/multi_archive.a")
	isok(t, err)

	ar, err := deb.LoadAr(file)
	isok(t, err)

	firstEntry, err := ar.Next()
	isok(t, err)

	assert(t, firstEntry.Name == `hello.txt`)
	assert(t, firstEntry.Timestamp == 1361157466)
	assert(t, firstEntry.OwnerID == 501)
	assert(t, firstEntry.GroupID == 20)

	firstContent, err := ioutil.ReadAll(firstEntry.Data)
	isok(t, err)
	assert(t, firstEntry.Size == int64(len(firstContent)))
	assert(t, string(firstContent) == "Hello world!\n")

	secondEntry, err := ar.Next()
	isok(t, err)
	secondContent, err := ioutil.ReadAll(secondEntry.Data)
	isok(t, err)
	assert(t, secondEntry.Size == int64(len(secondContent)))
	assert(t, string(secondContent) == "I love lamp.\n")

	// Now, test that we can rewind and reread the first file even after
	// reading the second one.
	_, err = firstEntry.Data.Seek(0, 0)
	isok(t, err)
	firstRereadContent, err := ioutil.ReadAll(firstEntry.Data)
	isok(t, err)
	assert(t, string(firstContent) == string(firstRereadContent))
}