2018-07-15 07:19:14 +02:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2023-07-19 00:57:09 +02:00
|
|
|
"hash/crc32"
|
2018-07-15 07:19:14 +02:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-11-01 19:18:45 +01:00
|
|
|
"time"
|
2018-07-15 07:19:14 +02:00
|
|
|
|
|
|
|
"android/soong/jar"
|
|
|
|
"android/soong/third_party/zip"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testZipEntry struct {
|
2023-11-01 19:18:45 +01:00
|
|
|
name string
|
|
|
|
mode os.FileMode
|
|
|
|
data []byte
|
|
|
|
method uint16
|
|
|
|
timestamp time.Time
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-11-01 19:18:45 +01:00
|
|
|
A = testZipEntry{"A", 0755, []byte("foo"), zip.Deflate, jar.DefaultTime}
|
|
|
|
a = testZipEntry{"a", 0755, []byte("foo"), zip.Deflate, jar.DefaultTime}
|
|
|
|
a2 = testZipEntry{"a", 0755, []byte("FOO2"), zip.Deflate, jar.DefaultTime}
|
|
|
|
a3 = testZipEntry{"a", 0755, []byte("Foo3"), zip.Deflate, jar.DefaultTime}
|
|
|
|
bDir = testZipEntry{"b/", os.ModeDir | 0755, nil, zip.Deflate, jar.DefaultTime}
|
|
|
|
bbDir = testZipEntry{"b/b/", os.ModeDir | 0755, nil, zip.Deflate, jar.DefaultTime}
|
|
|
|
bbb = testZipEntry{"b/b/b", 0755, nil, zip.Deflate, jar.DefaultTime}
|
|
|
|
ba = testZipEntry{"b/a", 0755, []byte("foo"), zip.Deflate, jar.DefaultTime}
|
|
|
|
bc = testZipEntry{"b/c", 0755, []byte("bar"), zip.Deflate, jar.DefaultTime}
|
|
|
|
bd = testZipEntry{"b/d", 0700, []byte("baz"), zip.Deflate, jar.DefaultTime}
|
|
|
|
be = testZipEntry{"b/e", 0700, []byte(""), zip.Deflate, jar.DefaultTime}
|
|
|
|
|
|
|
|
withTimestamp = testZipEntry{"timestamped", 0755, nil, zip.Store, jar.DefaultTime.Add(time.Hour)}
|
|
|
|
withoutTimestamp = testZipEntry{"timestamped", 0755, nil, zip.Store, jar.DefaultTime}
|
|
|
|
|
|
|
|
service1a = testZipEntry{"META-INF/services/service1", 0755, []byte("class1\nclass2\n"), zip.Store, jar.DefaultTime}
|
|
|
|
service1b = testZipEntry{"META-INF/services/service1", 0755, []byte("class1\nclass3\n"), zip.Deflate, jar.DefaultTime}
|
|
|
|
service1combined = testZipEntry{"META-INF/services/service1", 0755, []byte("class1\nclass2\nclass3\n"), zip.Store, jar.DefaultTime}
|
|
|
|
service2 = testZipEntry{"META-INF/services/service2", 0755, []byte("class1\nclass2\n"), zip.Deflate, jar.DefaultTime}
|
|
|
|
|
|
|
|
metainfDir = testZipEntry{jar.MetaDir, os.ModeDir | 0755, nil, zip.Deflate, jar.DefaultTime}
|
|
|
|
manifestFile = testZipEntry{jar.ManifestFile, 0755, []byte("manifest"), zip.Deflate, jar.DefaultTime}
|
|
|
|
manifestFile2 = testZipEntry{jar.ManifestFile, 0755, []byte("manifest2"), zip.Deflate, jar.DefaultTime}
|
|
|
|
moduleInfoFile = testZipEntry{jar.ModuleInfoClass, 0755, []byte("module-info"), zip.Deflate, jar.DefaultTime}
|
2018-07-15 07:19:14 +02:00
|
|
|
)
|
|
|
|
|
2019-07-17 03:45:24 +02:00
|
|
|
type testInputZip struct {
|
|
|
|
name string
|
|
|
|
entries []testZipEntry
|
|
|
|
reader *zip.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tiz *testInputZip) Name() string {
|
|
|
|
return tiz.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tiz *testInputZip) Open() error {
|
|
|
|
if tiz.reader == nil {
|
|
|
|
tiz.reader = testZipEntriesToZipReader(tiz.entries)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tiz *testInputZip) Close() error {
|
|
|
|
tiz.reader = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tiz *testInputZip) Entries() []*zip.File {
|
|
|
|
if tiz.reader == nil {
|
|
|
|
panic(fmt.Errorf("%s: should be open to get entries", tiz.Name()))
|
|
|
|
}
|
|
|
|
return tiz.reader.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tiz *testInputZip) IsOpen() bool {
|
|
|
|
return tiz.reader != nil
|
|
|
|
}
|
|
|
|
|
2018-07-15 07:19:14 +02:00
|
|
|
func TestMergeZips(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
in [][]testZipEntry
|
|
|
|
stripFiles []string
|
|
|
|
stripDirs []string
|
|
|
|
jar bool
|
2024-02-09 22:06:49 +01:00
|
|
|
par bool
|
2018-07-15 07:19:14 +02:00
|
|
|
sort bool
|
|
|
|
ignoreDuplicates bool
|
|
|
|
stripDirEntries bool
|
|
|
|
zipsToNotStrip map[string]bool
|
|
|
|
|
|
|
|
out []testZipEntry
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "duplicates error",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a},
|
|
|
|
{a2},
|
|
|
|
{a3},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a},
|
|
|
|
err: "duplicate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicates take first",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a},
|
|
|
|
{a2},
|
|
|
|
{a3},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a},
|
|
|
|
|
|
|
|
ignoreDuplicates: true,
|
|
|
|
},
|
2018-10-18 00:05:56 +02:00
|
|
|
{
|
|
|
|
name: "duplicates identical",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a},
|
|
|
|
{a},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a},
|
|
|
|
},
|
2018-07-15 07:19:14 +02:00
|
|
|
{
|
|
|
|
name: "sort",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{be, bc, bDir, bbDir, bbb, A, metainfDir, manifestFile},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{A, metainfDir, manifestFile, bDir, bbDir, bbb, bc, be},
|
|
|
|
|
|
|
|
sort: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jar sort",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{be, bc, bDir, A, metainfDir, manifestFile},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{metainfDir, manifestFile, A, bDir, bc, be},
|
|
|
|
|
|
|
|
jar: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jar merge",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{metainfDir, manifestFile, bDir, be},
|
|
|
|
{metainfDir, manifestFile2, bDir, bc},
|
|
|
|
{metainfDir, manifestFile2, A},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{metainfDir, manifestFile, A, bDir, bc, be},
|
|
|
|
|
|
|
|
jar: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "merge",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{bDir, be},
|
|
|
|
{bDir, bc},
|
|
|
|
{A},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{bDir, be, bc, A},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "strip dir entries",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, bbDir, bbb, bc, bd, be},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a, bbb, bc, bd, be},
|
|
|
|
|
|
|
|
stripDirEntries: true,
|
|
|
|
},
|
|
|
|
{
|
2018-07-15 17:16:31 +02:00
|
|
|
name: "strip files",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, bbDir, bbb, bc, bd, be},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a, bDir, bbDir, bbb, bc},
|
|
|
|
|
|
|
|
stripFiles: []string{"b/d", "b/e"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// merge_zips used to treat -stripFile a as stripping any file named a, it now only strips a in the
|
|
|
|
// root of the zip.
|
2018-07-15 07:19:14 +02:00
|
|
|
name: "strip file name",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, ba},
|
|
|
|
},
|
2018-07-15 17:16:31 +02:00
|
|
|
out: []testZipEntry{bDir, ba},
|
2018-07-15 07:19:14 +02:00
|
|
|
|
|
|
|
stripFiles: []string{"a"},
|
|
|
|
},
|
2018-07-15 17:16:31 +02:00
|
|
|
{
|
|
|
|
name: "strip files glob",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, ba},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{bDir},
|
|
|
|
|
|
|
|
stripFiles: []string{"**/a"},
|
|
|
|
},
|
2018-07-15 07:19:14 +02:00
|
|
|
{
|
|
|
|
name: "strip dirs",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, bbDir, bbb, bc, bd, be},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a},
|
|
|
|
|
|
|
|
stripDirs: []string{"b"},
|
|
|
|
},
|
2018-07-15 17:16:31 +02:00
|
|
|
{
|
|
|
|
name: "strip dirs glob",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, bbDir, bbb, bc, bd, be},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a, bDir, bc, bd, be},
|
|
|
|
|
|
|
|
stripDirs: []string{"b/*"},
|
|
|
|
},
|
2018-07-15 07:19:14 +02:00
|
|
|
{
|
|
|
|
name: "zips to not strip",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{a, bDir, bc},
|
|
|
|
{bDir, bd},
|
|
|
|
{bDir, be},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{a, bDir, bd},
|
|
|
|
|
|
|
|
stripDirs: []string{"b"},
|
|
|
|
zipsToNotStrip: map[string]bool{
|
|
|
|
"in1": true,
|
|
|
|
},
|
|
|
|
},
|
2023-07-19 00:57:09 +02:00
|
|
|
{
|
|
|
|
name: "services",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{service1a, service2},
|
|
|
|
{service1b},
|
|
|
|
},
|
|
|
|
jar: true,
|
|
|
|
out: []testZipEntry{service1combined, service2},
|
|
|
|
},
|
2023-11-01 19:18:45 +01:00
|
|
|
{
|
|
|
|
name: "strip timestamps",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{withTimestamp},
|
|
|
|
{a},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{withoutTimestamp, a},
|
|
|
|
},
|
2024-02-09 22:06:49 +01:00
|
|
|
{
|
|
|
|
name: "emulate par",
|
|
|
|
in: [][]testZipEntry{
|
|
|
|
{
|
|
|
|
testZipEntry{name: "3/main.py"},
|
|
|
|
testZipEntry{name: "c/main.py"},
|
|
|
|
testZipEntry{name: "a/main.py"},
|
|
|
|
testZipEntry{name: "2/main.py"},
|
|
|
|
testZipEntry{name: "b/main.py"},
|
|
|
|
testZipEntry{name: "1/main.py"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: []testZipEntry{
|
|
|
|
testZipEntry{name: "3/__init__.py", mode: 0700, timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "c/__init__.py", mode: 0700, timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "a/__init__.py", mode: 0700, timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "2/__init__.py", mode: 0700, timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "b/__init__.py", mode: 0700, timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "1/__init__.py", mode: 0700, timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "3/main.py", timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "c/main.py", timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "a/main.py", timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "2/main.py", timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "b/main.py", timestamp: jar.DefaultTime},
|
|
|
|
testZipEntry{name: "1/main.py", timestamp: jar.DefaultTime},
|
|
|
|
},
|
|
|
|
par: true,
|
|
|
|
},
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2019-07-17 03:45:24 +02:00
|
|
|
inputZips := make([]InputZip, len(test.in))
|
2018-07-15 07:19:14 +02:00
|
|
|
for i, in := range test.in {
|
2019-07-17 03:45:24 +02:00
|
|
|
inputZips[i] = &testInputZip{name: "in" + strconv.Itoa(i), entries: in}
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
want := testZipEntriesToBuf(test.out)
|
|
|
|
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
writer := zip.NewWriter(out)
|
|
|
|
|
2019-07-17 03:45:24 +02:00
|
|
|
err := mergeZips(inputZips, writer, "", "",
|
2024-02-09 22:06:49 +01:00
|
|
|
test.sort, test.jar, test.par, test.stripDirEntries, test.ignoreDuplicates,
|
2018-07-15 07:19:14 +02:00
|
|
|
test.stripFiles, test.stripDirs, test.zipsToNotStrip)
|
|
|
|
|
|
|
|
closeErr := writer.Close()
|
|
|
|
if closeErr != nil {
|
2023-07-19 00:57:09 +02:00
|
|
|
t.Fatal(closeErr)
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if test.err != "" {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("missing err, expected: ", test.err)
|
|
|
|
} else if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(test.err)) {
|
|
|
|
t.Fatal("incorrect err, want:", test.err, "got:", err)
|
|
|
|
}
|
|
|
|
return
|
2023-07-19 00:57:09 +02:00
|
|
|
} else if err != nil {
|
|
|
|
t.Fatal("unexpected err: ", err)
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(want, out.Bytes()) {
|
|
|
|
t.Error("incorrect zip output")
|
|
|
|
t.Errorf("want:\n%s", dumpZip(want))
|
|
|
|
t.Errorf("got:\n%s", dumpZip(out.Bytes()))
|
2023-07-19 00:57:09 +02:00
|
|
|
os.WriteFile("/tmp/got.zip", out.Bytes(), 0755)
|
|
|
|
os.WriteFile("/tmp/want.zip", want, 0755)
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testZipEntriesToBuf(entries []testZipEntry) []byte {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
zw := zip.NewWriter(b)
|
|
|
|
|
|
|
|
for _, e := range entries {
|
|
|
|
fh := zip.FileHeader{
|
|
|
|
Name: e.name,
|
|
|
|
}
|
|
|
|
fh.SetMode(e.mode)
|
2023-07-19 00:57:09 +02:00
|
|
|
fh.Method = e.method
|
2023-11-01 19:18:45 +01:00
|
|
|
fh.SetModTime(e.timestamp)
|
2023-07-19 00:57:09 +02:00
|
|
|
fh.UncompressedSize64 = uint64(len(e.data))
|
|
|
|
fh.CRC32 = crc32.ChecksumIEEE(e.data)
|
|
|
|
if fh.Method == zip.Store {
|
|
|
|
fh.CompressedSize64 = fh.UncompressedSize64
|
|
|
|
}
|
2018-07-15 07:19:14 +02:00
|
|
|
|
2023-07-19 00:57:09 +02:00
|
|
|
w, err := zw.CreateHeaderAndroid(&fh)
|
2018-07-15 07:19:14 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(e.data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := zw.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testZipEntriesToZipReader(entries []testZipEntry) *zip.Reader {
|
|
|
|
b := testZipEntriesToBuf(entries)
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
|
|
|
|
zr, err := zip.NewReader(r, int64(len(b)))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zr
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpZip(buf []byte) string {
|
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
zr, err := zip.NewReader(r, int64(len(buf)))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret string
|
|
|
|
|
|
|
|
for _, f := range zr.File {
|
2023-11-01 19:18:45 +01:00
|
|
|
ret += fmt.Sprintf("%v: %v %v %08x %s\n", f.Name, f.Mode(), f.UncompressedSize64, f.CRC32, f.ModTime())
|
2018-07-15 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
2019-07-17 03:45:24 +02:00
|
|
|
|
|
|
|
type DummyInpuZip struct {
|
|
|
|
isOpen bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (diz *DummyInpuZip) Name() string {
|
|
|
|
return "dummy"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (diz *DummyInpuZip) Open() error {
|
|
|
|
diz.isOpen = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (diz *DummyInpuZip) Close() error {
|
|
|
|
diz.isOpen = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (DummyInpuZip) Entries() []*zip.File {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (diz *DummyInpuZip) IsOpen() bool {
|
|
|
|
return diz.isOpen
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputZipsManager(t *testing.T) {
|
|
|
|
const nInputZips = 20
|
|
|
|
const nMaxOpenZips = 10
|
|
|
|
izm := NewInputZipsManager(20, 10)
|
|
|
|
managedZips := make([]InputZip, nInputZips)
|
|
|
|
for i := 0; i < nInputZips; i++ {
|
|
|
|
managedZips[i] = izm.Manage(&DummyInpuZip{})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("InputZipsManager", func(t *testing.T) {
|
|
|
|
for i, iz := range managedZips {
|
|
|
|
if err := iz.Open(); err != nil {
|
|
|
|
t.Fatalf("Step %d: open failed: %s", i, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if izm.nOpenZips > nMaxOpenZips {
|
|
|
|
t.Errorf("Step %d: should be <=%d open zips", i, nMaxOpenZips)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !managedZips[nInputZips-1].IsOpen() {
|
|
|
|
t.Error("The last input should stay open")
|
|
|
|
}
|
|
|
|
for _, iz := range managedZips {
|
|
|
|
iz.Close()
|
|
|
|
}
|
|
|
|
if izm.nOpenZips > 0 {
|
|
|
|
t.Error("Some input zips are still open")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|