push_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2026, 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 push
  20. import (
  21. "fmt"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  28. )
  29. func TestPush(t *testing.T) {
  30. err := runTestPush()
  31. if err != nil {
  32. t.Fatal(err.Error())
  33. }
  34. }
  35. func runTestPush() error {
  36. obfuscationKey, publicKey, privateKey, err := GenerateKeys()
  37. if err != nil {
  38. return errors.Trace(err)
  39. }
  40. minPadding := 0
  41. maxPadding := 65535
  42. _, incorrectPublicKey, incorrectPrivateKey, err := GenerateKeys()
  43. if err != nil {
  44. return errors.Trace(err)
  45. }
  46. var serverEntries []*PrioritizedServerEntry
  47. for i := 0; i < 128; i++ {
  48. serverEntry := &protocol.ServerEntry{
  49. Tag: prng.Base64String(32),
  50. IpAddress: fmt.Sprintf("192.0.2.%d", i),
  51. SshUsername: prng.HexString(8),
  52. SshPassword: prng.HexString(32),
  53. SshHostKey: prng.Base64String(280),
  54. SshObfuscatedPort: prng.Range(1, 65535),
  55. SshObfuscatedKey: prng.HexString(32),
  56. Capabilities: []string{"OSSH"},
  57. Region: prng.HexString(1),
  58. ProviderID: strings.ToUpper(prng.HexString(8)),
  59. ConfigurationVersion: 0,
  60. Signature: prng.Base64String(80),
  61. }
  62. serverEntryFields, err := serverEntry.GetServerEntryFields()
  63. if err != nil {
  64. return errors.Trace(err)
  65. }
  66. packed, err := protocol.EncodePackedServerEntryFields(serverEntryFields)
  67. if err != nil {
  68. return errors.Trace(err)
  69. }
  70. serverEntries = append(serverEntries, &PrioritizedServerEntry{
  71. ServerEntryFields: packed,
  72. Source: fmt.Sprintf("source-%d", i),
  73. PrioritizeDial: i < 32 || i >= 96,
  74. })
  75. }
  76. // Test: successful import
  77. pushServerEntries := [][]*PrioritizedServerEntry{
  78. serverEntries[0:32], serverEntries[32:64],
  79. serverEntries[64:96], serverEntries[96:128],
  80. }
  81. payloads, err := MakePushPayloads(
  82. obfuscationKey,
  83. minPadding,
  84. maxPadding,
  85. publicKey,
  86. privateKey,
  87. 1*time.Hour,
  88. pushServerEntries)
  89. if err != nil {
  90. return errors.Trace(err)
  91. }
  92. if len(payloads) != len(pushServerEntries) {
  93. return errors.TraceNew("unexpected payload count")
  94. }
  95. expectPrioritizeDial := true
  96. importer := func(
  97. packedServerEntryFields protocol.PackedServerEntryFields,
  98. source string,
  99. prioritizeDial bool) error {
  100. serverEntryFields, err := protocol.DecodePackedServerEntryFields(packedServerEntryFields)
  101. if err != nil {
  102. return errors.Trace(err)
  103. }
  104. if !strings.HasPrefix(serverEntryFields["ipAddress"].(string), "192.0.2") {
  105. return errors.TraceNew("unexpected server entry IP address")
  106. }
  107. if prioritizeDial != expectPrioritizeDial {
  108. return errors.TraceNew("unexpected prioritize dial")
  109. }
  110. return nil
  111. }
  112. for i, payload := range payloads {
  113. expectPrioritizeDial = i == 0 || i == 3
  114. n, err := ImportPushPayload(
  115. obfuscationKey,
  116. publicKey,
  117. payload,
  118. importer)
  119. if err != nil {
  120. return errors.Trace(err)
  121. }
  122. if n != 32 {
  123. return errors.TraceNew("unexpected import count")
  124. }
  125. }
  126. // Test: expired
  127. payloads, err = MakePushPayloads(
  128. obfuscationKey,
  129. minPadding,
  130. maxPadding,
  131. publicKey,
  132. privateKey,
  133. 1*time.Microsecond,
  134. pushServerEntries)
  135. if err != nil {
  136. return errors.Trace(err)
  137. }
  138. time.Sleep(10 * time.Millisecond)
  139. _, err = ImportPushPayload(
  140. obfuscationKey,
  141. publicKey,
  142. payloads[0],
  143. importer)
  144. if err == nil {
  145. return errors.TraceNew("unexpected success")
  146. }
  147. // Test: invalid signature
  148. payloads, err = MakePushPayloads(
  149. obfuscationKey,
  150. minPadding,
  151. maxPadding,
  152. publicKey,
  153. incorrectPrivateKey,
  154. 1*time.Hour,
  155. pushServerEntries)
  156. if err != nil {
  157. return errors.Trace(err)
  158. }
  159. _, err = ImportPushPayload(
  160. obfuscationKey,
  161. publicKey,
  162. payloads[0],
  163. importer)
  164. if err == nil {
  165. return errors.TraceNew("unexpected success")
  166. }
  167. // Test: wrong signature key
  168. payloads, err = MakePushPayloads(
  169. obfuscationKey,
  170. minPadding,
  171. maxPadding,
  172. publicKey,
  173. privateKey,
  174. 1*time.Hour,
  175. pushServerEntries)
  176. if err != nil {
  177. return errors.Trace(err)
  178. }
  179. _, err = ImportPushPayload(
  180. obfuscationKey,
  181. incorrectPublicKey,
  182. payloads[0],
  183. importer)
  184. if err == nil {
  185. return errors.TraceNew("unexpected success")
  186. }
  187. // Test: mutate obfuscation layer
  188. payloads, err = MakePushPayloads(
  189. obfuscationKey,
  190. minPadding,
  191. maxPadding,
  192. publicKey,
  193. privateKey,
  194. 1*time.Hour,
  195. pushServerEntries)
  196. if err != nil {
  197. return errors.Trace(err)
  198. }
  199. payloads[0][0] = ^payloads[0][0]
  200. _, err = ImportPushPayload(
  201. obfuscationKey,
  202. publicKey,
  203. payloads[0],
  204. importer)
  205. if err == nil {
  206. return errors.TraceNew("unexpected success")
  207. }
  208. return nil
  209. }