github.com/iDigitalFlame/xmt@v0.5.4/c2/cfg/config_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 cfg 18 19 import ( 20 "bytes" 21 "testing" 22 ) 23 24 func TestLen(t *testing.T) { 25 if n := Pack(ConnectTCP, WrapGzip).Len(); n != 2 { 26 t.Fatalf(`TestLen(): Len should be "2" but got "%d"!`, n) 27 } 28 } 29 func TestGroup(t *testing.T) { 30 if v := (Config{}).Group(0); v != nil { 31 t.Fatalf(`TestGroup(): Empty Group should be "nil" but got "%v"!`, v) 32 } 33 c := Pack(ConnectICMP) 34 c.AddGroup(WrapCBK(1, 2, 3, 4), ConnectTCP, Host("test1")) 35 c.AddGroup(WrapXOR([]byte("derp-master"))) 36 c.Add(ConnectPipe) 37 if v := c.Group(-1); !bytes.Equal(v, c) { 38 t.Fatalf(`TestGroup(): Group(-1) should return itself but got "%v"!`, v) 39 } 40 if n := c.Group(0).Len(); n != 1 { 41 t.Fatalf(`TestGroup(): Group(0) len should be "1" but got "%d"!`, n) 42 } 43 if n := c.Group(1).Len(); n != 15 { 44 t.Fatalf(`TestGroup(): Group(1) len should be "15" but got "%d"!`, n) 45 } 46 if n := c.Group(2).Len(); n != 15 { 47 t.Fatalf(`TestGroup(): Group(2) len should be "15" but got "%d"!`, n) 48 } 49 if n := c.Group(3).Len(); n != 15 { 50 t.Fatalf(`TestGroup(): Group(3) len should be "15" but got "%d"!`, n) 51 } 52 } 53 func TestBuild(t *testing.T) { 54 if _, err := Build(ConnectICMP, WrapBase64, Host("test123")); err != nil { 55 t.Fatalf(`TestBuild(): Build should pass but got "%s"!`, err.Error()) 56 } 57 if err := Pack(ConnectICMP, WrapBase64, Host("test123")).Validate(); err != nil { 58 t.Fatalf(`TestBuild(): Validate should pass but got "%s"!`, err.Error()) 59 } 60 if _, err := Build(ConnectICMP, ConnectPipe, WrapBase64, Host("test123")); err == nil { 61 t.Fatalf("TestBuild(): Invalid Build should not pass!") 62 } 63 } 64 func TestGroups(t *testing.T) { 65 c := Pack(ConnectICMP) 66 c.AddGroup(WrapBase64) 67 c.AddGroup(WrapZlib, WrapHex) 68 c.AddGroup() 69 c.Add(ConnectPipe) 70 c.Add() 71 if n := c.Groups(); n != 3 { 72 t.Fatalf(`TestGroups(): Group count should be "3" but got "%d"!`, n) 73 } 74 } 75 func TestReadWrite(t *testing.T) { 76 var b bytes.Buffer 77 if err := Write(&b, ConnectICMP, WrapBase64, Host("test123")); err != nil { 78 t.Fatalf(`TestReadWrite(): Write failed with error "%s"!`, err.Error()) 79 } 80 if n := b.Len(); n != 12 { 81 t.Fatalf(`TestReadWrite(): Written bytes count should be "12" but got "%d"!`, n) 82 } 83 if _, err := Reader(bytes.NewReader(b.Bytes())); err != nil { 84 t.Fatalf(`TestReadWrite(): Reader should pass but got "%s"!`, err.Error()) 85 } 86 }