File: rwcompat_test.go

package info (click to toggle)
golang-github-mkrautz-goar 0.0~git20150919.282caa8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 112 kB
  • sloc: makefile: 2
file content (123 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download | duplicates (5)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2011 Mikkel Krautz
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.

package ar

// This file tests that our reader can read files written by our writer.

import (
	"bytes"
	"io"
	"testing"
)

type archiveTest struct {
	hdr  *Header
	data []byte
}

// Long filename test
var longFnArchive []archiveTest = []archiveTest{
	{
		&Header{
			Name:  "ReadWriteCompatFileName",
			Mode:  0755,
			Mtime: 0,
			Uid:   1000,
			Gid:   1000,
			Size:  6,
		},
		[]byte{0xf0, 0x00, 0xca, 0xfe, 0xba, 0xbe},
	},
}

// Short filename test (we don't special case short filenames,
// we always write out long-style filenames, but let's test it
// just to make sure)
var shortFnArchive []archiveTest = []archiveTest{
	{
		&Header{
			Name:  "a",
			Mode:  0755,
			Mtime: 0,
			Uid:   1000,
			Gid:   1000,
			Size:  2,
		},
		[]byte{'a', '\n'},
	},
}

func headerCmp(hdr1 *Header, hdr2 *Header) bool {
	if hdr1 == nil || hdr2 == nil {
		return false
	}
	if hdr1.Name != hdr2.Name {
		return false
	}
	if hdr1.Mode != hdr2.Mode {
		return false
	}
	if hdr1.Mtime != hdr2.Mtime {
		return false
	}
	if hdr1.Uid != hdr2.Uid {
		return false
	}
	if hdr1.Gid != hdr2.Gid {
		return false
	}
	if hdr1.Size != hdr2.Size {
		return false
	}
	return true
}

func testRwCompat(t *testing.T, testArchive []archiveTest) {
	buf := new(bytes.Buffer)

	w := NewWriter(buf)
	for _, testEntry := range testArchive {
		err := w.WriteHeader(testEntry.hdr)
		if err != nil {
			t.Fatal(err)
		}
		_, err = w.Write(testEntry.data)
		if err != nil {
			t.Fatal(err)
		}
	}
	err := w.Close()
	if err != nil {
		t.Fatal(err)
	}

	rbuf := bytes.NewBuffer(buf.Bytes())
	r := NewReader(rbuf)
	for _, testEntry := range testArchive {
		hdr, err := r.Next()
		if err != nil {
			t.Fatal(err)
		}
		if !headerCmp(hdr, testEntry.hdr) {
			t.Fatalf("header mismatch:\nread = %v\norig = %v", hdr, testEntry.hdr)
		}
		fbuf := make([]byte, hdr.Size)
		_, err = io.ReadFull(r, fbuf)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(fbuf, testEntry.data) {
			t.Fatalf("data mismatch\nread = %v\norig = %v", fbuf, testEntry.data)
		}
	}
}

func TestReadWriteCompatLongFn(t *testing.T) {
	testRwCompat(t, longFnArchive)
}

func TestReadWriteCompatShortFn(t *testing.T) {
	testRwCompat(t, shortFnArchive)
}