Skip to content

Commit

Permalink
Merge pull request #3477 from ipfs/test/multiple-different
Browse files Browse the repository at this point in the history
Add multiple small tests
  • Loading branch information
whyrusleeping authored Dec 7, 2016
2 parents f43811a + a3c3891 commit 19353df
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
8 changes: 8 additions & 0 deletions blocks/bloom/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"testing"
)

func TestBasicFilter(t *testing.T) {
f := BasicFilter().(*filter)

if len(f.filter) != 2048 {
t.Fatal("basic filter should have length 2048, has:", len(f.filter))
}
}

func TestFilter(t *testing.T) {
f := NewFilter(128)

Expand Down
20 changes: 20 additions & 0 deletions keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ func TestKeystoreBasics(t *testing.T) {
}
}

func TestNonExistingKey(t *testing.T) {
tdir, err := ioutil.TempDir("", "keystore-test")
if err != nil {
t.Fatal(err)
}

ks, err := NewFSKeystore(tdir)
if err != nil {
t.Fatal(err)
}

k, err := ks.Get("does-it-exist")
if err != ErrNoSuchKey {
t.Fatalf("expected: %s, got %s", ErrNoSuchKey, err)
}
if k != nil {
t.Fatalf("Get on nonexistant key should give nil")
}
}

func TestMakeKeystoreNoDir(t *testing.T) {
_, err := NewFSKeystore("/this/is/not/a/real/dir")
if err == nil {
Expand Down
82 changes: 82 additions & 0 deletions keystore/memkeystore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package keystore

import (
"sort"
"testing"
)

func TestMemKeyStoreBasics(t *testing.T) {
ks := NewMemKeystore()

l, err := ks.List()
if err != nil {
t.Fatal(err)
}

if len(l) != 0 {
t.Fatal("expected no keys")
}

k1 := privKeyOrFatal(t)
k2 := privKeyOrFatal(t)
k3 := privKeyOrFatal(t)
k4 := privKeyOrFatal(t)

err = ks.Put("foo", k1)
if err != nil {
t.Fatal(err)
}

err = ks.Put("bar", k2)
if err != nil {
t.Fatal(err)
}

l, err = ks.List()
if err != nil {
t.Fatal(err)
}

sort.Strings(l)
if l[0] != "bar" || l[1] != "foo" {
t.Fatal("wrong entries listed")
}

err = ks.Put("foo", k3)
if err == nil {
t.Fatal("should not be able to overwrite key")
}
if err := ks.Delete("bar"); err != nil {
t.Fatal(err)
}
if err := ks.Put("beep", k3); err != nil {
t.Fatal(err)
}

if err := ks.Put("boop", k4); err != nil {
t.Fatal(err)
}
if err := assertGetKey(ks, "foo", k1); err != nil {
t.Fatal(err)
}

if err := assertGetKey(ks, "beep", k3); err != nil {
t.Fatal(err)
}

if err := assertGetKey(ks, "boop", k4); err != nil {
t.Fatal(err)
}

if err := ks.Put("..///foo/", k1); err == nil {
t.Fatal("shouldnt be able to put a poorly named key")
}

if err := ks.Put("", k1); err == nil {
t.Fatal("shouldnt be able to put a key with no name")
}

if err := ks.Put(".foo", k1); err == nil {
t.Fatal("shouldnt be able to put a key with a 'hidden' name")
}
}

0 comments on commit 19353df

Please sign in to comment.