chiark / gitweb /
t/: Add a test suite.
[catacomb-python] / t / t-share.py
diff --git a/t/t-share.py b/t/t-share.py
new file mode 100644 (file)
index 0000000..089d2ff
--- /dev/null
@@ -0,0 +1,68 @@
+### -*-python-*-
+###
+### Testing (some) passsword management functionality
+###
+### (c) 2019 Straylight/Edgeware
+###
+
+###----- Licensing notice ---------------------------------------------------
+###
+### This file is part of the Python interface to Catacomb.
+###
+### Catacomb/Python is free software: you can redistribute it and/or
+### modify it under the terms of the GNU General Public License as
+### published by the Free Software Foundation; either version 2 of the
+### License, or (at your option) any later version.
+###
+### Catacomb/Python is distributed in the hope that it will be useful, but
+### WITHOUT ANY WARRANTY; without even the implied warranty of
+### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+### General Public License for more details.
+###
+### You should have received a copy of the GNU General Public License
+### along with Catacomb/Python.  If not, write to the Free Software
+### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+### USA.
+
+###--------------------------------------------------------------------------
+### Imported modules.
+
+import catacomb as C
+import unittest as U
+import testutils as T
+
+###--------------------------------------------------------------------------
+class TestShare (U.TestCase):
+
+  def check_share(me, splitcls, splitargs, joincls, joinargs, secret):
+
+    split = splitcls(3, secret, *splitargs)
+    me.assertEqual(split.threshold, 3)
+    shares = [split.get(i) for i in T.range(5)]
+
+    join = joincls(3, *joinargs)
+    me.assertEqual(join.threshold, 3)
+    me.assertFalse(join.addedp(1))
+    me.assertEqual(join.remain, 3)
+    join.add(1, shares[1])
+    me.assertTrue(join.addedp(1))
+    me.assertEqual(join.remain, 2)
+    me.assertRaises(ValueError, join.combine)
+    join.add(0, shares[0])
+    join.add(3, shares[3])
+    me.assertEqual(join.remain, 0)
+    me.assertEqual(join.combine(), secret)
+
+  def test_share(me):
+    rng = T.detrand("share")
+    p = C.MP(2)**127 - 1
+    me.check_share(C.ShareSplit, [p, rng], C.ShareJoin, [p], rng.range(p))
+
+  def test_gfshare(me):
+    rng = T.detrand("gfshare")
+    me.check_share(C.GFShareSplit, [rng], C.GFShareJoin, [123],
+                   rng.block(123))
+
+###----- That's all, folks --------------------------------------------------
+
+if __name__ == "__main__": U.main()