File: compression_test.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (159 lines) | stat: -rw-r--r-- 5,416 bytes parent folder | download | duplicates (2)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package http

import (
	"context"
	"errors"
	"io"
	"net/url"

	"github.com/aptly-dev/aptly/utils"

	. "gopkg.in/check.v1"
)

type CompressionSuite struct {
	baseURL *url.URL
	ctx     context.Context
}

var _ = Suite(&CompressionSuite{})

const (
	bzipData = "BZh91AY&SY\xcc\xc3q\xd4\x00\x00\x02A\x80\x00\x10\x02\x00\x0c\x00 \x00!\x9ah3M\x19\x97\x8b\xb9\"\x9c(Hfa\xb8\xea\x00"
	gzipData = "\x1f\x8b\x08\x00\xc8j\xb0R\x00\x03+I-.\xe1\x02\x00\xc65\xb9;\x05\x00\x00\x00"
	xzData   = "\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21\x01\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x04\x74\x65\x73\x74\x0a\x00\x00\x00\x00\x9d\xed\x31\x1d\x0f\x9f\xd7\xe6\x00\x01\x1d\x05\xb8\x2d\x80\xaf\x1f\xb6\xf3\x7d\x01\x00\x00\x00\x00\x04\x59\x5a"
	rawData  = "test"
)

func (s *CompressionSuite) SetUpTest(c *C) {
	s.baseURL, _ = url.Parse("http://example.com/")
	s.ctx = context.Background()
}

func (s *CompressionSuite) TestDownloadTryCompression(c *C) {
	var buf []byte

	expectedChecksums := map[string]utils.ChecksumInfo{
		"file.bz2": {Size: int64(len(bzipData))},
		"file.gz":  {Size: int64(len(gzipData))},
		"file.xz":  {Size: int64(len(xzData))},
		"file":     {Size: int64(len(rawData))},
	}

	// bzip2 only available
	buf = make([]byte, 4)
	d := NewFakeDownloader()
	d.ExpectResponse("http://example.com/file.bz2", bzipData)
	r, file, err := DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
	c.Assert(err, IsNil)
	defer func() {
		_ = file.Close()
	}()
	_, _ = io.ReadFull(r, buf)
	c.Assert(string(buf), Equals, rawData)
	c.Assert(d.Empty(), Equals, true)

	// bzip2 not available, but gz is
	buf = make([]byte, 4)
	d = NewFakeDownloader()
	d.ExpectError("http://example.com/file.bz2", &Error{Code: 404})
	d.ExpectResponse("http://example.com/file.gz", gzipData)
	r, file, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
	c.Assert(err, IsNil)
	defer func() {
		_ = file.Close()
	}()
	_, _ = io.ReadFull(r, buf)
	c.Assert(string(buf), Equals, rawData)
	c.Assert(d.Empty(), Equals, true)

	// bzip2 & gzip not available, but xz is
	buf = make([]byte, 4)
	d = NewFakeDownloader()
	d.ExpectError("http://example.com/file.bz2", &Error{Code: 404})
	d.ExpectError("http://example.com/file.gz", &Error{Code: 404})
	d.ExpectResponse("http://example.com/file.xz", xzData)
	r, file, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
	c.Assert(err, IsNil)
	defer func() {
		_ = file.Close()
	}()
	_, _ = io.ReadFull(r, buf)
	c.Assert(string(buf), Equals, rawData)
	c.Assert(d.Empty(), Equals, true)

	// bzip2, gzip & xz not available, but raw is
	buf = make([]byte, 4)
	d = NewFakeDownloader()
	d.ExpectError("http://example.com/file.bz2", &Error{Code: 404})
	d.ExpectError("http://example.com/file.gz", &Error{Code: 404})
	d.ExpectError("http://example.com/file.xz", &Error{Code: 404})
	d.ExpectResponse("http://example.com/file", rawData)
	r, file, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
	c.Assert(err, IsNil)
	defer func() {
		_ = file.Close()
	}()
	_, _ = io.ReadFull(r, buf)
	c.Assert(string(buf), Equals, rawData)
	c.Assert(d.Empty(), Equals, true)

	// gzip available, but broken
	d = NewFakeDownloader()
	d.ExpectError("http://example.com/file.bz2", &Error{Code: 404})
	d.ExpectResponse("http://example.com/file.gz", "x")
	_, _, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", nil, true)
	c.Assert(err, ErrorMatches, "unexpected EOF")
	c.Assert(d.Empty(), Equals, true)
}

func (s *CompressionSuite) TestDownloadTryCompressionLongestSuffix(c *C) {
	var buf []byte

	expectedChecksums := map[string]utils.ChecksumInfo{
		"file.bz2":          {Size: 1},
		"subdir/file.bz2":   {Size: int64(len(bzipData))},
		"otherdir/file.bz2": {Size: 1},
	}

	// longest suffix should be picked up
	buf = make([]byte, 4)
	d := NewFakeDownloader()
	d.ExpectResponse("http://example.com/subdir/file.bz2", bzipData)
	r, file, err := DownloadTryCompression(s.ctx, d, s.baseURL, "subdir/file", expectedChecksums, false)
	c.Assert(err, IsNil)
	defer func() {
		_ = file.Close()
	}()
	_, _ = io.ReadFull(r, buf)
	c.Assert(string(buf), Equals, rawData)
	c.Assert(d.Empty(), Equals, true)
}

func (s *CompressionSuite) TestDownloadTryCompressionErrors(c *C) {
	d := NewFakeDownloader()
	_, _, err := DownloadTryCompression(s.ctx, d, s.baseURL, "file", nil, true)
	c.Assert(err, ErrorMatches, "unexpected request.*")

	d = NewFakeDownloader()
	d.ExpectError("http://example.com/file.bz2", &Error{Code: 404})
	d.ExpectError("http://example.com/file.gz", &Error{Code: 404})
	d.ExpectError("http://example.com/file.xz", &Error{Code: 404})
	d.ExpectError("http://example.com/file", errors.New("403"))
	_, _, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", nil, true)
	c.Assert(err, ErrorMatches, "403")

	d = NewFakeDownloader()
	d.ExpectError("http://example.com/file.bz2", &Error{Code: 404})
	d.ExpectError("http://example.com/file.gz", &Error{Code: 404})
	d.ExpectError("http://example.com/file.xz", &Error{Code: 404})
	d.ExpectResponse("http://example.com/file", rawData)
	expectedChecksums := map[string]utils.ChecksumInfo{
		"file.bz2": {Size: 7},
		"file.gz":  {Size: 7},
		"file.xz":  {Size: 7},
		"file":     {Size: 7},
	}
	_, _, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
	c.Assert(err, ErrorMatches, "checksums don't match.*")
}