greeting.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Package testutil contains stuff for testing torrent-related behaviour.
  2. //
  3. // "greeting" is a single-file torrent of a file called "greeting" that
  4. // "contains "hello, world\n".
  5. package testutil
  6. import (
  7. "os"
  8. "path/filepath"
  9. "github.com/anacrolix/torrent/metainfo"
  10. )
  11. var Greeting = Torrent{
  12. Files: []File{{
  13. Data: GreetingFileContents,
  14. }},
  15. Name: GreetingFileName,
  16. }
  17. const (
  18. // A null in the middle triggers an error if SQLite stores data as text instead of blob.
  19. GreetingFileContents = "hello,\x00world\n"
  20. GreetingFileName = "greeting"
  21. )
  22. func CreateDummyTorrentData(dirName string) string {
  23. f, _ := os.Create(filepath.Join(dirName, "greeting"))
  24. defer f.Close()
  25. f.WriteString(GreetingFileContents)
  26. return f.Name()
  27. }
  28. func GreetingMetaInfo() *metainfo.MetaInfo {
  29. return Greeting.Metainfo(5)
  30. }
  31. // Gives a temporary directory containing the completed "greeting" torrent,
  32. // and a corresponding metainfo describing it. The temporary directory can be
  33. // cleaned away with os.RemoveAll.
  34. func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
  35. tempDir, err := os.MkdirTemp(os.TempDir(), "")
  36. if err != nil {
  37. panic(err)
  38. }
  39. CreateDummyTorrentData(tempDir)
  40. metaInfo = GreetingMetaInfo()
  41. return
  42. }