region_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package device
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "testing"
  7. )
  8. // injectSnapshot stores a snapshot on the managed device so region guards observe
  9. // its IMSI without replaying the full readSnapshot AT transcript.
  10. func injectSnapshot(t *testing.T, manager *Manager, id string, snapshot *Snapshot) {
  11. t.Helper()
  12. state, err := manager.lookup(id)
  13. if err != nil {
  14. t.Fatalf("lookup: %v", err)
  15. }
  16. manager.setResult(id, state, snapshot, nil)
  17. }
  18. func TestCardMCCMNC(t *testing.T) {
  19. t.Parallel()
  20. if mcc, _ := CardMCCMNC("460001234567890"); mcc != "460" {
  21. t.Fatalf("CardMCCMNC mcc = %q, want 460", mcc)
  22. }
  23. for _, bad := range []string{"", "4600", "4600X1234"} {
  24. if mcc, _ := CardMCCMNC(bad); mcc != "" {
  25. t.Fatalf("CardMCCMNC(%q) mcc = %q, want empty", bad, mcc)
  26. }
  27. }
  28. }
  29. func TestRegionBlockReason(t *testing.T) {
  30. t.Parallel()
  31. for _, imsi := range []string{"460001234567890", "461001234567890"} {
  32. reason := RegionBlockReason(imsi)
  33. if reason == "" {
  34. t.Fatalf("RegionBlockReason(%q) did not block", imsi)
  35. }
  36. if !strings.Contains(reason, "中国") {
  37. t.Fatalf("RegionBlockReason(%q) = %q, want it to name 中国", imsi, reason)
  38. }
  39. }
  40. for _, imsi := range []string{"310260123456789", "001011234567890", ""} {
  41. if reason := RegionBlockReason(imsi); reason != "" {
  42. t.Fatalf("RegionBlockReason(%q) = %q, want empty (fail-open)", imsi, reason)
  43. }
  44. }
  45. }
  46. func TestSetNetworkBlockedForRestrictedRegionSIM(t *testing.T) {
  47. client := &transcriptClient{}
  48. manager, id := newStartedTestManager(t, client)
  49. injectSnapshot(t, manager, id, &Snapshot{DeviceID: id, IMSI: "460001234567890"})
  50. _, err := manager.SetNetwork(context.Background(), id, NetworkRequest{
  51. Enabled: true, APN: "internet", IPVersion: "IPV4V6",
  52. })
  53. if !errors.Is(err, ErrRegionBlocked) {
  54. t.Fatalf("error = %v, want ErrRegionBlocked", err)
  55. }
  56. client.assertDone(t)
  57. }
  58. func TestSetNetworkAllowedForServedRegionSIM(t *testing.T) {
  59. client := &transcriptClient{steps: []clientStep{
  60. {command: `AT+CGDCONT=1,"IPV4V6","internet"`, response: okResponse()},
  61. {command: "AT+CGATT=1", response: okResponse()},
  62. {command: "AT+CGACT=1,1", response: okResponse()},
  63. }}
  64. manager, id := newStartedTestManager(t, client)
  65. injectSnapshot(t, manager, id, &Snapshot{DeviceID: id, IMSI: "310260123456789"})
  66. result, err := manager.SetNetwork(context.Background(), id, NetworkRequest{
  67. Enabled: true, APN: "internet", IPVersion: "IPV4V6",
  68. })
  69. if err != nil {
  70. t.Fatalf("enable network: %v", err)
  71. }
  72. if !result.Enabled {
  73. t.Fatalf("enable result = %#v", result)
  74. }
  75. client.assertDone(t)
  76. }
  77. // A device whose SIM region is not yet known (no snapshot) must not be denied:
  78. // only a confirmed blocked MCC blocks service (fail-open).
  79. func TestSetNetworkAllowedWhenSIMRegionUnknown(t *testing.T) {
  80. client := &transcriptClient{steps: []clientStep{
  81. {command: `AT+CGDCONT=1,"IPV4V6","internet"`, response: okResponse()},
  82. {command: "AT+CGATT=1", response: okResponse()},
  83. {command: "AT+CGACT=1,1", response: okResponse()},
  84. }}
  85. manager, id := newStartedTestManager(t, client)
  86. if _, err := manager.SetNetwork(context.Background(), id, NetworkRequest{
  87. Enabled: true, APN: "internet", IPVersion: "IPV4V6",
  88. }); err != nil {
  89. t.Fatalf("enable network with unknown region: %v", err)
  90. }
  91. client.assertDone(t)
  92. }
  93. func TestSendSMSBlockedForRestrictedRegionSIM(t *testing.T) {
  94. client := &transcriptClient{}
  95. manager, id := newStartedTestManager(t, client)
  96. injectSnapshot(t, manager, id, &Snapshot{DeviceID: id, IMSI: "460001234567890"})
  97. result, err := manager.SendSMS(context.Background(), id, "+15551234567", "hello")
  98. if !errors.Is(err, ErrRegionBlocked) {
  99. t.Fatalf("error = %v, want ErrRegionBlocked", err)
  100. }
  101. if result.PartsAttempted != 0 {
  102. t.Fatalf("PartsAttempted = %d, want 0 for a blocked region", result.PartsAttempted)
  103. }
  104. if result.SubmissionStatus != "region_blocked" {
  105. t.Fatalf("SubmissionStatus = %q, want region_blocked", result.SubmissionStatus)
  106. }
  107. client.assertDone(t)
  108. }