backend.lua 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)

This file is part of the turris updater.

Updater is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Updater is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Updater.  If not, see <http://www.gnu.org/licenses/>.
]]--

require 'lunit'
local B = require 'backend'
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
22 23 24
require 'utils'

local lines2set = utils.lines2set
25 26 27

module("backend-tests", package.seeall, lunit.testcase)

28 29
local datadir = (os.getenv("S") or ".") .. "/tests/data/"

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
30 31
-- Tests for the block_parse function
function test_block_parse()
32 33 34 35 36
	-- Simple case
	assert_table_equal({
		val1 = "value 1",
		val2 = "value 2",
		val3 = "value 3"
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
37
	}, B.block_parse([[val1: value 1
38 39 40 41 42 43 44 45
val2:  value 2
val3:	value 3]]))
	-- Continuations of fields
	assert_table_equal({
		val1 = [[value 1
 line 2
 line 3]],
		val2 = "value 2"
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
46
	}, B.block_parse([[val1: value 1
47 48 49 50
 line 2
 line 3
val2: value 2]]))
	-- Continuation on the first line, several ways
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
51 52
	assert_error(function() B.block_parse(" x") end)
	assert_error(function() B.block_parse(" x: y") end)
53
	-- Some other strange lines
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
54 55
	assert_error(function() B.block_parse("xyz") end)
	assert_error(function() B.block_parse(" ") end)
56
end
57 58

--[[
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
59
Call the B.block_split on inputs. Then go in through the iterator
60 61 62 63
returned and in the outputs table in tandem, checking the things match.
]]
local function blocks_check(input, outputs)
	local exp_i, exp_v = next(outputs)
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
64
	for b in B.block_split(input) do
65 66 67 68 69 70 71
		assert_equal(exp_v, b)
		exp_i, exp_v = next(outputs, exp_i)
	end
	-- Nothing left.
	assert_nil(exp_i)
end

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
72 73
-- Tests for the block_split function.
function test_block_split()
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	-- Just splitting into blocks
	blocks_check([[block 1
next line
another line

block 2
multi line]], {[[block 1
next line
another line]], [[block 2
multi line]]})
	-- More than one empty line (should not produce extra empty block)
	blocks_check([[block 1


block 2]], {'block 1', 'block 2'})
	-- Few empty lines at the end - should not produce an empty block
	blocks_check([[block 1

block 2


]], {'block 1', 'block 2'})
	-- Few empty lines at the beginning - should not produce an empty block
end
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

