psinet_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2017, 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 psinet
  20. import (
  21. "bytes"
  22. "fmt"
  23. "io/ioutil"
  24. "os"
  25. "path/filepath"
  26. "testing"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  28. )
  29. func TestDatabase(t *testing.T) {
  30. testDataDirName, err := ioutil.TempDir("", "psinet-test")
  31. if err != nil {
  32. t.Fatalf("TempDir failed: %s\n", err)
  33. }
  34. defer os.RemoveAll(testDataDirName)
  35. server1, err := protocol.EncodeServerEntry(&protocol.ServerEntry{
  36. IpAddress: "1",
  37. })
  38. if err != nil {
  39. t.Fatalf("EncodeServerEntry failed: %s\n", err)
  40. }
  41. server2, err := protocol.EncodeServerEntry(&protocol.ServerEntry{
  42. IpAddress: "2",
  43. })
  44. if err != nil {
  45. t.Fatalf("EncodeServerEntry failed: %s\n", err)
  46. }
  47. databaseJSON := fmt.Sprintf(`
  48. {
  49. "sponsors" : {
  50. "SPONSOR-ID" : {
  51. "id" : "SPONSOR-ID",
  52. "home_pages" : {
  53. "CLIENT-REGION" : [{
  54. "region" : "CLIENT-REGION",
  55. "url" : "HOME-PAGE-URL?client_region=XX&device_region=XX"
  56. }],
  57. "None" : [{
  58. "region" : "None",
  59. "url" : "DEFAULT-HOME-PAGE-URL?client_region=XX&device_region=XX"
  60. }]
  61. },
  62. "mobile_home_pages": {
  63. "CLIENT-REGION" : [{
  64. "region" : "CLIENT-REGION",
  65. "url" : "MOBILE-HOME-PAGE-URL?client_region=XX&client_asn=XX"
  66. }],
  67. "None" : [{
  68. "region" : "None",
  69. "url" : "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=XX&client_asn=XX"
  70. }]
  71. },
  72. "alert_action_urls" : {
  73. "ALERT-REASON-1" : ["SPONSOR-ALERT-1-ACTION-URL?client_region=XX&device_region=XX"]
  74. },
  75. "https_request_regexes" : [{
  76. "regex" : "REGEX-VALUE",
  77. "replace" : "REPLACE-VALUE"
  78. }]
  79. }
  80. },
  81. "client_versions" : {
  82. "CLIENT-PLATFORM" : [
  83. {"version" : "1"},
  84. {"version" : "2"}
  85. ]
  86. },
  87. "default_sponsor_id" : "SPONSOR-ID",
  88. "default_alert_action_urls" : {
  89. "ALERT-REASON-1" : ["DEFAULT-ALERT-1-ACTION-URL?client_region=XX"],
  90. "ALERT-REASON-2" : ["DEFAULT-ALERT-2-ACTION-URL?client_region=XX"]
  91. },
  92. "valid_server_entry_tags" : {
  93. "SERVER-ENTRY-TAG" : true
  94. },
  95. "discovery_servers" : [
  96. {"discovery_date_range" : ["1900-01-01T00:00:00Z", "2000-01-01T00:00:00Z"], "encoded_server_entry" : "%s"},
  97. {"discovery_date_range" : ["2000-01-01T00:00:00Z", "2100-01-01T00:00:00Z"], "encoded_server_entry" : "%s"}
  98. ]
  99. }`, server1, server2)
  100. filename := filepath.Join(testDataDirName, "psinet.json")
  101. err = ioutil.WriteFile(filename, []byte(databaseJSON), 0600)
  102. if err != nil {
  103. t.Fatalf("WriteFile failed: %s", err)
  104. }
  105. db, err := NewDatabase(filename)
  106. if err != nil {
  107. t.Fatalf("NewDatabase failed: %s", err)
  108. }
  109. homePageTestCases := []struct {
  110. sponsorID string
  111. clientRegion string
  112. clientASN string
  113. deviceRegion string
  114. isMobile bool
  115. expectedURL string
  116. }{
  117. {"SPONSOR-ID", "CLIENT-REGION", "65535", "DEVICE-REGION", false, "HOME-PAGE-URL?client_region=CLIENT-REGION&device_region=DEVICE-REGION"},
  118. {"SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", "DEVICE-REGION", false, "DEFAULT-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION&device_region=DEVICE-REGION"},
  119. {"SPONSOR-ID", "CLIENT-REGION", "65535", "DEVICE-REGION", true, "MOBILE-HOME-PAGE-URL?client_region=CLIENT-REGION&client_asn=65535"},
  120. {"SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", "DEVICE-REGION", true, "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION&client_asn=65535"},
  121. {"UNCONFIGURED-SPONSOR-ID", "CLIENT-REGION", "65535", "DEVICE-REGION", false, "HOME-PAGE-URL?client_region=CLIENT-REGION&device_region=DEVICE-REGION"},
  122. {"UNCONFIGURED-SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", "DEVICE-REGION", false, "DEFAULT-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION&device_region=DEVICE-REGION"},
  123. {"UNCONFIGURED-SPONSOR-ID", "CLIENT-REGION", "65535", "DEVICE-REGION", true, "MOBILE-HOME-PAGE-URL?client_region=CLIENT-REGION&client_asn=65535"},
  124. {"UNCONFIGURED-SPONSOR-ID", "UNCONFIGURED-CLIENT-REGION", "65535", "DEVICE-REGION", true, "DEFAULT-MOBILE-HOME-PAGE-URL?client_region=UNCONFIGURED-CLIENT-REGION&client_asn=65535"},
  125. }
  126. for _, testCase := range homePageTestCases {
  127. t.Run(fmt.Sprintf("%+v", testCase), func(t *testing.T) {
  128. homepages := db.GetHomepages(testCase.sponsorID, testCase.clientRegion, testCase.clientASN, testCase.deviceRegion, testCase.isMobile)
  129. if len(homepages) != 1 || homepages[0] != testCase.expectedURL {
  130. t.Fatalf("unexpected home page: %+v", homepages)
  131. }
  132. })
  133. }
  134. alertActionURLTestCases := []struct {
  135. alertReason string
  136. sponsorID string
  137. expectedURLCount int
  138. expectedURL string
  139. }{
  140. {"ALERT-REASON-1", "SPONSOR-ID", 1, "SPONSOR-ALERT-1-ACTION-URL?client_region=CLIENT-REGION&device_region=DEVICE-REGION"},
  141. {"ALERT-REASON-1", "UNCONFIGURED-SPONSOR-ID", 1, "DEFAULT-ALERT-1-ACTION-URL?client_region=CLIENT-REGION"},
  142. {"ALERT-REASON-2", "SPONSOR-ID", 1, "DEFAULT-ALERT-2-ACTION-URL?client_region=CLIENT-REGION"},
  143. {"ALERT-REASON-2", "UNCONFIGURED-SPONSOR-ID", 1, "DEFAULT-ALERT-2-ACTION-URL?client_region=CLIENT-REGION"},
  144. {"UNCONFIGURED-ALERT-REASON", "SPONSOR-ID", 0, ""},
  145. }
  146. for _, testCase := range alertActionURLTestCases {
  147. t.Run(fmt.Sprintf("%+v", testCase), func(t *testing.T) {
  148. URLs := db.GetAlertActionURLs(testCase.alertReason, testCase.sponsorID, "CLIENT-REGION", "", "DEVICE-REGION")
  149. if len(URLs) != testCase.expectedURLCount || (len(URLs) > 0 && URLs[0] != testCase.expectedURL) {
  150. t.Fatalf("unexpected URLs: %d %+v, %+v", testCase.expectedURLCount, testCase.expectedURL, URLs)
  151. }
  152. })
  153. }
  154. versionTestCases := []struct {
  155. currentClientVersion string
  156. clientPlatform string
  157. expectedUpgradeClientVersion string
  158. }{
  159. {"0", "CLIENT-PLATFORM", "2"},
  160. {"1", "CLIENT-PLATFORM", "2"},
  161. {"2", "CLIENT-PLATFORM", ""},
  162. {"3", "CLIENT-PLATFORM", ""},
  163. {"2", "UNCONFIGURED-CLIENT-PLATFORM", ""},
  164. }
  165. for _, testCase := range versionTestCases {
  166. t.Run(fmt.Sprintf("%+v", testCase), func(t *testing.T) {
  167. upgradeVersion := db.GetUpgradeClientVersion(testCase.currentClientVersion, testCase.clientPlatform)
  168. if upgradeVersion != testCase.expectedUpgradeClientVersion {
  169. t.Fatalf("unexpected upgrade version: %s", upgradeVersion)
  170. }
  171. })
  172. }
  173. httpsRegexTestCases := []struct {
  174. sponsorID string
  175. expectedRegexValue string
  176. expectedReplaceValue string
  177. }{
  178. {"SPONSOR-ID", "REGEX-VALUE", "REPLACE-VALUE"},
  179. {"UNCONFIGURED-SPONSOR-ID", "REGEX-VALUE", "REPLACE-VALUE"},
  180. }
  181. for _, testCase := range httpsRegexTestCases {
  182. t.Run(fmt.Sprintf("%+v", testCase), func(t *testing.T) {
  183. regexes, checksum := db.GetHttpsRequestRegexes(testCase.sponsorID)
  184. if !bytes.Equal(checksum, db.GetDomainBytesChecksum(testCase.sponsorID)) {
  185. t.Fatalf("unexpected checksum: %+v", checksum)
  186. }
  187. var regexValue, replaceValue string
  188. ok := false
  189. if len(regexes) == 1 && len(regexes[0]) == 2 {
  190. regexValue, ok = regexes[0]["regex"]
  191. if ok {
  192. replaceValue, ok = regexes[0]["replace"]
  193. }
  194. }
  195. if !ok || regexValue != testCase.expectedRegexValue || replaceValue != testCase.expectedReplaceValue {
  196. t.Fatalf("unexpected regexes: %+v", regexes)
  197. }
  198. })
  199. }
  200. if !db.IsValidServerEntryTag("SERVER-ENTRY-TAG") {
  201. t.Fatalf("unexpected invalid server entry tag")
  202. }
  203. if db.IsValidServerEntryTag("INVALID-SERVER-ENTRY-TAG") {
  204. t.Fatalf("unexpected valid server entry tag")
  205. }
  206. }