func TestHashSetStringer(t *testing.T) { h := fs.NewHashSet(fs.HashSHA1, fs.HashMD5) assert.Equal(t, h.String(), "[MD5, SHA-1]") h = fs.NewHashSet(fs.HashSHA1) assert.Equal(t, h.String(), "[SHA-1]") h = fs.NewHashSet() assert.Equal(t, h.String(), "[]") }
func TestHashStreamTypes(t *testing.T) { h := fs.HashSHA1 for _, test := range hashTestSet { sums, err := fs.HashStreamTypes(bytes.NewBuffer(test.input), fs.NewHashSet(h)) require.NoError(t, err) assert.Len(t, sums, 1) assert.Equal(t, sums[h], test.output[h]) } }
func TestHashSetStringer(t *testing.T) { h := fs.NewHashSet(fs.HashSHA1, fs.HashMD5) s := h.String() expect := "[MD5, SHA-1]" if s != expect { t.Errorf("unexpected stringer: was %q, expected %q", s, expect) } h = fs.NewHashSet(fs.HashSHA1) s = h.String() expect = "[SHA-1]" if s != expect { t.Errorf("unexpected stringer: was %q, expected %q", s, expect) } h = fs.NewHashSet() s = h.String() expect = "[]" if s != expect { t.Errorf("unexpected stringer: was %q, expected %q", s, expect) } }
func TestMultiHasherTypes(t *testing.T) { h := fs.HashSHA1 for _, test := range hashTestSet { mh, err := fs.NewMultiHasherTypes(fs.NewHashSet(h)) if err != nil { t.Fatal(err) } n, err := io.Copy(mh, bytes.NewBuffer(test.input)) require.NoError(t, err) assert.Len(t, test.input, int(n)) sums := mh.Sums() assert.Len(t, sums, 1) assert.Equal(t, sums[h], test.output[h]) } }
func TestHashStreamTypes(t *testing.T) { h := fs.HashSHA1 for _, test := range hashTestSet { sums, err := fs.HashStreamTypes(bytes.NewBuffer(test.input), fs.NewHashSet(h)) if err != nil { t.Fatal(err) } if len(sums) != 1 { t.Fatalf("expected 1 sum, got %d", len(sums)) } expect := test.output[h] if expect != sums[h] { t.Errorf("hash %d mismatch %q != %q", h, sums[h], expect) } } }
func TestHashSet(t *testing.T) { var h fs.HashSet assert.Equal(t, 0, h.Count()) a := h.Array() assert.Len(t, a, 0) h = h.Add(fs.HashMD5) assert.Equal(t, 1, h.Count()) assert.Equal(t, fs.HashMD5, h.GetOne()) a = h.Array() assert.Len(t, a, 1) assert.Equal(t, a[0], fs.HashMD5) // Test overlap, with all hashes h = h.Overlap(fs.SupportedHashes) assert.Equal(t, 1, h.Count()) assert.Equal(t, fs.HashMD5, h.GetOne()) assert.True(t, h.SubsetOf(fs.SupportedHashes)) assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5))) h = h.Add(fs.HashSHA1) assert.Equal(t, 2, h.Count()) one := h.GetOne() if !(one == fs.HashMD5 || one == fs.HashSHA1) { t.Fatalf("expected to be either MD5 or SHA1, got %v", one) } assert.True(t, h.SubsetOf(fs.SupportedHashes)) assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5))) assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashSHA1))) assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1))) a = h.Array() assert.Len(t, a, 2) ol := h.Overlap(fs.NewHashSet(fs.HashMD5)) assert.Equal(t, 1, ol.Count()) assert.True(t, ol.Contains(fs.HashMD5)) assert.False(t, ol.Contains(fs.HashSHA1)) ol = h.Overlap(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)) assert.Equal(t, 2, ol.Count()) assert.True(t, ol.Contains(fs.HashMD5)) assert.True(t, ol.Contains(fs.HashSHA1)) }
func TestMultiHasherTypes(t *testing.T) { h := fs.HashSHA1 for _, test := range hashTestSet { mh, err := fs.NewMultiHasherTypes(fs.NewHashSet(h)) if err != nil { t.Fatal(err) } n, err := io.Copy(mh, bytes.NewBuffer(test.input)) if err != nil { t.Fatal(err) } if int(n) != len(test.input) { t.Fatalf("copy mismatch: %d != %d", n, len(test.input)) } sums := mh.Sums() if len(sums) != 1 { t.Fatalf("expected 1 sum, got %d", len(sums)) } expect := test.output[h] if expect != sums[h] { t.Errorf("hash %v mismatch %q != %q", h, sums[h], expect) } } }
func TestHashSet(t *testing.T) { var h fs.HashSet if h.Count() != 0 { t.Fatalf("expected empty set to have 0 elements, got %d", h.Count()) } a := h.Array() if len(a) != 0 { t.Fatalf("expected empty slice, got %d", len(a)) } h = h.Add(fs.HashMD5) if h.Count() != 1 { t.Fatalf("expected 1 element, got %d", h.Count()) } if h.GetOne() != fs.HashMD5 { t.Fatalf("expected HashMD5, got %v", h.GetOne()) } a = h.Array() if len(a) != 1 { t.Fatalf("expected 1 element, got %d", len(a)) } if a[0] != fs.HashMD5 { t.Fatalf("expected HashMD5, got %v", a[0]) } // Test overlap, with all hashes h = h.Overlap(fs.SupportedHashes) if h.Count() != 1 { t.Fatalf("expected 1 element, got %d", h.Count()) } if h.GetOne() != fs.HashMD5 { t.Fatalf("expected HashMD5, got %v", h.GetOne()) } if !h.SubsetOf(fs.SupportedHashes) { t.Fatalf("expected to be subset of all hashes") } if !h.SubsetOf(fs.NewHashSet(fs.HashMD5)) { t.Fatalf("expected to be subset of itself") } h = h.Add(fs.HashSHA1) if h.Count() != 2 { t.Fatalf("expected 2 elements, got %d", h.Count()) } one := h.GetOne() if !(one == fs.HashMD5 || one == fs.HashSHA1) { t.Fatalf("expected to be either MD5 or SHA1, got %v", one) } if !h.SubsetOf(fs.SupportedHashes) { t.Fatalf("expected to be subset of all hashes") } if h.SubsetOf(fs.NewHashSet(fs.HashMD5)) { t.Fatalf("did not expect to be subset of only MD5") } if h.SubsetOf(fs.NewHashSet(fs.HashSHA1)) { t.Fatalf("did not expect to be subset of only SHA1") } if !h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)) { t.Fatalf("expected to be subset of MD5/SHA1") } a = h.Array() if len(a) != 2 { t.Fatalf("expected 2 elements, got %d", len(a)) } ol := h.Overlap(fs.NewHashSet(fs.HashMD5)) if ol.Count() != 1 { t.Fatalf("expected 1 element overlap, got %d", ol.Count()) } if !ol.Contains(fs.HashMD5) { t.Fatalf("expected overlap to be MD5, got %v", ol) } if ol.Contains(fs.HashSHA1) { t.Fatalf("expected overlap NOT to contain SHA1, got %v", ol) } ol = h.Overlap(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)) if ol.Count() != 2 { t.Fatalf("expected 2 element overlap, got %d", ol.Count()) } if !ol.Contains(fs.HashMD5) { t.Fatalf("expected overlap to contain MD5, got %v", ol) } if !ol.Contains(fs.HashSHA1) { t.Fatalf("expected overlap to contain SHA1, got %v", ol) } }