-
-
Notifications
You must be signed in to change notification settings - Fork 41
add: mobilpay crypto unit tests #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f20f2b8
55d480c
22ddaa5
c5acdee
ee144c8
6c692ba
c28f089
96bf708
b496bfd
5c53dfa
6ef4a72
11ccf70
38052ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # not "-alpine", because "cryptography" has no wheel for it, it tries to | ||
| # compile, wants rust, and fails. | ||
| FROM python:3.7.4 | ||
|
|
||
| WORKDIR /build | ||
| ADD requirements-crypto*.txt ./ | ||
| RUN pip install --upgrade pip \ | ||
| && pip install -r requirements-crypto-test.txt | ||
|
|
||
| ADD docker/crypto-test/gen_testdata.sh . | ||
| RUN ./gen_testdata.sh | ||
|
|
||
| ADD ro_help/mobilpay mobilpay/ | ||
| RUN pytest mobilpay/mobilpay/test.py -v |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/bin/sh | ||
| # Generate data for crypto tests | ||
|
|
||
| for size in 1024 ; do | ||
| for side in us them ; do | ||
| d=pki/${side}-rsa${size} | ||
| mkdir -p $d | ||
| openssl genrsa -out $d/private.pem $size | ||
| openssl req \ | ||
| -x509 \ | ||
| -key $d/private.pem \ | ||
| -out $d/public.pem \ | ||
| -subj "/OU=test/CN=$side" \ | ||
| -days 36525 | ||
| openssl rsa \ | ||
| -in $d/private.pem \ | ||
| -out $d/rawpublic.pem \ | ||
| -outform PEM \ | ||
| -pubout | ||
| echo "message from $side" > $d/msg.clear | ||
| openssl rand 16 > $d/enc.key | ||
| openssl enc \ | ||
| -rc4 \ | ||
| -nosalt \ | ||
| -in $d/msg.clear \ | ||
| -out $d/msg.enc \ | ||
| -K $(od -tx1 -An < $d/enc.key | tr -d ' ') | ||
| base64 -w0 < $d/msg.enc > $d/msg.enc.b64 | ||
| openssl rsautl \ | ||
| -encrypt \ | ||
| -inkey $d/rawpublic.pem \ | ||
| -pubin \ | ||
| -in $d/enc.key \ | ||
| -out $d/enc.key.rsaenc | ||
| base64 -w0 < $d/enc.key.rsaenc > $d/enc.key.rsaenc.b64 | ||
| done | ||
| done |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -r requirements-crypto.txt | ||
| requests==2.23.0 | ||
| pytest==5.4.1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pycrypto>=2.6.1,<3.0.0 | ||
| pyOpenSSL>=19.1.0,<20.0.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import unittest | ||
| from mobilpay.mobilpay.util.encrypt_data import Crypto | ||
|
|
||
| class TestCrypto(unittest.TestCase): | ||
| def test_load_pub(self): | ||
| pubkey = Crypto().get_rsa_key('pki/us-rsa1024/public.pem') | ||
| self.assertIsNotNone(pubkey) | ||
| self.assertTrue(pubkey) | ||
| def test_load_priv(self): | ||
| privkey = Crypto().get_private_key('pki/us-rsa1024/private.pem') | ||
| self.assertIsNotNone(privkey) | ||
| self.assertTrue(privkey) | ||
| def test_self_roundtrip(self): | ||
| pubkey = Crypto().get_rsa_key('pki/us-rsa1024/public.pem') | ||
| privkey = Crypto().get_private_key('pki/us-rsa1024/private.pem') | ||
| clear_msg = b'forty-two' | ||
| enc_msg, enc_block_key = Crypto().encrypt(clear_msg, pubkey) | ||
| dec_msg = Crypto().decrypt(enc_msg, privkey, enc_block_key) | ||
| self.assertEqual(dec_msg, clear_msg) | ||
| def test_decrypt(self): | ||
| d = 'pki/them-rsa1024/' | ||
| def _slurp(fname): | ||
| with open(d + fname, 'r') as fp: | ||
| return fp.read() | ||
|
|
||
| msg_enc = _slurp('msg.enc.b64') | ||
| enc_key_rsaenc = _slurp('enc.key.rsaenc.b64') | ||
| privkey = Crypto().get_private_key(d + 'private.pem') | ||
| dec_msg = Crypto().decrypt(msg_enc, privkey, enc_key_rsaenc) | ||
| self.assertEqual(dec_msg, b'message from them\n') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are great. 👍 I would only recommend that we use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about moving to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. I would still propose we have a separate |
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the dev requirements for the test env?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because #387 . After that is fixed, I've prepared a rebased v2 branch that could be used instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, yeah. 🤕 I will try to find the time to backport the dependency management from https://github.com/code4romania/votong and get this fixed.