destBytes_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 server
  20. import (
  21. "encoding/json"
  22. "io"
  23. "sync"
  24. "testing"
  25. "time"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  29. )
  30. func TestDestBytes(t *testing.T) {
  31. err := runTestDestBytes()
  32. if err != nil {
  33. t.Error(errors.Trace(err).Error())
  34. }
  35. }
  36. func runTestDestBytes() error {
  37. var logsMutex sync.Mutex
  38. var asnDestBytesLogs []map[string]interface{}
  39. var domainDestBytesLogs []map[string]interface{}
  40. // Discard logs, skipping InitLogging. Force disable useProtobufLogging,
  41. // which would otherwise require the udsipc.Reader/handler scheme in
  42. // server_test. Both asn_dest_bytes and domain_dest_bytes contents are
  43. // checked in server_test in PSIPHON_RUN_PROTOBUF_LOGGING_TEST mode.
  44. logWriter := log.Logger.Out
  45. protobufLogging := useProtobufLogging
  46. defer func() {
  47. log.Logger.Out = logWriter
  48. useProtobufLogging = protobufLogging
  49. }()
  50. log.Logger.Out = io.Discard
  51. logCallback := func(log []byte) {
  52. logFields := make(map[string]interface{})
  53. err := json.Unmarshal(log, &logFields)
  54. if err != nil {
  55. panic(err.Error())
  56. }
  57. logsMutex.Lock()
  58. defer logsMutex.Unlock()
  59. switch logFields["event_name"].(string) {
  60. case "asn_dest_bytes":
  61. asnDestBytesLogs = append(asnDestBytesLogs, logFields)
  62. case "domain_dest_bytes":
  63. domainDestBytesLogs = append(domainDestBytesLogs, logFields)
  64. }
  65. }
  66. setLogCallback(logCallback)
  67. defer setLogCallback(nil)
  68. const logPeriod = 250 * time.Millisecond
  69. destBytesLogger := newDestBytesLogger(&SupportServices{
  70. Config: &Config{
  71. destinationBytesPeriod: logPeriod,
  72. },
  73. })
  74. err := destBytesLogger.Start()
  75. if err != nil {
  76. return errors.Trace(err)
  77. }
  78. defer destBytesLogger.Stop()
  79. destASNs := []string{"00001", "00002"}
  80. destDomains := []string{"example.com", "example.org"}
  81. clientRegions := []string{"R1", "R2"}
  82. clientASNs := []string{"00003", "00004"}
  83. clientPlatformPrefixes := []string{"iOS", "Android"}
  84. sponsorIDs := []string{prng.HexString(SPONSOR_ID_LENGTH)}
  85. bytesTCP := int64(2048)
  86. bytesUDP := int64(1024)
  87. eventCount := 10
  88. addBytes := func() {
  89. for i := 0; i < eventCount; i++ {
  90. for _, clientRegion := range clientRegions {
  91. for _, clientASN := range clientASNs {
  92. geoIPData := GeoIPData{
  93. Country: clientRegion,
  94. ASN: clientASN,
  95. }
  96. for _, clientPlatformPrefix := range clientPlatformPrefixes {
  97. clientPlatform := clientPlatformPrefix + prng.DefaultPRNG().HexString(4)
  98. apiParams := common.APIParameters{
  99. "client_platform": clientPlatform,
  100. "sponsor_id": sponsorIDs[0],
  101. }
  102. for _, destASN := range destASNs {
  103. destBytesLogger.AddASNBytes(destASN, geoIPData, apiParams, bytesTCP, bytesUDP)
  104. }
  105. for _, destDomain := range destDomains {
  106. destBytesLogger.AddDomainBytes(destDomain, geoIPData, apiParams, bytesTCP, bytesUDP)
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. checkLogs := func() error {
  114. logsMutex.Lock()
  115. defer logsMutex.Unlock()
  116. for i, logs := range [][]map[string]interface{}{asnDestBytesLogs, domainDestBytesLogs} {
  117. destCount := len(destASNs)
  118. if i != 0 {
  119. destCount = len(destDomains)
  120. }
  121. if len(logs) !=
  122. destCount*len(clientRegions)*len(clientASNs)*len(clientPlatformPrefixes)*len(sponsorIDs) {
  123. return errors.Tracef("unexpected log count: %d", len(logs))
  124. }
  125. loggedDestASNs := make(map[string]struct{})
  126. loggedDestDomains := make(map[string]struct{})
  127. loggedClientRegions := make(map[string]struct{})
  128. loggedClientASNs := make(map[string]struct{})
  129. loggedClientPlatforms := make(map[string]struct{})
  130. loggedSponsorIDs := make(map[string]struct{})
  131. sumBytesTCP := int64(0)
  132. sumBytesUDP := int64(0)
  133. sumBytes := int64(0)
  134. for _, logFields := range logs {
  135. if i == 0 {
  136. loggedDestASNs[logFields["asn"].(string)] = struct{}{}
  137. } else {
  138. loggedDestDomains[logFields["domain"].(string)] = struct{}{}
  139. }
  140. loggedClientRegions[logFields["client_region"].(string)] = struct{}{}
  141. loggedClientASNs[logFields["client_asn"].(string)] = struct{}{}
  142. loggedClientPlatforms[logFields["client_platform"].(string)] = struct{}{}
  143. loggedSponsorIDs[logFields["sponsor_id"].(string)] = struct{}{}
  144. sumBytesTCP += int64(logFields["bytes_tcp"].(float64))
  145. sumBytesUDP += int64(logFields["bytes_udp"].(float64))
  146. sumBytes += int64(logFields["bytes"].(float64))
  147. }
  148. checkFields := func(logged map[string]struct{}, expected []string) error {
  149. if len(logged) != len(expected) {
  150. return errors.Tracef("unexpected length: %d", len(logged))
  151. }
  152. for _, key := range expected {
  153. if _, ok := logged[key]; !ok {
  154. return errors.Tracef("missing %v", key)
  155. }
  156. }
  157. return nil
  158. }
  159. if i == 0 {
  160. err := checkFields(loggedDestASNs, destASNs)
  161. if err != nil {
  162. return errors.Trace(err)
  163. }
  164. } else {
  165. err = checkFields(loggedDestDomains, destDomains)
  166. if err != nil {
  167. return errors.Trace(err)
  168. }
  169. }
  170. err := checkFields(loggedClientRegions, clientRegions)
  171. if err != nil {
  172. return errors.Trace(err)
  173. }
  174. err = checkFields(loggedClientASNs, clientASNs)
  175. if err != nil {
  176. return errors.Trace(err)
  177. }
  178. err = checkFields(loggedClientPlatforms, clientPlatformPrefixes)
  179. if err != nil {
  180. return errors.Trace(err)
  181. }
  182. err = checkFields(loggedSponsorIDs, sponsorIDs)
  183. if err != nil {
  184. return errors.Trace(err)
  185. }
  186. if sumBytesTCP != int64(len(logs)*eventCount)*bytesTCP {
  187. return errors.Tracef("unexpected TCP bytes: %d", sumBytesTCP)
  188. }
  189. if sumBytesUDP != int64(len(logs)*eventCount)*bytesUDP {
  190. return errors.Tracef("unexpected UDP bytes: %d", sumBytesUDP)
  191. }
  192. if sumBytes != int64(len(logs)*eventCount)*(bytesTCP+bytesUDP) {
  193. return errors.Tracef("unexpected bytes: %d", sumBytes)
  194. }
  195. }
  196. asnDestBytesLogs = nil
  197. domainDestBytesLogs = nil
  198. return nil
  199. }
  200. for i := 0; i < 3; i++ {
  201. addBytes()
  202. time.Sleep(logPeriod * 2)
  203. err := checkLogs()
  204. if err != nil {
  205. return errors.Trace(err)
  206. }
  207. }
  208. addBytes()
  209. destBytesLogger.Stop()
  210. err = checkLogs()
  211. if err != nil {
  212. return errors.Trace(err)
  213. }
  214. return nil
  215. }