github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/otto/encrypt_test.go (about)

     1  package otto
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestCrypt(t *testing.T) {
    11  	cases := []struct {
    12  		Password string
    13  		Data     []byte
    14  	}{
    15  		{
    16  			"foo",
    17  			[]byte("bar"),
    18  		},
    19  	}
    20  
    21  	for _, tc := range cases {
    22  		// Create a temporary file. We only need the path
    23  		f, err := ioutil.TempFile("", "otto")
    24  		if err != nil {
    25  			t.Fatalf("err: %s", err)
    26  		}
    27  		f.Close()
    28  		path := f.Name()
    29  		os.Remove(path)
    30  
    31  		// Encrypt and decrypt!
    32  		if err := cryptWrite(path, tc.Password, tc.Data); err != nil {
    33  			t.Fatalf("write err: %s\n\n%s", tc.Password, err)
    34  		}
    35  
    36  		actual, err := cryptRead(path, tc.Password)
    37  		if err != nil {
    38  			t.Fatalf("read err: %s\n\n%s", tc.Password, err)
    39  		}
    40  
    41  		if !reflect.DeepEqual(actual, tc.Data) {
    42  			t.Fatalf("read err: %s\n\n%#v\n\n%#v", tc.Password, actual, tc.Data)
    43  		}
    44  	}
    45  }