1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-06-01 11:09:30 +02:00

Fix false positive in ContainsAll function

As the `ContainsAll` is working with a match counter, it could return
a false positive when the `haystack` slice contains duplicate elements.

This can be checked with the included testing scenario, with
`haystack = [1, 1]` and `needles = [1, 2]`. Iterating over the haystack
to check for items to be present in needles will increase the match
counter to 2, even if `2` is not present in the first slice.
This commit is contained in:
Michael Manganiello 2023-05-12 09:51:47 -03:00
parent f4e6874ff0
commit ebd4367dda
2 changed files with 13 additions and 9 deletions

View file

@ -2,7 +2,6 @@ package util
import (
"errors"
"golang.org/x/time/rate"
"io"
"net/netip"
"os"
@ -11,6 +10,8 @@ import (
"testing"
"time"
"golang.org/x/time/rate"
"github.com/stretchr/testify/require"
)
@ -49,6 +50,11 @@ func TestContains(t *testing.T) {
require.False(t, Contains(s, 3))
}
func TestContainsAll(t *testing.T) {
require.True(t, ContainsAll([]int{1, 2, 3}, []int{2, 3}))
require.False(t, ContainsAll([]int{1, 1}, []int{1, 2}))
}
func TestContainsIP(t *testing.T) {
require.True(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("1.1.1.1")))
require.True(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("fd12:1234:5678::9876")))