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