go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/raw.go (about)

     1  // Copyright 2019 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use 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  package test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.nanomsg.org/mangos/v3"
    21  	"go.nanomsg.org/mangos/v3/protocol"
    22  )
    23  
    24  // VerifyRaw verifies that the socket created is raw, and cannot be changed to cooked.
    25  func VerifyRaw(t *testing.T, f func() (mangos.Socket, error)) {
    26  	s, err := f()
    27  	MustSucceed(t, err)
    28  	val, err := s.GetOption(mangos.OptionRaw)
    29  	MustSucceed(t, err)
    30  	if b, ok := val.(bool); ok {
    31  		MustBeTrue(t, b)
    32  	} else {
    33  		t.Fatalf("Not a boolean")
    34  	}
    35  
    36  	err = s.SetOption(mangos.OptionRaw, false)
    37  	MustFail(t, err)
    38  	err = s.SetOption(mangos.OptionRaw, 1)
    39  	MustFail(t, err)
    40  
    41  	// Raw Sockets also don't support contexts.
    42  	_, err = s.OpenContext()
    43  	MustFail(t, err)
    44  	MustBeTrue(t, err == protocol.ErrProtoOp)
    45  	MustSucceed(t, s.Close())
    46  }
    47  
    48  // VerifyCooked verifies that the socket created is cooked, and cannot be changed to raw.
    49  func VerifyCooked(t *testing.T, f func() (mangos.Socket, error)) {
    50  	s, err := f()
    51  	MustSucceed(t, err)
    52  	val, err := s.GetOption(mangos.OptionRaw)
    53  	MustSucceed(t, err)
    54  	if b, ok := val.(bool); ok {
    55  		MustBeFalse(t, b)
    56  	} else {
    57  		t.Fatalf("Not a boolean")
    58  	}
    59  
    60  	err = s.SetOption(mangos.OptionRaw, true)
    61  	MustFail(t, err)
    62  	err = s.SetOption(mangos.OptionRaw, 0)
    63  	MustFail(t, err)
    64  	MustSucceed(t, s.Close())
    65  }