tstor/daemons/torrent/fs_test.go

143 lines
3.6 KiB
Go
Raw Normal View History

2024-05-19 21:24:09 +00:00
package torrent
import (
"os"
"testing"
"github.com/anacrolix/torrent"
)
const testMagnet = "magnet:?xt=urn:btih:a88fda5954e89178c372716a6a78b8180ed4dad3&dn=The+WIRED+CD+-+Rip.+Sample.+Mash.+Share&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fwired-cd.torrent"
2021-11-29 10:07:54 +00:00
var Cli *torrent.Client
2021-11-29 10:07:54 +00:00
func TestMain(m *testing.M) {
cfg := torrent.NewDefaultClientConfig()
cfg.DataDir = os.TempDir()
// disable webseeds to avoid a panic when closing client on tests
cfg.DisableWebseeds = true
client, err := torrent.NewClient(cfg)
2021-11-29 10:07:54 +00:00
if err != nil {
panic(err)
}
Cli = client
exitVal := m.Run()
client.Close()
2021-11-29 10:07:54 +00:00
os.Exit(exitVal)
}
2023-10-16 09:18:40 +00:00
// func TestTorrentFilesystem(t *testing.T) {
// require := require.New(t)
2021-11-29 10:07:54 +00:00
2023-10-16 09:18:40 +00:00
// to, err := Cli.AddMagnet(testMagnet)
// require.NoError(err)
2023-10-16 09:18:40 +00:00
// tfs := NewTorrentFs(600)
// tfs.AddTorrent(to)
2023-10-16 09:18:40 +00:00
// files, err := tfs.ReadDir("/")
// require.NoError(err)
// require.Len(files, 1)
// require.Contains(files, "The WIRED CD - Rip. Sample. Mash. Share")
2023-10-16 09:18:40 +00:00
// files, err = tfs.ReadDir("/The WIRED CD - Rip. Sample. Mash. Share")
// require.NoError(err)
// require.Len(files, 18)
2023-10-16 09:18:40 +00:00
// f, err := tfs.Open("/The WIRED CD - Rip. Sample. Mash. Share/not_existing_file.txt")
// require.Equal(os.ErrNotExist, err)
// require.Nil(f)
2023-10-16 09:18:40 +00:00
// f, err = tfs.Open("/The WIRED CD - Rip. Sample. Mash. Share/01 - Beastie Boys - Now Get Busy.mp3")
// require.NoError(err)
// require.NotNil(f)
// require.Equal(f.Size(), int64(1964275))
2023-10-16 09:18:40 +00:00
// b := make([]byte, 10)
2023-10-16 09:18:40 +00:00
// n, err := f.Read(b)
// require.NoError(err)
// require.Equal(10, n)
// require.Equal([]byte{0x49, 0x44, 0x33, 0x3, 0x0, 0x0, 0x0, 0x0, 0x1f, 0x76}, b)
2023-10-16 09:18:40 +00:00
// n, err = f.ReadAt(b, 10)
// require.NoError(err)
// require.Equal(10, n)
2023-10-16 09:18:40 +00:00
// n, err = f.ReadAt(b, 10000)
// require.NoError(err)
// require.Equal(10, n)
2023-10-16 09:18:40 +00:00
// tfs.RemoveTorrent(to.InfoHash().String())
// files, err = tfs.ReadDir("/")
// require.NoError(err)
// require.Len(files, 0)
2021-11-29 10:07:54 +00:00
2023-10-16 09:18:40 +00:00
// require.NoError(f.Close())
// }
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// func TestReadAtTorrent(t *testing.T) {
// t.Parallel()
2023-10-18 09:52:48 +00:00
2024-03-28 13:09:42 +00:00
// ctx := context.Background()
2024-03-20 21:47:51 +00:00
2024-03-28 13:09:42 +00:00
// require := require.New(t)
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// to, err := Cli.AddMagnet(testMagnet)
// require.NoError(err)
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// <-to.GotInfo()
// torrFile := to.Files()[0]
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// tf, err := openTorrentFile(ctx, "torr", torrFile)
// require.NoError(err)
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// defer tf.Close(ctx)
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// toRead := make([]byte, 5)
// n, err := tf.ReadAt(ctx, toRead, 6)
// require.NoError(err)
// require.Equal(5, n)
// require.Equal([]byte{0x0, 0x0, 0x1f, 0x76, 0x54}, toRead)
2021-11-29 10:07:54 +00:00
2024-03-28 13:09:42 +00:00
// n, err = tf.ReadAt(ctx, toRead, 0)
// require.NoError(err)
// require.Equal(5, n)
// require.Equal([]byte{0x49, 0x44, 0x33, 0x3, 0x0}, toRead)
// }
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// func TestReadAtWrapper(t *testing.T) {
// t.Parallel()
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// ctx := context.Background()
2024-03-20 21:47:51 +00:00
2024-03-28 13:09:42 +00:00
// require := require.New(t)
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// to, err := Cli.AddMagnet(testMagnet)
// require.NoError(err)
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// <-to.GotInfo()
// torrFile := to.Files()[0]
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// r, err := openTorrentFile(ctx, "file", torrFile)
// require.NoError(err)
// defer r.Close(ctx)
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// toRead := make([]byte, 5)
// n, err := r.ReadAt(ctx, toRead, 6)
// require.NoError(err)
// require.Equal(5, n)
// require.Equal([]byte{0x0, 0x0, 0x1f, 0x76, 0x54}, toRead)
2021-12-01 18:59:21 +00:00
2024-03-28 13:09:42 +00:00
// n, err = r.ReadAt(ctx, toRead, 0)
// require.NoError(err)
// require.Equal(5, n)
// require.Equal([]byte{0x49, 0x44, 0x33, 0x3, 0x0}, toRead)
// }