Project

General

Profile

Feature #96 » credenza.sh

jahoti, 10/04/2021 05:57 AM

 
1
#!/bin/sh
2

    
3
# Copyright © 2021 jahoti <jahoti@tilde.team>
4
# Copyright © 2021 Wojtek Kosior <koszko@koszko.org>
5
#
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
#
10
#    http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17

    
18
breakout () {
19
    EXIT_CODE=$1
20
    shift
21
    
22
    case $EXIT_CODE in
23
	0)     echo "$@"; rm -rf /tmp/credenza;;
24
	111)                     echo "$@" >&2;;
25
	*) echo "$@" >&2; rm -rf /tmp/credenza;;
26
    esac
27
    
28
    exit $EXIT_CODE
29
}
30

    
31
hash_manifest () {
32
    grep -vE '[[:space:]]*//' "$1"/manifest.json |\
33
    sed 's/[[:space:]]*\(.\|"\([^\\"]\|\\u[a-fA-F0-9]{4}\|\\.\)*"\)/\1/g' |\
34
    tr -d '\n' | sha256sum
35
}
36

    
37
MY_COPY="$1"
38
MOZ_COPY="$2"
39

    
40
if [ ! -d "$MY_COPY" ]; then
41
    breakout 111 Native copy of the extension \
42
    '(first argument)' is not a directory.
43
elif [ ! -f "$MOZ_COPY" ]; then
44
    breakout 111 Mozilla-signed copy of the extension \
45
    '(second argument)' is not a file.
46
elif [ -e /tmp/credenza ]; then
47
    breakout 111 The testing location /tmp/credenza is already in use.
48
fi
49

    
50
${CMD_UNZIP:-unzip -d }/tmp/credenza "$MOZ_COPY"
51
COULD_UNZIP=$?
52
if [ $COULD_UNZIP != 0 ]; then
53
    breakout $COULD_UNZIP Something went wrong while unbundling \
54
    the Mozilla-signed copy of the extension.
55
fi
56

    
57
if [ "$(hash_manifest "$MY_COPY")" != "$(hash_manifest /tmp/credenza)" ]; then
58
    breakout 2 '\033'[33mValidation failed\; manifests differ.'\033'[30m
59
fi
60

    
61
cp "$MY_COPY"/manifest.json /tmp/credenza
62
rm -rf /tmp/credenza/META-INF/
63

    
64
if diff -r "$MY_COPY" /tmp/credenza; then
65
    breakout 0 '\033'[33mValidation was successful.'\033'[30m
66
else
67
    breakout 3 '\033'[33mValidation failed\; see above 'for' differences.'\033'[30m
68
fi
69

    
(2-2/2)