phone_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package device
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "testing"
  7. "vocat/internal/modem"
  8. )
  9. func TestReadPhoneNumberFallsBackToOwnNumbersAndRestoresPhonebook(t *testing.T) {
  10. client := &transcriptClient{steps: []clientStep{
  11. {command: "AT+CNUM", response: okResponse()},
  12. {command: "AT+CPBS?", response: okResponse(`+CPBS: "SM",0,250`)},
  13. {command: `AT+CPBS="ON"`, response: okResponse()},
  14. {command: "AT+CPBR=?", response: okResponse("+CPBR: (1-3),40,20")},
  15. {command: "AT+CPBR=1", response: okResponse(`+CPBR: 1,"",129,""`)},
  16. {
  17. command: "AT+CPBR=2",
  18. response: okResponse(`+CPBR: 2,"+44 7700 900123",145,"Own"`),
  19. },
  20. {
  21. command: `AT+CPBS="SM"`,
  22. err: errors.New("restore failed"),
  23. },
  24. }}
  25. manager, err := NewManager(Options{})
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. phone, warnings := manager.readPhoneNumber(context.Background(), client)
  30. if phone.Number != "+447700900123" || phone.Source != PhoneSourceOwnNumber {
  31. t.Fatalf("phone = %#v", phone)
  32. }
  33. if len(warnings) != 1 || !strings.Contains(warnings[0], "restore phonebook") {
  34. t.Fatalf("warnings = %#v", warnings)
  35. }
  36. client.assertDone(t)
  37. }
  38. func TestReadPhoneNumberFallsBackToEFMSISDN(t *testing.T) {
  39. record := strings.Repeat("FF", 18) +
  40. "0791447700091032FFFFFFFFFFFF"
  41. client := &transcriptClient{steps: []clientStep{
  42. {command: "AT+CNUM", response: okResponse()},
  43. {command: "AT+CPBS?", response: okResponse(`+CPBS: "SM",0,250`)},
  44. {
  45. command: `AT+CPBS="ON"`,
  46. err: errors.New("Own Numbers unavailable"),
  47. },
  48. {
  49. command: "AT+CRSM=192,28480,0,0,0",
  50. err: errors.New("zero-length GET RESPONSE unsupported"),
  51. },
  52. {
  53. command: "AT+CRSM=192,28480,0,0,15",
  54. response: okResponse(`+CRSM: 144,0,"62198205422100200283026F408A01"`),
  55. },
  56. {
  57. command: "AT+CRSM=178,28480,1,4,32",
  58. response: okResponse(`+CRSM: 144,0,"` + record + `"`),
  59. },
  60. }}
  61. manager, err := NewManager(Options{})
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. phone, warnings := manager.readPhoneNumber(context.Background(), client)
  66. if phone.Number != "+447700900123" || phone.Source != PhoneSourceEFMSISDN {
  67. t.Fatalf("phone = %#v; warnings = %#v", phone, warnings)
  68. }
  69. client.assertDone(t)
  70. }
  71. func TestPhoneNumberIsNeverDerivedFromSubscriberIdentifiers(t *testing.T) {
  72. if got := parsePhoneResponse(
  73. modem.Response{Lines: []string{"+CNUM: ,,,"}},
  74. "+CNUM:",
  75. ); got != "" {
  76. t.Fatalf("empty CNUM parsed as %q", got)
  77. }
  78. if got := normalizePhoneNumber("460001234567890/89860012345678901234"); got != "" {
  79. t.Fatalf("invalid combined subscriber identifiers parsed as %q", got)
  80. }
  81. }