1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2023 Huawei Device Co., Ltd. 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 19from .base_rule import BaseRule 20 21class SystemParameterRule(BaseRule): 22 RULE_NAME = "NO-Config-SystemParameter-In-INIT" 23 CONFIG_DAC_MAX_NUM = 200 24 25 def _check_param_name(self, param_name, empty_flag): 26 # len: (0, 96] 27 # Only allow alphanumeric, plus '.', '-', '@', ':', or '_'/ 28 # Don't allow ".." to appear in a param name 29 if len(param_name) > 96 or len(param_name) < 1 or param_name[0] == '.' or '..' in param_name: 30 return False 31 32 if empty_flag is False: 33 if param_name[-1] == '.': 34 return False 35 36 if param_name == "#": 37 return True 38 39 for char_value in param_name: 40 if char_value in '._-@:': 41 continue 42 43 if char_value.isalnum(): 44 continue 45 return False 46 return True 47 48 def _check_Param_in_init(self): 49 passed = True 50 value_empty_flag = True 51 white_list =self.get_white_lists() 52 parser = self.get_mgr().get_parser_by_name('system_parameter_whitelist') 53 counts = 0 54 for key, item in parser._parameters.items(): 55 if (item.get("dacMode") != 0): 56 counts += 1 57 if str(item)[-1] == "=": 58 value_empty_flag = True 59 else: 60 value_empty_flag = False 61 62 if not self._check_param_name(key, value_empty_flag): 63 self.error("Invalid param: %s" % key) 64 continue 65 if key in white_list: 66 continue 67 if counts > SystemParameterRule.CONFIG_DAC_MAX_NUM: 68 self.error("DAC overallocated memory") 69 passed = False 70 return passed 71 72 def __check__(self): 73 return self._check_Param_in_init() 74