forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and remove Duplicated Hex() for identity
- Loading branch information
Showing
8 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.idea/ | ||
noise.exe | ||
noise | ||
chat | ||
|
||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package crypto | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func TestHash(t *testing.T) { | ||
r := Hash(big.NewInt(123)) | ||
|
||
n := new(big.Int) | ||
n, ok := n.SetString("89391711502145780362310349925943903708999319576398061903082165979787487688967", 10) | ||
if ok { | ||
if n.String() != r.String() { | ||
fmt.Printf("%v \n%v \n", r, n) | ||
panic("Hash error") | ||
} | ||
} else { | ||
panic("Big Int error") | ||
} | ||
} | ||
|
||
func TestHashBytes(t *testing.T) { | ||
r := HashBytes([]byte("123")) | ||
n := []byte{245, 214, 123, 174, 115, 176, 225, 13, 13, 253, 48, 67, 179, 244, 241, 0, 173, 160, 20, 197, 195, 123, 213, 206, 151, 129, 59, 19, 245, 171, 43, 207} | ||
if !bytes.Equal(n, r) { | ||
fmt.Printf("%v \n%v \n", r, n) | ||
panic("Hash error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dht | ||
|
||
//TODO make function testable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package peer | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/perlin-network/noise/peer" | ||
) | ||
|
||
func TestID(t *testing.T) { | ||
|
||
testPublicKey := []byte("12345678901234567890123456789012") | ||
testPublicKey1 := []byte("12345678901234567890123456789011") | ||
testPublicKey2 := []byte("12345678901234567890123456789013") | ||
testAddr := "localhost:12345" | ||
|
||
id := peer.CreateID(testAddr, testPublicKey) | ||
|
||
if !bytes.Equal(id.PublicKey, testPublicKey) { | ||
fmt.Printf("%s \n%s", id.PublicKey, testPublicKey) | ||
panic("Wrong Public Key") | ||
} | ||
|
||
if id.Address != testAddr { | ||
panic("Wrong Address") | ||
} | ||
|
||
if id.String() != "ID{PublicKey: [49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 49 50], Address: localhost:12345}" { | ||
fmt.Printf(id.String()) | ||
panic("String() error") | ||
} | ||
|
||
if !id.Equals(peer.CreateID(testAddr, testPublicKey)) { | ||
panic("Equals() error") | ||
} | ||
|
||
if id.Less(peer.CreateID(testAddr, testPublicKey1)) { | ||
panic("Less() error 1") | ||
} | ||
|
||
if !id.Less(peer.CreateID(testAddr, testPublicKey2)) { | ||
panic("Less() error 2") | ||
} | ||
|
||
if id.PublicKeyHex() != "3132333435363738393031323334353637383930313233343536373839303132" { | ||
fmt.Print(id.PublicKeyHex()) | ||
panic("PublicKeyHex() error or hex.EncodeToString() changed defination?") | ||
} | ||
|
||
comparee := peer.CreateID( | ||
testAddr, | ||
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, | ||
) | ||
|
||
if !comparee.Equals(id.Xor(peer.CreateID(testAddr, testPublicKey2))) { | ||
fmt.Printf("%v\n%v", comparee, id.Xor(peer.CreateID(testAddr, testPublicKey2))) | ||
panic("Xor() error") | ||
} | ||
|
||
} |