Skip to content
This repository was archived by the owner on Jan 3, 2022. It is now read-only.

Add tests #28

Merged
merged 2 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
go-libp2p-netutil
==================

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![codecov](https://codecov.io/gh/libp2p/go-libp2p-netutil/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/go-libp2p-netutil)
[![Travis CI](https://travis-ci.org/libp2p/go-libp2p-netutil.svg?branch=master)](https://travis-ci.org/libp2p/go-libp2p-netutil)

> Utility methods for creating public and private keys for use in tests.
73 changes: 73 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package testutil

import (
"bytes"
"crypto/rand"
ic "github.com/libp2p/go-libp2p-crypto"
pb "github.com/libp2p/go-libp2p-crypto/pb"
"testing"
)

func TestBogusPublicKeyGeneration(t *testing.T) {
public := RandTestBogusPublicKeyOrFatal(t)
if public.Type() != pb.KeyType_RSA {
t.Fatalf("Expected public key to be of type RSA but got %s", pb.KeyType_name[int32(public.Type())])
}
if val, _ := public.Raw(); val == nil {
t.Fatal("Expected raw bytes of public key to not be nil")
}

otherPublic, _, _ := ic.GenerateRSAKeyPair(512, rand.Reader)
if public.Equals(otherPublic) {
t.Fatal("Expect keys of different length to not be equal")
}
}

func TestBogusPrivateKeyGeneration(t *testing.T) {
private := RandTestBogusPrivateKeyOrFatal(t)
if private.Type() != pb.KeyType_RSA {
t.Fatalf("Expected private key to be of type RSA but got %s", pb.KeyType_name[int32(private.Type())])
}

secret := private.GenSecret()
signedSecret, _ := private.Sign(secret)
publicBytes, _ := private.GetPublic().Raw()
public := TestBogusPublicKey(publicBytes)
if signed, err := public.Verify(secret, signedSecret); !signed || err != nil {
t.Fatal("Expected to verify signed message")
}

encrypted, err := public.Encrypt(secret)
if err != nil {
t.Fatal("Failed to encrypt")
}

if decrypted, _ := private.Decrypt(encrypted); !bytes.Equal(decrypted, secret) {
t.Fatal("Decrypting secret did not correspond to plaintext")
}

_, otherPrivate, _ := ic.GenerateRSAKeyPair(512, rand.Reader)
if private.Equals(otherPrivate) {
t.Fatal("Expect keys of different length to not be equal")
}

if val, _ := private.Raw(); val == nil {
t.Fatal("Expected raw bytes of private key to not be nil")
}
}

func TestGenerateRandomBogusIdentity(t *testing.T) {
identity := RandTestBogusIdentityOrFatal(t)
if identity.ID() == "" {
t.Fatal("Expected non-empty identity ID")
}
if identity.Address() == nil {
t.Fatal("Expected non-nil address")
}
if identity.PrivateKey() == nil {
t.Fatal("Expected non-nil private key")
}
if identity.PublicKey() == nil {
t.Fatal("Expected non-nil public key")
}
}