<commit_before>package torrentapproval

import (
	"context"
	"encoding/hex"
	"fmt"
	"testing"

	"github.com/chihaya/chihaya/bittorrent"
	"github.com/stretchr/testify/require"
)

var cases = []struct {
	cfg      Config
	ih       string
	approved bool
}{
	// Infohash is whitelisted
	{
		Config{
			Whitelist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"3532cf2d327fad8448c075b4cb42c8136964a435",
		true,
	},
	// Infohash is not whitelisted
	{
		Config{
			Whitelist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"4532cf2d327fad8448c075b4cb42c8136964a435",
		false,
	},
	// Infohash is not blacklisted
	{
		Config{
			Blacklist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"4532cf2d327fad8448c075b4cb42c8136964a435",
		true,
	},
	// Infohash is blacklisted
	{
		Config{
			Blacklist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"3532cf2d327fad8448c075b4cb42c8136964a435",
		false,
	},
}

func TestHandleAnnounce(t *testing.T) {
	for _, tt := range cases {
		t.Run(fmt.Sprintf("testing hash %s", tt.ih), func(t *testing.T) {
			h, err := NewHook(tt.cfg)
			require.Nil(t, err)

			ctx := context.Background()
			req := &bittorrent.AnnounceRequest{}
			resp := &bittorrent.AnnounceResponse{}

			hashbytes, err := hex.DecodeString(tt.ih)
			require.Nil(t, err)

			hashinfo := bittorrent.InfoHashFromBytes(hashbytes)

			req.InfoHash = hashinfo

			nctx, err := h.HandleAnnounce(ctx, req, resp)
			require.Equal(t, ctx, nctx)
			if tt.approved == true {
				require.Nil(t, err)
			} else {
				require.NotNil(t, err)
			}
		})
	}
}
<commit_msg>middleware/torrentapproval : make use of ErrTorrentUnapproved<commit_after>package torrentapproval

import (
	"context"
	"encoding/hex"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/chihaya/chihaya/bittorrent"
)

var cases = []struct {
	cfg      Config
	ih       string
	approved bool
}{
	// Infohash is whitelisted
	{
		Config{
			Whitelist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"3532cf2d327fad8448c075b4cb42c8136964a435",
		true,
	},
	// Infohash is not whitelisted
	{
		Config{
			Whitelist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"4532cf2d327fad8448c075b4cb42c8136964a435",
		false,
	},
	// Infohash is not blacklisted
	{
		Config{
			Blacklist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"4532cf2d327fad8448c075b4cb42c8136964a435",
		true,
	},
	// Infohash is blacklisted
	{
		Config{
			Blacklist: []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
		},
		"3532cf2d327fad8448c075b4cb42c8136964a435",
		false,
	},
}

func TestHandleAnnounce(t *testing.T) {
	for _, tt := range cases {
		t.Run(fmt.Sprintf("testing hash %s", tt.ih), func(t *testing.T) {
			h, err := NewHook(tt.cfg)
			require.Nil(t, err)

			ctx := context.Background()
			req := &bittorrent.AnnounceRequest{}
			resp := &bittorrent.AnnounceResponse{}

			hashbytes, err := hex.DecodeString(tt.ih)
			require.Nil(t, err)

			hashinfo := bittorrent.InfoHashFromBytes(hashbytes)

			req.InfoHash = hashinfo

			nctx, err := h.HandleAnnounce(ctx, req, resp)
			require.Equal(t, ctx, nctx)
			if tt.approved == true {
				require.NotEqual(t, err, ErrTorrentUnapproved)
			} else {
				require.Equal(t, err, ErrTorrentUnapproved)
			}
		})
	}
}

{'fraction_non_alphanumeric': 0.13099909447630545, 'fraction_numerical': 0.1352248717174766, 'mean_word_length': 4.0672782874617734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4933527', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1389, 'n_words': 208}