• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package nativelib
16
17import (
18	"archive/zip"
19	"fmt"
20	"io"
21	"io/ioutil"
22	"os"
23	"path/filepath"
24	"testing"
25
26	"src/common/golang/runfilelocation"
27)
28
29const (
30	expectedName = "lib/x86/dummy.so"
31	dummyLib     = "src/tools/ak/nativelib/testdata/dummy.so"
32)
33
34func makeLibZip(t *testing.T, entry io.Reader, entryName, zipPath string) error {
35	f, err := os.Create(zipPath)
36	if err != nil {
37		return err
38	}
39	defer func() {
40		if err := f.Close(); err != nil {
41			t.Error(err)
42		}
43	}()
44
45	archive := zip.NewWriter(f)
46	wr, err := archive.CreateHeader(&zip.FileHeader{Name: entryName, Method: zip.Store})
47	if err != nil {
48		return err
49	}
50	if _, err := io.Copy(wr, entry); err != nil {
51		return err
52	}
53	return archive.Close()
54}
55
56func TestCreateNativeLibZip(t *testing.T) {
57	tmpDir, err := ioutil.TempDir("", "shelltest")
58	if err != nil {
59		t.Errorf("Error creating temp directory: %v", err)
60	}
61	defer os.RemoveAll(tmpDir)
62	out := filepath.Join(tmpDir, "lib.zip")
63	dummyLibPath, err := runfilelocation.Find(dummyLib)
64	if err != nil {
65		t.Errorf("Error finding dummy lib runfile: %v", err)
66	}
67	in := []string{"x86:" + dummyLibPath}
68	if err := doWork(in, out); err != nil {
69		t.Errorf("Error creating native lib zip: %v", err)
70	}
71
72	z, err := zip.OpenReader(out)
73	if err != nil {
74		t.Fatalf("Error opening output zip: %v", err)
75	}
76	defer z.Close()
77
78	if len(z.File) != 1 {
79		t.Fatalf("Got %d files in zip, expected 1", len(z.File))
80	}
81
82	if z.File[0].Name != expectedName {
83		t.Fatalf("Got .so file %s, expected %s", z.File[0].Name, expectedName)
84	}
85}
86
87func TestExtractLibs(t *testing.T) {
88	tmpDir, err := ioutil.TempDir("", "shelltest")
89	if err != nil {
90		t.Fatalf("Error creating temp directory: %v", err)
91	}
92	defer os.RemoveAll(tmpDir)
93
94	lib, err := os.Create(filepath.Join(tmpDir, "dmmylib.so"))
95	if err != nil {
96		t.Fatalf("Error creating dummy lib: %v", err)
97	}
98
99	libZip := filepath.Join(tmpDir, "libs.zip")
100	if err := makeLibZip(t, lib, expectedName, libZip); err != nil {
101		t.Fatalf("error creating aar lib zip: %v", err)
102	}
103
104	dstDir, err := ioutil.TempDir("", "ziplibs")
105	if err != nil {
106		t.Fatalf("Error extracting creating zip dir: %v", err)
107	}
108	defer os.RemoveAll(dstDir)
109
110	libs, err := extractLibs(libZip, dstDir)
111	if err != nil {
112		t.Fatalf("Error extracting libs from zip: %v", err)
113	}
114
115	if len(libs) != 1 {
116		t.Fatalf("Got %d files in zip, expected 1", len(libs))
117	}
118	expected := fmt.Sprintf("x86:%s", filepath.Join(dstDir, "lib/x86/dummy.so"))
119	if libs[0] != expected {
120		t.Fatalf("Got %s lib, expected %s", libs[0], expected)
121	}
122
123}
124