1#!/bin/bash -e
2#
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# To access gen_sdk
18PATH=.:$PATH
19
20tmp_dir="$(mktemp -d -t gen_sdk_test-XXXXXXXXXX)"
21# Verifies the tool correctly prints the binary representation of a db.
22function test_print_binary() {
23  # Golden binary rep generated with:
24  # $ gqui from textproto:testdata/test_extensions_db.textpb \
25  #        proto ../../common/proto/sdk.proto:ExtensionDatabase \
26  #        --outfile rawproto:- | xxd -p
27  cat > ${tmp_dir}/golden_binary << EOF
280a0a080112060803120208010a1a08021206080312020801120608021202
29080212060801120208020a1a080312060803120208031206080212020802
3012060801120208020a220804120608031202080312060802120208021206
310801120208041206080512020804
32EOF
33  # Uses "toybox xxd" to avoid directly calling "xxd" which might be not found on a clean system.
34  diff -b ${tmp_dir}/golden_binary <(gen_sdk --action print_binary --database testdata/test_extensions_db.textpb | toybox xxd -p)
35}
36test_print_binary
37
38# Verifies the tool is able to re-create the test DB correctly.
39function test_new_sdk_filtered() {
40  db=${tmp_dir}/extensions_db.textpb
41  rm -f ${db} && touch ${db}
42  gen_sdk --action new_sdk --sdk 1 --modules MEDIA_PROVIDER --database ${db}
43  gen_sdk --action new_sdk --sdk 2 --modules MEDIA,IPSEC --database ${db}
44  gen_sdk --action new_sdk --sdk 3 --modules MEDIA_PROVIDER --database ${db}
45  gen_sdk --action new_sdk --sdk 4 --modules SDK_EXTENSIONS,IPSEC --database ${db}
46
47  diff -u0 testdata/test_extensions_db.textpb ${db}
48}
49test_new_sdk_filtered
50
51# Verifies the tool can create new SDKs requiring all modules
52function test_new_sdk_all_modules() {
53  db=${tmp_dir}/extensions_db.textpb
54  rm -f ${db} && touch ${db}
55  gen_sdk --action new_sdk --sdk 1 --database ${db} | grep -q MEDIA_PROVIDER
56  gen_sdk --action new_sdk --sdk 2 --database ${db} | grep -q ART
57  ! gen_sdk --action new_sdk --sdk 3 --database ${db} | grep -q UNKNOWN
58}
59test_new_sdk_all_modules
60
61# Verifies the tool won't allow bogus SDK updates
62function test_validate() {
63  set +e
64  db=${tmp_dir}/extensions_db.textpb
65  rm -f ${db} && echo bogus > ${db}
66  if gen_sdk --action validate --database ${db}; then
67    echo "expected validate to fail on bogus db"
68    exit 1
69  fi
70
71  rm -f ${db} && touch ${db}
72  gen_sdk --action new_sdk --sdk 1 --modules MEDIA_PROVIDER --database ${db}
73  if gen_sdk --action new_sdk --sdk 1 --modules SDK_EXTENSIONS --database ${db}; then
74    echo "FAILED: expected duplicate sdk numbers to fail"
75    echo "DB:"
76    cat ${db}
77    exit 1
78  fi
79
80  if gen_sdk --action validate --database testdata/dupe_req.textpb; then
81    echo "FAILED: expected duplicate module in one sdk level to fail"
82    exit 1
83  fi
84
85  if gen_sdk --action validate --database testdata/backward_req.textpb; then
86    echo "FAILED: expect version requirement going backward to fail"
87    exit 1
88  fi
89
90  if gen_sdk --action validate --database testdata/unknown_module.textpb; then
91    echo "FAILED: expect sdk containing UNKNOWN module to be invalid"
92    exit 1
93  fi
94
95  if gen_sdk --action validate --database testdata/undefined_module.textpb; then
96    echo "FAILED: expect sdk containing undefined module to be invalid"
97    exit 1
98  fi
99
100  rm -f ${db} && touch ${db}
101  if gen_sdk --action new_sdk --sdk 1 --modules UNKNOWN --database ${db}; then
102    echo "FAILED: expected new sdk with UNKNOWN module to be invalid"
103    exit 1
104  fi
105
106  set -e
107}
108test_validate
109
110function test_dependent_modules() {
111  db=${tmp_dir}/extensions_db.textpb
112  rm -f ${db} && touch ${db}
113
114  set +e
115  for m in AD_SERVICES EXT_SERVICES; do
116    if gen_sdk --action new_sdk --sdk 9 --modules $m --database ${db}; then
117      echo "FAILED: expected new sdk with module $m to be invalid"
118      exit 1
119    fi
120  done
121  set -e
122
123  gen_sdk --action new_sdk --sdk 9 --modules AD_SERVICES,EXT_SERVICES --database ${db}
124}
125test_dependent_modules
126
127function test_checked_in_db() {
128  gen_sdk --action validate --database extensions_db.textpb
129}
130test_checked_in_db
131