github.com/sentienttechnologies/studio-go-runner@v0.0.0-20201118202441-6d21f2ced8ee/internal/runner/envelope_test.go (about)

     1  // Copyright 2020 (c) Cognizant Digital Business, Evolutionary AI. All rights reserved. Issued under the Apache 2.0 License.
     2  
     3  package runner
     4  
     5  import (
     6  	"bytes"
     7  	"io/ioutil"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/go-stack/stack"
    12  	"github.com/jjeffery/kv"
    13  
    14  	"github.com/go-test/deep"
    15  )
    16  
    17  // This file contains a number of tests related to JSON document management including
    18  // encoding, decoding, and encryption of payloads.
    19  
    20  func TestEnvelopeDetectNeg(t *testing.T) {
    21  	// Read and load a default unencrypted payload
    22  	payload, errGo := ioutil.ReadFile(filepath.Join(*topDir, "assets/stock/plain_text.json"))
    23  	if errGo != nil {
    24  		t.Fatal(kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()))
    25  	}
    26  
    27  	// Test IsEnvelope
    28  	if isEnvelope, _ := IsEnvelope(payload); isEnvelope {
    29  		t.Fatal(kv.NewError("mis-recognized envelope").With("stack", stack.Trace().TrimRuntime()))
    30  	}
    31  
    32  	// Read an encrypted payload
    33  	// Test IsEnvelope
    34  }
    35  
    36  func TestEnvelopeDetectPos(t *testing.T) {
    37  	// Read and load an encrypted payload
    38  	payload, errGo := ioutil.ReadFile(filepath.Join(*topDir, "assets/stock/encrypted.json"))
    39  	if errGo != nil {
    40  		t.Fatal(kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()))
    41  	}
    42  
    43  	// Test IsEnvelope
    44  	if isEnvelope, err := IsEnvelope(payload); !isEnvelope {
    45  		t.Fatal(kv.NewError("unrecognized envelope").With("stack", stack.Trace().TrimRuntime()))
    46  	} else {
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  	}
    51  }
    52  
    53  func setupWrapper() (w *Wrapper, err kv.Error) {
    54  	passphrase := RandomString(64)
    55  	privatePEM, publicPEM, err := GenerateKeyPair(passphrase)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return NewWrapper(publicPEM, privatePEM, []byte(passphrase))
    60  }
    61  
    62  func TestEnvelopeConv(t *testing.T) {
    63  	// Read and load a default unencrypted payload
    64  	payload, errGo := ioutil.ReadFile(filepath.Join(*topDir, "assets/stock/plain_text.json"))
    65  	if errGo != nil {
    66  		t.Fatal(kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()))
    67  	}
    68  
    69  	r, err := UnmarshalRequest(payload)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	wrapper, err := setupWrapper()
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	// Push the request to an envelope and then back again
    80  	e, err := wrapper.Envelope(r)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	rFinal, err := wrapper.Request(e)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	// Do a deep equal test as the first check
    91  	if diff := deep.Equal(r, rFinal); diff != nil {
    92  		t.Fatal(diff)
    93  	}
    94  }
    95  
    96  func TestEnvelopeCrypt(t *testing.T) {
    97  	// Read and load a default unencrypted payload
    98  	payload, errGo := ioutil.ReadFile(filepath.Join(*topDir, "assets/stock/plain_text.json"))
    99  	if errGo != nil {
   100  		t.Fatal(kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()))
   101  	}
   102  
   103  	r, err := UnmarshalRequest(payload)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	wrapper, err := setupWrapper()
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	// Encrypt envelope with full message using PEMs that are self generated
   114  	// for our test
   115  	encrypted, err := wrapper.WrapRequest(r)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	// Decrypt envelope check against original
   121  	rUnwrapped, err := wrapper.UnwrapRequest(encrypted)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	// Do a deep equal test as the first check
   127  	if diff := deep.Equal(r, rUnwrapped); diff != nil {
   128  		t.Fatal(diff)
   129  	}
   130  
   131  	finalPayload, err := rUnwrapped.Marshal()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  
   136  	// Repack the original request so that they can be compared without
   137  	// the pretty print getting in the way
   138  	minifiedRequest, err := r.Marshal()
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	if bytes.Compare(minifiedRequest, finalPayload) != 0 {
   144  		t.Fatal(kv.NewError("in/out payloads mismatched").With("stack", stack.Trace().TrimRuntime()))
   145  	}
   146  }