1# Lint as: python3
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"""Tests for target_file_handler."""
18
19import unittest
20from target_file_handler import *
21
22class BuildFileHandlerTest(unittest.TestCase):
23
24  def test_get_hash(self):
25    """Test TargetFileHandler could get hash from target file."""
26    build_file = './testdata/base_build_target-files.zip'
27    handler = TargetFileHandler(build_file)
28    deqp_deps = ['/system/deqp_dependency_file_a.so', '/vendor/deqp_dependency_file_b.so',
29                 '/vendor/file_not_exists.so']
30    hash_dict = handler.get_file_hash(deqp_deps)
31
32    self.assertEqual(hash_dict['/system/deqp_dependency_file_a.so'],
33                     hash(b'placeholder\nplaceholder\n'))
34    self.assertEqual(hash_dict['/vendor/deqp_dependency_file_b.so'],
35                     hash(b'placeholder\nplaceholder\nplaceholder\n\n'))
36    self.assertEqual(len(hash_dict), 2)
37
38  def test_get_system_fingerprint(self):
39    """Test TargetFileHandler could get SYSTEM fingerprint from target file."""
40    build_file = './testdata/base_build_target-files.zip'
41    handler = TargetFileHandler(build_file)
42    self.assertEqual(('generic/aosp_cf_x86_64_phone/vsoc_x86_64:S/AOSP.MASTER/7363308:'
43                      'userdebug/test-keys'), handler.get_system_fingerprint())
44
45  def test_get_system_fingerprint_without_buildprop(self):
46    """Test TargetFileHandler get fingerprint raises exception if build.prop doesn't exist."""
47    build_file = './testdata/current_build_target-files.zip'
48    handler = TargetFileHandler(build_file)
49    with self.assertRaises(KeyError):
50      handler.get_system_fingerprint()
51
52
53if __name__ == '__main__':
54  unittest.main()
55