github.com/ledgerwatch/erigon-lib@v1.0.0/downloader/downloader_test.go (about)

     1  package downloader
     2  
     3  import (
     4  	"context"
     5  	lg "github.com/anacrolix/log"
     6  	"github.com/ledgerwatch/erigon-lib/common/datadir"
     7  	downloadercfg2 "github.com/ledgerwatch/erigon-lib/downloader/downloadercfg"
     8  	"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
     9  	"github.com/stretchr/testify/require"
    10  	"path/filepath"
    11  	"testing"
    12  )
    13  
    14  func TestChangeInfoHashOfSameFile(t *testing.T) {
    15  	require := require.New(t)
    16  	dirs := datadir.New(t.TempDir())
    17  	cfg, err := downloadercfg2.New(dirs, "", lg.Info, 0, 0, 0, 0, 0, nil, "")
    18  	require.NoError(err)
    19  	d, err := New(context.Background(), cfg)
    20  	require.NoError(err)
    21  	defer d.Close()
    22  	err = d.AddInfoHashAsMagnetLink(d.ctx, snaptype.Hex2InfoHash("aa"), "a.seg")
    23  	require.NoError(err)
    24  	tt, ok := d.torrentClient.Torrent(snaptype.Hex2InfoHash("aa"))
    25  	require.True(ok)
    26  	require.Equal("a.seg", tt.Name())
    27  
    28  	// adding same file twice is ok
    29  	err = d.AddInfoHashAsMagnetLink(d.ctx, snaptype.Hex2InfoHash("aa"), "a.seg")
    30  	require.NoError(err)
    31  
    32  	// adding same file with another infoHash - is ok, must be skipped
    33  	// use-cases:
    34  	//	- release of re-compressed version of same file,
    35  	//	- ErigonV1.24 produced file X, then ErigonV1.25 released with new compression algorithm and produced X with anouther infoHash.
    36  	//		ErigonV1.24 node must keep using existing file instead of downloading new one.
    37  	err = d.AddInfoHashAsMagnetLink(d.ctx, snaptype.Hex2InfoHash("bb"), "a.seg")
    38  	require.NoError(err)
    39  	tt, ok = d.torrentClient.Torrent(snaptype.Hex2InfoHash("aa"))
    40  	require.True(ok)
    41  	require.Equal("a.seg", tt.Name())
    42  
    43  	// allow adding files only if they are insidesnapshots dir
    44  	_, err = BuildTorrentIfNeed(d.ctx, "a.seg", dirs.Snap)
    45  	require.NoError(err)
    46  	_, err = BuildTorrentIfNeed(d.ctx, "b/a.seg", dirs.Snap)
    47  	require.NoError(err)
    48  	_, err = BuildTorrentIfNeed(d.ctx, filepath.Join(dirs.Snap, "a.seg"), dirs.Snap)
    49  	require.NoError(err)
    50  	_, err = BuildTorrentIfNeed(d.ctx, filepath.Join(dirs.Snap, "b", "a.seg"), dirs.Snap)
    51  	require.NoError(err)
    52  
    53  	// reject escaping snapshots dir
    54  	_, err = BuildTorrentIfNeed(d.ctx, filepath.Join(dirs.Chaindata, "b", "a.seg"), dirs.Snap)
    55  	require.Error(err)
    56  	_, err = BuildTorrentIfNeed(d.ctx, "./../a.seg", dirs.Snap)
    57  	require.Error(err)
    58  }