Beispiel #1
0
func TestPrivateID(t *testing.T) {
	// Unmarshalling errors
	var id keymgr.PrivateID

	// Invalid format
	err := json.Unmarshal([]byte(`{"address": 4}`), &id)
	if err == nil {
		t.Errorf("got no error")
	}

	// No address/address too short
	err = json.Unmarshal([]byte(`{"somerandomkey": false}`), &id)
	if err == nil {
		t.Errorf("got no error")
	}

	// Invalid address
	err = json.Unmarshal([]byte(`{
      "address":"BM-2cUJvFYHhXpBHyd",
      "disabled":false,
      "encryptionKey":"5KUXLFRz2pTfKT9ThC2C1Y3MxWRieXq5Fbm2NiBTFbSaeLzmEwp",
      "extraBytes":1000,
      "isChan":true,
      "nonceTrialsPerByte":1000,
      "signingKey":"5HuxWwwZjGvB2zCXRAYVyUiNfavskWKCGxGwtyGCdPZPUQ22YJM"
    }`), &id)
	if err == nil {
		t.Errorf("got no error")
	}

	// Invalid signing key
	err = json.Unmarshal([]byte(`{
      "address":"BM-2cUJvFYHhXpBHyd96KHfjxsgTYi44BajdE",
      "disabled":false,
      "encryptionKey":"5KUXLFRz2pTfKT9ThC2C1Y3MxWRieXq5Fbm2NiBTFbSaeLzmEwp",
      "extraBytes":1000,
      "isChan":true,
      "nonceTrialsPerByte":1000,
      "signingKey":"5HuxWwwZjGvB2zCXRAYVyUiNfavskZPUQ22YJM"
    }`), &id)
	if err == nil {
		t.Errorf("got no error")
	}

	// Invalid encryption key
	err = json.Unmarshal([]byte(`{
      "address":"BM-2cUJvFYHhXpBHyd96KHfjxsgTYi44BajdE",
      "disabled":false,
      "encryptionKey":"5KUXLFRz2pTfKT9ThCbSaeLzmEwp",
      "extraBytes":1000,
      "isChan":true,
      "nonceTrialsPerByte":1000,
      "signingKey":"5HuxWwwZjGvB2zCXRAYVyUiNfavskWKCGxGwtyGCdPZPUQ22YJM"
    }`), &id)
	if err == nil {
		t.Errorf("got no error")
	}

	// Marshalling error, invalid address
	// This test fails because the address cannot be encoded (unknown version,
	// stream).
	ids, _ := identity.NewDeterministic("privacy", 1, 1)
	privacyChan := &keymgr.PrivateID{
		Private: *ids[0],
		IsChan:  true,
	}
	_, err = json.Marshal(privacyChan)
	if err == nil {
		t.Errorf("got no error")
	}
}
Beispiel #2
0
func TestOperation(t *testing.T) {
	// Initialize a new key manager.
	seed := []byte("a secure psuedorandom seed (clearly not)")
	mgr, err := keymgr.New(seed)
	if err != nil {
		t.Fatal(err)
	}

	// Check that everything is correctly zeroed.
	if n := mgr.NumDeterministic(); n != 0 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			0, n)
	}
	if n := mgr.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}

	// Generate first identity and check if everything works as expected.
	gen1 := mgr.NewHDIdentity(1, "Seven Anvil")
	if gen1.IsChan != false {
		t.Error("generated identity not a channel")
	}
	if n := mgr.NumDeterministic(); n != 1 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			1, n)
	}
	if n := mgr.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}

	// Generate second identity and check if everything works as expected.
	gen2 := mgr.NewHDIdentity(1, "Intelligent Glue")
	if gen2.IsChan != false {
		t.Error("generated identity not a channel")
	}
	if n := mgr.NumDeterministic(); n != 2 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			1, n)
	}
	if n := mgr.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}

	// Import a channel and check if it's imported as expected.
	ids, _ := identity.NewDeterministic("privacy", 1, 1)
	privacyChan := &keymgr.PrivateID{
		Private: *ids[0],
		IsChan:  true,
		Name:    "Hyperluminous",
	}
	// Create an address (export fails without this).
	privacyChan.CreateAddress(4, 1)

	mgr.ImportIdentity(*privacyChan)
	if n := mgr.NumDeterministic(); n != 2 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			1, n)
	}
	if n := mgr.NumImported(); n != 1 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			1, n)
	}

	// Try to retrieve private identity from address.
	privacyAddr := privacyChan.Address()
	privacyRetrieved := mgr.LookupByAddress(privacyAddr)
	if privacyRetrieved == nil {
		t.Errorf("LookupByAddress returned nil address")
	}
	if !bytes.Equal(privacyRetrieved.Private.Address.Ripe[:], privacyChan.Private.Address.Ripe[:]) {
		t.Errorf("got different ripe, expected %v got %v",
			privacyChan.Private.Address.Ripe, privacyRetrieved.Private.Address.Ripe)
	}

	// Save and encrypt the private keys held by the key manager.
	pass := []byte("a very nice and secure password for my keyfile")
	encData, err := mgr.ExportEncrypted(pass)
	if err != nil {
		t.Fatal(err)
	}

	// Create a new key manager from the encrypted data and check if it's like
	// the original.
	mgr1, err := keymgr.FromEncrypted(encData, pass)
	if err != nil {
		t.Fatal(err)
	}
	if mgr.NumImported() != mgr1.NumImported() {
		t.Errorf("invalid number of imported keys, expected %d got %d",
			mgr.NumImported(), mgr1.NumImported())
	}
	if mgr.NumDeterministic() != mgr1.NumDeterministic() {
		t.Errorf("invalid number of deterministic keys, expected %d got %d",
			mgr.NumDeterministic(), mgr1.NumDeterministic())
	}

	// Remove an imported key and check if the operation is successful.
	/*mgr1.RemoveImported(privacyChan.Address())
	if n := mgr1.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}
	err = mgr1.ForEach(func(id keymgr.PrivateID) error {
		if bytes.Equal(id.Tag(), privacyChan.Tag()) {
			return errors.New("should not happen")
		}
		return nil
	})
	if err != nil {
		t.Error("imported key not removed from database")
	}*/

	// Try to remove a key that doesn't exist in the database.
	// Should not crash the program. (function removed)
	//mgr1.RemoveImported(privacyChan.Address())

	// Try to retrieve non-existant private identity from address.
	privacyRetrieved = mgr1.LookupByAddress("BM-2cUfDTJXLeMxAVe7pWXBEneBjDuQ783VSq")
	if privacyRetrieved != nil {
		t.Errorf("expected nil id")
	}
}