aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/pkg/build_test.sh
blob: cbb44c3e04a5fe5deb97d908dd41cec0253df3ce (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
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
#!/bin/bash

# Copyright 2015 The Bazel Authors. 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.

# Unit tests for pkg_deb and pkg_tar

DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; }

function get_tar_listing() {
  local input=$1
  local test_data="${TEST_DATA_DIR}/${input}"
  tar tvf "${test_data}" | sed -e 's/^.*:00 //'
}

function get_tar_permission() {
  local input=$1
  local file=$2
  local test_data="${TEST_DATA_DIR}/${input}"
  tar tvf "${test_data}" | fgrep "00 $file" | cut -d " " -f 1
}

function get_deb_listing() {
  local input=$1
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb -c "${test_data}" | sed -e 's/^.*:00 //'
}

function get_deb_description() {
  local input=$1
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb -I "${test_data}"
}

function get_deb_permission() {
  local input=$1
  local file=$2
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb -c "${test_data}" | fgrep "00 $file" | cut -d " " -f 1
}

function get_deb_ctl_file() {
  local input=$1
  local file=$2
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb -I "${test_data}" "${file}"
}

function get_deb_ctl_listing() {
  local input=$1
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb --ctrl-tarfile "${test_data}" | tar tf - | sort
}

function get_deb_ctl_permission() {
  local input=$1
  local file=$2
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb --ctrl-tarfile "${test_data}" | tar tvf - | egrep " $file\$" | cut -d " " -f 1
}

function dpkg_deb_supports_ctrl_tarfile() {
  local input=$1
  local test_data="${TEST_DATA_DIR}/${input}"
  dpkg-deb --ctrl-tarfile "${test_data}" > /dev/null 2> /dev/null
}


function test_tar() {
  local listing="./
./etc/
./etc/nsswitch.conf
./usr/
./usr/titi
./usr/bin/
./usr/bin/java -> /path/to/bin/java"
  for i in "" ".gz" ".bz2" ".xz"; do
    check_eq "$listing" "$(get_tar_listing test-tar-${i:1}.tar$i)"
    check_eq "-rwxr-xr-x" "$(get_tar_permission test-tar-${i:1}.tar$i ./usr/titi)"
    check_eq "-rw-r--r--" "$(get_tar_permission test-tar-${i:1}.tar$i ./etc/nsswitch.conf)"
    # Test merging tar files
    check_eq "$listing" "$(get_tar_listing test-tar-inclusion-${i:1}.tar)"
    check_eq "-rwxr-xr-x" "$(get_tar_permission test-tar-inclusion-${i:1}.tar ./usr/titi)"
    check_eq "-rw-r--r--" "$(get_tar_permission test-tar-inclusion-${i:1}.tar ./etc/nsswitch.conf)"
  done;

  check_eq "./
./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-empty.tar)"
  check_eq "./
./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-none.tar)"
  check_eq "./
./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-etc.tar)"
  check_eq "./
./etc/
./etc/nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-dot.tar)"
}

function test_deb() {
  if ! (which dpkg-deb); then
    echo "Unable to run test for debian, no dpkg-deb!" >&2
    return 0
  fi
  local listing="./
./etc/
./etc/nsswitch.conf
./usr/
./usr/titi
./usr/bin/
./usr/bin/java -> /path/to/bin/java"
  check_eq "$listing" "$(get_deb_listing test-deb.deb)"
  check_eq "-rwxr-xr-x" "$(get_deb_permission test-deb.deb ./usr/titi)"
  check_eq "-rw-r--r--" "$(get_deb_permission test-deb.deb ./etc/nsswitch.conf)"
  get_deb_description test-deb.deb >$TEST_log
  expect_log "Description: toto"
  expect_log "Package: titi"
  expect_log "Depends: dep1, dep2"

  if ! dpkg_deb_supports_ctrl_tarfile test-deb.deb ; then
    echo "Unable to test deb control files, too old dpkg-deb!" >&2
    return 0
  fi
  local ctrl_listing="conffiles
control"
  check_eq "$ctrl_listing" "$(get_deb_ctl_listing test-deb.deb)"
  check_eq "-rw-r--r--" "$(get_deb_ctl_permission test-deb.deb conffiles)"
  check_eq "-rw-r--r--" "$(get_deb_ctl_permission test-deb.deb control)"
  local conffiles="/etc/nsswitch.conf
/etc/other"
  check_eq "$conffiles" "$(get_deb_ctl_file test-deb.deb conffiles)"
}

run_suite "build_test"