aboutsummaryrefslogtreecommitdiff
path: root/models/infohash_test.go
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2018-02-26 11:27:15 +0000
committerFelix Hanley <felix@userspace.com.au>2018-02-26 11:27:15 +0000
commit19f63bb03bf2a83515fd47e6cf10a4db18a923d7 (patch)
tree2f5b66f49904ed1a5e17bcf566587abb7f894dd3 /models/infohash_test.go
parent6e449039520843c8df5203d6a16a0c6fadfe312b (diff)
downloaddhtsearch-19f63bb03bf2a83515fd47e6cf10a4db18a923d7.tar.gz
dhtsearch-19f63bb03bf2a83515fd47e6cf10a4db18a923d7.tar.bz2
Moved shared structs to package
Diffstat (limited to 'models/infohash_test.go')
-rw-r--r--models/infohash_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/models/infohash_test.go b/models/infohash_test.go
new file mode 100644
index 0000000..e855e35
--- /dev/null
+++ b/models/infohash_test.go
@@ -0,0 +1,76 @@
+package models
+
+import (
+ "encoding/hex"
+ "testing"
+)
+
+func TestInfohashImport(t *testing.T) {
+
+ tests := []struct {
+ str string
+ ok bool
+ }{
+ {str: "5a3ce1c14e7a08645677bbd1cfe7d8f956d53256", ok: true},
+ {str: "5a3ce1c14e7a08645677bbd1cfe7d8f956d53256000", ok: false},
+ }
+
+ for _, tt := range tests {
+ ih, err := InfohashFromString(tt.str)
+ if tt.ok {
+ if err != nil {
+ t.Errorf("FromString failed with %s", err)
+ }
+
+ idBytes, err := hex.DecodeString(tt.str)
+ if err != nil {
+ t.Errorf("failed to decode %s to hex", tt.str)
+ }
+ ih2 := Infohash(idBytes)
+ if !ih.Equal(ih2) {
+ t.Errorf("expected %s to equal %s", ih, ih2)
+ }
+ } else {
+ if err == nil {
+ t.Errorf("FromString should have failed for %s", tt.str)
+ }
+ }
+ }
+}
+
+func TestInfohashLength(t *testing.T) {
+ ih := GenInfohash()
+ if len(ih) != 20 {
+ t.Errorf("%s as string should be length 20, got %d", ih, len(ih))
+ }
+}
+
+func TestInfohashDistance(t *testing.T) {
+ id := "d1c5676ae7ac98e8b19f63565905105e3c4c37a2"
+
+ var tests = []struct {
+ ih string
+ other string
+ distance int
+ }{
+ {id, id, 160},
+ {id, "d1c5676ae7ac98e8b19f63565905105e3c4c37a3", 159},
+ }
+
+ ih, err := InfohashFromString(id)
+ if err != nil {
+ t.Errorf("Failed to create Infohash: %s", err)
+ }
+
+ for _, tt := range tests {
+ other, err := InfohashFromString(tt.other)
+ if err != nil {
+ t.Errorf("Failed to create Infohash: %s", err)
+ }
+
+ dist := ih.Distance(*other)
+ if dist != tt.distance {
+ t.Errorf("Distance() => %d, expected %d", dist, tt.distance)
+ }
+ }
+}