...

Source file src/google.golang.org/grpc/test/clienttester.go

Documentation: google.golang.org/grpc/test

     1  /*
     2   * Copyright 2022 gRPC authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package test
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"net"
    23  	"testing"
    24  
    25  	"golang.org/x/net/http2"
    26  )
    27  
    28  var (
    29  	clientPreface = []byte(http2.ClientPreface)
    30  )
    31  
    32  func newClientTester(t *testing.T, conn net.Conn) *clientTester {
    33  	ct := &clientTester{
    34  		t:    t,
    35  		conn: conn,
    36  	}
    37  	ct.fr = http2.NewFramer(conn, conn)
    38  	ct.greet()
    39  	return ct
    40  }
    41  
    42  type clientTester struct {
    43  	t    *testing.T
    44  	conn net.Conn
    45  	fr   *http2.Framer
    46  }
    47  
    48  // greet() performs the necessary steps for http2 connection establishment on
    49  // the server side.
    50  func (ct *clientTester) greet() {
    51  	ct.wantClientPreface()
    52  	ct.wantSettingsFrame()
    53  	ct.writeSettingsFrame()
    54  	ct.writeSettingsAck()
    55  
    56  	for {
    57  		f, err := ct.fr.ReadFrame()
    58  		if err != nil {
    59  			ct.t.Errorf("error reading frame from client side: %v", err)
    60  		}
    61  		switch f := f.(type) {
    62  		case *http2.SettingsFrame:
    63  			if f.IsAck() { // HTTP/2 handshake completed.
    64  				return
    65  			}
    66  		default:
    67  			ct.t.Errorf("during greet, unexpected frame type %T", f)
    68  		}
    69  	}
    70  }
    71  
    72  func (ct *clientTester) wantClientPreface() {
    73  	preface := make([]byte, len(clientPreface))
    74  	if _, err := io.ReadFull(ct.conn, preface); err != nil {
    75  		ct.t.Errorf("Error at server-side while reading preface from client. Err: %v", err)
    76  	}
    77  	if !bytes.Equal(preface, clientPreface) {
    78  		ct.t.Errorf("received bogus greeting from client %q", preface)
    79  	}
    80  }
    81  
    82  func (ct *clientTester) wantSettingsFrame() {
    83  	frame, err := ct.fr.ReadFrame()
    84  	if err != nil {
    85  		ct.t.Errorf("error reading initial settings frame from client: %v", err)
    86  	}
    87  	_, ok := frame.(*http2.SettingsFrame)
    88  	if !ok {
    89  		ct.t.Errorf("initial frame sent from client is not a settings frame, type %T", frame)
    90  	}
    91  }
    92  
    93  func (ct *clientTester) writeSettingsFrame() {
    94  	if err := ct.fr.WriteSettings(); err != nil {
    95  		ct.t.Fatalf("Error writing initial SETTINGS frame from client to server: %v", err)
    96  	}
    97  }
    98  
    99  func (ct *clientTester) writeSettingsAck() {
   100  	if err := ct.fr.WriteSettingsAck(); err != nil {
   101  		ct.t.Fatalf("Error writing ACK of client's SETTINGS: %v", err)
   102  	}
   103  }
   104  
   105  func (ct *clientTester) writeGoAway(maxStreamID uint32, code http2.ErrCode, debugData []byte) {
   106  	if err := ct.fr.WriteGoAway(maxStreamID, code, debugData); err != nil {
   107  		ct.t.Fatalf("Error writing GOAWAY: %v", err)
   108  	}
   109  }
   110  

View as plain text