Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

155 wiersze
3.2 KiB

  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package secretbox
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "encoding/hex"
  9. "testing"
  10. )
  11. func TestSealOpen(t *testing.T) {
  12. var key [32]byte
  13. var nonce [24]byte
  14. rand.Reader.Read(key[:])
  15. rand.Reader.Read(nonce[:])
  16. var box, opened []byte
  17. for msgLen := 0; msgLen < 128; msgLen += 17 {
  18. message := make([]byte, msgLen)
  19. rand.Reader.Read(message)
  20. box = Seal(box[:0], message, &nonce, &key)
  21. var ok bool
  22. opened, ok = Open(opened[:0], box, &nonce, &key)
  23. if !ok {
  24. t.Errorf("%d: failed to open box", msgLen)
  25. continue
  26. }
  27. if !bytes.Equal(opened, message) {
  28. t.Errorf("%d: got %x, expected %x", msgLen, opened, message)
  29. continue
  30. }
  31. }
  32. for i := range box {
  33. box[i] ^= 0x20
  34. _, ok := Open(opened[:0], box, &nonce, &key)
  35. if ok {
  36. t.Errorf("box was opened after corrupting byte %d", i)
  37. }
  38. box[i] ^= 0x20
  39. }
  40. }
  41. func TestSecretBox(t *testing.T) {
  42. var key [32]byte
  43. var nonce [24]byte
  44. var message [64]byte
  45. for i := range key[:] {
  46. key[i] = 1
  47. }
  48. for i := range nonce[:] {
  49. nonce[i] = 2
  50. }
  51. for i := range message[:] {
  52. message[i] = 3
  53. }
  54. box := Seal(nil, message[:], &nonce, &key)
  55. // expected was generated using the C implementation of NaCl.
  56. expected, _ := hex.DecodeString("8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad")
  57. if !bytes.Equal(box, expected) {
  58. t.Fatalf("box didn't match, got\n%x\n, expected\n%x", box, expected)
  59. }
  60. }
  61. func TestAppend(t *testing.T) {
  62. var key [32]byte
  63. var nonce [24]byte
  64. var message [8]byte
  65. out := make([]byte, 4)
  66. box := Seal(out, message[:], &nonce, &key)
  67. if !bytes.Equal(box[:4], out[:4]) {
  68. t.Fatalf("Seal didn't correctly append")
  69. }
  70. out = make([]byte, 4, 100)
  71. box = Seal(out, message[:], &nonce, &key)
  72. if !bytes.Equal(box[:4], out[:4]) {
  73. t.Fatalf("Seal didn't correctly append with sufficient capacity.")
  74. }
  75. }
  76. func benchmarkSealSize(b *testing.B, size int) {
  77. message := make([]byte, size)
  78. out := make([]byte, size+Overhead)
  79. var nonce [24]byte
  80. var key [32]byte
  81. b.SetBytes(int64(size))
  82. b.ResetTimer()
  83. for i := 0; i < b.N; i++ {
  84. out = Seal(out[:0], message, &nonce, &key)
  85. }
  86. }
  87. func BenchmarkSeal8Bytes(b *testing.B) {
  88. benchmarkSealSize(b, 8)
  89. }
  90. func BenchmarkSeal100Bytes(b *testing.B) {
  91. benchmarkSealSize(b, 100)
  92. }
  93. func BenchmarkSeal1K(b *testing.B) {
  94. benchmarkSealSize(b, 1024)
  95. }
  96. func BenchmarkSeal8K(b *testing.B) {
  97. benchmarkSealSize(b, 8192)
  98. }
  99. func benchmarkOpenSize(b *testing.B, size int) {
  100. msg := make([]byte, size)
  101. result := make([]byte, size)
  102. var nonce [24]byte
  103. var key [32]byte
  104. box := Seal(nil, msg, &nonce, &key)
  105. b.SetBytes(int64(size))
  106. b.ResetTimer()
  107. for i := 0; i < b.N; i++ {
  108. if _, ok := Open(result[:0], box, &nonce, &key); !ok {
  109. panic("Open failed")
  110. }
  111. }
  112. }
  113. func BenchmarkOpen8Bytes(b *testing.B) {
  114. benchmarkOpenSize(b, 8)
  115. }
  116. func BenchmarkOpen100Bytes(b *testing.B) {
  117. benchmarkOpenSize(b, 100)
  118. }
  119. func BenchmarkOpen1K(b *testing.B) {
  120. benchmarkOpenSize(b, 1024)
  121. }
  122. func BenchmarkOpen8K(b *testing.B) {
  123. benchmarkOpenSize(b, 8192)
  124. }