-- 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 ncsi_protocol_intf = require 'ncsi_protocol_intf'
local ncsi_utils = require 'ncsi.ncsi_protocol.ncsi_utils'
local ncsi_def = require 'ncsi.ncsi_protocol.ncsi_def'
local ncsi_vlan_mode = require 'ncsi.ncsi_protocol.ncsi_vlan_mode'
local ncsi_parameter = require 'ncsi.ncsi_protocol.ncsi_parameter'

-- 测试套件
TestNCSIVlanMode = {}

-- 模拟发送NCSI命令的函数
local function mock_send_ncsi_cmd(req_data, len, eth_name)
    TestNCSIVlanMode.last_req_data = req_data
    TestNCSIVlanMode.last_len = len
    TestNCSIVlanMode.last_eth_name = eth_name
    return 0
end

-- 初始化函数，在每个测试用例前执行
function TestNCSIVlanMode:setUp()
    ncsi_parameter.get_instance():init_ncsi_parameter()
    -- 保存原始函数
    self.original_send_ncsi_cmd = ncsi_protocol_intf.send_ncsi_cmd
    self.original_cmd_ctrl = ncsi_utils.ncsi_cmd_ctrl

    -- 使用模拟函数替换原始函数
    ncsi_protocol_intf.send_ncsi_cmd = mock_send_ncsi_cmd

    -- 初始化测试变量
    self.last_req_data = nil
    self.last_len = nil
    self.last_eth_name = nil

    -- 初始化NCSI参数
    local ncsi_para = ncsi_parameter.get_instance():get_ncsi_parameter()
    ncsi_para.current_channel = 0

    -- 模拟cmd_ctrl函数以便测试
    ncsi_utils.ncsi_cmd_ctrl = function(package_id, channel_id, req_packet, eth_name, cmd_process_table)
        -- 确保cmd_process_table存在
        assert(cmd_process_table, "cmd_process_table is nil")

        -- 调用请求处理函数
        if req_packet and req_packet.packet_head and req_packet.packet_head.packet_type and
           cmd_process_table[req_packet.packet_head.packet_type] then
            cmd_process_table[req_packet.packet_head.packet_type](req_packet, eth_name)
        end

        -- 返回成功
        return ncsi_def.NCSI_SUCCESS
    end
end

-- 清理函数，在每个测试用例后执行
function TestNCSIVlanMode:tearDown()
    -- 恢复原始函数
    ncsi_protocol_intf.send_ncsi_cmd = self.original_send_ncsi_cmd
    ncsi_utils.ncsi_cmd_ctrl = self.original_cmd_ctrl
end

-- 测试启用VLAN模式
function TestNCSIVlanMode:test_enable_vlan_mode()
    -- 测试参数
    local package_id = 0
    local channel_id = 1
    local eth_name = "eth0"
    local vlan_mode = ncsi_vlan_mode.VLAN_ONLY

    -- 执行测试
    local result = ncsi_vlan_mode.ncsi_enable_vlan_req(package_id, channel_id, eth_name, vlan_mode)

    -- 验证结果
    lu.assertEquals(result, ncsi_def.NCSI_SUCCESS)
    lu.assertNotNil(self.last_req_data)
    lu.assertEquals(self.last_eth_name, eth_name)

    -- 解包请求数据进行验证
    local req_packet = ncsi_utils.ncsi_packet_bs:unpack(self.last_req_data, true)
    lu.assertEquals(req_packet.packet_head.package_id, package_id)
    lu.assertEquals(req_packet.packet_head.channel_id, channel_id)
    lu.assertEquals(req_packet.packet_head.packet_type, 0x0C) -- ENABLE_VLAN_REQ
end

-- 测试禁用VLAN模式
function TestNCSIVlanMode:test_disable_vlan_mode()
    -- 测试参数
    local package_id = 0
    local channel_id = 1
    local eth_name = "eth0"

    -- 执行测试
    local result = ncsi_vlan_mode.ncsi_disable_vlan_req(package_id, channel_id, eth_name)

    -- 验证结果
    lu.assertEquals(result, ncsi_def.NCSI_SUCCESS)
    lu.assertNotNil(self.last_req_data)
    lu.assertEquals(self.last_eth_name, eth_name)

    -- 解包请求数据进行验证
    local req_packet = ncsi_utils.ncsi_packet_bs:unpack(self.last_req_data, true)
    lu.assertEquals(req_packet.packet_head.package_id, package_id)
    lu.assertEquals(req_packet.packet_head.channel_id, channel_id)
    lu.assertEquals(req_packet.packet_head.packet_type, 0x0D) -- DISABLE_VLAN_REQ
