| @@ -0,0 +1,265 @@ |
| +-- Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| +-- openUBMC is licensed under Mulan PSL v2. |
| +-- You can use this software according to the terms and conditions of the Mulan PSL v2. |
| +-- You may obtain a copy of Mulan PSL v2 at: |
| +-- http://license.coscl.org.cn/MulanPSL2 |
| +-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, |
| +-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, |
| +-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| +-- See the Mulan PSL v2 for more details. |
| + |
| +local lu = require('luaunit') |
| +local class = require('mc.class') |
| +local log = require('mc.logging') |
| + |
| +-- 用最小桩替换 stream.base:保留 ctor(mc.class 会自动调用父类 ctor,self.id 由此设置), |
| +-- 同时避免 unit 环境引入 stream.base 的额外依赖 |
| +local stream = class() |
| + |
| +function stream:ctor(property, bus_name) |
| + self.id = property.Id or 0 |
| + self.bus_name = bus_name |
| +end |
| + |
| +-- 注意:mc.class 的 new 在构造时会自动执行 pre_init/init, |
| +-- 因此用例构造对象即触发被测逻辑,无需再显式调用 init; |
| +-- 期望初始化失败的用例应对 gpio.new 做 pcall |
| +local original_stream = package.loaded['stream.base'] |
| +local original_gpio = package.loaded['stream.gpio'] |
| +package.loaded['stream.base'] = stream |
| +package.loaded['stream.gpio'] = nil |
| +local gpio = require('stream.gpio') |
| +package.loaded['stream.base'] = original_stream |
| +package.loaded['stream.gpio'] = original_gpio |
| + |
| +TestGpio = {} |
| + |
| +local RW_GPIO_GROUP = 0xff |
| + |
| +function TestGpio:setUp() |
| + self.open_drive = stream.open_drive |
| + self.log_warn = log.warn |
| + self.log_error = log.error |
| + self.driver = { |
| + init_calls = {}, |
| + write_calls = {}, |
| + fail_write = false, |
| + } |
| + self.driver.init = function(drv, id, direction) |
| + drv.init_calls[#drv.init_calls + 1] = { id = id, direction = direction } |
| + end |
| + self.driver.read = function(drv, id, direction) |
| + return 0 |
| + end |
| + self.driver.write = function(drv, id, level) |
| + drv.write_calls[#drv.write_calls + 1] = { id = id, level = level } |
| + if drv.fail_write then |
| + error('stub gpio write failed') |
| + end |
| + end |
| + stream.open_drive = function(driver_name) |
| + lu.assertEquals(driver_name, 'libsoc_adapter.gpio') |
| + return self.driver |
| + end |
| +end |
| + |
| +-- log.warn/log.error 桩的恢复放在 tearDown 兜底:用例中途失败也不会污染后续用例 |
| +function TestGpio:tearDown() |
| + stream.open_drive = self.open_drive |
| + log.warn = self.log_warn |
| + log.error = self.log_error |
| +end |
| + |
| +-- 配了 InitialLevel + 输出脚 → 下发一次 |
| +function TestGpio:test_initial_level_applied_when_configured_output() |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = 1 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 1) |
| + lu.assertEquals(self.driver.write_calls[1].id, 5) |
| + lu.assertEquals(self.driver.write_calls[1].level, 1) |
| +end |
| + |
| +-- 显式配置为 0 也要下发,以区别于「未配置」 |
| +function TestGpio:test_initial_level_zero_configured_still_writes() |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = 0 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 1) |
| + lu.assertEquals(self.driver.write_calls[1].level, 0) |
| +end |
| + |
| +-- 未配 → 完全不动作 |
| +function TestGpio:test_no_write_when_initial_level_absent() |
| + gpio.new({ Id = 5, Direction = 1 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 输入脚 → 跳过并告警,初始化本身成功 |
| +function TestGpio:test_no_write_for_input_pin() |
| + local warned = {} |
| + log.warn = function(_, fmt, ...) |
| + warned[#warned + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 0, InitialLevel = 1 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#warned, 1) |
| + lu.assertStrContains(warned[1], 'ignored for input pin') |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- Direction 未配置时默认输入,不按 InitialLevel 修改电平 |
| +function TestGpio:test_no_write_when_direction_absent() |
| + gpio.new({ Id = 5, InitialLevel = 1 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 输出极化位 = 1 → 逻辑 1 写成物理 0 |
| +function TestGpio:test_initial_level_with_output_polarity_reversal() |
| + gpio.new({ Id = 5, Direction = 1, ReverseBit = 0x02, InitialLevel = 1 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 1) |
| + lu.assertEquals(self.driver.write_calls[1].level, 0) |
| +end |
| + |
| +-- 显式 0 + 输出极化反转 → 物理 1:锁定显式 0 在反转分支下的语义 |
| +function TestGpio:test_initial_level_zero_with_output_polarity_reversal() |
| + gpio.new({ Id = 5, Direction = 1, ReverseBit = 0x02, InitialLevel = 0 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 1) |
| + lu.assertEquals(self.driver.write_calls[1].level, 1) |
| +end |
| + |
| +-- CSR 未配置时属性层回落无效默认值 0xFF → 等价于未配置,静默不动作 |
| +function TestGpio:test_initial_level_invalid_default_ignored() |
| + local warned = {} |
| + log.warn = function(_, fmt, ...) |
| + warned[#warned + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = 0xff }, 'Gpio_1') |
| + |
| + lu.assertEquals(#warned, 0) |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 字符串形式回落无效默认值(如 "255")同样视为未配置:tonumber 归一化后匹配 0xFF,静默不动作 |
| +function TestGpio:test_initial_level_string_invalid_default_ignored() |
| + local logged = {} |
| + log.error = function(_, fmt, ...) |
| + logged[#logged + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = '255' }, 'Gpio_1') |
| + |
| + lu.assertEquals(#logged, 0) |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 十六进制字符串 "0xFF" 同样归一化为 255 并视为未配置 |
| +function TestGpio:test_initial_level_hex_string_invalid_default_ignored() |
| + local logged = {} |
| + log.error = function(_, fmt, ...) |
| + logged[#logged + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = '0xFF' }, 'Gpio_1') |
| + |
| + lu.assertEquals(#logged, 0) |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 配置层以字符串下发数值(如 "1")→ tonumber 归一化后正常下发 |
| +function TestGpio:test_initial_level_string_value_normalized() |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = '1' }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 1) |
| + lu.assertEquals(self.driver.write_calls[1].level, 1) |
| +end |
| + |
| +-- 字符串 "0" 归一化后按数值 0 下发(区别于未配置;锁定 tonumber("0")→0 真值路径) |
| +function TestGpio:test_initial_level_string_zero_normalized() |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = '0' }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 1) |
| + lu.assertEquals(self.driver.write_calls[1].level, 0) |
| +end |
| + |
| +-- 非数值字符串归一化失败 → 按非法值处理,记 error 日志并跳过下发 |
| +function TestGpio:test_initial_level_non_numeric_string_skipped() |
| + local logged = {} |
| + log.error = function(_, fmt, ...) |
| + logged[#logged + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = 'abc' }, 'Gpio_1') |
| + |
| + lu.assertEquals(#logged, 1) |
| + lu.assertStrContains(logged[1], 'Invalid') |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 非法值(非 0/1)→ 记 error 日志并跳过下发,不阻断初始化 |
| +function TestGpio:test_invalid_initial_level_skipped_for_output_pin() |
| + local logged = {} |
| + log.error = function(_, fmt, ...) |
| + logged[#logged + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = 0x03 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#logged, 1) |
| + lu.assertStrContains(logged[1], 'Invalid') |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 输入脚无论 InitialLevel 合法与否都忽略:非法值不阻断初始化,仅告警、不下发 |
| +function TestGpio:test_invalid_initial_level_ignored_for_input_pin() |
| + local warned = {} |
| + log.warn = function(_, fmt, ...) |
| + warned[#warned + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 0, InitialLevel = 0x03 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#warned, 1) |
| + lu.assertStrContains(warned[1], 'ignored for input pin') |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 组模式(Id == 255)不解析 InitialLevel:不产生写操作 |
| +function TestGpio:test_no_write_in_group_mode() |
| + gpio.new({ Id = RW_GPIO_GROUP, Direction = 1, InitialLevel = 1, Bit0GpioNum = 10, Bit1GpioNum = 11 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 组模式(Id == 255)静默忽略 InitialLevel:不告警、不产生写操作 |
| +function TestGpio:test_group_mode_ignores_initial_level_silently() |
| + local warned = {} |
| + log.warn = function(_, fmt, ...) |
| + warned[#warned + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = RW_GPIO_GROUP, Direction = 1, InitialLevel = 1, Bit0GpioNum = 10, Bit1GpioNum = 11 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#warned, 0) |
| + lu.assertEquals(#self.driver.write_calls, 0) |
| +end |
| + |
| +-- 下发失败(硬件写失败)→ 记 error 日志,不阻断初始化 |
| +function TestGpio:test_write_failure_skipped() |
| + self.driver.fail_write = true |
| + |
| + local logged = {} |
| + log.error = function(_, fmt, ...) |
| + logged[#logged + 1] = string.format(fmt, ...) |
| + end |
| + |
| + gpio.new({ Id = 5, Direction = 1, InitialLevel = 1 }, 'Gpio_1') |
| + |
| + lu.assertEquals(#logged, 1) |
| + lu.assertStrContains(logged[1], 'apply failed') |
| + lu.assertEquals(#self.driver.write_calls, 1) -- 尝试下发过一次 |
| +end |
💡 建议:未配置默认值仅归一化 0xFF,空字符串等其余占位值会误触发 error 日志
ctor中if tonumber(initial_level) == GPIO_INVALID_LEVEL then initial_level = nil end只归一化了0xFF/255。若属性层以其它「未配置」占位值回落(如空字符串""/ 纯空白),tonumber("")为 nil,不等于 255,会被当作「已配置」保留下来;随后init内tonumber("")仍为 nil,非 0/1,落入else分支打印log:error 'Invalid...',而同一「未配置」语义下0xFF却是静默不动作,行为不一致。这虽与 CHANGELOG「未配置(含属性层无效默认值 0xFF)不下发」仅覆盖 0xFF 的口径一致,但既然这里已引入归一化逻辑,建议一并覆盖空串/空白等占位值,使所有「未配置」形态都静默不动作。
修复建议:归一化时把 nil 化扩展到所有无法解析为值的占位:
local raw = property.InitialLevel local n = tonumber(raw) if raw == nil or n == nil or n == GPIO_INVALID_LEVEL then initial_level = nil else initial_level = raw end(或在
init校验前统一先tonumber,对 nil 结果同样走「未配置」静默路径,与下方 init 口径保持一致。)