github.com/iDigitalFlame/xmt@v0.5.4/c2/transform/transform_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 transform 18 19 import ( 20 "bytes" 21 "io" 22 "testing" 23 24 "github.com/iDigitalFlame/xmt/util" 25 ) 26 27 type transform interface { 28 Read([]byte, io.Writer) error 29 Write([]byte, io.Writer) error 30 } 31 32 func TestTransformDNS(t *testing.T) { 33 testWrapper(t, DNS) 34 } 35 func TestTransformBase64(t *testing.T) { 36 testWrapper(t, Base64) 37 } 38 func TestTransformBase64Shift(t *testing.T) { 39 testWrapper(t, B64Shift(int(util.FastRand()))) 40 } 41 func testWrapper(t *testing.T, x transform) { 42 var i, o bytes.Buffer 43 if err := x.Write([]byte("hello world!"), &o); err != nil { 44 t.Fatalf("TestTransform(): Write failed with error: %s!", err.Error()) 45 } 46 if err := x.Read(o.Bytes(), &i); err != nil { 47 t.Fatalf("TestTransform(): Write failed with error: %s!", err.Error()) 48 } 49 v := make([]byte, 12) 50 if _, err := i.Read(v); err != nil && err != io.EOF { 51 t.Fatalf("TestTransform(): Read failed with error: %s!", err.Error()) 52 } 53 if string(v) != "hello world!" { 54 t.Fatalf(`TestTransform(): Result output "%s" did not match "hello world!"!`, v) 55 } 56 }