github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/decode_test.go (about) 1 package processor 2 3 import ( 4 "bytes" 5 "encoding/ascii85" 6 "encoding/base64" 7 "encoding/hex" 8 "reflect" 9 "testing" 10 11 "github.com/Jeffail/benthos/v3/lib/log" 12 "github.com/Jeffail/benthos/v3/lib/message" 13 "github.com/Jeffail/benthos/v3/lib/metrics" 14 "github.com/tilinna/z85" 15 ) 16 17 func TestDecodeBadAlgo(t *testing.T) { 18 conf := NewConfig() 19 conf.Decode.Scheme = "does not exist" 20 21 testLog := log.Noop() 22 23 _, err := NewDecode(conf, nil, testLog, metrics.Noop()) 24 if err == nil { 25 t.Error("Expected error from bad algo") 26 } 27 } 28 29 func TestDecodeBase64(t *testing.T) { 30 conf := NewConfig() 31 conf.Decode.Scheme = "base64" 32 33 testLog := log.Noop() 34 35 exp := [][]byte{ 36 []byte("hello world first part"), 37 []byte("hello world second part"), 38 []byte("third part"), 39 []byte("fourth"), 40 []byte("5"), 41 } 42 43 input := [][]byte{} 44 45 for i := range exp { 46 var buf bytes.Buffer 47 48 zw := base64.NewEncoder(base64.StdEncoding, &buf) 49 zw.Write(exp[i]) 50 zw.Close() 51 52 input = append(input, buf.Bytes()) 53 } 54 55 if reflect.DeepEqual(input, exp) { 56 t.Fatal("Input and exp output are the same") 57 } 58 59 proc, err := NewDecode(conf, nil, testLog, metrics.Noop()) 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 msgs, res := proc.ProcessMessage(message.New(input)) 65 if len(msgs) != 1 { 66 t.Error("Decode failed") 67 } else if res != nil { 68 t.Errorf("Expected nil response: %v", res) 69 } 70 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 71 t.Errorf("Unexpected output: %s != %s", act, exp) 72 } 73 } 74 75 func TestDecodeHex(t *testing.T) { 76 conf := NewConfig() 77 conf.Decode.Scheme = "hex" 78 79 exp := [][]byte{ 80 []byte("hello world first part"), 81 []byte("hello world second part"), 82 []byte("third part"), 83 []byte("fourth"), 84 []byte("5"), 85 } 86 87 input := [][]byte{} 88 89 for i := range exp { 90 var buf bytes.Buffer 91 92 zw := hex.NewEncoder(&buf) 93 zw.Write(exp[i]) 94 95 input = append(input, buf.Bytes()) 96 } 97 98 if reflect.DeepEqual(input, exp) { 99 t.Fatal("Input and exp output are the same") 100 } 101 102 proc, err := NewDecode(conf, nil, log.Noop(), metrics.Noop()) 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 msgs, res := proc.ProcessMessage(message.New(input)) 108 if len(msgs) != 1 { 109 t.Error("Decode failed") 110 } else if res != nil { 111 t.Errorf("Expected nil response: %v", res) 112 } 113 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 114 t.Errorf("Unexpected output: %s != %s", act, exp) 115 } 116 } 117 118 func TestDecodeAscii85(t *testing.T) { 119 conf := NewConfig() 120 conf.Decode.Scheme = "ascii85" 121 122 exp := [][]byte{ 123 []byte("hello world first part"), 124 []byte("hello world second part"), 125 []byte("third part"), 126 []byte("fourth"), 127 []byte("5"), 128 } 129 130 input := [][]byte{} 131 132 for i := range exp { 133 var buf bytes.Buffer 134 135 zw := ascii85.NewEncoder(&buf) 136 zw.Write(exp[i]) 137 zw.Close() 138 139 input = append(input, buf.Bytes()) 140 } 141 142 if reflect.DeepEqual(input, exp) { 143 t.Fatal("Input and exp output are the same") 144 } 145 146 proc, err := NewDecode(conf, nil, log.Noop(), metrics.Noop()) 147 if err != nil { 148 t.Fatal(err) 149 } 150 151 msgs, res := proc.ProcessMessage(message.New(input)) 152 if len(msgs) != 1 { 153 t.Error("Decode failed") 154 } else if res != nil { 155 t.Errorf("Expected nil response: %v", res) 156 } 157 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 158 t.Errorf("Unexpected output: %s != %s", act, exp) 159 } 160 } 161 162 func TestDecodeZ85(t *testing.T) { 163 conf := NewConfig() 164 conf.Decode.Scheme = "z85" 165 166 exp := [][]byte{ 167 []byte("hello world first part!!"), 168 []byte("hello world second p"), 169 []byte("third part abcde"), 170 []byte("fourth part!"), 171 []byte("five"), 172 } 173 174 input := [][]byte{} 175 176 for i := range exp { 177 enc := make([]byte, z85.EncodedLen(len(exp[i]))) 178 _, err := z85.Encode(enc, exp[i]) 179 if err != nil { 180 t.Fatalf("Failed to prep example %d: %s", i, err) 181 } 182 input = append(input, enc) 183 } 184 185 if reflect.DeepEqual(input, exp) { 186 t.Fatal("Input and exp output are the same") 187 } 188 189 proc, err := NewDecode(conf, nil, log.Noop(), metrics.Noop()) 190 if err != nil { 191 t.Fatal(err) 192 } 193 194 msgs, res := proc.ProcessMessage(message.New(input)) 195 if len(msgs) != 1 { 196 t.Error("Decode failed") 197 } else if res != nil { 198 t.Errorf("Expected nil response: %v", res) 199 } 200 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 201 t.Errorf("Unexpected output: %s != %s", act, exp) 202 } 203 } 204 205 func TestDecodeIndexBounds(t *testing.T) { 206 conf := NewConfig() 207 208 testLog := log.Noop() 209 210 input := [][]byte{} 211 212 decoded := [][]byte{ 213 []byte("0"), 214 []byte("1"), 215 []byte("2"), 216 []byte("3"), 217 []byte("4"), 218 } 219 220 for i := range decoded { 221 var buf bytes.Buffer 222 223 zw := base64.NewEncoder(base64.StdEncoding, &buf) 224 zw.Write(decoded[i]) 225 zw.Close() 226 227 input = append(input, buf.Bytes()) 228 } 229 230 tests := map[int]int{ 231 -5: 0, 232 -4: 1, 233 -3: 2, 234 -2: 3, 235 -1: 4, 236 0: 0, 237 1: 1, 238 2: 2, 239 3: 3, 240 4: 4, 241 } 242 243 for i, expIndex := range tests { 244 conf.Decode.Parts = []int{i} 245 proc, err := NewDecode(conf, nil, testLog, metrics.Noop()) 246 if err != nil { 247 t.Fatal(err) 248 } 249 250 msgs, res := proc.ProcessMessage(message.New(input)) 251 if len(msgs) != 1 { 252 t.Errorf("Decode failed on index: %v", i) 253 } else if res != nil { 254 t.Errorf("Expected nil response: %v", res) 255 } 256 if exp, act := string(decoded[expIndex]), string(message.GetAllBytes(msgs[0])[expIndex]); exp != act { 257 t.Errorf("Unexpected output for index %v: %v != %v", i, act, exp) 258 } 259 if exp, act := string(decoded[expIndex]), string(message.GetAllBytes(msgs[0])[(expIndex+1)%5]); exp == act { 260 t.Errorf("Processor was applied to wrong index %v: %v != %v", (expIndex+1)%5, act, exp) 261 } 262 } 263 } 264 265 func TestDecodeEmpty(t *testing.T) { 266 conf := NewConfig() 267 conf.Decode.Parts = []int{0, 1} 268 269 testLog := log.Noop() 270 proc, err := NewDecode(conf, nil, testLog, metrics.Noop()) 271 if err != nil { 272 t.Error(err) 273 return 274 } 275 276 msgs, _ := proc.ProcessMessage(message.New([][]byte{})) 277 if len(msgs) > 0 { 278 t.Error("Expected failure with zero part message") 279 } 280 }