2024-09-11 11:14:03 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import defl
|
|
|
|
|
from defl import log, cl, Assert
|
2025-03-09 09:17:53 -04:00
|
|
|
from defl.testing_ import Tester, Test, TestState, testFail, atexit
|
2024-09-11 11:14:03 -04:00
|
|
|
|
|
|
|
|
import dataclasses, contextlib
|
|
|
|
|
import itertools
|
|
|
|
|
import inspect
|
|
|
|
|
from typing import *
|
|
|
|
|
from types import *
|
|
|
|
|
import typing
|
|
|
|
|
from defl._typeCheck_ import *
|
|
|
|
|
from defl._dataclass_ import *
|
2025-03-09 09:17:53 -04:00
|
|
|
from defl.pass_ import *
|
|
|
|
|
from defl.gocryptfs_ import *
|
|
|
|
|
from defl.lsblk_ import MountPoints
|
|
|
|
|
from defl.otp_ import OTP
|
2024-09-11 11:14:03 -04:00
|
|
|
|
2025-03-09 09:17:53 -04:00
|
|
|
defl.log.setLevel('i')
|
2024-09-11 11:14:03 -04:00
|
|
|
tester = Tester(name=__file__)
|
|
|
|
|
|
2025-03-09 09:17:53 -04:00
|
|
|
|
2024-09-11 11:14:03 -04:00
|
|
|
@tester.add()
|
2025-03-09 09:17:53 -04:00
|
|
|
def setup():
|
|
|
|
|
# == Gocryptfs
|
|
|
|
|
testRootPath = defl.sp.tmp.tempFile(direct=T, autoRemove=F, fmt='{r}')
|
|
|
|
|
testEncPath = testRootPath / 'enc'
|
|
|
|
|
testDecPath = testRootPath / 'dec'
|
|
|
|
|
log.info('testEncPath', lambda x: x.testEncPath.makeDir())
|
|
|
|
|
log.info('testDecPath', lambda x: x.testDecPath.makeDir())
|
|
|
|
|
gocryptfs = Gocryptfs.Initialize(
|
|
|
|
|
path=testEncPath,
|
|
|
|
|
passwd='1',
|
|
|
|
|
rootMountDir=testDecPath,
|
|
|
|
|
).mount(
|
|
|
|
|
idleMin=5,
|
|
|
|
|
passwd='1',
|
|
|
|
|
dontUseGpgUnlock=T,
|
2024-09-11 11:14:03 -04:00
|
|
|
)
|
2025-03-09 09:17:53 -04:00
|
|
|
|
|
|
|
|
@atexit.register
|
|
|
|
|
def _unmount():
|
|
|
|
|
gocryptfs.unmount()
|
|
|
|
|
|
|
|
|
|
decPath = testDecPath / f'tmp_{testRootPath.name}_enc'
|
|
|
|
|
assert gocryptfs.mountPath == decPath, print(f'{gocryptfs.mountPath}\n{decPath}')
|
|
|
|
|
a = MountPoints().getMountedOn(gocryptfs.mountPath)
|
|
|
|
|
assert a and a.device == testEncPath
|
|
|
|
|
|
|
|
|
|
# == passdb
|
|
|
|
|
pdb = PassDB(passPath=testEncPath, gocryptfs=gocryptfs)
|
|
|
|
|
assert pdb.clearPath == decPath, print(f'{pdb.clearPath}\n{decPath}')
|
|
|
|
|
|
|
|
|
|
# == pass key
|
|
|
|
|
test1Key = pdb.getPassKey('test1')
|
|
|
|
|
assert test1Key.root == decPath, test1Key.root
|
|
|
|
|
assert test1Key.relative == Path('test1'), test1Key.relative
|
|
|
|
|
assert test1Key.absolute == decPath / 'test1', test1Key.absolute
|
|
|
|
|
|
|
|
|
|
# == insert
|
|
|
|
|
pdb.insert('test1', data='data1')
|
|
|
|
|
Assert(test1Key.absolute.readText()) == 'data1'
|
|
|
|
|
Assert(pdb.cat('test1')) == ['data1']
|
|
|
|
|
|
|
|
|
|
with testFail(PassKeyAlreadyExistsError):
|
|
|
|
|
pdb.insert('test1', data='data2')
|
|
|
|
|
Assert(test1Key.absolute.readText()) == 'data1' # | didn't overwrite
|
|
|
|
|
Assert(pdb.cat('test1')) == ['data1']
|
|
|
|
|
|
|
|
|
|
pdb.insert('test1', data='data3', force=True)
|
|
|
|
|
Assert(test1Key.absolute.readText()) == 'data3'
|
|
|
|
|
Assert(pdb.cat('test1')) == ['data3']
|
|
|
|
|
|
|
|
|
|
# == edit
|
|
|
|
|
with pdb.tempEdit('test2') as path:
|
|
|
|
|
p = path
|
|
|
|
|
path.write('data4')
|
|
|
|
|
assert not p.isdir() # | when deleted it is recreated as dir to prevent saving in vscode
|
|
|
|
|
pdb.cat('test2') == ['data4']
|
|
|
|
|
|
|
|
|
|
pdb.edit('test3', editor='echo data5 > {path}', _runArgs=(N, N, N))
|
|
|
|
|
pdb.cat('test3') == ['data5']
|
|
|
|
|
|
|
|
|
|
# == rm
|
|
|
|
|
pdb.insert('test5', data='data6')
|
|
|
|
|
pdb.rm('test5')
|
|
|
|
|
trash = decPath / '_trash/test5'
|
|
|
|
|
assert trash.isfile()
|
|
|
|
|
assert trash.readtext() == 'data6'
|
|
|
|
|
|
|
|
|
|
# == dict
|
|
|
|
|
otp = '554ODB57ZVRRILJEKQ7Z6HMFMQMXLC3K'
|
|
|
|
|
pdb.insert('test6', data=f'pass\notp:{otp}\nurl:http\n\notp:dog\nurl:ftp\n\n')
|
|
|
|
|
res = pdb.toDict('test6')
|
|
|
|
|
Assert(res) == {'key': 'test6', 'url': 'http', 'username': 'test6', 'password': 'pass', 'otp': otp}
|
|
|
|
|
|
|
|
|
|
Assert(pdb.passwd('test6')) == 'pass'
|
|
|
|
|
Assert(pdb.otpToken('test6')) == OTP(cert=otp).generate()
|
|
|
|
|
|
|
|
|
|
# == keys
|
|
|
|
|
res = [x.s for x in pdb._allKeys()]
|
|
|
|
|
Assert(res) == ['test1', 'test2', 'test3', 'test6']
|
|
|
|
|
|
|
|
|
|
# == generate pss
|
|
|
|
|
pdb.generatePass('test7')
|
|
|
|
|
new = pdb.cat('test7')
|
|
|
|
|
newLen = [len(x) for x in new]
|
|
|
|
|
Assert(newLen) == [120]
|
|
|
|
|
old1 = new[0]
|
|
|
|
|
|
|
|
|
|
pdb.generatePass('test7')
|
|
|
|
|
new = [x for x in pdb.cat('test7')]
|
|
|
|
|
old2 = new[0]
|
|
|
|
|
newLen = [len(x) for x in new]
|
|
|
|
|
Assert(len(new[0])) == 120
|
|
|
|
|
Assert(len(new[1])) == 0
|
|
|
|
|
Assert(len(new[2])) > 120
|
|
|
|
|
Assert(new[2].split(' | ')[2]) == old1
|
|
|
|
|
|
|
|
|
|
pdb.generatePass('test7')
|
|
|
|
|
new = [x for x in pdb.cat('test7')]
|
|
|
|
|
newLen = [len(x) for x in new]
|
|
|
|
|
Assert(len(new[0])) == 120
|
|
|
|
|
Assert(len(new[1])) == 0
|
|
|
|
|
Assert(len(new[2])) > 120
|
|
|
|
|
Assert(len(new[3])) > 120
|
|
|
|
|
Assert(new[2].split(' | ')[2]) == old1
|
|
|
|
|
Assert(new[3].split(' | ')[2]) == old2
|
|
|
|
|
|
2024-09-11 11:14:03 -04:00
|
|
|
|
|
|
|
|
log.info(tester.run())
|
|
|
|
|
tester.exitWithStatus()
|