Пример #1
0
// TestUnitGenerateDeterministicKeyPari checks that the GenerateDeterministic
// method is properly calling its dependencies and returning the expected key
// pair.
func TestUnitGenerateDeterministicKeyPair(t *testing.T) {
	// Create entropy bytes, setting a few bytes explicitly instead of using a
	// buffer of random bytes.
	var mockEntropy [EntropySize]byte
	mockEntropy[0] = 4
	mockEntropy[EntropySize-1] = 5

	sk := ed25519.SecretKey(&[SecretKeySize]byte{})
	sk[0] = 7
	sk[32] = 8
	pk := ed25519.PublicKey(&[PublicKeySize]byte{})
	pk[0] = sk[32]
	kd := mockKeyDeriver{sk: sk, pk: pk}
	g := sigKeyGen{keyDeriver: &kd}

	// Create key pair.
	skActual, pkActual := g.generateDeterministic(mockEntropy)

	// Verify that we got back the right results.
	if *sk != skActual {
		t.Errorf("Generated secret key does not match expected! expected = %v, actual = %v", sk, skActual)
	}
	if *pk != pkActual {
		t.Errorf("Generated public key does not match expected! expected = %v, actual = %v", pk, pkActual)
	}

	// Verify the dependencies were called correctly.
	if !kd.called {
		t.Error("keyDeriver was never called.")
	}
	if mockEntropy != kd.entropy {
		t.Errorf("keyDeriver was called with the wrong entropy. expected = %v, actual = %v", mockEntropy, kd.entropy)
	}
}
Пример #2
0
// TestUnitGenerateRandomKeyPair checks that the Generate method is properly
// calling its dependencies and returning the expected key pair.
func TestUnitGenerateRandomKeyPair(t *testing.T) {
	var mockEntropy [EntropySize]byte
	mockEntropy[0] = 5
	mockEntropy[EntropySize-1] = 5
	entropyReader := bytes.NewReader(mockEntropy[:])

	sk := ed25519.SecretKey(&[SecretKeySize]byte{})
	sk[0] = 7
	sk[32] = 8
	pk := ed25519.PublicKey(&[PublicKeySize]byte{})
	pk[0] = sk[32]
	kd := mockKeyDeriver{sk: sk, pk: pk}

	// Create a SignatureKeyGenerator using mocks.
	g := sigKeyGen{entropyReader, &kd}

	// Create key pair.
	skActual, pkActual, err := g.generate()

	// Verify that we got back the expected results.
	if err != nil {
		t.Error(err)
	}
	if *sk != skActual {
		t.Errorf("Generated secret key does not match expected! expected = %v, actual = %v", sk, skActual)
	}
	if *pk != pkActual {
		t.Errorf("Generated public key does not match expected! expected = %v, actual = %v", pk, pkActual)
	}

	// Verify the dependencies were called correctly
	if !kd.called {
		t.Error("keyDeriver was never called.")
	}
	if mockEntropy != kd.entropy {
		t.Errorf("keyDeriver was called with the wrong entropy. expected = %v, actual = %v", mockEntropy, kd.entropy)
	}
}