github.com/iDigitalFlame/xmt@v0.5.4/c2/wrapper/wrapper_test.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package wrapper
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/aes"
    22  	"io"
    23  	"testing"
    24  
    25  	"github.com/iDigitalFlame/xmt/data"
    26  	"github.com/iDigitalFlame/xmt/util"
    27  )
    28  
    29  type wrapper interface {
    30  	Unwrap(io.Reader) (io.Reader, error)
    31  	Wrap(io.WriteCloser) (io.WriteCloser, error)
    32  }
    33  
    34  func TestWrapHex(t *testing.T) {
    35  	testWrapper(t, Hex)
    36  }
    37  func TestWrapCBK(t *testing.T) {
    38  	c := NewCBK(uint8(util.FastRand()), uint8(util.FastRand()), uint8(util.FastRand()), uint8(util.FastRand()), 128)
    39  	testWrapper(t, c)
    40  }
    41  func TestWrapAES(t *testing.T) {
    42  	b := make([]byte, 32)
    43  	util.Rand.Read(b)
    44  	c, err := aes.NewCipher(b)
    45  	if err != nil {
    46  		t.Fatalf("TestWrapAES(): NewCipher failed with error: %s!", err.Error())
    47  	}
    48  	i := make([]byte, 16)
    49  	util.Rand.Read(i)
    50  	x, err := NewBlock(c, i)
    51  	if err != nil {
    52  		t.Fatalf("TestWrapAES(): Block failed with error: %s!", err.Error())
    53  	}
    54  	testWrapper(t, x)
    55  }
    56  func TestWrapXOR(t *testing.T) {
    57  	b := make([]byte, 64)
    58  	util.Rand.Read(b)
    59  	x := NewXOR(b)
    60  	testWrapper(t, x)
    61  }
    62  func TestWrapGzip(t *testing.T) {
    63  	testWrapper(t, Gzip)
    64  }
    65  func TestWrapZlib(t *testing.T) {
    66  	testWrapper(t, Zlib)
    67  }
    68  func TestWrapBase64(t *testing.T) {
    69  	testWrapper(t, Base64)
    70  }
    71  func testWrapper(t *testing.T, x wrapper) {
    72  	var b bytes.Buffer
    73  	w, err := x.Wrap(data.WriteCloser(&b))
    74  	if err != nil {
    75  		t.Fatalf("TestWrapper(): Wrap failed with error: %s!", err.Error())
    76  	}
    77  	if _, err = w.Write([]byte("hello world!")); err != nil {
    78  		t.Fatalf("TestWrapper(): Write failed with error: %s!", err.Error())
    79  	}
    80  	w.Close()
    81  	r, err := x.Unwrap(bytes.NewReader(b.Bytes()))
    82  	if err != nil {
    83  		t.Fatalf("Unwrap failed with error: %s!", err.Error())
    84  	}
    85  	o := make([]byte, 12)
    86  	if _, err = r.Read(o); err != nil && err != io.EOF {
    87  		t.Fatalf("TestWrapper(): Read failed with error: %s!", err.Error())
    88  	}
    89  	if string(o) != "hello world!" {
    90  		t.Fatalf(`TestWrapper(): Result output "%s" did not match "hello world!"!`, o)
    91  	}
    92  }