1f6603c60Sopenharmony_ci#!/usr/bin/env python3
2f6603c60Sopenharmony_ci# -*- coding: utf-8 -*-
3f6603c60Sopenharmony_ci#
4f6603c60Sopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd.
5f6603c60Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6f6603c60Sopenharmony_ci# you may not use this file except in compliance with the License.
7f6603c60Sopenharmony_ci# You may obtain a copy of the License at
8f6603c60Sopenharmony_ci#
9f6603c60Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10f6603c60Sopenharmony_ci#
11f6603c60Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12f6603c60Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
13f6603c60Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14f6603c60Sopenharmony_ci# See the License for the specific language governing permissions and
15f6603c60Sopenharmony_ci# limitations under the License.
16f6603c60Sopenharmony_ci
17f6603c60Sopenharmony_cifrom devicetest.core.test_case import TestCase, Step
18f6603c60Sopenharmony_ciimport re
19f6603c60Sopenharmony_ci
20f6603c60Sopenharmony_ci# @tc.number: C-ALL-SECURITY-0304
21f6603c60Sopenharmony_ci# @tc.name: testRealRandomGenerate
22f6603c60Sopenharmony_ci# @tc.desc: 【C-ALL-SECURITY-0304】必须支持硬件真随机数生成,满足 FIPS140-2 随机测试标准。
23f6603c60Sopenharmony_ciclass testRealRandomGenerate(TestCase):
24f6603c60Sopenharmony_ci
25f6603c60Sopenharmony_ci    def __init__(self, controllers):
26f6603c60Sopenharmony_ci        self.TAG = self.__class__.__name__
27f6603c60Sopenharmony_ci        super().__init__(self.TAG, controllers)
28f6603c60Sopenharmony_ci
29f6603c60Sopenharmony_ci    def setup(self):
30f6603c60Sopenharmony_ci        Step("Setup")
31f6603c60Sopenharmony_ci
32f6603c60Sopenharmony_ci    def process(self):
33f6603c60Sopenharmony_ci        Step("Process")
34f6603c60Sopenharmony_ci        randomFile = self.device1.execute_shell_command("ls  /dev/random | wc -l").strip()
35f6603c60Sopenharmony_ci        assert randomFile == '1'
36f6603c60Sopenharmony_ci        urandomFile = self.device1.execute_shell_command("ls  /dev/urandom | wc -l").strip()
37f6603c60Sopenharmony_ci        assert urandomFile == '1'
38f6603c60Sopenharmony_ci        pattern = r'[a-z0-9]{10}'
39f6603c60Sopenharmony_ci        try:
40f6603c60Sopenharmony_ci            randomNumber = self.device1.execute_shell_command("cat /dev/random | head -n 10 | md5sum | head -c 10", timeout = 1000).strip()
41f6603c60Sopenharmony_ci        except:
42f6603c60Sopenharmony_ci            randomNumber = ''
43f6603c60Sopenharmony_ci        if not re.match(pattern, randomNumber):
44f6603c60Sopenharmony_ci            randomNumber = self.device1.execute_shell_command("cat /dev/urandom | head -n 10 | md5sum | head -c 10").strip()
45f6603c60Sopenharmony_ci        assert re.match(pattern, randomNumber)
46f6603c60Sopenharmony_ci
47f6603c60Sopenharmony_ci    def teardown(self):
48f6603c60Sopenharmony_ci        Step("Teardown")