some preliminary unit tests
[secretsharing.git] / test / test_shamir.rb
blobedefc16ee880b1ea8428bb6573b00e62fcebbec6
1 require 'test/unit'
2 require 'secretsharing/shamir'
4 DEFAULT_SECRET_BITLENGTH = 128
6 class TestShamir < Test::Unit::TestCase
7         def test_instantiation
8                 assert_raise( ArgumentError ) { SecretSharing::Shamir.new }
9                 s1 = SecretSharing::Shamir.new(5)
10                 assert_equal(s1.n, 5)
11                 assert_equal(s1.k, 5)
12                 assert_false(s1.secret_set?)
13                 s2 = SecretSharing::Shamir.new(5, 3)
14                 assert_equal(s2.n, 5)
15                 assert_equal(s2.k, 3)
16                 assert_false(s2.secret_set?)
17         end
19         def test_create_random_secret
20                 s = SecretSharing::Shamir.new(5)
21                 s.create_random_secret()
22                 assert_true(s.secret_set?)
23                 assert_not_nil(s.secret)
24                 assert_not_nil(s.shares)
25                 assert_equal(s.shares.class, Array)
26                 assert_equal(s.shares.length, 5)
27                 assert_equal(s.shares[0].class, SecretSharing::Shamir::Share)
28                 assert_equal(s.secret_bitlength, DEFAULT_SECRET_BITLENGTH)
30                 # can only be called once
31                 assert_raise( RunTimeError) { s.create_random_secret() }
33                 s2 = SecretSharing::Shamir.new(7)
34                 s2.create_random_secret(256)
35                 assert_equal(s2.secret_bitlength, 256)
36         end
38         def test_recover_secret_k_eq_n
39                 s = SecretSharing::Shamir.new(5)
40                 s.create_random_secret()
41                 
42                 s2 = SecretSharing::Shamir.new(5)
43                 s2.shares << s.shares[0]
44                 assert_equal(s2.shares.length, 1)
45                 assert_false(s2.secret_set?)
46                 assert_nil(s2.secret)
47                 # adding the same share raises an error
48                 assert_raise( RunTimeError ) { s2.shares << s.shares[0] }
49                 # add more shares
50                 s2.shares << s.shares[1]
51                 assert_false(s2.secret_set?)
52                 s2.shares << s.shares[2]
53                 assert_false(s2.secret_set?)
54                 s2.shares << s.shares[3]
55                 assert_false(s2.secret_set?)
56                 s2.shares << s.shares[4]
57                 assert_true(s2.secret_set?)
58                 assert_equal(s.secret, s2.secret)
59         end
61         def test_recover_secret_k_le_n
62                 s = SecretSharing::Shamir.new(5, 3)
63                 s.create_random_secret()
64                 
65                 s2 = SecretSharing::Shamir.new(5, 3)
66                 s2.shares << s.shares[0]
67                 assert_equal(s2.shares.length, 1)
68                 assert_false(s2.secret_set?)
69                 assert_nil(s2.secret)
70                 # add more shares
71                 s2.shares << s.shares[1]
72                 assert_false(s2.secret_set?)
73                 s2.shares << s.shares[2]
74                 assert_true(s2.secret_set?)
75                 assert_equal(s.secret, s2.secret)
77                 # adding more shares than needed raises an error
78                 assert_raise( RunTimeError ) { s2.shares << s.shares[3] }
79         end     
80 end