reduced_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 inproxy
  20. import (
  21. "testing"
  22. "time"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  24. )
  25. func TestReduced(t *testing.T) {
  26. err := runTestReduced()
  27. if err != nil {
  28. t.Error(errors.Trace(err).Error())
  29. }
  30. }
  31. func runTestReduced() error {
  32. now := time.Now().UTC()
  33. minuteOfDay := now.Hour()*60 + now.Minute()
  34. addMinutes := func(minute, delta int) int {
  35. m := (minute + delta) % (24 * 60)
  36. if m < 0 {
  37. m += 24 * 60
  38. }
  39. return m
  40. }
  41. // Test: inside reduced period
  42. start := addMinutes(minuteOfDay, -60)
  43. end := addMinutes(minuteOfDay, 60)
  44. config := &ProxyConfig{
  45. MaxClients: 10,
  46. ReducedMaxClients: 5,
  47. LimitUpstreamBytesPerSecond: 100,
  48. LimitDownstreamBytesPerSecond: 200,
  49. ReducedLimitUpstreamBytesPerSecond: 10,
  50. ReducedLimitDownstreamBytesPerSecond: 20,
  51. }
  52. config.ReducedStartTime = time.Unix(int64(start*60), 0).UTC().Format("15:04")
  53. config.ReducedEndTime = time.Unix(int64(end*60), 0).UTC().Format("15:04")
  54. p, err := NewProxy(config)
  55. if err != nil {
  56. return errors.Trace(err)
  57. }
  58. maxClients1, until := p.isReducedUntil()
  59. maxClients2, limits := p.getLimits()
  60. if maxClients1 != 5 || maxClients2 != 5 {
  61. return errors.TraceNew("unexpected maxClients")
  62. }
  63. if until.IsZero() || time.Until(until) <= 0 {
  64. return errors.TraceNew("unexpected until")
  65. }
  66. if limits.ReadBytesPerSecond != 10 || limits.WriteBytesPerSecond != 20 {
  67. return errors.TraceNew("unexpected rate limits")
  68. }
  69. // Test: outside reduced period
  70. start = addMinutes(minuteOfDay, 60)
  71. end = addMinutes(minuteOfDay, 120)
  72. config.ReducedStartTime = time.Unix(int64(start*60), 0).UTC().Format("15:04")
  73. config.ReducedEndTime = time.Unix(int64(end*60), 0).UTC().Format("15:04")
  74. p, err = NewProxy(config)
  75. if err != nil {
  76. return errors.Trace(err)
  77. }
  78. maxClients1, until = p.isReducedUntil()
  79. maxClients2, limits = p.getLimits()
  80. if maxClients1 != 10 || maxClients2 != 10 {
  81. return errors.TraceNew("unexpected maxClients")
  82. }
  83. if !until.IsZero() {
  84. return errors.TraceNew("unexpected until")
  85. }
  86. if limits.ReadBytesPerSecond != 100 || limits.WriteBytesPerSecond != 200 {
  87. return errors.TraceNew("unexpected rate limits")
  88. }
  89. return nil
  90. }