github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/sendfile_test.go (about)

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