summaryrefslogtreecommitdiff
path: root/src/lib/archive/tar/untar_test.go
blob: a9c92dbf05b77ed5aae8aa81193f1e30f20ebc18 (plain)
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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tar

import (
	"archive/tar";
	"bytes";
	"fmt";
	"io";
	"os";
	"testing";
)

func TestUntar(t *testing.T) {
	f, err := os.Open("testdata/test.tar", os.O_RDONLY, 0444);
	if err != nil {
		t.Fatalf("Unexpected error: %v", err);
	}
	defer f.Close();

	tr := NewReader(f);

	// First file
	hdr, err := tr.Next();
	if err != nil || hdr == nil {
		t.Fatalf("Didn't get first file: %v", err);
	}
	if hdr.Name != "small.txt" {
		t.Errorf(`hdr.Name = %q, want "small.txt"`, hdr.Name);
	}
	if hdr.Mode != 0640 {
		t.Errorf("hdr.Mode = %v, want 0640", hdr.Mode);
	}
	if hdr.Size != 5 {
		t.Errorf("hdr.Size = %v, want 5", hdr.Size);
	}

	// Read the first four bytes; Next() should skip the last one.
	buf := make([]byte, 4);
	if n, err := io.FullRead(tr, buf); err != nil {
		t.Fatalf("Unexpected error: %v", err);
	}
	if expected := io.StringBytes("Kilt"); !bytes.Equal(buf, expected) {
		t.Errorf("Contents = %v, want %v", buf, expected);
	}

	// Second file
	hdr, err = tr.Next();
	if err != nil {
		t.Fatalf("Didn't get second file: %v", err);
	}
	if hdr.Name != "small2.txt" {
		t.Errorf(`hdr.Name = %q, want "small2.txt"`, hdr.Name);
	}
	if hdr.Mode != 0640 {
		t.Errorf("hdr.Mode = %v, want 0640", hdr.Mode);
	}
	if hdr.Size != 11 {
		t.Errorf("hdr.Size = %v, want 11", hdr.Size);
	}


	hdr, err = tr.Next();
	if hdr != nil || err != nil {
		t.Fatalf("Unexpected third file or error: %v", err);
	}
}