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 269 270 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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 366 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 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 577 578 579 580 581 582 583 584 585 586
|
import os
import platform
import sys
import time
import unittest
import libkcov
from libkcov import cobertura
class illegal_insn(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
@unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
def runTest(self):
rv, output = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/illegal-insn",
False,
)
assert rv != 0
assert b"Illegal instructions are" in output
class fork_no_wait(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/fork_no_wait")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/fork_no_wait",
False,
)
assert rv == noKcovRv
dom = cobertura.parseFile(self.outbase + "/kcov/fork_no_wait/cobertura.xml")
assert cobertura.hitsPerLine(dom, "fork-no-wait.c", 20) >= 1
assert cobertura.hitsPerLine(dom, "fork-no-wait.c", 22) >= 1
assert cobertura.hitsPerLine(dom, "fork-no-wait.c", 24) >= 1
class ForkBase(libkcov.TestCase):
def doTest(self, binary):
noKcovRv, o = self.doCmd(self.binaries + "/" + binary)
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/" + binary,
False,
)
assert rv == noKcovRv
dom = cobertura.parseFile(self.outbase + "/kcov/" + binary + "/cobertura.xml")
assert cobertura.hitsPerLine(dom, "fork.c", 21) == 0
assert cobertura.hitsPerLine(dom, "fork.c", 26) >= 1
assert cobertura.hitsPerLine(dom, "fork.c", 34) >= 1
assert cobertura.hitsPerLine(dom, "fork.c", 37) >= 1
assert cobertura.hitsPerLine(dom, "fork.c", 46) >= 1
class fork_64(ForkBase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
def runTest(self):
self.doTest("fork")
class vfork(libkcov.TestCase):
@unittest.skipIf(
sys.platform.startswith("darwin"),
"Not for OSX (does not work with the mach-engine for now)",
)
def runTest(self):
rv, o = self.do(self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/vfork", False)
assert rv == 0
dom = cobertura.parseFile(self.outbase + "/kcov/vfork/cobertura.xml")
assert cobertura.hitsPerLine(dom, "vfork.c", 12) >= 1
assert cobertura.hitsPerLine(dom, "vfork.c", 18) >= 1
class popen_test(libkcov.TestCase):
@unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/test_popen")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/test_popen",
False,
)
assert rv == noKcovRv
assert b"popen OK" in o
class short_filename(libkcov.TestCase):
@unittest.expectedFailure
def runTest(self):
rv, o = self.do(self.kcov + " " + self.outbase + "/kcov ./s", False)
assert rv == 99
class pie(libkcov.TestCase):
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/pie")
rv, o = self.do(self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/pie", False)
assert rv == noKcovRv
dom = cobertura.parseFile(self.outbase + "/kcov/pie/cobertura.xml")
assert cobertura.hitsPerLine(dom, "pie.c", 5) == 1
class pie_argv_basic(libkcov.TestCase):
def runTest(self):
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/pie-test",
False,
)
assert rv == 0
dom = cobertura.parseFile(self.outbase + "/kcov/pie-test/cobertura.xml")
assert cobertura.hitsPerLine(dom, "argv-dependent.c", 5) == 1
assert cobertura.hitsPerLine(dom, "argv-dependent.c", 11) == 0
class pie_accumulate(libkcov.TestCase):
@unittest.skipIf(
sys.platform.startswith("darwin"),
"Not for OSX (does not work with the mach-engine for now)",
)
def runTest(self):
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/pie-test",
False,
)
assert rv == 0
dom = cobertura.parseFile(self.outbase + "/kcov/pie-test/cobertura.xml")
assert cobertura.hitsPerLine(dom, "argv-dependent.c", 5) == 1
assert cobertura.hitsPerLine(dom, "argv-dependent.c", 11) == 0
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/pie-test a",
False,
)
assert rv == 0
dom = cobertura.parseFile(self.outbase + "/kcov/pie-test/cobertura.xml")
assert cobertura.hitsPerLine(dom, "argv-dependent.c", 5) == 1
assert cobertura.hitsPerLine(dom, "argv-dependent.c", 11) == 1
class daemon_wait_for_last_child(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX, Issue #158")
@unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/test_daemon")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/test_daemon",
False,
)
assert rv == 4
assert noKcovRv == 2
dom = cobertura.parseFile(self.outbase + "/kcov/test_daemon/cobertura.xml")
assert cobertura.hitsPerLine(dom, "test-daemon.cc", 31) == 1
class SignalsBase(libkcov.TestCase):
def cmpOne(self, sig):
noKcovRv, o = self.doCmd(self.binaries + "/signals " + sig + " " + self.m_self)
rv, o = self.do(
self.kcov
+ " "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/signals "
+ sig
+ " "
+ self.m_self,
False,
)
assert rv == noKcovRv
return cobertura.parseFile(self.outbase + "/kcov/signals/cobertura.xml")
def doTest(self):
dom = self.cmpOne("hup")
assert cobertura.hitsPerLine(dom, "test-signals.c", 14) == 1
dom = self.cmpOne("int")
assert cobertura.hitsPerLine(dom, "test-signals.c", 19) == 1
dom = self.cmpOne("quit")
assert cobertura.hitsPerLine(dom, "test-signals.c", 24) == 1
dom = self.cmpOne("bus")
assert cobertura.hitsPerLine(dom, "test-signals.c", 44) == 1
dom = self.cmpOne("fpe")
assert cobertura.hitsPerLine(dom, "test-signals.c", 49) == 1
dom = self.cmpOne("term")
assert cobertura.hitsPerLine(dom, "test-signals.c", 84) == 1
class signals(SignalsBase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX, Issue #158")
def runTest(self):
self.m_self = ""
self.doTest()
class signals_self(SignalsBase):
def runTest(self):
self.m_self = "self"
self.doTest()
class signals_crash(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX (macho-parser for now)")
def runTest(self):
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/signals segv self",
False,
)
assert b"kcov: Process exited with signal 11" in o
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/signals abrt self",
False,
)
assert b"kcov: Process exited with signal 6" in o
class collect_and_report_only(libkcov.TestCase):
# Cannot work with combined Engine / Parser
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/main-tests ")
rv, o = self.do(
self.kcov
+ " --collect-only "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/main-tests",
False,
)
self.skipTest("Fickle test, ignoring")
assert rv == noKcovRv
try:
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests/cobertura.xml")
self.fail("File unexpectedly found")
except Exception:
# Exception is expected here
pass
rv, o = self.do(
self.kcov + " --report-only " + self.outbase + "/kcov " + self.binaries + "/main-tests",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) == 1
class setpgid_kill(libkcov.TestCase):
def runTest(self):
noKcovRv, o = self.doCmd(
self.sources + "/tests/setpgid-kill/test-script.sh " + self.binaries + "/setpgid-kill"
)
assert b"SUCCESS" in o
rv, o = self.do(
self.sources
+ "/tests/setpgid-kill/test-script.sh "
+ self.kcov
+ " "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/setpgid-kill",
False,
)
assert b"SUCCESS" in o
class attach_process_with_threads(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
def runTest(self):
rv, o = self.do(
self.sources
+ "/tests/daemon/test-script.sh "
+ self.kcov
+ " "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/issue31",
False,
# 60 seconds
timeout=60
)
dom = cobertura.parseFile(self.outbase + "/kcov/issue31/cobertura.xml")
self.skipTest("Fickle test, ignoring")
assert cobertura.hitsPerLine(dom, "test-issue31.cc", 28) >= 1
assert cobertura.hitsPerLine(dom, "test-issue31.cc", 11) >= 1
assert cobertura.hitsPerLine(dom, "test-issue31.cc", 9) == 0
class attach_process_with_threads_creates_threads(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
def runTest(self):
rv, o = self.do(
self.sources
+ "/tests/daemon/test-script.sh "
+ self.kcov
+ " "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/thread-test",
False,
# 60 seconds
timeout=60
)
dom = cobertura.parseFile(self.outbase + "/kcov/thread-test/cobertura.xml")
self.skipTest("Fickle test, ignoring")
assert cobertura.hitsPerLine(dom, "thread-main.c", 21) >= 1
assert cobertura.hitsPerLine(dom, "thread-main.c", 9) >= 1
class merge_same_file_in_multiple_binaries(libkcov.TestCase):
def runTest(self):
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/multi_1",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/multi_1/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main_1.c", 10) == 0
assert cobertura.hitsPerLine(dom, "file.c", 3) == 0
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/multi_2",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/kcov-merged/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main_2.c", 9) == 1
assert cobertura.hitsPerLine(dom, "file.c", 3) == 0
assert cobertura.hitsPerLine(dom, "file.c", 8) == 0
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/multi_2 1",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/kcov-merged/cobertura.xml")
assert cobertura.hitsPerLine(dom, "file.c", 3) == 0
assert cobertura.hitsPerLine(dom, "file.c", 8) == 1
class debuglink(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
def runTest(self):
os.system(f"rm -rf {(self.outbase)}/.debug")
os.system(f"cp {self.binaries}/main-tests {self.binaries}/main-tests-debug-file")
os.system(
f"objcopy --only-keep-debug {self.binaries}/main-tests-debug-file {self.binaries}/main-tests-debug-file.debug"
)
os.system(
f"cp {self.binaries}/main-tests-debug-file.debug {self.binaries}/main-tests-debug-file1.debug"
)
os.system(
f"cp {self.binaries}/main-tests-debug-file.debug {self.binaries}/main-tests-debug-file12.debug"
)
os.system(
f"cp {self.binaries}/main-tests-debug-file.debug {self.binaries}/main-tests-debug-file123.debug"
)
os.system(
f"cp {self.binaries}/main-tests-debug-file {self.binaries}/main-tests-debug-file1"
)
os.system(
f"cp {self.binaries}/main-tests-debug-file {self.binaries}/main-tests-debug-file2"
)
os.system(
f"cp {self.binaries}/main-tests-debug-file {self.binaries}/main-tests-debug-file3"
)
os.system(f"strip -g {(self.binaries)}/main-tests-debug-file")
os.system(f"strip -g {(self.binaries)}/main-tests-debug-file1")
os.system(f"strip -g {(self.binaries)}/main-tests-debug-file2")
os.system(f"strip -g {(self.binaries)}/main-tests-debug-file3")
os.system(
f"objcopy --add-gnu-debuglink={self.binaries}/main-tests-debug-file.debug {self.binaries}/main-tests-debug-file"
)
os.system(
f"objcopy --add-gnu-debuglink={self.binaries}/main-tests-debug-file1.debug {self.binaries}/main-tests-debug-file1"
)
os.system(
f"objcopy --add-gnu-debuglink={self.binaries}/main-tests-debug-file12.debug {self.binaries}/main-tests-debug-file2"
)
os.system(
f"objcopy --add-gnu-debuglink={self.binaries}/main-tests-debug-file123.debug {self.binaries}/main-tests-debug-file3"
)
noKcovRv, o = self.doCmd(self.binaries + "/main-tests-debug-file")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/main-tests-debug-file",
False,
)
assert rv == noKcovRv
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests-debug-file/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) == 1
# Check alignment
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/main-tests-debug-file1",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests-debug-file1/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) == 1
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/main-tests-debug-file2",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests-debug-file2/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) == 1
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/main-tests-debug-file3",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests-debug-file3/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) == 1
# Look in .debug
os.system(f"rm -rf {(self.outbase)}/kcov")
os.system(f"mkdir -p {(self.binaries)}/.debug")
os.system(f"mv {self.binaries}/main-tests-debug-file.debug {self.binaries}/.debug")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/main-tests-debug-file",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests-debug-file/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) == 1
os.system(f"rm -rf {(self.outbase)}/kcov")
os.system(f"echo 'abc' >> {(self.binaries)}/.debug/main-tests-debug-file.debug")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/main-tests-debug-file",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-tests-debug-file/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 9) is None
# Todo: Look in /usr/lib/debug as well
class collect_no_source(libkcov.TestCase):
@unittest.expectedFailure
def runTest(self):
os.system(f"cp {self.sources}/tests/short-file.c {self.binaries}/main.cc")
os.system(f"gcc -g -o {self.binaries}/main-collect-only {self.binaries}/main.cc")
os.system(f"mv {self.binaries}/main.cc {self.binaries}/tmp-main.cc")
rv, o = self.do(
self.kcov
+ " --collect-only "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/main-collect-only",
False,
)
os.system(f"mv {self.binaries}/tmp-main.cc {self.binaries}/main.cc")
rv, o = self.do(
self.kcov
+ " --report-only "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/main-collect-only"
)
dom = cobertura.parseFile(self.outbase + "/kcov/main-collect-only/cobertura.xml")
assert cobertura.hitsPerLine(dom, "main.cc", 1) == 1
class dlopen(libkcov.TestCase):
@unittest.expectedFailure
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/dlopen")
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.binaries + "/dlopen",
False,
)
assert noKcovRv == rv
dom = cobertura.parseFile(self.outbase + "/kcov/dlopen/cobertura.xml")
assert cobertura.hitsPerLine(dom, "dlopen.cc", 11) == 1
assert cobertura.hitsPerLine(dom, "dlopen.cc", 12) == 0
assert cobertura.hitsPerLine(dom, "solib.c", 5) == 1
assert cobertura.hitsPerLine(dom, "solib.c", 15) == 0
class dlopen_in_ignored_source_file(libkcov.TestCase):
@unittest.expectedFailure
def runTest(self):
rv, o = self.do(
self.kcov
+ " --exclude-pattern=dlopen.cc "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/dlopen",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/dlopen/cobertura.xml")
assert cobertura.hitsPerLine(dom, "dlopen-main.cc", 10) == 1
assert cobertura.hitsPerLine(dom, "solib.c", 5) == 1
assert cobertura.hitsPerLine(dom, "solib.c", 15) == 0
class daemon_no_wait_for_last_child(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
@unittest.expectedFailure
def runTest(self):
noKcovRv, o = self.doCmd(self.binaries + "/test_daemon")
rv, o = self.do(
self.kcov
+ " --output-interval=1 --exit-first-process "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/test_daemon",
False,
)
assert noKcovRv == rv
time.sleep(2)
dom = cobertura.parseFile(self.outbase + "/kcov/test_daemon/cobertura.xml")
assert cobertura.hitsPerLine(dom, "test-daemon.cc", 31) == 0
time.sleep(5)
dom = cobertura.parseFile(self.outbase + "/kcov/test_daemon/cobertura.xml")
assert cobertura.hitsPerLine(dom, "test-daemon.cc", 31) == 1
class address_sanitizer_coverage(libkcov.TestCase):
@unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
@unittest.expectedFailure
def runTest(self):
if not os.path.isfile(self.binaries + "/sanitizer-coverage"):
self.skipTest("Clang only")
rv, o = self.do(
self.kcov
+ " --clang "
+ self.outbase
+ "/kcov "
+ self.binaries
+ "/sanitizer-coverage",
False,
)
dom = cobertura.parseFile(self.outbase + "/kcov/sanitizer-coverage/cobertura.xml")
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 5) == 1
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 7) == 1
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 8) == 1
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 16) == 1
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 18) == 1
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 22) == 0
assert cobertura.hitsPerLine(dom, "sanitizer-coverage.c", 25) == 0
# Issue #414
class outdir_is_executable(libkcov.TestCase):
def runTest(self):
# Running a system executable on Linux may cause ptrace to fails with
# "Operation not permitted", even with ptrace_scope set to 0.
# See https://www.kernel.org/doc/Documentation/security/Yama.txt
executable = self.sources + "/tests/python/short-test.py"
rv, o = self.do(self.kcov + " echo " + executable)
assert rv == 0
|