github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/cache/server/rpc_server_test.go (about)

     1  // Test for the rpc server.
     2  package server
     3  
     4  import (
     5  	"bytes"
     6  	"crypto/tls"
     7  	"crypto/x509"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"runtime"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"golang.org/x/net/context"
    16  	"google.golang.org/grpc"
    17  	"google.golang.org/grpc/credentials"
    18  
    19  	pb "cache/proto/rpc_cache"
    20  )
    21  
    22  const (
    23  	testDir   = "src/cache/test_data"
    24  	testKey   = "src/cache/test_data/key.pem"
    25  	testCert  = "src/cache/test_data/cert_signed.pem"
    26  	testCert2 = "src/cache/test_data/cert.pem"
    27  	testCa    = "src/cache/test_data/ca.pem"
    28  )
    29  
    30  func startServer(port int, auth bool, readonlyCerts, writableCerts string) *grpc.Server {
    31  	cache := NewCache(testDir, 20*time.Hour, 100, 1000000, 1000000)
    32  	if !auth {
    33  		s, lis := BuildGrpcServer(port, cache, nil, "", "", "", readonlyCerts, writableCerts)
    34  		go s.Serve(lis)
    35  		return s
    36  	}
    37  	s, lis := BuildGrpcServer(port, cache, nil, testKey, testCert, testCa, readonlyCerts, writableCerts)
    38  	go s.Serve(lis)
    39  	return s
    40  }
    41  
    42  func buildClient(t *testing.T, port int, auth bool) pb.RpcCacheClient {
    43  	const maxSize = 10 * 1024 * 1024
    44  	sizeOpts := grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxSize), grpc.MaxCallSendMsgSize(maxSize))
    45  	url := fmt.Sprintf("localhost:%d", port)
    46  	if !auth {
    47  		conn, err := grpc.Dial(url, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second), sizeOpts)
    48  		assert.NoError(t, err)
    49  		return pb.NewRpcCacheClient(conn)
    50  	}
    51  	cert, err := tls.LoadX509KeyPair(testCert, testKey)
    52  	assert.NoError(t, err)
    53  	ca, err := ioutil.ReadFile(testCa)
    54  	assert.NoError(t, err)
    55  	config := tls.Config{
    56  		Certificates: []tls.Certificate{cert},
    57  		RootCAs:      x509.NewCertPool(),
    58  	}
    59  	assert.True(t, config.RootCAs.AppendCertsFromPEM(ca))
    60  	conn, err := grpc.Dial(url, grpc.WithTransportCredentials(credentials.NewTLS(&config)), grpc.WithTimeout(5*time.Second), sizeOpts)
    61  	assert.NoError(t, err)
    62  	return pb.NewRpcCacheClient(conn)
    63  }
    64  
    65  func ctx() (context.Context, context.CancelFunc) {
    66  	return context.WithTimeout(context.Background(), 5*time.Second)
    67  }
    68  
    69  func TestNoAuth(t *testing.T) {
    70  	s := startServer(7677, false, "", "")
    71  	defer s.Stop()
    72  	c := buildClient(t, 7677, false)
    73  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    74  	defer cancel()
    75  	_, err := c.Store(ctx, &pb.StoreRequest{})
    76  	assert.NoError(t, err)
    77  }
    78  
    79  func TestAuthRequired(t *testing.T) {
    80  	s := startServer(7678, true, "", "")
    81  	defer s.Stop()
    82  	c := buildClient(t, 7678, false)
    83  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    84  	defer cancel()
    85  	_, err := c.Store(ctx, &pb.StoreRequest{})
    86  	assert.Error(t, err, "Fails because the client doesn't use TLS")
    87  }
    88  
    89  func TestReadonlyAuth(t *testing.T) {
    90  	s := startServer(7679, true, testCert, testCert2)
    91  	defer s.Stop()
    92  	c := buildClient(t, 7679, true)
    93  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    94  	defer cancel()
    95  	_, err := c.Retrieve(ctx, &pb.RetrieveRequest{})
    96  	assert.NoError(t, err)
    97  	ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
    98  	defer cancel()
    99  	_, err = c.Store(ctx, &pb.StoreRequest{})
   100  	assert.Error(t, err, "Fails because the client isn't authenticated")
   101  }
   102  
   103  func TestWritableAuth(t *testing.T) {
   104  	s := startServer(7680, true, testCert, testCert)
   105  	defer s.Stop()
   106  	c := buildClient(t, 7680, true)
   107  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   108  	defer cancel()
   109  	_, err := c.Retrieve(ctx, &pb.RetrieveRequest{})
   110  	assert.NoError(t, err)
   111  	ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
   112  	defer cancel()
   113  	_, err = c.Store(ctx, &pb.StoreRequest{})
   114  	assert.NoError(t, err)
   115  }
   116  
   117  func TestDeleteNoAuth(t *testing.T) {
   118  	s := startServer(7681, true, testCert, testCert2)
   119  	defer s.Stop()
   120  	c := buildClient(t, 7681, true)
   121  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   122  	defer cancel()
   123  	_, err := c.Delete(ctx, &pb.DeleteRequest{})
   124  	assert.Error(t, err, "Fails because the client isn't authenticated")
   125  }
   126  
   127  func TestDeleteAuth(t *testing.T) {
   128  	s := startServer(7682, true, testCert, testCert)
   129  	defer s.Stop()
   130  	c := buildClient(t, 7682, true)
   131  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   132  	defer cancel()
   133  	_, err := c.Delete(ctx, &pb.DeleteRequest{})
   134  	assert.NoError(t, err)
   135  }
   136  
   137  func TestMaxMessageSize(t *testing.T) {
   138  	s := startServer(7677, false, "", "")
   139  	defer s.Stop()
   140  	c := buildClient(t, 7677, false)
   141  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   142  	defer cancel()
   143  	_, err := c.Store(ctx, &pb.StoreRequest{
   144  		Os:   runtime.GOOS,
   145  		Arch: runtime.GOARCH,
   146  		Hash: bytes.Repeat([]byte{'a'}, 28),
   147  		Artifacts: []*pb.Artifact{
   148  			{
   149  				Package: "src/cache/server",
   150  				Target:  "size_test",
   151  				File:    "size_test.txt",
   152  				Body:    bytes.Repeat([]byte{'a'}, 5*1024*1024),
   153  			},
   154  		},
   155  	})
   156  	assert.NoError(t, err)
   157  }