| 1 | module nspkg |
| 2 | |
| 3 | import nspkg.engine |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import nspkg.tests.signaturefixtures |
| 6 | import os |
| 7 | |
| 8 | #flag -Wl,--wrap=calloc |
| 9 | #flag -Wl,--wrap=strdup |
| 10 | #flag -Wl,--wrap=gpgme_key_unref |
| 11 | |
| 12 | @[c_extern] |
| 13 | fn C.__real_calloc(uint, uint) voidptr |
| 14 | |
| 15 | @[c_extern] |
| 16 | fn C.__real_strdup(&u8) &u8 |
| 17 | |
| 18 | @[c_extern] |
| 19 | fn C.__real_gpgme_key_unref(voidptr) |
| 20 | |
| 21 | struct SignatureFaultState { |
| 22 | mut: |
| 23 | watch bool |
| 24 | fail_array bool |
| 25 | fail_string int |
| 26 | string_calls int |
| 27 | failures int |
| 28 | key voidptr |
| 29 | releases int |
| 30 | } |
| 31 | |
| 32 | const signature_fault_state = &SignatureFaultState{} |
| 33 | |
| 34 | @[export: '__wrap_calloc'] |
| 35 | fn signature_fault_calloc(count uint, size uint) voidptr { |
| 36 | mut state := unsafe { signature_fault_state } |
| 37 | if state != unsafe { nil } && state.watch && state.fail_array && size == sizeof(SigResult) && count == 3 { |
| 38 | state.fail_array = false |
| 39 | state.failures++ |
| 40 | C.errno = C.ENOMEM |
| 41 | return unsafe { nil } |
| 42 | } |
| 43 | return C.__real_calloc(count, size) |
| 44 | } |
| 45 | |
| 46 | @[export: '__wrap_strdup'] |
| 47 | fn signature_fault_strdup(value &u8) &u8 { |
| 48 | mut state := unsafe { signature_fault_state } |
| 49 | if state != unsafe { nil } && state.watch && state.fail_string > 0 { |
| 50 | state.string_calls++ |
| 51 | if state.string_calls == state.fail_string { |
| 52 | state.failures++ |
| 53 | C.errno = C.ENOMEM |
| 54 | return unsafe { nil } |
| 55 | } |
| 56 | } |
| 57 | return C.__real_strdup(value) |
| 58 | } |
| 59 | |
| 60 | @[export: '__wrap_gpgme_key_unref'] |
| 61 | fn signature_fault_unref(key voidptr) { |
| 62 | mut state := unsafe { signature_fault_state } |
| 63 | if state != unsafe { nil } && state.watch && state.key == key { state.releases++ } |
| 64 | C.__real_gpgme_key_unref(key) |
| 65 | } |
| 66 | |
| 67 | fn test_signature_conversion_allocation_failures_release_owned_keys() ! { |
| 68 | base := os.join_path(os.vtmp_dir(), 'alpm-signature-allocation-${os.getpid()}') |
| 69 | os.mkdir_all(base)! |
| 70 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 71 | ring := signaturefixtures.create(os.join_path(base, 'keys'))! |
| 72 | defer { signaturefixtures.cleanup(ring.path) or { eprintln(err) } } |
| 73 | path := os.join_path(base, 'package.pkg.tar') |
| 74 | archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = app\npkgver = 1-1\n' }], false)! |
| 75 | ring.sign(path, [ring.fingerprint], false)! |
| 76 | handle := initialize('/', base)! |
| 77 | defer { handle.release() or { eprintln(err) } } |
| 78 | handle.option_set_gpgdir(ring.path)! |
| 79 | for failure in 0 .. 3 { |
| 80 | mut core := handle.native_handle()! |
| 81 | original := core.check_signature_owned(path, none)! |
| 82 | assert original.results.len == 1 |
| 83 | key := original.key_data[0] |
| 84 | assert key != unsafe { nil } |
| 85 | // the public conversion takes the original reference; this extra one proves liveness. |
| 86 | C.gpgme_key_ref(unsafe { &C._gpgme_key(key) }) |
| 87 | defer { engine.release_signature_key(key) } |
| 88 | verification := engine.SignatureVerification{ |
| 89 | results: [original.results[0], |
| 90 | engine.SigResult{ key: engine.PGPKey{ fingerprint: 'A'.repeat(40) } }, |
| 91 | engine.SigResult{ key: engine.PGPKey{ fingerprint: 'B'.repeat(40) } }] |
| 92 | key_data: [key, unsafe { nil }, unsafe { nil }] |
| 93 | } |
| 94 | mut state := unsafe { signature_fault_state } |
| 95 | state.watch = true |
| 96 | state.fail_array = failure == 0 |
| 97 | state.fail_string = failure |
| 98 | state.string_calls = 0 |
| 99 | state.failures = 0 |
| 100 | state.key = key |
| 101 | state.releases = 0 |
| 102 | defer { state.watch = false } |
| 103 | mut accepted := false |
| 104 | if list := handle.native_siglist(verification) { |
| 105 | accepted = true |
| 106 | list.cleanup()! |
| 107 | } |
| 108 | state.watch = false |
| 109 | assert !accepted |
| 110 | assert state.failures == 1 |
| 111 | assert state.releases == 1 |
| 112 | assert handle.errno() == .memory |
| 113 | view := engine.signature_key_view(key) |
| 114 | assert unsafe { cstring_to_vstring(view.fingerprint) } == ring.fingerprint |
| 115 | } |
| 116 | } |
| 117 | |