|
| 1 | +package.path = "../src/lua-fibers/?.lua;" -- fibers submodule src |
| 2 | + .. "../src/lua-trie/src/?.lua;" -- trie submodule src |
| 3 | + .. "../src/lua-bus/src/?.lua;" -- bus submodule src |
| 4 | + .. "../src/?.lua;" |
| 5 | + .. "./test_utils/?.lua;" |
| 6 | + .. package.path |
| 7 | + .. ";/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua" |
| 8 | + |
| 9 | +local fiber = require "fibers.fiber" |
| 10 | +local context = require "fibers.context" |
| 11 | + |
| 12 | +local function test_uim_get_gids_simple() |
| 13 | + -- Clear cached module |
| 14 | + package.loaded["services.hal.drivers.modem.mode.qmi"] = nil |
| 15 | + |
| 16 | + -- Mock dependencies |
| 17 | + local mock_context = { |
| 18 | + with_timeout = function(ctx, timeout) |
| 19 | + return ctx |
| 20 | + end |
| 21 | + } |
| 22 | + |
| 23 | + local mock_cmd = { |
| 24 | + setpgid_called = false, |
| 25 | + setpgid = function(self, val) |
| 26 | + self.setpgid_called = true |
| 27 | + end, |
| 28 | + combined_output = function(self) |
| 29 | + local output = [[ |
| 30 | +[/dev/cdc-wdm1] Successfully read information from the UIM: |
| 31 | +Card result: |
| 32 | + SW1: '0x90' |
| 33 | + SW2: '0x00' |
| 34 | +Read result: |
| 35 | + FF]] |
| 36 | + return output, nil |
| 37 | + end |
| 38 | + } |
| 39 | + |
| 40 | + local mock_qmicli = { |
| 41 | + uim_read_transparent = function(ctx, port, id) |
| 42 | + return mock_cmd |
| 43 | + end |
| 44 | + } |
| 45 | + |
| 46 | + local mock_wraperr = { |
| 47 | + new = function(err) return err end |
| 48 | + } |
| 49 | + |
| 50 | + -- Load the qmi module with mocked dependencies |
| 51 | + package.loaded["services.hal.drivers.modem.qmicli"] = mock_qmicli |
| 52 | + package.loaded["fibers.context"] = mock_context |
| 53 | + package.loaded["wraperr"] = mock_wraperr |
| 54 | + package.loaded["services.hal.utils"] = require("services.hal.utils") |
| 55 | + package.loaded["services.log"] = require("services.log") |
| 56 | + |
| 57 | + local qmi_module = require("services.hal.drivers.modem.mode.qmi") |
| 58 | + |
| 59 | + -- Create a mock modem object |
| 60 | + local modem = { |
| 61 | + ctx = context.background(), |
| 62 | + primary_port = "/dev/cdc-wdm1" |
| 63 | + } |
| 64 | + |
| 65 | + -- Initialize the modem with qmi functions |
| 66 | + qmi_module(modem) |
| 67 | + |
| 68 | + -- Test the function |
| 69 | + local gids, err = modem.uim_get_gids() |
| 70 | + |
| 71 | + assert(err == nil, "expected err to be nil but got " .. tostring(err)) |
| 72 | + assert(gids ~= nil, "expected gids to be a table but got nil") |
| 73 | + assert(gids.gid1 == "FF", "expected gid1 to be 'FF' but got '" .. tostring(gids.gid1) .. "'") |
| 74 | + assert(mock_cmd.setpgid_called, "expected setpgid to be called") |
| 75 | +end |
| 76 | + |
| 77 | +local function test_uim_get_gids_complex() |
| 78 | + -- Clear cached module |
| 79 | + package.loaded["services.hal.drivers.modem.mode.qmi"] = nil |
| 80 | + |
| 81 | + -- Mock dependencies |
| 82 | + local mock_context = { |
| 83 | + with_timeout = function(ctx, timeout) |
| 84 | + return ctx |
| 85 | + end |
| 86 | + } |
| 87 | + |
| 88 | + local mock_cmd = { |
| 89 | + setpgid_called = false, |
| 90 | + setpgid = function(self, val) |
| 91 | + self.setpgid_called = true |
| 92 | + end, |
| 93 | + combined_output = function(self) |
| 94 | + local output = [[ |
| 95 | +[/dev/cdc-wdm1] Successfully read information from the UIM: |
| 96 | +Card result: |
| 97 | + SW1: '0x90' |
| 98 | + SW2: '0x00' |
| 99 | +Read result: |
| 100 | + 85:FF:FF:FF:FF:FF:47:45:4E:49:45:49:4E:20:20:20:20:20:20:20]] |
| 101 | + return output, nil |
| 102 | + end |
| 103 | + } |
| 104 | + |
| 105 | + local mock_qmicli = { |
| 106 | + uim_read_transparent = function(ctx, port, id) |
| 107 | + return mock_cmd |
| 108 | + end |
| 109 | + } |
| 110 | + |
| 111 | + local mock_wraperr = { |
| 112 | + new = function(err) return err end |
| 113 | + } |
| 114 | + |
| 115 | + -- Load the qmi module with mocked dependencies |
| 116 | + package.loaded["services.hal.drivers.modem.qmicli"] = mock_qmicli |
| 117 | + package.loaded["fibers.context"] = mock_context |
| 118 | + package.loaded["wraperr"] = mock_wraperr |
| 119 | + package.loaded["services.hal.utils"] = require("services.hal.utils") |
| 120 | + package.loaded["services.log"] = require("services.log") |
| 121 | + |
| 122 | + local qmi_module = require("services.hal.drivers.modem.mode.qmi") |
| 123 | + |
| 124 | + -- Create a mock modem object |
| 125 | + local modem = { |
| 126 | + ctx = context.background(), |
| 127 | + primary_port = "/dev/cdc-wdm1" |
| 128 | + } |
| 129 | + |
| 130 | + -- Initialize the modem with qmi functions |
| 131 | + qmi_module(modem) |
| 132 | + |
| 133 | + -- Test the function |
| 134 | + local gids, err = modem.uim_get_gids() |
| 135 | + |
| 136 | + assert(err == nil, "expected err to be nil but got " .. tostring(err)) |
| 137 | + assert(gids ~= nil, "expected gids to be a table but got nil") |
| 138 | + |
| 139 | + local expected = "85FFFFFFFFFF47454E4945494E20202020202020" |
| 140 | + assert(gids.gid1 == expected, |
| 141 | + "expected gid1 to be '" .. expected .. "' but got '" .. tostring(gids.gid1) .. "'") |
| 142 | + assert(mock_cmd.setpgid_called, "expected setpgid to be called") |
| 143 | +end |
| 144 | + |
| 145 | +local function test_uim_get_gids_error() |
| 146 | + -- Clear cached module |
| 147 | + package.loaded["services.hal.drivers.modem.mode.qmi"] = nil |
| 148 | + |
| 149 | + -- Mock dependencies |
| 150 | + local mock_context = { |
| 151 | + with_timeout = function(ctx, timeout) |
| 152 | + return ctx |
| 153 | + end |
| 154 | + } |
| 155 | + |
| 156 | + local mock_cmd = { |
| 157 | + setpgid_called = false, |
| 158 | + setpgid = function(self, val) |
| 159 | + self.setpgid_called = true |
| 160 | + end, |
| 161 | + combined_output = function(self) |
| 162 | + return "", "command failed" |
| 163 | + end |
| 164 | + } |
| 165 | + |
| 166 | + local mock_qmicli = { |
| 167 | + uim_read_transparent = function(ctx, port, id) |
| 168 | + return mock_cmd |
| 169 | + end |
| 170 | + } |
| 171 | + |
| 172 | + local mock_wraperr = { |
| 173 | + new = function(err) return "wrapped: " .. err end |
| 174 | + } |
| 175 | + |
| 176 | + -- Load the qmi module with mocked dependencies |
| 177 | + package.loaded["services.hal.drivers.modem.qmicli"] = mock_qmicli |
| 178 | + package.loaded["fibers.context"] = mock_context |
| 179 | + package.loaded["wraperr"] = mock_wraperr |
| 180 | + package.loaded["services.hal.utils"] = require("services.hal.utils") |
| 181 | + package.loaded["services.log"] = require("services.log") |
| 182 | + |
| 183 | + local qmi_module = require("services.hal.drivers.modem.mode.qmi") |
| 184 | + |
| 185 | + -- Create a mock modem object |
| 186 | + local modem = { |
| 187 | + ctx = context.background(), |
| 188 | + primary_port = "/dev/cdc-wdm1" |
| 189 | + } |
| 190 | + |
| 191 | + -- Initialize the modem with qmi functions |
| 192 | + qmi_module(modem) |
| 193 | + |
| 194 | + -- Test the function |
| 195 | + local gids, err = modem.uim_get_gids() |
| 196 | + |
| 197 | + assert(err ~= nil, "expected err to be set but got nil") |
| 198 | + assert(err == "wrapped: command failed", "expected wrapped error but got " .. tostring(err)) |
| 199 | + assert(gids ~= nil, "expected gids to be a table but got nil") |
| 200 | + assert(next(gids) == nil, "expected gids to be empty but it has values") |
| 201 | +end |
| 202 | + |
| 203 | +fiber.spawn(function () |
| 204 | + test_uim_get_gids_simple() |
| 205 | + test_uim_get_gids_complex() |
| 206 | + test_uim_get_gids_error() |
| 207 | + fiber.stop() |
| 208 | +end) |
| 209 | + |
| 210 | +print("running hal qmi tests") |
| 211 | +fiber.main() |
| 212 | +print("passed") |
0 commit comments