Skip to content

Commit

Permalink
small test
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Oct 2, 2023
1 parent 4525fd5 commit be31aa4
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions go/enclave/crypto/data_enc_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package crypto

import (
"fmt"

Check failure on line 4 in go/enclave/crypto/data_enc_service_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"github.com/obscuronet/go-obscuro/integration/common/testlog"
"golang.org/x/exp/rand"
"golang.org/x/exp/slices"
"testing"

Check failure on line 8 in go/enclave/crypto/data_enc_service_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
)

const N = 1000

func Test_dataEncryptionServiceImpl_Encrypt(t1 *testing.T) {

Check warning on line 13 in go/enclave/crypto/data_enc_service_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 't1' seems to be unused, consider removing or renaming it as _ (revive)
d := NewDataEncryptionService(testlog.Logger())

plaintext := make([]byte, 512*1024)
_, err := rand.Read(plaintext)
if err != nil {
panic(err)
}

var gains = float64(0)

Check failure on line 22 in go/enclave/crypto/data_enc_service_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
for i := 0; i < N; i++ {
encryptedBlob, err := d.Encrypt(plaintext)
if err != nil {
panic(err)
}

byteFrequency := make(map[byte]int)
for _, b := range encryptedBlob {
byteFrequency[b] = byteFrequency[b] + 1
}

freqs := make([]int, 0)
for _, v := range byteFrequency {
freqs = append(freqs, v)
}

max := slices.Max(freqs)
avg := len(encryptedBlob) / 256

// calculate the gain as the difference between the numbers of the best byte compared to 0
gain := float64(max-byteFrequency[byte(0)]) * 100 / float64(avg)
gains += gain
}

fmt.Printf("GAIN: %f", gains/N)

}

0 comments on commit be31aa4

Please sign in to comment.