go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/streamserver/namedPipe_posix_test.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build unix
    16  // +build unix
    17  
    18  package streamserver
    19  
    20  import (
    21  	"context"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  
    28  	"go.chromium.org/luci/logdog/client/butlerlib/streamclient"
    29  
    30  	. "github.com/smartystreets/goconvey/convey"
    31  	. "go.chromium.org/luci/common/testing/assertions"
    32  )
    33  
    34  func withTempDir(t *testing.T, fn func(string)) func() {
    35  	return func() {
    36  		tdir, err := ioutil.TempDir("", "butler_test")
    37  		if err != nil {
    38  			t.Fatalf("failed to create temporary directory: %s", err)
    39  		}
    40  		defer func() {
    41  			if err := os.RemoveAll(tdir); err != nil {
    42  				t.Errorf("failed to clean up temporary directory [%s]: %s", tdir, err)
    43  			}
    44  		}()
    45  		fn(tdir)
    46  	}
    47  }
    48  
    49  func TestUNIXDomainSocketServer(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	Convey(`A UNIX domain socket server`, t, func() {
    53  		ctx := context.Background()
    54  
    55  		Convey(`Will create a temporary name if given an empty path`, func() {
    56  			s, err := newStreamServer(ctx, "")
    57  			So(err, ShouldBeNil)
    58  			So(s.Address(), ShouldStartWith, "unix:"+os.TempDir())
    59  		})
    60  
    61  		Convey(`Will refuse to create if longer than maximum length.`, func() {
    62  			_, err := newStreamServer(ctx, strings.Repeat("A", maxPOSIXNamedSocketLength+1))
    63  			So(err, ShouldErrLike, "path exceeds maximum length")
    64  		})
    65  
    66  		Convey(`When created and listening.`, withTempDir(t, func(tdir string) {
    67  			svr, err := newStreamServer(ctx, filepath.Join(tdir, "butler.sock"))
    68  			So(err, ShouldBeNil)
    69  
    70  			So(svr.Listen(), ShouldBeNil)
    71  			defer svr.Close()
    72  
    73  			client, err := streamclient.New(svr.Address(), "")
    74  			So(err, ShouldBeNil)
    75  
    76  			testClientServer(svr, client)
    77  		}))
    78  	})
    79  }