orchestrator_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. package vowifi
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. "strings"
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. func TestClassifyErrorEAPAuthenticationRejected(t *testing.T) {
  13. err := fmt.Errorf("tunnel setup: %w", ErrEAPAuthenticationRejected)
  14. if got := classifyError(PhaseTunnelReady, err); got != "eap_authentication_rejected" {
  15. t.Fatalf("classifyError = %q", got)
  16. }
  17. }
  18. type fakeEnvironment struct {
  19. mu sync.Mutex
  20. calls []string
  21. failCounts map[string]int
  22. blockAt string
  23. blocked chan struct{}
  24. blockOnce sync.Once
  25. identity SIMIdentity
  26. akaEvidence AKAEvidence
  27. radioSnapshot RadioSnapshot
  28. proxy ProxyRoute
  29. tunnelEvidence TunnelEvidence
  30. imsEvidence IMSEvidence
  31. smsEvidence SMSEvidence
  32. phoneRecords []PhoneRecord
  33. tunnelRequests []TunnelRequest
  34. tunnelFailures chan error
  35. imsFailures chan error
  36. }
  37. func newFakeEnvironment() *fakeEnvironment {
  38. return &fakeEnvironment{
  39. failCounts: make(map[string]int),
  40. blocked: make(chan struct{}),
  41. identity: SIMIdentity{
  42. ICCID: "8944100000000000000",
  43. IMSI: "234150000000000",
  44. IMEI: "860000000000000",
  45. HomeMCC: "234",
  46. HomeMNC: "15",
  47. HomeCountryCode: "GB",
  48. },
  49. akaEvidence: AKAEvidence{
  50. Ready: true,
  51. Application: "usim",
  52. },
  53. radioSnapshot: RadioSnapshot{
  54. CellularDataEnabled: true,
  55. OperatingMode: 1,
  56. PureAirplanePolicy: false,
  57. },
  58. proxy: ProxyRoute{Mode: ProxyModeDirect},
  59. tunnelEvidence: TunnelEvidence{
  60. Established: true,
  61. Name: "vowifi0",
  62. ResponderAUTH: ResponderAUTHVerified,
  63. IKEEncryption: "aes-cbc-128",
  64. IKEIntegrity: "hmac-sha2-256",
  65. IKEDHGroup: "modp2048",
  66. ESPEncryption: "aes-cbc-128",
  67. ESPIntegrity: "hmac-sha1-96",
  68. },
  69. imsEvidence: IMSEvidence{
  70. Registered: true,
  71. RegistrationState: "registered",
  72. AssociatedMSISDN: "[email protected]",
  73. PAssociatedURI: []string{"sip:[email protected]"},
  74. Transport: "tcp",
  75. LastSIPCode: 200,
  76. },
  77. smsEvidence: SMSEvidence{Ready: true},
  78. }
  79. }
  80. func (environment *fakeEnvironment) record(ctx context.Context, call string) error {
  81. environment.mu.Lock()
  82. environment.calls = append(environment.calls, call)
  83. block := environment.blockAt == call
  84. if remaining := environment.failCounts[call]; remaining > 0 {
  85. environment.failCounts[call] = remaining - 1
  86. environment.mu.Unlock()
  87. return errors.New(call + " failed")
  88. }
  89. environment.mu.Unlock()
  90. if block {
  91. environment.blockOnce.Do(func() { close(environment.blocked) })
  92. <-ctx.Done()
  93. return ctx.Err()
  94. }
  95. select {
  96. case <-ctx.Done():
  97. return ctx.Err()
  98. default:
  99. return nil
  100. }
  101. }
  102. func (environment *fakeEnvironment) callsSnapshot() []string {
  103. environment.mu.Lock()
  104. defer environment.mu.Unlock()
  105. return append([]string(nil), environment.calls...)
  106. }
  107. func (environment *fakeEnvironment) callCount(call string) int {
  108. count := 0
  109. for _, recorded := range environment.callsSnapshot() {
  110. if recorded == call {
  111. count++
  112. }
  113. }
  114. return count
  115. }
  116. func (environment *fakeEnvironment) setFailure(call string, count int) {
  117. environment.mu.Lock()
  118. environment.failCounts[call] = count
  119. environment.mu.Unlock()
  120. }
  121. type fakeSIM struct{ environment *fakeEnvironment }
  122. func (fake fakeSIM) ReadIdentity(ctx context.Context, _ string) (SIMIdentity, error) {
  123. if err := fake.environment.record(ctx, "sim.identity"); err != nil {
  124. return SIMIdentity{}, err
  125. }
  126. return fake.environment.identity, nil
  127. }
  128. type fakeAKA struct{ environment *fakeEnvironment }
  129. func (fake fakeAKA) CheckReady(ctx context.Context, _ SIMIdentity) (AKAEvidence, error) {
  130. if err := fake.environment.record(ctx, "aka.ready"); err != nil {
  131. return AKAEvidence{}, err
  132. }
  133. return fake.environment.akaEvidence, nil
  134. }
  135. func (fake fakeAKA) Authenticate(ctx context.Context, _ SIMIdentity, _ AKAChallenge) (AKAResult, error) {
  136. if err := fake.environment.record(ctx, "aka.authenticate"); err != nil {
  137. return AKAResult{}, err
  138. }
  139. return AKAResult{
  140. RES: []byte{0x01, 0x02, 0x03, 0x04},
  141. CK: make([]byte, 16),
  142. IK: make([]byte, 16),
  143. }, nil
  144. }
  145. type fakeRadio struct{ environment *fakeEnvironment }
  146. func (fake fakeRadio) Snapshot(ctx context.Context, _ string) (RadioSnapshot, error) {
  147. if err := fake.environment.record(ctx, "radio.snapshot"); err != nil {
  148. return RadioSnapshot{}, err
  149. }
  150. return fake.environment.radioSnapshot, nil
  151. }
  152. func (fake fakeRadio) StopCellularData(ctx context.Context, _ string) error {
  153. return fake.environment.record(ctx, "radio.stop_data")
  154. }
  155. func (fake fakeRadio) EnterVoWiFiRFOff(ctx context.Context, _ string) error {
  156. return fake.environment.record(ctx, "radio.rf_off")
  157. }
  158. func (fake fakeRadio) Restore(ctx context.Context, _ string, _ RadioSnapshot) error {
  159. return fake.environment.record(ctx, "radio.restore")
  160. }
  161. type fakeProxy struct{ environment *fakeEnvironment }
  162. func (fake fakeProxy) Resolve(ctx context.Context, _ ProxyRequest) (ProxyRoute, error) {
  163. if err := fake.environment.record(ctx, "proxy.resolve"); err != nil {
  164. return ProxyRoute{}, err
  165. }
  166. return fake.environment.proxy, nil
  167. }
  168. type fakeTunnelProvider struct{ environment *fakeEnvironment }
  169. func (fake fakeTunnelProvider) Start(ctx context.Context, request TunnelRequest) (TunnelSession, error) {
  170. if err := fake.environment.record(ctx, "tunnel.start"); err != nil {
  171. return nil, err
  172. }
  173. fake.environment.mu.Lock()
  174. fake.environment.tunnelRequests = append(fake.environment.tunnelRequests, request)
  175. fake.environment.mu.Unlock()
  176. return &fakeTunnelSession{environment: fake.environment}, nil
  177. }
  178. type fakeTunnelSession struct{ environment *fakeEnvironment }
  179. func (fake *fakeTunnelSession) Evidence() TunnelEvidence {
  180. _ = fake.environment.record(context.Background(), "tunnel.evidence")
  181. return fake.environment.tunnelEvidence
  182. }
  183. func (fake *fakeTunnelSession) Close(ctx context.Context) error {
  184. return fake.environment.record(ctx, "tunnel.close")
  185. }
  186. func (fake *fakeTunnelSession) Failures() <-chan error {
  187. return fake.environment.tunnelFailures
  188. }
  189. type fakeIMSProvider struct{ environment *fakeEnvironment }
  190. func (fake fakeIMSProvider) Start(ctx context.Context, _ IMSRequest) (IMSSession, error) {
  191. if err := fake.environment.record(ctx, "ims.start"); err != nil {
  192. return nil, err
  193. }
  194. return &fakeIMSSession{environment: fake.environment}, nil
  195. }
  196. type fakeIMSSession struct{ environment *fakeEnvironment }
  197. func (fake *fakeIMSSession) Evidence() IMSEvidence {
  198. _ = fake.environment.record(context.Background(), "ims.evidence")
  199. return fake.environment.imsEvidence
  200. }
  201. func (fake *fakeIMSSession) EnableSMS(ctx context.Context) (SMSEvidence, error) {
  202. if err := fake.environment.record(ctx, "ims.sms"); err != nil {
  203. return SMSEvidence{}, err
  204. }
  205. return fake.environment.smsEvidence, nil
  206. }
  207. func (fake *fakeIMSSession) Close(ctx context.Context) error {
  208. return fake.environment.record(ctx, "ims.close")
  209. }
  210. func (fake *fakeIMSSession) Failures() <-chan error {
  211. return fake.environment.imsFailures
  212. }
  213. type fakePhones struct{ environment *fakeEnvironment }
  214. func (fake fakePhones) SaveAssociatedNumber(ctx context.Context, record PhoneRecord) error {
  215. if err := fake.environment.record(ctx, "phone.save"); err != nil {
  216. return err
  217. }
  218. fake.environment.mu.Lock()
  219. fake.environment.phoneRecords = append(fake.environment.phoneRecords, record)
  220. fake.environment.mu.Unlock()
  221. return nil
  222. }
  223. func newTestOrchestrator(t *testing.T, environment *fakeEnvironment, allowMissingAUTH bool) *Orchestrator {
  224. t.Helper()
  225. return newTestOrchestratorWithOptions(t, environment, Options{
  226. DeviceID: "EC20",
  227. AllowMissingResponderAUTH: allowMissingAUTH,
  228. CleanupTimeout: time.Second,
  229. })
  230. }
  231. func newTestOrchestratorWithOptions(
  232. t *testing.T,
  233. environment *fakeEnvironment,
  234. options Options,
  235. ) *Orchestrator {
  236. t.Helper()
  237. orchestrator, err := New(Dependencies{
  238. SIM: fakeSIM{environment},
  239. AKA: fakeAKA{environment},
  240. Radio: fakeRadio{environment},
  241. Proxy: fakeProxy{environment},
  242. Tunnel: fakeTunnelProvider{environment},
  243. IMS: fakeIMSProvider{environment},
  244. Phones: fakePhones{environment},
  245. }, options)
  246. if err != nil {
  247. t.Fatalf("New() error = %v", err)
  248. }
  249. return orchestrator
  250. }
  251. func TestEnableKeepsIMSAndNumberWhenSMSCapabilityIsOptional(t *testing.T) {
  252. environment := newFakeEnvironment()
  253. environment.setFailure("ims.sms", 1)
  254. orchestrator := newTestOrchestratorWithOptions(t, environment, Options{
  255. DeviceID: "EC20",
  256. AllowIMSWithoutSMS: true,
  257. CleanupTimeout: time.Second,
  258. })
  259. state, err := orchestrator.Enable(context.Background())
  260. if err != nil {
  261. t.Fatalf("Enable() error = %v", err)
  262. }
  263. if state.Phase != PhaseIMSReady || !state.Active || !state.TunnelReady ||
  264. !state.IMSReady || state.SMSReady {
  265. t.Fatalf("Enable() state = %+v", state)
  266. }
  267. if state.PhoneNumber != "+447700900123" {
  268. t.Fatalf("phone number = %q", state.PhoneNumber)
  269. }
  270. if state.LastReason != "ims_registered_sms_unavailable" ||
  271. len(state.Warnings) == 0 {
  272. t.Fatalf("optional SMS evidence = %+v", state)
  273. }
  274. if environment.callCount("ims.close") != 0 ||
  275. environment.callCount("tunnel.close") != 0 {
  276. t.Fatal("optional SMS failure tore down a valid IMS registration")
  277. }
  278. }
  279. func TestEnableUsesEvidenceBackedOrderAndDisableRollsBackInReverse(t *testing.T) {
  280. environment := newFakeEnvironment()
  281. orchestrator := newTestOrchestrator(t, environment, false)
  282. state, err := orchestrator.Enable(context.Background())
  283. if err != nil {
  284. t.Fatalf("Enable() error = %v", err)
  285. }
  286. if state.Phase != PhaseSMSReady ||
  287. !state.Enabled ||
  288. !state.Active ||
  289. !state.SIMReady ||
  290. !state.AccessReady ||
  291. !state.TunnelReady ||
  292. !state.IMSReady ||
  293. !state.SMSReady {
  294. t.Fatalf("Enable() state = %+v", state)
  295. }
  296. if state.PhoneNumber != "+447700900123" ||
  297. state.PhoneNumberSource != PhoneSourceAssociatedMSISDN {
  298. t.Fatalf("phone projection = %q (%q)", state.PhoneNumber, state.PhoneNumberSource)
  299. }
  300. if state.Security.ResponderAUTH != ResponderAUTHVerified || state.Security.HighRisk {
  301. t.Fatalf("security audit = %+v", state.Security)
  302. }
  303. if state.PureAirplanePolicy {
  304. t.Fatal("VoWiFi RF off must not enable the independent pure-airplane policy")
  305. }
  306. wantEnableCalls := []string{
  307. "sim.identity",
  308. "aka.ready",
  309. "radio.snapshot",
  310. "radio.rf_off",
  311. "radio.stop_data",
  312. "proxy.resolve",
  313. "tunnel.start",
  314. "tunnel.evidence",
  315. "ims.start",
  316. "ims.evidence",
  317. "phone.save",
  318. "ims.sms",
  319. }
  320. if calls := environment.callsSnapshot(); !reflect.DeepEqual(calls, wantEnableCalls) {
  321. t.Fatalf("enable calls = %#v, want %#v", calls, wantEnableCalls)
  322. }
  323. if len(environment.tunnelRequests) != 1 {
  324. t.Fatalf("tunnel request count = %d", len(environment.tunnelRequests))
  325. }
  326. request := environment.tunnelRequests[0]
  327. if request.EPDG != "epdg.epc.mnc015.mcc234.pub.3gppnetwork.org" {
  328. t.Fatalf("EPDG = %q", request.EPDG)
  329. }
  330. if request.Proxy.Mode != ProxyModeDirect || request.Security.AllowMissingResponderAUTH {
  331. t.Fatalf("tunnel request = %+v", request)
  332. }
  333. state, err = orchestrator.Disable(context.Background())
  334. if err != nil {
  335. t.Fatalf("Disable() error = %v", err)
  336. }
  337. if state.Phase != PhaseIdle || state.Enabled || state.Active ||
  338. state.TunnelReady || state.IMSReady || state.SMSReady {
  339. t.Fatalf("Disable() state = %+v", state)
  340. }
  341. if state.PhoneNumber != "+447700900123" {
  342. t.Fatal("disabling the runtime must not erase the ICCID-associated number projection")
  343. }
  344. calls := environment.callsSnapshot()
  345. wantCleanup := []string{"ims.close", "tunnel.close", "radio.restore"}
  346. if !reflect.DeepEqual(calls[len(calls)-len(wantCleanup):], wantCleanup) {
  347. t.Fatalf("cleanup tail = %#v, want %#v", calls, wantCleanup)
  348. }
  349. }
  350. func TestEnableFailuresCleanUpEveryAcquiredLayer(t *testing.T) {
  351. tests := []struct {
  352. name string
  353. failCall string
  354. mutate func(*fakeEnvironment)
  355. wantError error
  356. wantCleanupTail []string
  357. }{
  358. {name: "identity", failCall: "sim.identity"},
  359. {name: "aka", failCall: "aka.ready"},
  360. {name: "radio snapshot", failCall: "radio.snapshot"},
  361. {
  362. name: "stop data can partially mutate",
  363. failCall: "radio.stop_data",
  364. wantCleanupTail: []string{"radio.restore"},
  365. },
  366. {
  367. name: "rf off",
  368. failCall: "radio.rf_off",
  369. wantCleanupTail: []string{"radio.restore"},
  370. },
  371. {
  372. name: "proxy",
  373. failCall: "proxy.resolve",
  374. wantCleanupTail: []string{"radio.restore"},
  375. },
  376. {
  377. name: "tunnel start",
  378. failCall: "tunnel.start",
  379. wantCleanupTail: []string{"radio.restore"},
  380. },
  381. {
  382. name: "tunnel evidence",
  383. mutate: func(environment *fakeEnvironment) {
  384. environment.tunnelEvidence.Established = false
  385. environment.tunnelEvidence.ResponderAUTH = ResponderAUTHUnknown
  386. },
  387. wantError: ErrTunnelNotEstablished,
  388. wantCleanupTail: []string{"tunnel.close", "radio.restore"},
  389. },
  390. {
  391. name: "IMS start",
  392. failCall: "ims.start",
  393. wantCleanupTail: []string{"tunnel.close", "radio.restore"},
  394. },
  395. {
  396. name: "IMS registration evidence",
  397. mutate: func(environment *fakeEnvironment) {
  398. environment.imsEvidence.Registered = false
  399. },
  400. wantError: ErrIMSNotRegistered,
  401. wantCleanupTail: []string{"ims.close", "tunnel.close", "radio.restore"},
  402. },
  403. {
  404. name: "SMS activation",
  405. failCall: "ims.sms",
  406. wantCleanupTail: []string{"ims.close", "tunnel.close", "radio.restore"},
  407. },
  408. {
  409. name: "SMS evidence",
  410. mutate: func(environment *fakeEnvironment) {
  411. environment.smsEvidence.Ready = false
  412. },
  413. wantError: ErrSMSNotReady,
  414. wantCleanupTail: []string{"ims.close", "tunnel.close", "radio.restore"},
  415. },
  416. }
  417. for _, test := range tests {
  418. t.Run(test.name, func(t *testing.T) {
  419. environment := newFakeEnvironment()
  420. if test.failCall != "" {
  421. environment.setFailure(test.failCall, 1)
  422. }
  423. if test.mutate != nil {
  424. test.mutate(environment)
  425. }
  426. orchestrator := newTestOrchestrator(t, environment, false)
  427. state, err := orchestrator.Enable(context.Background())
  428. if err == nil {
  429. t.Fatal("Enable() unexpectedly succeeded")
  430. }
  431. if test.wantError != nil && !errors.Is(err, test.wantError) {
  432. t.Fatalf("Enable() error = %v, want errors.Is(%v)", err, test.wantError)
  433. }
  434. if state.Phase != PhaseFailed || state.Active ||
  435. state.TunnelReady || state.IMSReady || state.SMSReady {
  436. t.Fatalf("failed state = %+v", state)
  437. }
  438. if len(test.wantCleanupTail) > 0 {
  439. calls := environment.callsSnapshot()
  440. if len(calls) < len(test.wantCleanupTail) {
  441. t.Fatalf("calls = %#v", calls)
  442. }
  443. tail := calls[len(calls)-len(test.wantCleanupTail):]
  444. if !reflect.DeepEqual(tail, test.wantCleanupTail) {
  445. t.Fatalf("cleanup tail = %#v, want %#v", tail, test.wantCleanupTail)
  446. }
  447. }
  448. })
  449. }
  450. }
  451. func TestResponderAUTHPolicyIsStrictByDefaultAndAuditsExplicitCompatibility(t *testing.T) {
  452. t.Run("strict", func(t *testing.T) {
  453. environment := newFakeEnvironment()
  454. environment.tunnelEvidence.ResponderAUTH = ResponderAUTHMissing
  455. orchestrator := newTestOrchestrator(t, environment, false)
  456. state, err := orchestrator.Enable(context.Background())
  457. if !errors.Is(err, ErrResponderAUTHRequired) {
  458. t.Fatalf("Enable() error = %v", err)
  459. }
  460. if state.Phase != PhaseFailed || state.Security.HighRisk ||
  461. state.Security.CompatibilityOverride {
  462. t.Fatalf("strict security state = %+v", state.Security)
  463. }
  464. })
  465. t.Run("explicit compatibility", func(t *testing.T) {
  466. environment := newFakeEnvironment()
  467. environment.tunnelEvidence.ResponderAUTH = ResponderAUTHMissing
  468. orchestrator := newTestOrchestrator(t, environment, true)
  469. state, err := orchestrator.Enable(context.Background())
  470. if err != nil {
  471. t.Fatalf("Enable() error = %v", err)
  472. }
  473. if state.Phase != PhaseSMSReady ||
  474. !state.Security.HighRisk ||
  475. !state.Security.CompatibilityOverride ||
  476. state.Security.Level != AuditLevelHigh ||
  477. state.Security.Code != AuditCodeMissingResponderAUTH {
  478. t.Fatalf("compatibility security state = %+v", state.Security)
  479. }
  480. if !environment.tunnelRequests[0].Security.AllowMissingResponderAUTH {
  481. t.Fatal("explicit compatibility policy was not passed to the tunnel provider")
  482. }
  483. })
  484. t.Run("invalid is never compatible", func(t *testing.T) {
  485. environment := newFakeEnvironment()
  486. environment.tunnelEvidence.ResponderAUTH = ResponderAUTHInvalid
  487. orchestrator := newTestOrchestrator(t, environment, true)
  488. state, err := orchestrator.Enable(context.Background())
  489. if !errors.Is(err, ErrResponderAUTHRequired) || state.Phase != PhaseFailed {
  490. t.Fatalf("Enable() = (%+v, %v)", state, err)
  491. }
  492. })
  493. }
  494. func TestPhoneNumberIsNeverInferredFromIMSI(t *testing.T) {
  495. environment := newFakeEnvironment()
  496. environment.identity.IMSI = "234159999999999"
  497. environment.imsEvidence.AssociatedMSISDN = ""
  498. environment.imsEvidence.PAssociatedURI = []string{
  499. "sip:[email protected]",
  500. }
  501. orchestrator := newTestOrchestrator(t, environment, false)
  502. state, err := orchestrator.Enable(context.Background())
  503. if err != nil {
  504. t.Fatalf("Enable() error = %v", err)
  505. }
  506. if state.PhoneNumber != "" || environment.callCount("phone.save") != 0 {
  507. t.Fatalf("number was inferred: state=%+v records=%+v", state, environment.phoneRecords)
  508. }
  509. if len(state.Warnings) != 1 || !strings.Contains(state.Warnings[0], "not inferred from IMSI") {
  510. t.Fatalf("warnings = %#v", state.Warnings)
  511. }
  512. }
  513. func TestPhoneStoreFailureDoesNotMisreportOrTearDownWorkingIMS(t *testing.T) {
  514. environment := newFakeEnvironment()
  515. environment.setFailure("phone.save", 1)
  516. orchestrator := newTestOrchestrator(t, environment, false)
  517. state, err := orchestrator.Enable(context.Background())
  518. if err != nil {
  519. t.Fatalf("Enable() error = %v", err)
  520. }
  521. if state.Phase != PhaseSMSReady || !state.IMSReady || state.PhoneNumber != "" {
  522. t.Fatalf("state = %+v", state)
  523. }
  524. if len(state.Warnings) != 1 || !strings.Contains(state.Warnings[0], "could not be persisted") {
  525. t.Fatalf("warnings = %#v", state.Warnings)
  526. }
  527. }
  528. func TestDisableCancelsAnInFlightEnableAndRestoresRadio(t *testing.T) {
  529. environment := newFakeEnvironment()
  530. environment.blockAt = "tunnel.start"
  531. orchestrator := newTestOrchestrator(t, environment, false)
  532. enableResult := make(chan error, 1)
  533. go func() {
  534. _, err := orchestrator.Enable(context.Background())
  535. enableResult <- err
  536. }()
  537. select {
  538. case <-environment.blocked:
  539. case <-time.After(2 * time.Second):
  540. t.Fatal("Enable() did not reach blocking tunnel provider")
  541. }
  542. disableContext, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  543. defer cancel()
  544. state, err := orchestrator.Disable(disableContext)
  545. if err != nil {
  546. t.Fatalf("Disable() error = %v", err)
  547. }
  548. if state.Phase != PhaseIdle || state.Enabled || state.Active {
  549. t.Fatalf("Disable() state = %+v", state)
  550. }
  551. select {
  552. case err := <-enableResult:
  553. if !errors.Is(err, context.Canceled) {
  554. t.Fatalf("Enable() error = %v, want context.Canceled", err)
  555. }
  556. case <-time.After(2 * time.Second):
  557. t.Fatal("Enable() did not exit after Disable() cancellation")
  558. }
  559. if environment.callCount("radio.restore") != 1 {
  560. t.Fatalf("radio.restore count = %d", environment.callCount("radio.restore"))
  561. }
  562. }
  563. func TestConcurrentEnableStartsOnlyOneRuntime(t *testing.T) {
  564. environment := newFakeEnvironment()
  565. orchestrator := newTestOrchestrator(t, environment, false)
  566. const goroutines = 24
  567. start := make(chan struct{})
  568. results := make(chan error, goroutines)
  569. var group sync.WaitGroup
  570. for index := 0; index < goroutines; index++ {
  571. group.Add(1)
  572. go func() {
  573. defer group.Done()
  574. <-start
  575. _, err := orchestrator.Enable(context.Background())
  576. results <- err
  577. }()
  578. }
  579. close(start)
  580. group.Wait()
  581. close(results)
  582. successes := 0
  583. alreadyEnabled := 0
  584. for err := range results {
  585. switch {
  586. case err == nil:
  587. successes++
  588. case errors.Is(err, ErrAlreadyEnabled):
  589. alreadyEnabled++
  590. default:
  591. t.Fatalf("unexpected Enable() error = %v", err)
  592. }
  593. }
  594. if successes != 1 || alreadyEnabled != goroutines-1 {
  595. t.Fatalf("successes=%d alreadyEnabled=%d", successes, alreadyEnabled)
  596. }
  597. if environment.callCount("tunnel.start") != 1 {
  598. t.Fatalf("tunnel.start count = %d", environment.callCount("tunnel.start"))
  599. }
  600. }
  601. func TestRetryAfterFailureCreatesANewAttempt(t *testing.T) {
  602. environment := newFakeEnvironment()
  603. environment.setFailure("tunnel.start", 1)
  604. orchestrator := newTestOrchestrator(t, environment, false)
  605. first, err := orchestrator.Enable(context.Background())
  606. if err == nil || first.Phase != PhaseFailed || first.Attempt != 1 {
  607. t.Fatalf("first Enable() = (%+v, %v)", first, err)
  608. }
  609. second, err := orchestrator.Retry(context.Background())
  610. if err != nil {
  611. t.Fatalf("Retry() error = %v", err)
  612. }
  613. if second.Phase != PhaseSMSReady || second.Attempt != 2 {
  614. t.Fatalf("Retry() state = %+v", second)
  615. }
  616. if environment.callCount("tunnel.start") != 2 {
  617. t.Fatalf("tunnel.start count = %d", environment.callCount("tunnel.start"))
  618. }
  619. }
  620. func TestReconnectClosesThenRebuildsTheRuntime(t *testing.T) {
  621. environment := newFakeEnvironment()
  622. orchestrator := newTestOrchestrator(t, environment, false)
  623. if _, err := orchestrator.Enable(context.Background()); err != nil {
  624. t.Fatal(err)
  625. }
  626. state, err := orchestrator.Reconnect(context.Background())
  627. if err != nil {
  628. t.Fatalf("Reconnect() error = %v", err)
  629. }
  630. if state.Phase != PhaseSMSReady || state.Attempt != 2 {
  631. t.Fatalf("Reconnect() state = %+v", state)
  632. }
  633. if environment.callCount("tunnel.start") != 2 ||
  634. environment.callCount("tunnel.close") != 1 ||
  635. environment.callCount("radio.restore") != 1 {
  636. t.Fatalf("calls = %#v", environment.callsSnapshot())
  637. }
  638. }
  639. // A non-fatal teardown error (for example the network rejecting SIP
  640. // deregistration during IMS close) must not stop a reconnect from rebuilding
  641. // the runtime; Disable still releases the local IMS, tunnel, and radio layers.
  642. func TestReconnectToleratesCleanupFailureAndRebuilds(t *testing.T) {
  643. environment := newFakeEnvironment()
  644. orchestrator := newTestOrchestrator(t, environment, false)
  645. if _, err := orchestrator.Enable(context.Background()); err != nil {
  646. t.Fatal(err)
  647. }
  648. environment.setFailure("ims.close", 1)
  649. state, err := orchestrator.Reconnect(context.Background())
  650. if err != nil {
  651. t.Fatalf("Reconnect() error = %v", err)
  652. }
  653. if state.Phase != PhaseSMSReady || state.Attempt != 2 {
  654. t.Fatalf("Reconnect() state = %+v", state)
  655. }
  656. if environment.callCount("ims.close") != 1 ||
  657. environment.callCount("tunnel.close") != 1 ||
  658. environment.callCount("radio.restore") != 1 ||
  659. environment.callCount("tunnel.start") != 2 {
  660. t.Fatalf("calls = %#v", environment.callsSnapshot())
  661. }
  662. }
  663. func TestRuntimeTunnelFailureRevokesReadinessAndCleansEveryLayer(t *testing.T) {
  664. environment := newFakeEnvironment()
  665. environment.tunnelFailures = make(chan error, 1)
  666. orchestrator := newTestOrchestrator(t, environment, false)
  667. if _, err := orchestrator.Enable(context.Background()); err != nil {
  668. t.Fatal(err)
  669. }
  670. environment.tunnelFailures <- errors.New("ESP relay stopped")
  671. deadline := time.Now().Add(2 * time.Second)
  672. for {
  673. state := orchestrator.State()
  674. if state.Phase == PhaseFailed {
  675. if state.Active || state.TunnelReady || state.IMSReady || state.SMSReady {
  676. t.Fatalf("stale runtime readiness survived failure: %+v", state)
  677. }
  678. if !state.Enabled || state.LastErrorClass != "tunnel_runtime" ||
  679. state.LastReason != "runtime_tunnel_failed" ||
  680. !strings.Contains(state.LastError, "ESP relay stopped") {
  681. t.Fatalf("runtime failure evidence = %+v", state)
  682. }
  683. break
  684. }
  685. if time.Now().After(deadline) {
  686. t.Fatalf("timed out waiting for runtime failure; state = %+v", state)
  687. }
  688. time.Sleep(10 * time.Millisecond)
  689. }
  690. calls := environment.callsSnapshot()
  691. wantTail := []string{"ims.close", "tunnel.close", "radio.restore"}
  692. if len(calls) < len(wantTail) ||
  693. !reflect.DeepEqual(calls[len(calls)-len(wantTail):], wantTail) {
  694. t.Fatalf("runtime failure cleanup tail = %#v", calls)
  695. }
  696. }
  697. func TestRuntimeIMSFailureRevokesRegistrationEvidence(t *testing.T) {
  698. environment := newFakeEnvironment()
  699. environment.imsFailures = make(chan error, 1)
  700. orchestrator := newTestOrchestrator(t, environment, false)
  701. if _, err := orchestrator.Enable(context.Background()); err != nil {
  702. t.Fatal(err)
  703. }
  704. environment.imsFailures <- errors.New("registration refresh failed")
  705. deadline := time.Now().Add(2 * time.Second)
  706. for {
  707. state := orchestrator.State()
  708. if state.Phase == PhaseFailed {
  709. if state.TunnelReady || state.IMSReady || state.SMSReady ||
  710. state.LastErrorClass != "ims_runtime" ||
  711. state.LastReason != "runtime_ims_failed" {
  712. t.Fatalf("IMS runtime failure evidence = %+v", state)
  713. }
  714. return
  715. }
  716. if time.Now().After(deadline) {
  717. t.Fatalf("timed out waiting for IMS runtime failure; state = %+v", state)
  718. }
  719. time.Sleep(10 * time.Millisecond)
  720. }
  721. }
  722. func TestSubscriptionPublishesOrderedEvidencePhases(t *testing.T) {
  723. environment := newFakeEnvironment()
  724. orchestrator := newTestOrchestrator(t, environment, false)
  725. updates, unsubscribe := orchestrator.Subscribe(32)
  726. defer unsubscribe()
  727. if _, err := orchestrator.Enable(context.Background()); err != nil {
  728. t.Fatal(err)
  729. }
  730. var phases []Phase
  731. deadline := time.After(2 * time.Second)
  732. for {
  733. select {
  734. case state := <-updates:
  735. if len(phases) == 0 || phases[len(phases)-1] != state.Phase {
  736. phases = append(phases, state.Phase)
  737. }
  738. if state.Phase == PhaseSMSReady {
  739. want := []Phase{
  740. PhaseIdle,
  741. PhaseSIMReady,
  742. PhaseAccessReady,
  743. PhaseTunnelReady,
  744. PhaseIMSReady,
  745. PhaseSMSReady,
  746. }
  747. if !reflect.DeepEqual(phases, want) {
  748. t.Fatalf("phases = %#v, want %#v", phases, want)
  749. }
  750. return
  751. }
  752. case <-deadline:
  753. t.Fatalf("timed out waiting for phases; got %#v", phases)
  754. }
  755. }
  756. }
  757. func TestCleanupAttemptsEveryLayerAndReportsAllErrors(t *testing.T) {
  758. environment := newFakeEnvironment()
  759. environment.setFailure("ims.sms", 1)
  760. environment.setFailure("ims.close", 1)
  761. environment.setFailure("tunnel.close", 1)
  762. environment.setFailure("radio.restore", 1)
  763. orchestrator := newTestOrchestrator(t, environment, false)
  764. state, err := orchestrator.Enable(context.Background())
  765. if err == nil {
  766. t.Fatal("Enable() unexpectedly succeeded")
  767. }
  768. if len(state.CleanupErrors) != 3 {
  769. t.Fatalf("cleanup errors = %#v", state.CleanupErrors)
  770. }
  771. calls := environment.callsSnapshot()
  772. wantTail := []string{"ims.close", "tunnel.close", "radio.restore"}
  773. if !reflect.DeepEqual(calls[len(calls)-3:], wantTail) {
  774. t.Fatalf("cleanup tail = %#v", calls[len(calls)-3:])
  775. }
  776. for _, text := range []string{"close IMS", "close tunnel", "restore radio"} {
  777. if !strings.Contains(err.Error(), text) {
  778. t.Fatalf("error %q does not contain %q", err, text)
  779. }
  780. }
  781. }
  782. func TestDisableCleanupWarningStillSettlesIdle(t *testing.T) {
  783. environment := newFakeEnvironment()
  784. orchestrator := newTestOrchestrator(t, environment, false)
  785. if _, err := orchestrator.Enable(context.Background()); err != nil {
  786. t.Fatalf("Enable() error = %v", err)
  787. }
  788. environment.setFailure("ims.close", 1)
  789. state, err := orchestrator.Disable(context.Background())
  790. if !errors.Is(err, ErrCleanupIncomplete) {
  791. t.Fatalf("Disable() error = %v, want ErrCleanupIncomplete", err)
  792. }
  793. if state.Phase != PhaseIdle || state.Enabled || state.Active ||
  794. state.SIMReady || state.AccessReady || state.TunnelReady ||
  795. state.IMSReady || state.SMSReady {
  796. t.Fatalf("Disable() warning state = %+v", state)
  797. }
  798. if state.LastErrorClass != "cleanup_warning" ||
  799. state.LastReason != "disabled_with_cleanup_errors" ||
  800. len(state.CleanupErrors) != 1 {
  801. t.Fatalf("Disable() warning evidence = %+v", state)
  802. }
  803. }
  804. func TestNewRejectsMissingProvidersAndInvalidOptions(t *testing.T) {
  805. environment := newFakeEnvironment()
  806. dependencies := Dependencies{
  807. SIM: fakeSIM{environment},
  808. AKA: fakeAKA{environment},
  809. Radio: fakeRadio{environment},
  810. Proxy: fakeProxy{environment},
  811. Tunnel: fakeTunnelProvider{environment},
  812. IMS: fakeIMSProvider{environment},
  813. Phones: fakePhones{environment},
  814. }
  815. if _, err := New(Dependencies{}, Options{DeviceID: "EC20"}); err == nil {
  816. t.Fatal("New() accepted missing providers")
  817. }
  818. if _, err := New(dependencies, Options{}); err == nil {
  819. t.Fatal("New() accepted empty device ID")
  820. }
  821. }