~aleteoryx/muditaos

ref: master muditaos/scripts/lua/test/test.lua -rw-r--r-- 8.2 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package.path = package.path .. ";../?.lua;../?/?.lua;../share/?.lua;../share/?/?.lua"

local helpers = require('helpers')
local lfs = require('lfs')

local recovery = {}

function recovery.version()
    return "0.0.0"
end

function recovery.branch()
    return "test"
end

function recovery.revision()
    return "BABEF00D"
end

local sys = {}
local bootctrl = {}
local gui = {}

sys.boot_reason_codes = {
    update = 0xF001,
    recovery = 0xF002,
    factory = 0xF003,
    pgm_keys = 0xF004,
    usb_mc_mode = 0xF005,
    backup = 0xF006,
    restore = 0xF007,
    os = 0xF008,
    unknown = 0xF009
}

bootctrl.slot = {
    a = 0,
    b = 1
}

function bootctrl.mark_as_active(slot)
end

function bootctrl.mark_as_bootable(slot)
end

function bootctrl.mark_as_unbootable(slot)
end

function bootctrl.mark_as_successful()
end

function bootctrl.get_current_slot()
    return bootctrl.slot.b
end

function bootctrl.get_next_active()
end

function sys.sleep(time)
end

function sys.set_os_boot_status()
end

function gui.clear()
end

function gui.display_raw_img(width, height, data)
end

-- Overwrite lfs.chdir method as we do not want to change working directory during running tests on the host
lfs.chdir = function(dir)
end

sys.user = stub()
sys.source_slot = stub()
sys.target_slot = stub()
sys.boot_reason = stub()
sys.boot_reason_str = stub()
sys.set_boot_reason = stub()
sys.flash_bootloader = stub()
sys.repartition_fs = stub()
sys.free_space = stub()

recovery.sys = sys
recovery.gui = gui
recovery.bootctrl = bootctrl

package.preload["recovery"] = function()
    return recovery
end

-- Used to recreate the original package.path during switching between products
local ppack = package.path

describe("Factory/backup/restore scripts", function()
    package.path = ppack .. ";../products/PurePhone/?.lua"
    recovery.sys.source_slot.returns("device/system")
    recovery.sys.target_slot.returns("device/target")
    recovery.sys.user.returns("device/user")

    local function invoke_entry()
        require('entry')
    end

    -- Force reload of the 'entry' module after execution of each unit test case
    after_each(function()
        package.loaded['entry'] = false
    end)

    it("invoke factory reset script", function()
        recovery.sys.free_space.returns(1024 * 1024 * 1024)
        recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.factory)
        recovery.sys.boot_reason_str.returns("factory")
        assert.has_no.error(invoke_entry)
    end)
    it("invoke backup script", function()
        recovery.sys.free_space.returns(1024 * 1024 * 1024)
        recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.backup)
        recovery.sys.boot_reason_str.returns("backup")
        assert.has_no.error(invoke_entry)
    end)
    it("invoke backup script, no free space", function()
        recovery.sys.free_space.returns(10)
        recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.backup)
        recovery.sys.boot_reason_str.returns("backup")
        assert.has_no.error(invoke_entry)
    end)
    it("invoke restore script", function()
        recovery.sys.free_space.returns(1024 * 1024 * 1024)
        recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.restore)
        recovery.sys.boot_reason_str.returns("restore")
        assert.has_no.error(invoke_entry)
    end)
end)

local function remove_test_package(path)
    if helpers.exists(path) then
        helpers.rmdir(path)
    end
end

local function extract_test_package(path, where)
    os.execute(string.format("tar xf %s -C %s", path, where))
end

