github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/store/mysql/encrypt_test.go (about)

     1  // Copyright (c) 2020-2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package mysql
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  func TestEncryptDecrypt(t *testing.T) {
    13  	blob := []byte("encryptmeyo")
    14  
    15  	// Setup a mysql struct
    16  	s, cleanup := newTestMySQL(t)
    17  	defer cleanup()
    18  
    19  	// Encrypt and make sure cleartext isn't the same as the encypted blob.
    20  	eb, err := s.encrypt(nil, nil, blob)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	if bytes.Equal(eb, blob) {
    25  		t.Fatal("equal")
    26  	}
    27  
    28  	// Decrypt and make sure cleartext is the same as the initial blob.
    29  	db, _, err := s.decrypt(eb)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if !bytes.Equal(db, blob) {
    34  		t.Fatal("not equal")
    35  	}
    36  
    37  	// Try to decrypt invalid blob.
    38  	_, _, err = s.decrypt(blob)
    39  	if err == nil {
    40  		t.Fatal("expected invalid sbox header")
    41  	}
    42  }