-- Copyright (c) 2024 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 c_drive = require 'drive.drive_object'
local drive_collection = require 'drive.drive_collection'
local common_def = require 'common_def'
local sml = require 'sml'

TestDriveGetPassthroughFwVersion = {}

-- ==================== 测试 update_drive_firmware_version (drive_object.lua 1622-1635) ====================

function TestDriveGetPassthroughFwVersion:test_update_drive_firmware_version_enable_request_false()
    local obj = setmetatable({
        enable_request = false,
        passthrough_firmware_version = "1.0.0"
    }, { __index = c_drive })
    obj:update_drive_firmware_version()
    lu.assertEquals(obj.passthrough_firmware_version, common_def.INVALID_STRING)
end

function TestDriveGetPassthroughFwVersion:test_update_drive_firmware_version_sml_failed()
    local orig_get_pd_firmware_version = sml.get_pd_firmware_version
    local called = false
    sml.get_pd_firmware_version = function(ref_controller_id, device_id)
        called = true
        error("test error")
    end

    local obj = setmetatable({
        enable_request = true,
        RefControllerId = 0,
        device_id = 1,
        passthrough_firmware_version = common_def.INVALID_STRING
    }, { __index = c_drive })
    obj:update_drive_firmware_version()
    lu.assertTrue(called)
    lu.assertEquals(obj.passthrough_firmware_version, common_def.INVALID_STRING)

    sml.get_pd_firmware_version = orig_get_pd_firmware_version
end

function TestDriveGetPassthroughFwVersion:test_update_drive_firmware_version_success()
    local orig_get_pd_firmware_version = sml.get_pd_firmware_version
    sml.get_pd_firmware_version = function(ref_controller_id, device_id)
        return "2.0.0"
    end

    local obj = setmetatable({
        enable_request = true,
        RefControllerId = 0,
        device_id = 1,
        passthrough_firmware_version = common_def.INVALID_STRING
    }, { __index = c_drive })
    obj:update_drive_firmware_version()
    lu.assertEquals(obj.passthrough_firmware_version, "2.0.0")

    sml.get_pd_firmware_version = orig_get_pd_firmware_version
end

function TestDriveGetPassthroughFwVersion:test_update_drive_firmware_version_empty_ret()
    local orig_get_pd_firmware_version = sml.get_pd_firmware_version
    sml.get_pd_firmware_version = function(ref_controller_id, device_id)
        return ""
    end

    local obj = setmetatable({
        enable_request = true,
        RefControllerId = 0,
        device_id = 1,
        passthrough_firmware_version = "1.0.0"
    }, { __index = c_drive })
    obj:update_drive_firmware_version()
    lu.assertEquals(obj.passthrough_firmware_version, "1.0.0")

    sml.get_pd_firmware_version = orig_get_pd_firmware_version
end