describe("Restore script", function()
    recovery.sys.free_space.returns(1024 * 1024 * 1024)
    recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.restore)
    recovery.sys.boot_reason_str.returns("restore")
    recovery.sys.source_slot.returns("restore/system")
    recovery.sys.target_slot.returns("restore/target")
    recovery.sys.user.returns("restore/user")

    package.loaded['paths'] = false
    package.loaded['restore'] = false

    it("the same set of databases", function()
        -- Prepare test directory and its data
        remove_test_package("restore/system")
        remove_test_package("restore/user")
        extract_test_package("restore/restore1.tar", "restore")
        assert.has_no.error(require('restore').execute)
    end)

    it("legacy backup", function()
        -- Prepare test directory and its data
        remove_test_package("restore/system")
        remove_test_package("restore/user")
        extract_test_package("restore/restore2.tar", "restore")
        assert.has_no.error(require('restore').execute)
    end)

    it("not enough disk space", function()
        recovery.sys.free_space.returns(10)
        -- Prepare test directory and its data
        remove_test_package("restore/system")
        remove_test_package("restore/user")
        extract_test_package("restore/restore1.tar", "restore")
        assert.has.error(require('restore').execute)
    end)
end)

describe("Update script", function()
    recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.update)
    recovery.sys.boot_reason_str.returns("update")
    recovery.sys.source_slot.returns("update/system")
    recovery.sys.target_slot.returns("update/target")
    recovery.sys.user.returns("update/user")

    package.loaded['paths'] = false
    package.loaded['update'] = false

    -- Prepare test directory and its data
    remove_test_package("update/system")
    remove_test_package("update/target")
    remove_test_package("update/user/temp/update")
    extract_test_package("update/system.tar", "update")
    extract_test_package("update/target.tar", "update")
    extract_test_package("update/update.tar", "update/user/temp")

    it("invoke update script", function()
        local update = require('update')
        assert.has_no.error(update.execute)
    end)
end)

describe("Update script UDM - PurePhone", function()
    package.path = ppack .. ";../products/PurePhone/?.lua"
    recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.update)
    recovery.sys.boot_reason_str.returns("update")
    recovery.sys.source_slot.returns("update_udm/system")
    recovery.sys.target_slot.returns("update_udm/target")
    recovery.sys.user.returns("update_udm/user")

    package.loaded['paths'] = false
    package.loaded['update_udm'] = false
    package.loaded['update_product'] = false

    -- Prepare test directory and its data
    remove_test_package("update_udm/system")
    remove_test_package("update_udm/target")
    remove_test_package("update_udm/user")
    remove_test_package("update_udm/user/temp/update")

    extract_test_package("update_udm/system.tar", "update_udm")
    extract_test_package("update_udm/target.tar", "update_udm")
    extract_test_package("update_udm/user.tar", "update_udm")
    helpers.mkdirp("update_udm/user/temp")
    extract_test_package("update_udm/update.tar", "update_udm/user/temp")

    it("invoke update UDM script", function()
        local update = require('update_udm')
        assert.has_no.error(update.execute)
    end)
end)

describe("Update script UDM - BellHybrid", function()
    package.path = ppack .. ";../products/BellHybrid/?.lua"
    recovery.sys.boot_reason.returns(recovery.sys.boot_reason_codes.update)
    recovery.sys.boot_reason_str.returns("update")
    recovery.sys.source_slot.returns("update_udm/system")
    recovery.sys.target_slot.returns("update_udm/target")
    recovery.sys.user.returns("update_udm/user")

    package.loaded['paths'] = false
    package.loaded['update_udm'] = false
    package.loaded['update_product'] = false

    -- Prepare test directory and its data
    remove_test_package("update_udm/system")
    remove_test_package("update_udm/target")
    remove_test_package("update_udm/user")
    remove_test_package("update_udm/user/temp/update")

    extract_test_package("update_udm/system.tar", "update_udm")
    extract_test_package("update_udm/target.tar", "update_udm")
    extract_test_package("update_udm/user.tar", "update_udm")
    helpers.mkdirp("update_udm/user/temp")
    extract_test_package("update_udm/update.tar", "update_udm/user/temp")

    it("invoke update UDM script", function()
        local update = require('update_udm')
        assert.has_no.error(update.execute)
    end)
end)