| 1 | ### -*-python-*- |
| 2 | ### |
| 3 | ### Testing (some) passsword management functionality |
| 4 | ### |
| 5 | ### (c) 2019 Straylight/Edgeware |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This file is part of the Python interface to Catacomb. |
| 11 | ### |
| 12 | ### Catacomb/Python is free software: you can redistribute it and/or |
| 13 | ### modify it under the terms of the GNU General Public License as |
| 14 | ### published by the Free Software Foundation; either version 2 of the |
| 15 | ### License, or (at your option) any later version. |
| 16 | ### |
| 17 | ### Catacomb/Python is distributed in the hope that it will be useful, but |
| 18 | ### WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | ### General Public License for more details. |
| 21 | ### |
| 22 | ### You should have received a copy of the GNU General Public License |
| 23 | ### along with Catacomb/Python. If not, write to the Free Software |
| 24 | ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 25 | ### USA. |
| 26 | |
| 27 | ###-------------------------------------------------------------------------- |
| 28 | ### Imported modules. |
| 29 | |
| 30 | import catacomb as C |
| 31 | import unittest as U |
| 32 | import testutils as T |
| 33 | |
| 34 | ###-------------------------------------------------------------------------- |
| 35 | class TestShare (U.TestCase): |
| 36 | |
| 37 | def check_share(me, splitcls, splitargs, joincls, joinargs, secret): |
| 38 | |
| 39 | split = splitcls(3, secret, *splitargs) |
| 40 | me.assertEqual(split.threshold, 3) |
| 41 | shares = [split.get(i) for i in T.range(5)] |
| 42 | |
| 43 | join = joincls(3, *joinargs) |
| 44 | me.assertEqual(join.threshold, 3) |
| 45 | me.assertFalse(join.addedp(1)) |
| 46 | me.assertEqual(join.remain, 3) |
| 47 | join.add(1, shares[1]) |
| 48 | me.assertTrue(join.addedp(1)) |
| 49 | me.assertEqual(join.remain, 2) |
| 50 | me.assertRaises(ValueError, join.combine) |
| 51 | join.add(0, shares[0]) |
| 52 | join.add(3, shares[3]) |
| 53 | me.assertEqual(join.remain, 0) |
| 54 | me.assertEqual(join.combine(), secret) |
| 55 | |
| 56 | def test_share(me): |
| 57 | rng = T.detrand("share") |
| 58 | p = C.MP(2)**127 - 1 |
| 59 | me.check_share(C.ShareSplit, [p, rng], C.ShareJoin, [p], rng.range(p)) |
| 60 | |
| 61 | def test_gfshare(me): |
| 62 | rng = T.detrand("gfshare") |
| 63 | me.check_share(C.GFShareSplit, [rng], C.GFShareJoin, [123], |
| 64 | rng.block(123)) |
| 65 | |
| 66 | ###----- That's all, folks -------------------------------------------------- |
| 67 | |
| 68 | if __name__ == "__main__": U.main() |