123module exchange45import os6import src.history7import src.snapshot8import src.storage910fn strip_test_repo(name string) !storage.Store {11 dir := os.join_path(os.temp_dir(), 'koh3_strip_${os.getpid()}_${name}')12 os.rmdir_all(dir) or {}13 os.mkdir_all(dir)!14 mut store := storage.create(dir)!15 os.write_file(os.join_path(dir, '.kohignore'), '*.log\n')!16 return store17}1819fn strip_test_put(root string, rel string, text string) ! {20 full := os.join_path(root, rel)21 os.mkdir_all(os.dir(full))!22 os.write_file(full, text)!23}2425fn strip_test_save(store &storage.Store, message string) !history.Save {26 return history.save_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{27 message: message28 })!29}3031fn strip_test_folder(store &storage.Store, root string, name string) !string {32 for entry in store.manifest_entries(root)! {33 if entry.name == name && entry.kind == storage.folder_kind {34 return entry.manifest35 }36 }37 return error('no folder ${name}')38}3940fn test_stripping_one_save_leaves_a_sharing_save_untouched() {41 mut store := strip_test_repo('isolation')!42 defer {43 store.close()44 os.rmdir_all(store.root) or {}45 }46 strip_test_put(store.root, 'a/keep', 'keep')!47 strip_test_put(store.root, 'a/secret', 'secret')!48 strip_test_put(store.root, 'b/x', 'x')!49 first := strip_test_save(store, 'first')!50 strip_test_put(store.root, 'a/secret', 'secret two')!51 second := strip_test_save(store, 'second')!52 first_saved := history.by_id(store, first.id) or { panic('first save is missing') }53 second_saved := history.by_id(store, second.id) or { panic('second save is missing') }54 first_root := first_saved.manifest55 second_root := second_saved.manifest56 assert first_root != second_root57 shared_b := strip_test_folder(store, second_root, 'b')!58 first_b := strip_test_folder(store, first_root, 'b')!59 assert first_b == shared_b60 strip_local(store, 'a/secret', first.id)!61 first_paths := history.files_of(store, first.id)!62 assert first_paths.map(it.path) == ['.kohignore', 'a/keep', 'b/x']63 second_paths := history.files_of(store, second.id)!64 assert second_paths.map(it.path) == ['.kohignore', 'a/keep', 'a/secret', 'b/x']65 assert store.manifest_body(second_root)!.contains('"name":"b"')66 assert store.manifest_body(shared_b)!.contains('"name":"x"')67 gone := store.has_object(storage.hash_hex('secret'.bytes()))!68 assert !gone69}70717273fn test_stripped_object_bytes_leave_the_file() {74 mut store := strip_test_repo('scrub')!75 defer {76 store.close()77 os.rmdir_all(store.root) or {}78 }79 fresh := storage.open(store.root)!80 secure_delete := strip_test_pragma(fresh, 'PRAGMA secure_delete')81 fresh.close()82 assert secure_delete == 183 mut secret_text := ''84 for i in 0 .. 12 {85 mut text := 'strip-canary-${i} '86 for j in 0 .. 20 + i {87 text += 'koh-strip-${i}-${j} '88 }89 strip_test_put(store.root, 'secrets/f${i}', text)!90 if i == 0 {91 secret_text = text92 }93 }94 strip_test_save(store, 'secrets')!95 hash := storage.hash_hex(secret_text.bytes())96 blob := store.object_blob(hash)!.bytestr()97 strip_local(store, 'secrets', '')!98 assert !store.has_object(hash)!99 store.scrub()!100 raw := os.read_bytes(os.join_path(store.root, storage.file_name))!.bytestr()101 assert !raw.contains(blob)102 wal := os.join_path(store.root, '${storage.file_name}-wal')103 if os.exists(wal) {104 assert !os.read_bytes(wal)!.bytestr().contains(blob)105 }106 assert strip_test_pragma(store, 'PRAGMA freelist_count') == 0107}108109110fn strip_test_pragma(store &storage.Store, query string) i64 {111 mut stmt := store.db.prepare(query) or { panic(err.msg()) }112 defer {113 stmt.finalize()114 }115 if !stmt.step() or { return -1 } {116 return -1117 }118 return stmt.int(0)119}