phone_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package vowifi
  2. import "testing"
  3. func TestExtractAssociatedMSISDN(t *testing.T) {
  4. tests := []struct {
  5. name string
  6. evidence IMSEvidence
  7. wantNumber string
  8. wantSource string
  9. wantOK bool
  10. }{
  11. {
  12. name: "explicit associated identity with IMS domain",
  13. evidence: IMSEvidence{
  14. AssociatedMSISDN: "[email protected]",
  15. },
  16. wantNumber: "+447700900123",
  17. wantSource: PhoneSourceAssociatedMSISDN,
  18. wantOK: true,
  19. },
  20. {
  21. name: "tel P-Associated-URI",
  22. evidence: IMSEvidence{
  23. PAssociatedURI: []string{"<tel:+44 7700-900123>"},
  24. },
  25. wantNumber: "+447700900123",
  26. wantSource: PhoneSourcePAssociatedURI,
  27. wantOK: true,
  28. },
  29. {
  30. name: "SIP E164 P-Associated-URI",
  31. evidence: IMSEvidence{
  32. PAssociatedURI: []string{
  33. "sip:[email protected]",
  34. "<sip:[email protected];user=phone>",
  35. },
  36. },
  37. wantNumber: "+447700900123",
  38. wantSource: PhoneSourcePAssociatedURI,
  39. wantOK: true,
  40. },
  41. {
  42. name: "percent encoded plus",
  43. evidence: IMSEvidence{
  44. PAssociatedURI: []string{"tel:%2B447700900123"},
  45. },
  46. wantNumber: "+447700900123",
  47. wantSource: PhoneSourcePAssociatedURI,
  48. wantOK: true,
  49. },
  50. {
  51. name: "reject IMSI-shaped SIP IMPU",
  52. evidence: IMSEvidence{
  53. PAssociatedURI: []string{
  54. "sip:[email protected]",
  55. },
  56. },
  57. wantOK: false,
  58. },
  59. {
  60. name: "reject arbitrary untyped P-Associated value",
  61. evidence: IMSEvidence{
  62. PAssociatedURI: []string{"+447700900123"},
  63. },
  64. wantOK: false,
  65. },
  66. {
  67. name: "reject command characters",
  68. evidence: IMSEvidence{
  69. AssociatedMSISDN: "+447700900123;AT+CFUN=0",
  70. },
  71. wantOK: false,
  72. },
  73. {
  74. name: "reject overlong E164",
  75. evidence: IMSEvidence{
  76. AssociatedMSISDN: "+1234567890123456",
  77. },
  78. wantOK: false,
  79. },
  80. }
  81. for _, test := range tests {
  82. t.Run(test.name, func(t *testing.T) {
  83. number, source, ok := ExtractAssociatedMSISDN(test.evidence)
  84. if number != test.wantNumber || source != test.wantSource || ok != test.wantOK {
  85. t.Fatalf(
  86. "ExtractAssociatedMSISDN() = (%q, %q, %t), want (%q, %q, %t)",
  87. number,
  88. source,
  89. ok,
  90. test.wantNumber,
  91. test.wantSource,
  92. test.wantOK,
  93. )
  94. }
  95. })
  96. }
  97. }
  98. func TestDeriveEPDGUsesExplicitPLMNAndNeverIMSIHeuristics(t *testing.T) {
  99. tests := []struct {
  100. name string
  101. identity SIMIdentity
  102. want string
  103. wantErr bool
  104. }{
  105. {
  106. name: "two digit MNC is padded",
  107. identity: SIMIdentity{
  108. ICCID: "one",
  109. IMSI: "untrusted-for-plmn",
  110. HomeMCC: "234",
  111. HomeMNC: "15",
  112. },
  113. want: "epdg.epc.mnc015.mcc234.pub.3gppnetwork.org",
  114. },
  115. {
  116. name: "three digit MNC is preserved",
  117. identity: SIMIdentity{
  118. ICCID: "one",
  119. HomeMCC: "310",
  120. HomeMNC: "260",
  121. },
  122. want: "epdg.epc.mnc260.mcc310.pub.3gppnetwork.org",
  123. },
  124. {
  125. name: "explicit endpoint",
  126. identity: SIMIdentity{
  127. ICCID: "one",
  128. HomeMCC: "234",
  129. HomeMNC: "15",
  130. EPDG: "EPDG.EXAMPLE.NET",
  131. },
  132. want: "epdg.example.net",
  133. },
  134. {
  135. name: "missing explicit MNC is not guessed from IMSI",
  136. identity: SIMIdentity{
  137. ICCID: "one",
  138. IMSI: "234150000000000",
  139. HomeMCC: "234",
  140. },
  141. wantErr: true,
  142. },
  143. }
  144. for _, test := range tests {
  145. t.Run(test.name, func(t *testing.T) {
  146. epdg, err := DeriveEPDG(test.identity)
  147. if (err != nil) != test.wantErr {
  148. t.Fatalf("DeriveEPDG() error = %v, wantErr %t", err, test.wantErr)
  149. }
  150. if epdg != test.want {
  151. t.Fatalf("DeriveEPDG() = %q, want %q", epdg, test.want)
  152. }
  153. })
  154. }
  155. }