api_test.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2025, 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 server
  20. import (
  21. "fmt"
  22. "strings"
  23. "testing"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  27. )
  28. func TestValidateAndGetProtobufBaseParams(t *testing.T) {
  29. params := make(common.APIParameters)
  30. params["session_id"] = prng.HexString(8)
  31. params["propagation_channel_id"] = strings.ToUpper(prng.HexString(8))
  32. params["sponsor_id"] = strings.ToUpper(prng.HexString(8))
  33. params["client_version"] = "1"
  34. params["client_platform"] = prng.HexString(8)
  35. params["client_features"] = []any{prng.HexString(8), prng.HexString(8)}
  36. params["client_build_rev"] = prng.HexString(8)
  37. params["device_region"] = "US"
  38. params["device_location"] = "gzzzz"
  39. params["egress_region"] = "US"
  40. params["network_type"] = prng.HexString(8)
  41. params["applied_tactics_tag"] = prng.HexString(8)
  42. packedParams, err := protocol.EncodePackedAPIParameters(params)
  43. if err != nil {
  44. t.Fatalf("protocol.EncodePackedAPIParameters failed: %v", err)
  45. }
  46. protoBaseParams, err := ValidateAndGetProtobufBaseParams(packedParams)
  47. if err != nil {
  48. t.Fatalf("ValidateAndGetProtobufBaseParams failed: %v", err)
  49. }
  50. if protoBaseParams.ClientAsn != nil ||
  51. protoBaseParams.ClientAso != nil ||
  52. protoBaseParams.ClientCity != nil ||
  53. protoBaseParams.ClientIsp != nil ||
  54. protoBaseParams.ClientRegion != nil ||
  55. protoBaseParams.LastConnected != nil ||
  56. protoBaseParams.AuthorizedAccessTypes != nil {
  57. t.Fatalf("unexpected non-nil field: %+v", protoBaseParams)
  58. }
  59. if *protoBaseParams.SessionId != params["session_id"].(string) ||
  60. *protoBaseParams.PropagationChannelId != params["propagation_channel_id"].(string) ||
  61. *protoBaseParams.SponsorId != params["sponsor_id"].(string) ||
  62. fmt.Sprintf("%+v", *protoBaseParams.ClientVersion) != fmt.Sprintf("%+v", params["client_version"]) ||
  63. *protoBaseParams.ClientPlatform != params["client_platform"].(string) ||
  64. fmt.Sprintf("%+v", protoBaseParams.ClientFeatures) != fmt.Sprintf("%+v", params["client_features"]) ||
  65. *protoBaseParams.ClientBuildRev != params["client_build_rev"].(string) ||
  66. *protoBaseParams.DeviceRegion != params["device_region"].(string) ||
  67. *protoBaseParams.DeviceLocation != params["device_location"].(string) ||
  68. *protoBaseParams.EgressRegion != params["egress_region"].(string) ||
  69. *protoBaseParams.NetworkType != params["network_type"].(string) ||
  70. *protoBaseParams.AppliedTacticsTag != params["applied_tactics_tag"].(string) {
  71. t.Fatalf("unexpected field: %+v", protoBaseParams)
  72. }
  73. }