1#!/usr/bin/python
2#
3# Copyright 2022 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"""Test cases for Trusty Linux Driver."""
18
19import os
20import unittest
21
22def ReadFile(file_path):
23    with open(file_path, 'r') as f:
24        # Strip all trailing spaces, newline and null characters.
25        return f.read().rstrip(' \n\x00')
26
27def WriteFile(file_path, s):
28    with open(file_path, 'w') as f:
29        f.write(s)
30
31def IsTrustySupported():
32    return os.path.exists("/dev/trusty-ipc-dev0")
33
34@unittest.skipIf(not IsTrustySupported(), "Device does not support Trusty")
35class TrustyDriverTest(unittest.TestCase):
36    def testIrqDriverBinding(self):
37        WriteFile("/sys/bus/platform/drivers/trusty-irq/unbind", "trusty:irq")
38        WriteFile("/sys/bus/platform/drivers/trusty-irq/bind", "trusty:irq")
39
40    def testLogDriverBinding(self):
41        WriteFile("/sys/bus/platform/drivers/trusty-log/unbind", "trusty:log")
42        WriteFile("/sys/bus/platform/drivers/trusty-log/bind", "trusty:log")
43
44    @unittest.skip("TODO(b/142275662): virtio remove currently hangs")
45    def testVirtioDriverBinding(self):
46        WriteFile("/sys/bus/platform/drivers/trusty-virtio/unbind",
47                  "trusty:virtio")
48        WriteFile("/sys/bus/platform/drivers/trusty-virtio/bind",
49                  "trusty:virtio")
50
51    @unittest.skip("TODO(b/142275662): virtio remove currently hangs")
52    def testTrustyDriverBinding(self):
53        WriteFile("/sys/bus/platform/drivers/trusty/unbind", "trusty")
54        WriteFile("/sys/bus/platform/drivers/trusty/bind", "trusty")
55
56    def testTrustyDriverVersion(self):
57        ver = ReadFile("/sys/bus/platform/devices/trusty/trusty_version")
58        self.assertTrue(ver.startswith("Project:"))
59
60    def testUntaintedLinux(self):
61        tainted = ReadFile("/proc/sys/kernel/tainted")
62        self.assertEqual(tainted, "0")
63
64    # stdcall test with shared memory buffers.
65    # Each test run takes up to 4 arguments:
66    # <obj_size>,<obj_count=1>,<repeat_share=1>,<repeat_access=3>
67    #
68    # Test single 4K shared memory object.
69    # Test 10 8MB objects, shared twice, each accessed twice. (8MB non-
70    # contiguous object is large enough to need several 4KB messages to
71    # describe)
72    # Test sharing 2 8MB objects 100 times without accessing it.
73    # Test 10 4K shared memory objects, shared 10 times, each accessed
74    # 10 times.
75    def testStdCall(self):
76        test = "/sys/devices/platform/trusty/trusty:test/trusty_test_run"
77        args = "0x1000 0x800000,10,2,2 0x800000,2,100,0 0x1000,10,10,10"
78        WriteFile(test, args)
79
80if __name__ == '__main__':
81  unittest.main()
82