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

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'
21
local print = print
22
local B = require 'backend'
23
require "syscnf"
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
24 25 26
require 'utils'

local lines2set = utils.lines2set
27 28 29

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

30
local datadir = (os.getenv("S") or ".") .. "/tests/data/"
31
local tmpdir = os.getenv("TMPDIR") or "/tmp"
32

33 34
-- Tests for the block_parse function
function test_block_parse()
35 36 37 38 39
	-- Simple case
	assert_table_equal({
		val1 = "value 1",
		val2 = "value 2",
		val3 = "value 3"
40 41 42
	}, B.block_parse(
[[
val1: value 1
43 44 45 46
val2:  value 2
val3:	value 3]]))
	-- Continuations of fields
	assert_table_equal({
47 48 49
		val1 =
[[
value 1
50 51 52
 line 2
 line 3]],
		val2 = "value 2"
53 54 55
	}, B.block_parse(
[[
val1: value 1
56 57 58 59
 line 2
 line 3
val2: value 2]]))
	-- Continuation on the first line, several ways
60 61
	assert_error(function() B.block_parse(" x") end)
	assert_error(function() B.block_parse(" x: y") end)
62
	-- Some other strange lines
63 64
	assert_error(function() B.block_parse("xyz") end)
	assert_error(function() B.block_parse(" ") end)
65
end
66 67

