github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/util/pair_test.go (about)

     1  // Copyright (c) 2014, Kevin Walsh.  All rights reserved.
     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  package util
    16  
    17  import (
    18  	"io"
    19  	"io/ioutil"
    20  	"os"
    21  	"testing"
    22  )
    23  
    24  type NopWriteCloser struct {
    25  	c io.Writer
    26  }
    27  
    28  func (w *NopWriteCloser) Write(p []byte) (n int, err error) {
    29  	return w.c.Write(p)
    30  }
    31  
    32  func (w *NopWriteCloser) Close() error {
    33  	return nil
    34  }
    35  
    36  func PairHelper(t *testing.T, closeA, closeB bool, msg string) {
    37  	a, _ := ioutil.TempFile(os.TempDir(), "tempA")
    38  	defer os.Remove(a.Name())
    39  	b, _ := ioutil.TempFile(os.TempDir(), "tempB")
    40  	defer os.Remove(b.Name())
    41  
    42  	var rA io.ReadCloser
    43  	if closeA {
    44  		rA = a
    45  	} else {
    46  		rA = ioutil.NopCloser(a)
    47  	}
    48  	var rB io.WriteCloser
    49  	if closeB {
    50  		rB = b
    51  	} else {
    52  		// rB = ioutil.NopCloser(b) // argh: doesn't exist?
    53  		rB = &NopWriteCloser{b}
    54  	}
    55  
    56  	p := NewPairReadWriteCloser(rA, rB)
    57  	_, err := io.WriteString(p, "hello")
    58  	if err != nil {
    59  		t.Error(err.Error)
    60  		return
    61  	}
    62  	err = p.Close()
    63  	if err != nil {
    64  		t.Error(err.Error)
    65  		return
    66  	}
    67  	if p.Close() == nil && (closeA || closeB) {
    68  		t.Error("pair did not detect double close for " + msg)
    69  	}
    70  	if (a.Close() != nil) != (closeA) {
    71  		t.Error("tempA close was not handled properly for " + msg)
    72  	}
    73  	if (b.Close() != nil) != (closeB) {
    74  		t.Error("tempB close was not handled properly for " + msg)
    75  	}
    76  
    77  	a, _ = os.Open(a.Name())
    78  	b, _ = os.Open(b.Name())
    79  
    80  	p = NewPairReadWriteCloser(b, a)
    81  	buf := make([]byte, 100)
    82  	n, err := io.ReadFull(p, buf)
    83  	if err != io.ErrUnexpectedEOF {
    84  		t.Error(err.Error())
    85  		return
    86  	}
    87  	if n != len("hello") {
    88  		t.Error("wrong number of chars")
    89  	}
    90  	p.Close()
    91  }
    92  
    93  func TestPairReadWriteCloser(t *testing.T) {
    94  	PairHelper(t, true, true, "double close")
    95  	PairHelper(t, true, false, "close only A")
    96  	PairHelper(t, false, true, "close only B")
    97  	PairHelper(t, false, false, "close neither")
    98  }