github.com/fumiama/gofastTEA@v0.0.10/tea_test.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package tea
     5  
     6  import (
     7  	"bytes"
     8  	"crypto/rand"
     9  	"encoding/hex"
    10  	"testing"
    11  
    12  	"github.com/Mrs4s/MiraiGo/utils"
    13  )
    14  
    15  var testTEA = NewTeaCipher([]byte("0123456789ABCDEF"))
    16  
    17  const (
    18  	KEY = iota
    19  	DAT
    20  	ENC
    21  )
    22  
    23  var sampleData = func() [][3]string {
    24  	out := [][3]string{
    25  		{"0123456789ABCDEF", "MiraiGO Here", "b7b2e52af7f5b1fbf37fc3d5546ac7569aecd01bbacf09bf"},
    26  		{"0123456789ABCDEF", "LXY Testing~", "9d0ab85aa14f5434ee83cd2a6b28bf306263cdf88e01264c"},
    27  
    28  		{"0123456789ABCDEF", "s", "528e8b5c48300b548e94262736ebb8b7"},
    29  		{"0123456789ABCDEF", "long long long long long long long", "95715fab6efbd0fd4b76dbc80bd633ebe805849dbc242053b06557f87e748effd9f613f782749fb9fdfa3f45c0c26161"},
    30  
    31  		{"LXY1226    Mrs4s", "LXY Testing~", "ab20caa63f3a6503a84f3cb28f9e26b6c18c051e995d1721"},
    32  	}
    33  	for i := range out {
    34  		c, _ := hex.DecodeString(out[i][ENC])
    35  		out[i][ENC] = utils.B2S(c)
    36  	}
    37  	return out
    38  }()
    39  
    40  func TestTEA(t *testing.T) {
    41  	// Self Testing
    42  	for _, sample := range sampleData {
    43  		tea := NewTeaCipher(utils.S2B(sample[KEY]))
    44  		dat := utils.B2S(tea.Decrypt(utils.S2B(sample[ENC])))
    45  		if dat != sample[DAT] {
    46  			t.Fatalf("error decrypt %v %x", sample, dat)
    47  		}
    48  		enc := utils.B2S(tea.Encrypt(utils.S2B(sample[DAT])))
    49  		dat = utils.B2S(tea.Decrypt(utils.S2B(enc)))
    50  		if dat != sample[DAT] {
    51  			t.Fatal("error self test", sample)
    52  		}
    53  	}
    54  
    55  	key := make([]byte, 16)
    56  	_, err := rand.Read(key)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  	// Random data testing
    61  	for i := 1; i < 0xFF; i++ {
    62  		_, err := rand.Read(key)
    63  		if err != nil {
    64  			panic(err)
    65  		}
    66  		tea := NewTeaCipher(key)
    67  
    68  		dat := make([]byte, i)
    69  		_, err = rand.Read(dat)
    70  		if err != nil {
    71  			panic(err)
    72  		}
    73  		enc := tea.Encrypt(dat)
    74  		dec := tea.Decrypt(enc)
    75  		if !bytes.Equal(dat, dec) {
    76  			t.Fatalf("error in %d, %x %x %x", i, key, dat, enc)
    77  		}
    78  	}
    79  }
    80  
    81  func benchEncrypt(b *testing.B, data []byte) {
    82  	_, err := rand.Read(data)
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  	b.SetBytes(int64(len(data)))
    87  	b.ResetTimer()
    88  	for i := 0; i < b.N; i++ {
    89  		testTEA.Encrypt(data)
    90  	}
    91  }
    92  
    93  func benchDecrypt(b *testing.B, data []byte) {
    94  	_, err := rand.Read(data)
    95  	if err != nil {
    96  		panic(err)
    97  	}
    98  	data = testTEA.Encrypt(data)
    99  	b.SetBytes(int64(len(data)))
   100  	b.ResetTimer()
   101  	for i := 0; i < b.N; i++ {
   102  		testTEA.Decrypt(data)
   103  	}
   104  }
   105  
   106  func BenchmarkTEAen(b *testing.B) {
   107  	b.Run("16", func(b *testing.B) {
   108  		data := make([]byte, 16)
   109  		benchEncrypt(b, data)
   110  	})
   111  	b.Run("256", func(b *testing.B) {
   112  		data := make([]byte, 256)
   113  		benchEncrypt(b, data)
   114  	})
   115  	b.Run("4K", func(b *testing.B) {
   116  		data := make([]byte, 1024*4)
   117  		benchEncrypt(b, data)
   118  	})
   119  	b.Run("32K", func(b *testing.B) {
   120  		data := make([]byte, 1024*32)
   121  		benchEncrypt(b, data)
   122  	})
   123  }
   124  
   125  func BenchmarkTEAde(b *testing.B) {
   126  	b.Run("16", func(b *testing.B) {
   127  		data := make([]byte, 16)
   128  		benchDecrypt(b, data)
   129  	})
   130  	b.Run("256", func(b *testing.B) {
   131  		data := make([]byte, 256)
   132  		benchDecrypt(b, data)
   133  	})
   134  	b.Run("4K", func(b *testing.B) {
   135  		data := make([]byte, 4096)
   136  		benchDecrypt(b, data)
   137  	})
   138  	b.Run("32K", func(b *testing.B) {
   139  		data := make([]byte, 1024*32)
   140  		benchDecrypt(b, data)
   141  	})
   142  }