--[[
68
Call the B.block_split on inputs. Then go in through the iterator
69 70 71 72
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)
73
	for b in B.block_split(input) do
74 75 76 77 78 79 80
		assert_equal(exp_v, b)
		exp_i, exp_v = next(outputs, exp_i)
	end
	-- Nothing left.
	assert_nil(exp_i)
end

81 82
-- Tests for the block_split function.
function test_block_split()
83
	-- Just splitting into blocks
84 85 86
	blocks_check(
[[
block 1
87 88 89 90 91 92 93 94 95 96 97 98 99 100
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
101 102 103
	blocks_check(
[[
block 1
104 105 106 107 108 109 110

block 2


]], {'block 1', 'block 2'})
	-- Few empty lines at the beginning - should not produce an empty block
end
111 112 113 114 115 116 117 118 119 120 121 122

--[[
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",
123
		ChangedFiles = {},
124 125
		Conffiles =
[[
126 127 128 129 130 131
 /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)
132
	assert_table_equal({"install", "user", "installed"}, output.Status)
133
	assert_table_equal({["/etc/config/dhcp"] = "f81fe9bd228dede2165be71e5c9dcf76cc", ["/etc/dnsmasq.conf"] = "1e6ab19c1ae5e70d609ac7b6246541d520"}, output.Conffiles)
134
	assert_table_equal("libc, kernel (= 3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10), kmod-nls-base", output.Depends)
135 136 137 138 139 140 141
	--[[
	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",
142 143
		Architecture = "mpc85xx",
		ChangedFiles = {},
144 145 146 147 148 149 150 151 152 153
	}
	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
154 155 156 157

-- Tests for status_parse ‒ which parses the whole thing
function test_status_parse()
	local result = B.status_parse()
158
	local function status_check(name, desc)
159 160 161 162
		local pkg = result[name]
		assert_not_nil(pkg)
		assert_table_equal(desc, pkg)
	end
163
	local std_status = {"install", "user", "installed"}
164 165 166 167
	status_check("kmod-usb-storage", {
		Package = "kmod-usb-storage",
		Version = "3.18.21+10-1-70ea6b9a4b789c558ac9d579b5c1022f-10",
		Architecture = "mpc85xx",
168
		ChangedFiles = {},
169 170 171 172 173
		Source = "package/kernel/linux",
		License = "GPLv2",
		Section = "kernel",
		["Installed-Size"] = "22537",
		Description = "Kernel support for USB Mass Storage devices",
174
		["Installed-Time"] = "1453896142",
175
		Depends = "kernel (= 3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10), kmod-scsi-core, kmod-usb-core",
176 177 178 179 180 181
		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
		}
182
	})
183 184 185 186
	status_check("terminfo", {
		Package = "terminfo",
		Version = "5.9-2",
		Architecture = "mpc85xx",
187
		ChangedFiles = {},
188 189 190 191 192 193
		Source = "package/libs/ncurses",
		License = "MIT",
		LicenseFiles = "README",
		Section = "libs",
		["Installed-Size"] = "5822",
		Description = "Terminal Info Database (ncurses)",
194
		["Installed-Time"] = "1453896265",
195
		Depends = "libc",
196 197 198 199 200 201 202 203 204 205 206 207 208 209
		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
		}
210
	})
211 212 213 214
	status_check("dnsmasq-dhcpv6", {
		Package = "dnsmasq-dhcpv6",
		Version = "2.73-1",
		Architecture = "mpc85xx",
215
		ChangedFiles = {},
216 217 218 219 220 221 222 223
		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]],
224
		["Installed-Time"] = "1453896240",
225
		Depends = "libc",
226 227 228 229 230 231 232 233 234 235 236 237
		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"
		}
238
	})
239 240 241 242 243
	-- Slightly broken package ‒ no relevant info files
	status_check("ucollect-count", {
		Package = "ucollect-count",
		Version = "27",
		Architecture = "mpc85xx",
244
		ChangedFiles = {},
245
		["Installed-Time"] = "1453896279",
246
		Depends = "libc, ucollect-prog",
247 248 249
		Status = std_status,
		files = {}
	})
250
	-- More broken case - the whole status file missing
251
	syscnf.status_file = "/does/not/exist"
252
	assert_error(B.status_parse)
253 254
end

255 256
local tmp_dirs = {}

257 258 259
--[[
Test the chain of functions ‒ unpack, examine
]]
260
function test_pkg_unpack()
261
	syscnf.set_root_dir(tmpdir)
262
	local path = B.pkg_unpack(datadir .. "repo/updater.ipk")
263 264 265 266 267
	-- 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")
268 269 270
		assert_table_equal(lines2set(
[[
.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
./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
299
]]), lines2set(stdout))
300
	end, function () chdir(path) end, nil, -1, -1, "/usr/bin/find"))
301
	local files, dirs, conffiles, control = B.pkg_examine(path)
302 303 304
	assert_table_equal(lines2set(
[[
/etc/init.d/updater
305 306 307 308 309 310 311 312 313 314
/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)
315 316 317
	assert_table_equal(lines2set(
[[
/
318 319 320 321 322 323 324 325 326 327 328
/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)
329
	assert_table_equal({
330
		["/etc/config/updater"] = "b5cf279732a87011eadfe522a0c163b98682bef2919afc4f96330f9f103a3230"
331
	}, conffiles)
332 333 334
	-- We want to take it out, the time changes every time
	assert_not_nil(control["Installed-Time"])
	control["Installed-Time"] = nil
335 336 337 338 339 340 341 342 343
	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",
344
		Depends = "libc, vixie-cron, openssl-util, libatsha204, curl, cert-backup, opkg, bzip2, cznic-cacert-bundle",
345
		Conffiles = conffiles,
346
		Status = {"install", "user", "installed"},
347
		files = files
348
	}, control)
349 350
	local test_root = mkdtemp()
	table.insert(tmp_dirs, test_root)
351
	syscnf.set_root_dir(test_root)
352 353 354 355 356 357 358
	-- 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
359 360 361 362 363 364 365
	-- Prepare a config file that was modified by a user
	mkdir(test_root .. "/etc")
	mkdir(test_root .. "/etc/config")
	io.open(test_root .. "/etc/config/updater", "w"):close()
	B.pkg_merge_files(path .. "/data", dirs, files, {
		["/etc/config/updater"] = "12345678901234567890123456789012"
	})
366 367 368 369 370 371
	-- 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")
372 373 374
		assert_table_equal(lines2set(
[[
.
375 376 377
./etc
./etc/config
./etc/config/updater
378
./etc/config/updater-opkg
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
./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"))
398 399
	-- Delete the backed-up file, it is not tracked
	os.remove(test_root .. "/etc/config/updater-opkg")
400
	-- Now try clearing the package. When we list all the files, it should remove the directories as well.
401
	B.pkg_cleanup_files(files, {})
402
	assert_table_equal({}, ls(test_root))
403
end
404

405 406 407 408 409 410
function test_cleanup_files_config()
	local test_root = mkdtemp()
	table.insert(tmp_dirs, test_root)
	-- Create an empty testing file
	local fname = test_root .. "/config"
	io.open(fname, "w"):close()
411
	syscnf.set_root_dir(test_root)
412 413 414 415 416 417 418 419 420 421
	-- First try with a non-matching hash ‒ the file has been modified
	B.pkg_cleanup_files({["/config"] = true}, {["/config"] = "12345678901234567890123456789012"})
	-- It is left there
	assert_equal("r", stat(fname))
	-- But when it matches, it is removed
	B.pkg_cleanup_files({["/config"] = true}, {["/config"] = "d41d8cd98f00b204e9800998ecf8427e"})
	-- The file disappeared
	assert_nil(stat(fname))
end

422 423 424 425
-- Test the collision_check function
function test_collisions()
	local status = B.status_parse()
	-- Just remove a package - no collisions, but files should disappear
426
	local col, erem, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, {})
427
	assert_table_equal({}, col)
428
	assert_table_equal({}, erem)
429 430 431 432 433 434
	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
435
	local col, erem, rem = B.collision_check(status, {}, {
436 437 438 439 440
		['package'] = {
			['/a/file'] = true
		}
	})
	assert_table_equal({}, col)
441
	assert_table_equal({}, erem)
442 443 444 445 446 447 448
	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
449
	local col, erem, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, test_pkg)
450
	assert_table_equal({}, col)
451
	assert_table_equal({}, erem)
452 453 454 455 456 457
	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
458
	local col, erem, rem = B.collision_check(status, {}, test_pkg)
459
	assert_table_equal({
460
		["/etc/modules.d/usb-storage"] = {
461 462
			["kmod-usb-storage"] = "existing-file",
			["package"] = "new-file"
463 464
		}
	}, col)
465
	assert_table_equal({}, erem)
466 467 468
	assert_table_equal({}, rem)
	-- A collision between two new packages
	test_pkg['another'] = test_pkg['package']
469
	local col, erem, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, test_pkg)
470
	assert_table_equal({
471
		["/etc/modules.d/usb-storage"] = {
472 473
			["package"] = "new-file",
			["another"] = "new-file"
474 475
		}
	}, col)
476
	assert_table_equal({}, erem)
477 478 479 480 481
	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)
Karel Koci's avatar
Karel Koci committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	-- Packaage is updated and one of its original files should be removed
	local test_pkg = {
		['terminfo'] = {
			["/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/vt102"] = true
		}
	}
	local col, erem, rem = B.collision_check(status, {}, test_pkg)
	assert_table_equal({}, col)
	assert_table_equal({}, erem)
	assert_table_equal({["/usr/share/terminfo/v/vt100"] = true}, rem)
501
	-- Collision of file with new directory
502 503 504 505 506 507 508
	local test_pkg = {
		["package"] = {
			["/etc/modules.d/usb-storage/other-file"] = true,
			["/etc/modules.d/usb-storage/new-file"] = true,
			["/etc/test-package"] = true
		}
	}
509 510 511
	local col, erem, rem = B.collision_check(status, {}, test_pkg)
	assert_table_equal({
		["/etc/modules.d/usb-storage"] = {
512 513
			["package"] = "new-dir",
			["kmod-usb-storage"] = "existing-file"
514 515 516 517 518
		}
	}, col)
	assert_table_equal({}, erem)
	assert_table_equal({}, rem)
	-- Collision resolved with early file remove in favor of new directory
519 520 521 522 523 524 525 526 527 528 529
	local col, erem, rem = B.collision_check(status, {['kmod-usb-storage'] = true}, test_pkg)
	assert_table_equal({}, col)
	assert_table_equal({
		["package"] = {
			["/etc/modules.d/usb-storage"] = true,
		}
	}, erem)
	assert_table_equal({
		["/lib/modules/3.18.21-70ea6b9a4b789c558ac9d579b5c1022f-10/usb-storage.ko"] = true,
		["/etc/modules-boot.d/usb-storage"] = true,
	}, rem)
530
	-- Collision of directory with new file
531 532 533 534 535
	local test_pkg = {
		["package"] = {
			["/usr/share/terminfo"] = true,
		}
	}
536 537 538
	local col, erem, rem = B.collision_check(status, {}, test_pkg)
	assert_table_equal({
		["/usr/share/terminfo"] = {
539 540
			["package"] = "new-file",
			["terminfo"] = "existing-dir"
541 542 543 544 545 546
		}
	}, col)
	assert_table_equal({}, erem)
	assert_table_equal({}, rem)
	-- Collision resolved with early directory remove in favor of new file
	test_pkg.package["/etc/modules.d/usb-storage"] = true
547 548 549
	local col, erem, rem = B.collision_check(status, {['terminfo'] = true}, test_pkg)
	assert_table_equal({
		["/etc/modules.d/usb-storage"] = {
550 551
			["package"] = "new-file",
			["kmod-usb-storage"] = "existing-file"
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		}
	}, col)
	assert_table_equal({
		["package"] = {
			["/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
	} }, erem)
	assert_table_equal({}, rem)
	-- Collision that could be resolved by removing directory but new package requires it.
	test_pkg["package"]["/etc/modules.d/usb-storage"] = nil
	test_pkg["another"] = {
			["/usr/share/terminfo/test"] = true,
	}
	local col, erem, rem = B.collision_check(status, {['terminfo'] = true}, test_pkg)
	assert_table_equal({
		["/usr/share/terminfo"] = {
577 578
			["another"] = "new-dir",
			["package"] = "new-file"
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
		}
	}, col)
	-- Note that we don't care about erem and rem. Their content depends on order packages are processed.
	-- Collision that could be resolved by removing file, but existing and new package requires it.
	local test_pkg = {
		["package"] = {
			["/etc/modules.d/usb-storage/other-file"] = true,
			["/etc/modules.d/usb-storage/new-file"] = true,
		},
		["another"] = {
			["/etc/modules.d/usb-storage"] = true,
		}
	}
	local col, erem, rem = B.collision_check(status, {}, test_pkg)
	assert_table_equal({
		["/etc/modules.d/usb-storage"] = {
595 596 597
			["package"] = "new-dir",
			["another"] = "new-file",
			["kmod-usb-storage"] = "existing-file"
598 599 600
		}
	}, col)
	-- For "erem" and "rem" see note few lines before this one.
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	-- We encountered problem with bird4 and bird6 so test for it. It happens when final file has same name as directory leading to it.
	local test_pkg = {
		["bird6"] = {
			["/etc/bird6/init.d/bird6"] = true,
			["/etc/bird6/config"] = true
		},
		["bird4"] = {
			["/etc/bird4/init.d/bird4"] = true,
			["/etc/bird4/config"] = true
		}
	}
	local col, erem, rem = B.collision_check(status, {}, test_pkg)
	assert_table_equal({}, col)
	assert_table_equal({}, erem)
	assert_table_equal({}, rem)
616 617
end

618
-- Test config_steal and installed_confs function
619 620 621 622
function test_config_steal()
	local status = B.status_parse()
	-- Lets set dnsmasq-dhcpv6 as not installed
	status["dnsmasq-dhcpv6"].Status[3] = "not-installed"
623 624
	-- Prepare installed_confs table and check it
	local installed_confs = B.installed_confs(status)
625 626 627
	assert_table_equal({
		["/etc/config/dhcp"] = { pkg = "dnsmasq-dhcpv6", hash = "f81fe9bd228dede2165be71e5c9dcf76cc" },
		["/etc/dnsmasq.conf"] = { pkg = "dnsmasq-dhcpv6", hash = "1e6ab19c1ae5e70d609ac7b6246541d520" }
628
	}, installed_confs)
629 630 631 632
	-- Now lets steal one of the configs
	local stealed_confs = {
		["/etc/config/dhcp"] = status["dnsmasq-dhcpv6"].Conffiles["/etc/config/dhcp"]
	}
633
	local steal = B.steal_configs(status, installed_confs, { ["/etc/config/dhcp"] = "hash" }) -- note that hash is not used
634
	assert_table_equal(stealed_confs, steal)
635
	assert_not_nil(status["dnsmasq-dhcpv6"]) -- package is one more configuration file so it should be still there
636 637 638 639 640
	assert_nil(status["dnsmasq-dhcpv6"].Conffiles["/etc/config/dhcp"])
	-- Now lets steal second one
	stealed_confs = {
		["/etc/dnsmasq.conf"] = status["dnsmasq-dhcpv6"].Conffiles["/etc/dnsmasq.conf"]
	}
641
	steal = B.steal_configs(status, installed_confs, { ["/etc/dnsmasq.conf"] = "pkg_test2" })
642 643 644 645 646
	assert_table_equal(stealed_confs, steal)
	assert_nil(status["dnsmasq-dhcpv6"]) -- Now whole package should disappear

	status = B.status_parse()
	status["dnsmasq-dhcpv6"].Status[3] = "not-installed"
647
	local installed_confs = B.installed_confs(status)
648 649 650 651 652
	-- Lets try again but now with package that steals both config files
	stealed_confs = {
			["/etc/config/dhcp"] = status["dnsmasq-dhcpv6"].Conffiles["/etc/config/dhcp"],
			["/etc/dnsmasq.conf"] = status["dnsmasq-dhcpv6"].Conffiles["/etc/dnsmasq.conf"]
	}
653
	steal = B.steal_configs(status, installed_confs, {
654 655 656 657 658 659
		["/etc/config/dhcp"] = "hash",
		["/etc/dnsmasq.conf"] = "hash",
		["/etc/newone.conf"] = "hash"
	})
	assert_table_equal(stealed_confs, steal)
	assert_nil(status["dnsmasq-dhcpv6"]) -- Whole package should disappear
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

	status = B.status_parse()
	local installed_confs = B.installed_confs(status)
	-- Now let's steal from installed package
	stealed_confs = {
			["/etc/config/dhcp"] = status["dnsmasq-dhcpv6"].Conffiles["/etc/config/dhcp"],
			["/etc/dnsmasq.conf"] = status["dnsmasq-dhcpv6"].Conffiles["/etc/dnsmasq.conf"]
	}
	steal = B.steal_configs(status, installed_confs, {
		["/etc/config/dhcp"] = "hash",
		["/etc/dnsmasq.conf"] = "hash"
	})
	assert_table_equal(stealed_confs, steal)
	assert_not_nil(status["dnsmasq-dhcpv6"]) -- Package should be there
	assert_nil(next(status["dnsmasq-dhcpv6"].Conffiles)) -- But there should be no configuration files in that package
675 676
end

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
function test_block_dump_ordered()
	-- Empty block should produce empty output
	assert_equal('', B.block_dump_ordered({}))
	-- An ordinary block, nothing special
	assert_equal(
[[
Header: value
Header2: value2
]], B.block_dump_ordered({
		{ header = "Header", value = "value" },
		{ header = "Header2", value = "value2" }
	}))
	-- Repeated headers. Not that we would actually need that in practice.
	assert_equal(
[[
Header: value
Header: value
]], B.block_dump_ordered({
		{ header = "Header", value = "value" },
		{ header = "Header", value = "value" }
	}))
	-- An empty object generates nothing
	assert_equal(
[[
Header: value
Header: value
]], B.block_dump_ordered({
		{ header = "Header", value = "value" },
		{},
		{ header = "Header", value = "value" }
	}))
	-- A multi-line value
	assert_equal(
[[
Header:
 value
 another line
]], B.block_dump_ordered({
		{ header = "Header", value =
-- Since lua eats the first newline directly after [[, we need to provide two.
[[

 value
 another line]]}}))
end

function test_pkg_status_dump()
	-- Simple package with just one-line headers
	assert_equal(
[[
Package: pkg-name
Version: 1
Installed-Time: 1
]], B.pkg_status_dump({
	Package = "pkg-name",
	Version = "1",
	["Installed-Time"] = "1"
	}))
	-- Package with some extra (unused) headers
	assert_equal(
[[
Package: pkg-name
Version: 1
Installed-Time: 1
]], B.pkg_status_dump({
	Package = "pkg-name",
	Version = "1",
	["Installed-Time"] = "1",
	Extra = "xxxx"
	}))
	-- Package with more complex headers
	assert_equal(
[[
Package: pkg-name
Version: 1
Depends: dep1, dep2
Status: flag
Conffiles:
 file 1234567890123456
Installed-Time: 1
]], B.pkg_status_dump({
	Package = "pkg-name",
	Version = "1",
	["Installed-Time"] = "1",
	Extra = "xxxx",
762
	Depends = "dep1, dep2",
763
	Status = { "flag" },
764 765 766 767
	Conffiles = { ["file"] = "1234567890123456" }
	}))
end

768
function test_status_parse_dump()
769 770 771 772 773
	-- Read the status
	local status = B.status_parse()
	-- Make a copy of the status file, we'are going to write into it
	local test_dir = mkdtemp()
	table.insert(tmp_dirs, test_dir)
774
	syscnf.status_file = test_dir .. "/status"
775 776 777 778 779 780 781 782
	B.status_dump(status)
	-- Now read it again. It must be the same
	local status2 = B.status_parse()
	assert_table_equal(status, status2)
	-- Change something in the status. Add a new package
	status["New"] = {
		Package = "New",
		Version = "1",
783
		ChangedFiles = {},
784
		["Installed-Time"] = "1",
785
		Depends = "Dep1, dep2",
786
		Status = { "flag" }
787 788 789 790 791 792 793 794 795
	}
	-- Do one more store-read-compare cycle
	B.status_dump(status)
	local status3 = B.status_parse()
	-- The status_parse always generates list of files, even if there are none
	status["New"].files = {}
	assert_table_equal(status, status3)
end

796 797 798 799 800 801 802
function test_control_cleanup()
	--[[
	Create few files in a test info dir.
	Some of them are bit stange.
	]]
	local test_dir = mkdtemp() .. "/"
	table.insert(tmp_dirs, test_dir)
803
	syscnf.info_dir = test_dir
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	local all_files = {
		["pkg1.control"] = "r",
		["pkg1.list"] = "r",
		["pkg2.control"] = "r",
		["pkg2.xyz.abc"] = "r",
		[".bad"] = "r",
		["another_bad"] = "r"
	}
	for f in pairs(all_files) do
		local f, err = io.open(test_dir .. f, "w")
		assert_not_nil(f, err)
		f:close()
	end
	assert_table_equal(all_files, ls(test_dir))
	--[[
Karel Koci's avatar
Karel Koci committed
819
	Run the cleanup, but with both pkg1 and pkg2 installed. Also, the strange files should stay except pkg2.xyz.abc, because it would be for package pkg2.xyz.
820 821 822

	The control_cleanup doesn't care about the content of the packages, so be lazy a bit.
	]]
823 824 825 826 827 828
	local function pkg_gen(name)
		return {
			Package = name,
			Status = {"install", "user", "installed"}
		}
	end
Karel Koci's avatar
Karel Koci committed
829
	all_files["pkg2.xyz.abc"] = nil
830
	B.control_cleanup({
831 832
		pkg1 = pkg_gen "pkg1",
		pkg2 = pkg_gen "pkg2"
833 834 835
	})
	assert_table_equal(all_files, ls(test_dir))
	-- Drop the things belonging to pkg2
836
	B.control_cleanup({ pkg1 = pkg_gen "pkg1" })
837 838 839 840 841 842 843 844
	all_files["pkg2.control"] = nil
	assert_table_equal(all_files, ls(test_dir))
end

function test_merge_control()
	--[[
	Create a control file in some directory.
	]]
845
	local src_dir = mkdtemp()
846
	table.insert(tmp_dirs, src_dir)
847
	local f, err = io.open(src_dir .. "/control", "w")
848 849 850
	assert_not_nil(f, err)
	f:write("test\n")
	f:close()
851
	local dst_dir = mkdtemp()
852
	table.insert(tmp_dirs, dst_dir)
853
	syscnf.info_dir = dst_dir
854
	-- Place an "outdated" file in the destination, which should disappear by the merge
855
	local f, err = io.open(dst_dir .. "/pkg1.outdated", "w")
856 857 858 859 860 861
	assert_not_nil(f, err)
	f:write("Old\n")
	f:close()
	B.pkg_merge_control(src_dir, "pkg1", { file = true })
	-- The files are in the destination directory with the right content
	assert_table_equal({["pkg1.control"] = 'r', ["pkg1.list"] = 'r'}, ls(dst_dir))
862 863
	assert_equal("test\n", utils.read_file(dst_dir .. "/pkg1.control"))
	assert_equal("file\n", utils.read_file(dst_dir .. "/pkg1.list"))
864
	-- The file stayed at the origin as well
865
	assert_table_equal({["control"] = 'r'}, ls(src_dir))
866 867
end

868
function test_script_run()
869
	subprocess_kill_timeout(0) -- Run tests faster
870
	syscnf.info_dir = (os.getenv("S") or ".") .. "/tests/data/scripts"
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	-- This one doesn't exist. So the call succeeds.
	local result, stderr = B.script_run("xyz", "preinst", "install")
	assert(result)
	assert_nil(stderr)
	-- This one fails and outputs some of the data passed to it on stderr
	result, stderr = B.script_run("xyz", "postinst", "install")
	assert_false(result)
	assert_equal([[
install
PKG_ROOT=
]], stderr)
	-- This one doesn't have executable permission, won't be run
	result, stderr = B.script_run("xyz", "prerm", "remove")
	assert(result)
	assert_nil(stderr)
	-- This one terminates successfully
	result, stderr = B.script_run("xyz", "postrm", "remove")
	assert(result)
	assert_equal("test\n", stderr)
890 891 892 893 894 895 896
	-- This one hangs and should timeout, we set shorter timeout just to make test faster
	local old_cmd_timeout = B.cmd_timeout
	B.cmd_timeout = 1000
	result, stderr = B.script_run("hang", "postinst", "install")
	assert_false(result)
	assert_equal("", stderr)
	B.cmd_timeout = old_cmd_timeout
897 898
end

899 900 901 902 903 904
function test_config_modified()
	-- Bad length of the hash, no matter what file:
	assert_error(function() B.config_modified("/file/does/not/exist", "1234") end)
	-- If a file doesn't exist, it returns nil
	assert_nil(B.config_modified("/file/does/not/exist", "12345678901234567890123456789012"))
	-- We test on a non-config file, but it the same.
905
	local file = (os.getenv("S") or ".") .. "/tests/data/repo/updater.ipk"
906 907 908 909 910 911 912
	assert_false(B.config_modified(file, "182171ccacfc32a9f684479509ac471a"))
	assert(B.config_modified(file, "282171ccacfc32a9f684479509ac471b"))
	assert_false(B.config_modified(file, "4f54362b30f53ae6862b11ff34d22a8d4510ed2b3e757b1f285dbd1033666e55"))
	assert(B.config_modified(file, "5f54362b30f53ae6862b11ff34d22a8d4510ed2b3e757b1f285dbd1033666e56"))
	-- Case insensitive checks
	assert_false(B.config_modified(file, "182171CCACFC32A9F684479509AC471A"))
	assert_false(B.config_modified(file, "4F54362B30F53AE6862B11FF34D22A8D4510ED2B3E757B1F285DBD1033666E55"))
913 914 915 916 917
	-- Truncated sha256
	assert_false(B.config_modified(file, "4F54362B30F53AE6862B11FF34D22A8D4510ED2B3E757B1F285DBD10336"))
	assert_false(B.config_modified(file, "4F54362B30F53AE6862B11FF34D22A8D4510ED2B3E757B1F"))
	assert(B.config_modified(file, "5f54362b30f53ae6862b11ff34d22a8d4510ed2b3e757b1f285dbd1033666e"))
	assert(B.config_modified(file, "5f54362b30f53ae6862b11ff34d22a8d4510ed2b3e757b1f285db"))
918 919
end

920 921 922 923 924
function test_repo_parse()
	assert_table_equal({
		["base-files"] = {
			Package = "base-files",
			Version = "160-r49274",
925
			Depends = "libc, netifd, procd, jsonfilter"
926 927 928 929
		},
		["block-mount"] = {
			Package = "block-mount",
			Version = "2015-05-24-09027fc86babc3986027a0e677aca1b6999a9e14",
930
			Depends = "libc, ubox, libubox, libuci"
931 932 933 934 935 936 937 938 939 940 941 942
		}
	}, B.repo_parse([[
Package: base-files
Version: 160-r49274
Depends: libc, netifd, procd, jsonfilter

Package: block-mount
Version: 2015-05-24-09027fc86babc3986027a0e677aca1b6999a9e14
Depends: libc, ubox, libubox, libuci
]]))
end

943 944 945 946 947 948 949 950 951
function test_version_cmp()
	assert_equal(0, B.version_cmp("1.2.3", "1.2.3"))
	assert_equal(-1, B.version_cmp("1.2.3", "1.2.4"))
	assert_equal(1, B.version_cmp("1.3.3", "1.2.4"))
	assert_equal(-1, B.version_cmp("1.2.3", "1.2.3-2"))
	assert_equal(-1, B.version_cmp("1.2.3a", "1.2.3c"))
	assert_equal(1, B.version_cmp("1.10", "1.2"))
end

952 953 954 955 956 957 958 959 960 961 962 963
function test_version_match()
	assert_true(B.version_match("1.2.3", ">1.2.0"))
	assert_false(B.version_match("1.2.3", ">1.2.3"))
	assert_true(B.version_match("1.2.3", ">=1.2.3"))
	assert_true(B.version_match("1.2.3", "=>1.2.3"))
	assert_true(B.version_match("1.2.3", "<1.2.4"))
	assert_false(B.version_match("1.2.3", "<1.2.3"))
	assert_true(B.version_match("1.2.3", "~^1%..*$"))
	assert_false(B.version_match("1.2.3", "~^2%..*$"))
	assert_true(B.version_match("1.2.3", "1.2.3")) -- without comparator do exact match (just as corner case)
	assert_false(B.version_match("1.2.3", "1.3.3"))
end
964 965 966
function setup()
	local sdir = os.getenv("S") or "."
	-- Use a shortened version of a real status file for tests
967 968
	syscnf.status_file = sdir .. "/tests/data/opkg/status"
	syscnf.info_dir = sdir .. "/tests/data/opkg/info/"
969 970 971 972
end

function teardown()
	-- Clean up, return the original file name
973
	syscnf.set_root_dir()
974
	utils.cleanup_dirs(tmp_dirs)
975
	tmp_dirs = {}
976
	end