cipher_rc4_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package qmc
  2. import (
  3. "os"
  4. "reflect"
  5. "testing"
  6. )
  7. func loadTestRC4CipherData(name string) ([]byte, []byte, []byte, error) {
  8. prefix := "./testdata/" + name
  9. key, err := os.ReadFile(prefix + "_key.bin")
  10. if err != nil {
  11. return nil, nil, nil, err
  12. }
  13. raw, err := os.ReadFile(prefix + "_raw.bin")
  14. if err != nil {
  15. return nil, nil, nil, err
  16. }
  17. target, err := os.ReadFile(prefix + "_target.bin")
  18. if err != nil {
  19. return nil, nil, nil, err
  20. }
  21. return key, raw, target, nil
  22. }
  23. func Test_rc4Cipher_Decrypt(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. wantErr bool
  27. }{
  28. {"mflac0_rc4", false},
  29. {"mflac_rc4", false},
  30. }
  31. for _, tt := range tests {
  32. t.Run(tt.name, func(t *testing.T) {
  33. key, raw, target, err := loadTestRC4CipherData(tt.name)
  34. if err != nil {
  35. t.Fatalf("load testing data failed: %s", err)
  36. }
  37. c, err := newRC4Cipher(key)
  38. if err != nil {
  39. t.Errorf("init rc4Cipher failed: %s", err)
  40. return
  41. }
  42. c.Decrypt(raw, 0)
  43. if !reflect.DeepEqual(raw, target) {
  44. t.Error("overall")
  45. }
  46. })
  47. }
  48. }
  49. func BenchmarkRc4Cipher_Decrypt(b *testing.B) {
  50. key, raw, _, err := loadTestRC4CipherData("mflac0_rc4")
  51. if err != nil {
  52. b.Fatalf("load testing data failed: %s", err)
  53. }
  54. c, err := newRC4Cipher(key)
  55. if err != nil {
  56. b.Errorf("init rc4Cipher failed: %s", err)
  57. return
  58. }
  59. b.ResetTimer()
  60. for i := 0; i < b.N; i++ {
  61. c.Decrypt(raw, 0)
  62. }
  63. }
  64. func Test_rc4Cipher_encFirstSegment(t *testing.T) {
  65. key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
  66. if err != nil {
  67. t.Fatalf("load testing data failed: %s", err)
  68. }
  69. t.Run("first-block(0~128)", func(t *testing.T) {
  70. c, err := newRC4Cipher(key)
  71. if err != nil {
  72. t.Errorf("init rc4Cipher failed: %s", err)
  73. return
  74. }
  75. c.Decrypt(raw[:128], 0)
  76. if !reflect.DeepEqual(raw[:128], target[:128]) {
  77. t.Error("first-block(0~128)")
  78. }
  79. })
  80. }
  81. func Test_rc4Cipher_encASegment(t *testing.T) {
  82. key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
  83. if err != nil {
  84. t.Fatalf("load testing data failed: %s", err)
  85. }
  86. t.Run("align-block(128~5120)", func(t *testing.T) {
  87. c, err := newRC4Cipher(key)
  88. if err != nil {
  89. t.Errorf("init rc4Cipher failed: %s", err)
  90. return
  91. }
  92. c.Decrypt(raw[128:5120], 128)
  93. if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
  94. t.Error("align-block(128~5120)")
  95. }
  96. })
  97. t.Run("simple-block(5120~10240)", func(t *testing.T) {
  98. c, err := newRC4Cipher(key)
  99. if err != nil {
  100. t.Errorf("init rc4Cipher failed: %s", err)
  101. return
  102. }
  103. c.Decrypt(raw[5120:10240], 5120)
  104. if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
  105. t.Error("align-block(128~5120)")
  106. }
  107. })
  108. }