Skip to content

Commit

Permalink
Adding a test
Browse files Browse the repository at this point in the history
  • Loading branch information
silverdaz committed Jul 11, 2024
1 parent 11897d3 commit 7f8a54b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crypt4gh/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,16 @@ def decrypt(keys, infile, outfile, sender_pubkey=None, offset=0, span=None):
)
)

session_keys, edit_list, expiration, link = header.deconstruct(infile, keys, sender_pubkey=sender_pubkey)
session_keys, edit_list, expiration, uri = header.deconstruct(infile, keys, sender_pubkey=sender_pubkey)

if expiration and (datetime.datetime.now(datetime.UTC) > expiration):
raise ValueError(f'Expired on {expiration}')

# Infile in now positioned at the beginning of the data portion
# or we fetch the data portion from the link.
if link:
# or we fetch the data portion from the URI.
if uri:
# replacing the infile with a fetcher
outfile = URLFetcher(link)
outfile = URLFetcher(uri)
# Note: the remainder of the infile might not be empty

# Generator to slice the output
Expand Down
52 changes: 52 additions & 0 deletions tests/expiration.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bats

load _common/helpers

function setup() {

# Defining the TMP dir
TESTFILES=${BATS_TEST_FILENAME}.d
mkdir -p "$TESTFILES"

}

function teardown() {
rm -rf ${TESTFILES}
}

@test "Bob sends the testfile secretly to Alice, with a past expiration date" {

TESTFILE=${BATS_TEST_DIRNAME}/_common/testfile.abcd

# Bob encrypts the testfile for Alice
export C4GH_PASSPHRASE=${BOB_PASSPHRASE}
EXP_DATE=$(date -jf %s $(( $(date +%s) - 86400 * 2 )))
crypt4gh encrypt --sk ${BOB_SECKEY} --recipient_pk ${BOB_PUBKEY} --recipient_pk ${ALICE_PUBKEY} --expiration "${EXP_DATE}" < $TESTFILE > $TESTFILES/message.c4gh

# Alice decrypts it
export C4GH_PASSPHRASE=${ALICE_PASSPHRASE}
crypt4gh decrypt --sk ${ALICE_SECKEY} < $TESTFILES/message.c4gh > $TESTFILES/message.alice.received
[ "$status" -ne 0 ]

unset C4GH_PASSPHRASE
}

@test "Bob sends the testfile secretly to Alice, with a future expiration date" {

TESTFILE=${BATS_TEST_DIRNAME}/_common/testfile.abcd

# Bob encrypts the testfile for Alice
export C4GH_PASSPHRASE=${BOB_PASSPHRASE}
EXP_DATE=$(date -jf %s $(( $(date +%s) + 86400 * 2 )))
crypt4gh encrypt --sk ${BOB_SECKEY} --recipient_pk ${BOB_PUBKEY} --recipient_pk ${ALICE_PUBKEY} --expiration "${EXP_DATE}" < $TESTFILE > $TESTFILES/message.c4gh

# Alice decrypts it
export C4GH_PASSPHRASE=${ALICE_PASSPHRASE}
crypt4gh decrypt --sk ${ALICE_SECKEY} < $TESTFILES/message.c4gh > $TESTFILES/message.alice.received

run diff $TESTFILE $TESTFILES/message.alice.received
[ "$status" -eq 0 ]


unset C4GH_PASSPHRASE
}

0 comments on commit 7f8a54b

Please sign in to comment.