/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.codahale.shamir.tests;

import com.codahale.shamir.SecretSharing;
import com.codahale.shamir.Share;
import com.google.common.collect.Sets;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.generator.InRange;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import org.junit.runner.RunWith;

import java.security.SecureRandom;
import java.util.Set;

import static org.junit.Assert.assertArrayEquals;

@RunWith(JUnitQuickcheck.class)
public class SecretSharingPropertyTest {
    @Property(trials = 10)
    public void roundTrip(@InRange(min = "2", max = "5") int a, @InRange(min = "2", max = "5") int k) throws Exception {
        final int n = a + k;

        // generate a random secret
        final SecureRandom random = new SecureRandom();
        final byte[] secret = new byte[1000];
        random.nextBytes(secret);

        // split the secret
        final Set<Share> shares = SecretSharing.split(n, k, secret);

        // All distinct subsets of shares which are at or over the threshold
        // should combine to recover the original secret.
        for (Set<Share> i : Sets.powerSet(shares)) {
            if (i.size() >= k) {
                final byte[] recovered = SecretSharing.combine(i);
                assertArrayEquals(secret, recovered);
            }
        }
    }
}

@@ -754,16 +754,58 @@
 operty;%0A
+import com.pholser.junit.quickcheck.When;%0A
 import c
@@ -949,43 +949,8 @@
 h;%0A%0A
-import java.security.SecureRandom;%0A
 impo
@@ -1115,17 +1115,18 @@
 rials = 
-1
+50
 0)%0A    p
@@ -1185,10 +1185,38 @@
 int 
-a,
+top,%0A                         
  @In
@@ -1252,231 +1252,124 @@
 nt k
-) throws Exception %7B%0A        final int n = a + k;%0A%0A        // generate a random secret%0A        final SecureRandom random = new SecureRandom();%0A        final byte%5B%5D secret = new byte%5B1000%5D;%0A        random.nextBytes(secret);%0A
+,%0A                          @When(satisfies = %22#_.length %3E 0 && #_.length %3C 1000%22) byte%5B%5D secret) throws Exception %7B
 %0A   
@@ -1447,17 +1447,23 @@
 g.split(
-n
+top + k
 , k, sec
@@ -1628,17 +1628,22 @@
 %3CShare%3E 
-i
+subset
  : Sets.
@@ -1678,17 +1678,22 @@
     if (
-i
+subset
 .size() 
@@ -1763,17 +1763,22 @@
 combine(
-i
+subset
 );%0A     

Use generated secrets to check round trips.