sendfile_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin,amd64 darwin,386 dragonfly freebsd linux solaris
  5. package unix_test
  6. import (
  7. "io/ioutil"
  8. "net"
  9. "os"
  10. "path/filepath"
  11. "testing"
  12. "golang.org/x/sys/unix"
  13. )
  14. func TestSendfile(t *testing.T) {
  15. // Set up source data file.
  16. tempDir, err := ioutil.TempDir("", "TestSendfile")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. defer os.RemoveAll(tempDir)
  21. name := filepath.Join(tempDir, "source")
  22. const contents = "contents"
  23. err = ioutil.WriteFile(name, []byte(contents), 0666)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. done := make(chan bool)
  28. // Start server listening on a socket.
  29. ln, err := net.Listen("tcp", "127.0.0.1:0")
  30. if err != nil {
  31. t.Skipf("listen failed: %s\n", err)
  32. }
  33. defer ln.Close()
  34. go func() {
  35. conn, err := ln.Accept()
  36. if err != nil {
  37. t.Errorf("failed to accept: %v", err)
  38. return
  39. }
  40. defer conn.Close()
  41. b, err := ioutil.ReadAll(conn)
  42. if err != nil {
  43. t.Errorf("failed to read: %v", err)
  44. return
  45. }
  46. if string(b) != contents {
  47. t.Errorf("contents not transmitted: got %s (len=%d), want %s", string(b), len(b), contents)
  48. }
  49. done <- true
  50. }()
  51. // Open source file.
  52. src, err := os.Open(name)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. // Send source file to server.
  57. conn, err := net.Dial("tcp", ln.Addr().String())
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. file, err := conn.(*net.TCPConn).File()
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. var off int64
  66. n, err := unix.Sendfile(int(file.Fd()), int(src.Fd()), &off, len(contents))
  67. if err != nil {
  68. t.Errorf("Sendfile failed %s\n", err)
  69. }
  70. if n != len(contents) {
  71. t.Errorf("written count wrong: want %d, got %d", len(contents), n)
  72. }
  73. // Note: off is updated on some systems and not others. Oh well.
  74. // Linux: increments off by the amount sent.
  75. // Darwin: leaves off unchanged.
  76. // It would be nice to fix Darwin if we can.
  77. if off != 0 && off != int64(len(contents)) {
  78. t.Errorf("offset wrong: god %d, want %d or %d", off, 0, len(contents))
  79. }
  80. // The cursor position should be unchanged.
  81. pos, err := src.Seek(0, 1)
  82. if err != nil {
  83. t.Errorf("can't get cursor position %s\n", err)
  84. }
  85. if pos != 0 {
  86. t.Errorf("cursor position wrong: got %d, want 0", pos)
  87. }
  88. file.Close() // Note: required to have the close below really send EOF to the server.
  89. conn.Close()
  90. // Wait for server to close.
  91. <-done
  92. }