--[[
Test post-processing packages. Examples taken and combined from real status file
(however, this exact package doesn't exist).
]]
function test_package_postprocces()
	local package = {
		Package = "dnsmasq-dhcpv6",
		Version = "2.73-1",
		Depends = "libc, kernel (= 3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10), kmod-nls-base",
		Status = "install user installed",
		Architecture = "mpc85xx",
		Conffiles = [[
 /etc/config/dhcp f81fe9bd228dede2165be71e5c9dcf76cc
 /etc/dnsmasq.conf 1e6ab19c1ae5e70d609ac7b6246541d520]]
	}
	local output = B.package_postprocess(package)
	-- Make sure it modifies the table in-place
	assert_equal(package, output)
	assert_table_equal({install = true, user = true, installed = true}, output.Status)
	assert_table_equal({["/etc/config/dhcp"] = "f81fe9bd228dede2165be71e5c9dcf76cc", ["/etc/dnsmasq.conf"] = "1e6ab19c1ae5e70d609ac7b6246541d520"}, output.Conffiles)
	assert_table_equal({"libc", "kernel (=3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10)", "kmod-nls-base"}, output.Depends)
	--[[
	Now check it doesn't get confused when some of the modified fields aren't there
	(or none, in this case).
	]]
	local pack_nomod = {
		Package = "wget",
		Version = "1.17.1-1",
		Architecture = "mpc85xx"
	}
	local pack_nomod_cp = {}
	for n, v in pairs(pack_nomod) do
		pack_nomod_cp[n] = v
	end
	local output = B.package_postprocess(pack_nomod)
	assert_not_equal(pack_nomod_cp, output)
	assert_equal(pack_nomod, output)
	assert_table_equal(pack_nomod_cp, output)
end
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
138 139 140 141

-- Tests for status_parse ‒ which parses the whole thing
function test_status_parse()
	local result = B.status_parse()
142
	local function status_check(name, desc)
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
143 144 145 146 147 148 149 150 151
		local pkg = result[name]
		assert_not_nil(pkg)
		assert_table_equal(desc, pkg)
	end
	local std_status = {install = true, user = true, installed = true}
	status_check("kmod-usb-storage", {
		Package = "kmod-usb-storage",
		Version = "3.18.21+10-1-70ea6b9a4b789c558ac9d579b5c1022f-10",
		Architecture = "mpc85xx",
152 153 154 155 156
		Source = "package/kernel/linux",
		License = "GPLv2",
		Section = "kernel",
		["Installed-Size"] = "22537",
		Description = "Kernel support for USB Mass Storage devices",
157 158 159 160 161 162 163 164 165 166 167 168
		["Installed-Time"] = "1453896142",
		Depends = {
			"kernel (=3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10)",
			"kmod-scsi-core",
			"kmod-usb-core"
		},
		Status = std_status,
		files = {
			["/lib/modules/3.18.21-70ea6b9a4b789c558ac9d579b5c1022f-10/usb-storage.ko"] = true,
			["/etc/modules-boot.d/usb-storage"] = true,
			["/etc/modules.d/usb-storage"] = true
		}
169
	})
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
170 171 172 173
	status_check("terminfo", {
		Package = "terminfo",
		Version = "5.9-2",
		Architecture = "mpc85xx",
174 175 176 177 178 179
		Source = "package/libs/ncurses",
		License = "MIT",
		LicenseFiles = "README",
		Section = "libs",
		["Installed-Size"] = "5822",
		Description = "Terminal Info Database (ncurses)",
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
		["Installed-Time"] = "1453896265",
		Depends = {"libc"},
		Status = std_status,
		files = {
			["/usr/share/terminfo/x/xterm"] = true,
			["/usr/share/terminfo/r/rxvt-unicode"] = true,
			["/usr/share/terminfo/d/dumb"] = true,
			["/usr/share/terminfo/a/ansi"] = true,
			["/usr/share/terminfo/x/xterm-color"] = true,
			["/usr/share/terminfo/r/rxvt"] = true,
			["/usr/share/terminfo/s/screen"] = true,
			["/usr/share/terminfo/x/xterm-256color"] = true,
			["/usr/share/terminfo/l/linux"] = true,
			["/usr/share/terminfo/v/vt100"] = true,
			["/usr/share/terminfo/v/vt102"] = true
		}
196
	})
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
197 198 199 200
	status_check("dnsmasq-dhcpv6", {
		Package = "dnsmasq-dhcpv6",
		Version = "2.73-1",
		Architecture = "mpc85xx",
201 202 203 204 205 206 207 208
		Source = "package/network/services/dnsmasq",
		License = "GPL-2.0",
		LicenseFiles = "COPYING",
		Section = "net",
		["Installed-Size"] = "142254",
		Description = [[It is intended to provide coupled DNS and DHCP service to a LAN.
 
 This is a variant with DHCPv6 support]],
209 210 211 212 213 214 215 216 217 218 219 220 221 222
		["Installed-Time"] = "1453896240",
		Depends = {"libc"},
		Status = std_status,
		files = {
			["/etc/dnsmasq.conf"] = true,
			["/etc/hotplug.d/iface/25-dnsmasq"] = true,
			["/etc/config/dhcp"] = true,
			["/etc/init.d/dnsmasq"] = true,
			["/usr/sbin/dnsmasq"] = true
		},
		Conffiles = {
			["/etc/config/dhcp"] = "f81fe9bd228dede2165be71e5c9dcf76cc",
			["/etc/dnsmasq.conf"] = "1e6ab19c1ae5e70d609ac7b6246541d520"
		}
223
	})
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
224 225 226 227 228
	-- Slightly broken package ‒ no relevant info files
	status_check("ucollect-count", {
		Package = "ucollect-count",
		Version = "27",
		Architecture = "mpc85xx",
229 230 231 232 233
		["Installed-Time"] = "1453896279",
		Depends = {"libc", "ucollect-prog"},
		Status = std_status,
		files = {}
	})
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
234 235 236
	-- More broken case - the whole status file missing
	B.status_file = "/does/not/exist"
	assert_error(B.status_parse)
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
237 238 239
end

local orig_status_file = B.status_file
240
local orig_info_dir = B.info_dir
241
local orig_root_dir = B.root_dir
242 243
local tmp_dirs = {}

244 245 246
--[[
Test the chain of functions ‒ unpack, examine
]]
247
function test_pkg_unpack()
248
	local path = B.pkg_unpack(utils.slurp(datadir .. "updater.ipk"))
249 250 251 252 253
	-- Make sure it is deleted on teardown
	table.insert(tmp_dirs, path)
	-- Check list of extracted files
	events_wait(run_command(function (ecode, killed, stdout)
		assert_equal(0, ecode, "Failed to check the list of files")
254
		assert_table_equal(lines2set([[.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
./control
./control/conffiles
./control/control
./control/postinst
./control/prerm
./data
./data/etc
./data/etc/config
./data/etc/config/updater
./data/etc/cron.d
./data/etc/init.d
./data/etc/init.d/updater
./data/etc/ssl
./data/etc/ssl/updater.pem
./data/usr
./data/usr/bin
./data/usr/bin/updater-resume.sh
./data/usr/bin/updater.sh
./data/usr/bin/updater-unstuck.sh
./data/usr/bin/updater-utils.sh
./data/usr/bin/updater-wipe.sh
./data/usr/bin/updater-worker.sh
./data/usr/share
./data/usr/share/updater
./data/usr/share/updater/hashes
./data/usr/share/updater/keys
./data/usr/share/updater/keys/release.pem
./data/usr/share/updater/keys/standby.pem
283
]]), lines2set(stdout))
284
	end, function () chdir(path) end, nil, -1, -1, "/usr/bin/find"))
285
	local files, dirs, conffiles, control = B.pkg_examine(path)
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	assert_table_equal(lines2set([[/etc/init.d/updater
/etc/config/updater
/etc/ssl/updater.pem
/usr/share/updater/keys/standby.pem
/usr/share/updater/keys/release.pem
/usr/bin/updater-resume.sh
/usr/bin/updater.sh
/usr/bin/updater-unstuck.sh
/usr/bin/updater-utils.sh
/usr/bin/updater-worker.sh
/usr/bin/updater-wipe.sh]]), files)
	assert_table_equal(lines2set([[/
/etc
/etc/init.d
/etc/cron.d
/etc/config
/etc/ssl
/usr
/usr/share
/usr/share/updater
/usr/share/updater/hashes
/usr/share/updater/keys
/usr/bin]]), dirs)
309 310 311
	assert_table_equal({
		["/etc/config/updater"] = "30843ef73412c8f6b4212c00724a1cc8"
	}, conffiles)
312 313 314 315 316 317 318 319 320 321 322
	assert_table_equal({
		Package = "updater",
		Version = "129",
		Source = "feeds/turrispackages/cznic/updater",
		Section = "opt",
		Maintainer = "Michal Vaner <michal.vaner@nic.cz>",
		Architecture = "mpc85xx",
		["Installed-Size"] = "14773",
		Description = "updater",
		Depends = {"libc", "vixie-cron", "openssl-util", "libatsha204", "curl", "cert-backup", "opkg", "bzip2", "cznic-cacert-bundle"}
	}, control)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	local test_root = mkdtemp()
	table.insert(tmp_dirs, test_root)
	B.root_dir = test_root
	-- Try merging it to a „root“ directory. We need to find all the files and directories.
	--[[
	Omit the empty directories. They wouldn't get cleared currently, and
	we want to test it. We may store list of directories in future.
	]]
	dirs["/usr/share/updater/hashes"] = nil
	dirs["/etc/cron.d"] = nil
	B.pkg_merge_files(path .. "/data", dirs, files, conffiles)
	-- The original directory disappeared.
	assert_table_equal({
		["control"] = "d"
	}, ls(path))
	events_wait(run_command(function (ecode, killed, stdout)
		assert_equal(0, ecode, "Failed to check the list of files")
		assert_table_equal(lines2set([[.
./etc
./etc/config
./etc/config/updater
./etc/init.d
./etc/init.d/updater
./etc/ssl
./etc/ssl/updater.pem
./usr
./usr/bin
./usr/bin/updater-resume.sh
./usr/bin/updater.sh
./usr/bin/updater-unstuck.sh
./usr/bin/updater-utils.sh
./usr/bin/updater-wipe.sh
./usr/bin/updater-worker.sh
./usr/share
./usr/share/updater
./usr/share/updater/keys
./usr/share/updater/keys/release.pem
./usr/share/updater/keys/standby.pem
]]), lines2set(stdout))
	end, function () chdir(test_root) end, nil, -1, -1, "/usr/bin/find"))
	-- Now try clearing the package. When we list all the files, it should remove the directories as well.
	B.pkg_cleanup_files(files)
	assert_table_equal({}, ls(test_root))
366
end
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
367

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
-- Test the collision_check function
function test_collisions()
	local status = B.status_parse()
	-- Just remove a package - no collisions, but files should disappear
	local col, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, {})
	assert_table_equal({}, col)
	assert_table_equal({
		["/lib/modules/3.18.21-70ea6b9a4b789c558ac9d579b5c1022f-10/usb-storage.ko"] = true,
		["/etc/modules-boot.d/usb-storage"] = true,
		["/etc/modules.d/usb-storage"] = true
	}, rem)
	-- Add a new package, but without any collisions
	local col, rem = B.collision_check(status, {}, {
		['package'] = {
			['/a/file'] = true
		}
	})
	assert_table_equal({}, col)
	assert_table_equal({}, rem)
	local test_pkg = {
		['package'] = {
			["/etc/modules.d/usb-storage"] = true
		}
	}
	-- Add a new package, collision isn't reported, because the original package owning it gets removed
	local col, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, test_pkg)
	assert_table_equal({}, col)
	assert_table_equal({
		["/lib/modules/3.18.21-70ea6b9a4b789c558ac9d579b5c1022f-10/usb-storage.ko"] = true,
		["/etc/modules-boot.d/usb-storage"] = true
		-- The usb-storage file is taken over, it doesn't disappear
	}, rem)
	-- A collision
	local col, rem = B.collision_check(status, {}, test_pkg)
	assert_table_equal({
403 404 405 406 407
		["/etc/modules.d/usb-storage"] = {
			["kmod-usb-storage"] = "existing",
			["package"] = "new"
		}
	}, col)
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	assert_table_equal({}, rem)
	-- A collision between two new packages
	test_pkg['another'] = test_pkg['package']
	local col, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, test_pkg)
	assert_not_equal({
		["/etc/modules.d/usb-storage"] = true
	}, utils.map(col, function (k) return k, true end))
	assert_table_equal({
		["package"] = "new",
		["another"] = "new"
	}, col["/etc/modules.d/usb-storage"])
	assert_table_equal({
		["/lib/modules/3.18.21-70ea6b9a4b789c558ac9d579b5c1022f-10/usb-storage.ko"] = true,
		["/etc/modules-boot.d/usb-storage"] = true
		-- The usb-storage file is taken over, it doesn't disappear
	}, rem)
end

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
426 427 428 429
function setup()
	local sdir = os.getenv("S") or "."
	-- Use a shortened version of a real status file for tests
	B.status_file = sdir .. "/tests/data/opkg/status"
430
	B.info_dir = sdir .. "/tests/data/opkg/info/"
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
431 432 433 434 435
end

function teardown()
	-- Clean up, return the original file name
	B.status_file = orig_status_file
436
	B.info_dir = orig_info_dir
437
	B.root_dir= orig_root_dir
438
	utils.cleanup_dirs(tmp_dirs)
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
439
end