end

-- 测试响应处理
function TestNCSIVlanMode:test_response_processing()
    -- 模拟响应包
    local function create_mock_response(rsp_code, reason_code)
        local rsp = {packet_head = {}, payload = ''}
        rsp.packet_head.payload_len_hi = 0
        rsp.packet_head.payload_len_lo = 4

        -- 创建响应payload
        local rsp_data = string.char(
            (rsp_code >> 8) & 0xFF, rsp_code & 0xFF,  -- rsp_code (2字节)
            (reason_code >> 8) & 0xFF, reason_code & 0xFF,  -- reason_code (2字节)
            0, 0, 0, 0  -- check_sum (4字节)
        ) .. string.rep("\0", 22)  -- pad_data (22字节)

        rsp.payload = rsp_data
        return rsp
    end

    -- 保存并替换ncsi_utils.ncsi_cmd_ctrl函数
    local original_cmd_ctrl = ncsi_utils.ncsi_cmd_ctrl

    -- 测试成功响应
    local success_rsp = create_mock_response(ncsi_def.CMD_COMPLETED, 0)
    ncsi_utils.ncsi_cmd_ctrl = function(package_id, channel_id, req_packet, eth_name, cmd_process_table)
        -- 直接调用响应处理函数
        return cmd_process_table[0x8C](success_rsp)  -- 0x8C 是ENABLE_VLAN_RSP
    end

    local result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", ncsi_vlan_mode.VLAN_ONLY)
    lu.assertEquals(result, ncsi_def.NCSI_SUCCESS)

    -- 测试失败响应
    local fail_rsp = create_mock_response(0x0123, 1)
    ncsi_utils.ncsi_cmd_ctrl = function(package_id, channel_id, req_packet, eth_name, cmd_process_table)
        return cmd_process_table[0x8C](fail_rsp)
    end

    result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", ncsi_vlan_mode.VLAN_ONLY)
    lu.assertEquals(result, ncsi_def.NCSI_FAIL)

    -- 恢复原始函数
    ncsi_utils.ncsi_cmd_ctrl = original_cmd_ctrl
end

-- 测试边界条件和错误处理
function TestNCSIVlanMode:test_error_handling()
    -- 保存并替换ncsi_utils.ncsi_cmd_ctrl函数
    local original_cmd_ctrl = ncsi_utils.ncsi_cmd_ctrl

    -- 模拟命令控制函数返回失败
    ncsi_utils.ncsi_cmd_ctrl = function(package_id, channel_id, req_packet, eth_name, cmd_process_table)
        return ncsi_def.NCSI_FAIL
    end

    -- 测试错误处理
    local result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", ncsi_vlan_mode.VLAN_ONLY)
    lu.assertEquals(result, ncsi_def.NCSI_FAIL)

    -- 测试无效的VLAN模式
    ncsi_utils.ncsi_cmd_ctrl = function(package_id, channel_id, req_packet, eth_name, cmd_process_table)
        -- 模拟请求处理成功但响应处理失败
        if cmd_process_table[0x0C] then
            cmd_process_table[0x0C](req_packet, eth_name)
        end
        return ncsi_def.NCSI_FAIL
    end

    result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", 4) -- 无效的VLAN模式
    lu.assertEquals(result, ncsi_def.NCSI_FAIL)

    -- 恢复原始函数
    ncsi_utils.ncsi_cmd_ctrl = original_cmd_ctrl
end

-- 测试不同的VLAN模式
function TestNCSIVlanMode:test_vlan_modes()
    -- 测试VLAN_ONLY模式
    local result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", ncsi_vlan_mode.VLAN_ONLY)
    lu.assertEquals(result, ncsi_def.NCSI_SUCCESS)

    -- 测试VLAN_NON_VLAN模式
    result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", 2) -- VLAN_NON_VLAN
    lu.assertEquals(result, ncsi_def.NCSI_SUCCESS)

    -- 测试ANYVLAN_NON_VLAN模式
    result = ncsi_vlan_mode.ncsi_enable_vlan_req(0, 1, "eth0", 3) -- ANYVLAN_NON_VLAN
    lu.assertEquals(result, ncsi_def.NCSI_SUCCESS)
end

-- 运行测试
os.exit(lu.LuaUnit.run())
