transforms_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2022, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package transforms
  20. import (
  21. "reflect"
  22. "strings"
  23. "testing"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  26. )
  27. func TestTransforms(t *testing.T) {
  28. err := runTestTransforms()
  29. if err != nil {
  30. t.Fatal(errors.Trace(err).Error())
  31. }
  32. }
  33. func runTestTransforms() error {
  34. transformNameAny := "exampleTransform1"
  35. transformNameScoped := "exampleTransform2"
  36. scopeName := "exampleScope"
  37. specs := Specs{
  38. transformNameAny: Spec{[2]string{"x", "y"}},
  39. transformNameScoped: Spec{
  40. [2]string{"aa", "cc"},
  41. [2]string{"bb", "(dd|ee)"},
  42. [2]string{"^([c0]{6})", "\\$\\{1\\}ff0"},
  43. },
  44. }
  45. scopedSpecs := ScopedSpecNames{
  46. SCOPE_ANY: []string{transformNameAny},
  47. scopeName: []string{transformNameScoped},
  48. }
  49. // Test: validation
  50. err := specs.Validate(false)
  51. if err != nil {
  52. return errors.Trace(err)
  53. }
  54. err = scopedSpecs.Validate(specs)
  55. if err != nil {
  56. return errors.Trace(err)
  57. }
  58. // Test: select based on scope
  59. name, spec := specs.Select(SCOPE_ANY, scopedSpecs)
  60. if name != transformNameAny || !reflect.DeepEqual(spec, specs[transformNameAny]) {
  61. return errors.TraceNew("unexpected select result")
  62. }
  63. name, spec = specs.Select(scopeName, scopedSpecs)
  64. if name != transformNameScoped || !reflect.DeepEqual(spec, specs[transformNameScoped]) {
  65. return errors.TraceNew("unexpected select result")
  66. }
  67. // Test: correct transform (assumes spec is transformNameScoped)
  68. seed, err := prng.NewSeed()
  69. if err != nil {
  70. return errors.Trace(err)
  71. }
  72. input := "aa0aa0aa0bb0aa0bb0aa0bb0aa0bb0aa0bb0aa0bb0aa0bb0aa0bb0aa0bb0aa"
  73. output, err := spec.ApplyString(seed, input)
  74. if err != nil {
  75. return errors.Trace(err)
  76. }
  77. if !strings.HasPrefix(output, "cc0cc0ff0") ||
  78. strings.IndexAny(output, "ab") != -1 ||
  79. strings.IndexAny(output, "de") == -1 {
  80. return errors.Tracef("unexpected apply result: %s", output)
  81. }
  82. // Test: same result with same seed
  83. previousOutput := output
  84. output, err = spec.ApplyString(seed, input)
  85. if err != nil {
  86. return errors.Trace(err)
  87. }
  88. if output != previousOutput {
  89. return errors.Tracef("unexpected different apply result")
  90. }
  91. // Test: different result with different seed (with high probability)
  92. different := false
  93. for i := 0; i < 1000; i++ {
  94. seed, err = prng.NewSeed()
  95. if err != nil {
  96. return errors.Trace(err)
  97. }
  98. output, err = spec.ApplyString(seed, input)
  99. if err != nil {
  100. return errors.Trace(err)
  101. }
  102. if output != previousOutput {
  103. different = true
  104. break
  105. }
  106. }
  107. if !different {
  108. return errors.Tracef("unexpected identical apply result")
  109. }
  110. return nil
  111. }