summaryrefslogtreecommitdiffstats
path: root/ios/net/cookies/cookie_store_ios_unittest.mm
blob: 66d5ba5bdd22b7035f2020c92a67edd74fc1bf4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/net/cookies/cookie_store_ios.h"

#import <Foundation/Foundation.h>

#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/sys_string_conversions.h"
#import "net/base/mac/url_conversions.h"
#include "net/cookies/cookie_store_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
// Clears the underlying NSHTTPCookieStorage.
void ClearCookies() {
  NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  [store setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
  NSArray* cookies = [store cookies];
  for (NSHTTPCookie* cookie in cookies)
    [store deleteCookie:cookie];
  EXPECT_EQ(0u, [[store cookies] count]);
}
}  // namespace

namespace net {

struct CookieStoreIOSTestTraits {
  static net::CookieStore* Create() {
    ClearCookies();
    CookieStoreIOS* store = new CookieStoreIOS(nullptr);
    store->synchronization_state_ = CookieStoreIOS::SYNCHRONIZED;
    return store;
  }

  static const bool is_cookie_monster = false;
  static const bool supports_http_only = false;
  static const bool supports_non_dotted_domains = false;
  static const bool preserves_trailing_dots = false;
  static const bool filters_schemes = false;
  static const bool has_path_prefix_bug = true;
  static const int creation_time_granularity_in_ms = 1000;

  base::MessageLoop loop_;
};

struct InactiveCookieStoreIOSTestTraits {
  static scoped_refptr<net::CookieStore> Create() {
    return new CookieStoreIOS(nullptr);
  }

  static const bool is_cookie_monster = false;
  static const bool supports_http_only = false;
  static const bool supports_non_dotted_domains = true;
  static const bool preserves_trailing_dots = true;
  static const bool filters_schemes = false;
  static const bool has_path_prefix_bug = false;
  static const int creation_time_granularity_in_ms = 0;
  static const int enforces_prefixes = true;

  base::MessageLoop loop_;
};

// RoundTripTestCookieStore is un-synchronized and re-synchronized after all
// cookie operations. This means all system cookies are converted to Chrome
// cookies and converted back.
// The purpose of this class is to test that cookie conversions do not break the
// cookie store.
class RoundTripTestCookieStore : public net::CookieStore {
 public:
  RoundTripTestCookieStore()
      : store_(new CookieStoreIOS(nullptr)),
        dummy_store_(new CookieStoreIOS(nullptr)) {
    CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  }

  // Inherited CookieStore methods.
  void SetCookieWithOptionsAsync(const GURL& url,
                                 const std::string& cookie_line,
                                 const net::CookieOptions& options,
                                 const SetCookiesCallback& callback) override {
    RoundTrip();
    store_->SetCookieWithOptionsAsync(url, cookie_line, options, callback);
  }

  void GetCookiesWithOptionsAsync(const GURL& url,
                                  const net::CookieOptions& options,
                                  const GetCookiesCallback& callback) override {
    RoundTrip();
    store_->GetCookiesWithOptionsAsync(url, options, callback);
  }

  void GetAllCookiesForURLAsync(
      const GURL& url,
      const GetCookieListCallback& callback) override {
    RoundTrip();
    store_->GetAllCookiesForURLAsync(url, callback);
  }

  void DeleteCookieAsync(const GURL& url,
                         const std::string& cookie_name,
                         const base::Closure& callback) override {
    RoundTrip();
    store_->DeleteCookieAsync(url, cookie_name, callback);
  }

  net::CookieMonster* GetCookieMonster() override { return nullptr; }

  void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin,
                                    const base::Time& delete_end,
                                    const DeleteCallback& callback) override {
    RoundTrip();
    store_->DeleteAllCreatedBetweenAsync(delete_begin, delete_end, callback);
  }

  void DeleteAllCreatedBetweenForHostAsync(
      const base::Time delete_begin,
      const base::Time delete_end,
      const GURL& url,
      const DeleteCallback& callback) override {
    RoundTrip();
    store_->DeleteAllCreatedBetweenForHostAsync(delete_begin, delete_end, url,
                                                callback);
  }

  void DeleteSessionCookiesAsync(const DeleteCallback& callback) override {
    RoundTrip();
    store_->DeleteSessionCookiesAsync(callback);
  }

  scoped_ptr<CookieStore::CookieChangedSubscription> AddCallbackForCookie(
      const GURL& url,
      const std::string& name,
      const CookieChangedCallback& callback) override {
    return scoped_ptr<CookieStore::CookieChangedSubscription>();
  }

 protected:
  ~RoundTripTestCookieStore() override { store_->UnSynchronize(); }

 private:
  void RoundTrip() {
    CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store_.get());
    // Check that the system store is empty, because it is synchronized with
    // |dummy_store_| which is empty.
    NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    EXPECT_EQ(0u, [[store cookies] count]);
    CookieStoreIOS::SwitchSynchronizedStore(dummy_store_.get(), store_.get());
  }

  scoped_refptr<CookieStoreIOS> store_;
  // |dummy_store_| is not directly used, but is needed to make |store_|
  // inactive.
  scoped_refptr<CookieStoreIOS> dummy_store_;
};

struct RoundTripTestCookieStoreTraits {
  static scoped_refptr<net::CookieStore> Create() {
    ClearCookies();
    return new RoundTripTestCookieStore();
  }

  static const bool is_cookie_monster = false;
  static const bool supports_http_only = false;
  static const bool supports_non_dotted_domains = false;
  static const bool preserves_trailing_dots = false;
  static const bool filters_schemes = false;
  static const bool has_path_prefix_bug = true;
  static const int creation_time_granularity_in_ms = 1000;
  static const int enforces_prefixes = true;
};

}  // namespace net

namespace net {

INSTANTIATE_TYPED_TEST_CASE_P(CookieStoreIOS,
                              CookieStoreTest,
                              CookieStoreIOSTestTraits);

INSTANTIATE_TYPED_TEST_CASE_P(InactiveCookieStoreIOS,
                              CookieStoreTest,
                              InactiveCookieStoreIOSTestTraits);

INSTANTIATE_TYPED_TEST_CASE_P(RoundTripTestCookieStore,
                              CookieStoreTest,
                              RoundTripTestCookieStoreTraits);

}  // namespace net

namespace {

// Test net::CookieMonster::PersistentCookieStore allowing to control when the
// initialization completes.
class TestPersistentCookieStore
    : public net::CookieMonster::PersistentCookieStore {
 public:
  TestPersistentCookieStore()
      : kTestCookieURL("http://foo.google.com/bar"), flushed_(false) {}

  // Runs the completion callback with a "a=b" cookie.
  void RunLoadedCallback() {
    std::vector<net::CanonicalCookie*> cookies;
    net::CookieOptions options;
    options.set_include_httponly();
    cookies.push_back(net::CanonicalCookie::Create(kTestCookieURL, "a=b",
                                                   base::Time::Now(), options));
    // Some canonical cookies cannot be converted into System cookies, for
    // example if value is not valid utf8. Such cookies are ignored.
    net::CanonicalCookie* bad_canonical_cookie = new net::CanonicalCookie(
        kTestCookieURL, "name", "\x81r\xe4\xbd\xa0\xe5\xa5\xbd", "domain",
        "path/",
        base::Time(),  // creation
        base::Time(),  // expires
        base::Time(),  // last_access
        false,         // secure
        false,         // httponly
        false,         // first_party_only
        net::COOKIE_PRIORITY_DEFAULT);
    cookies.push_back(bad_canonical_cookie);
    loaded_callback_.Run(cookies);
  }

  bool flushed() { return flushed_; }

 private:
  // net::CookieMonster::PersistentCookieStore implementation:
  void Load(const LoadedCallback& loaded_callback) override {
    loaded_callback_ = loaded_callback;
  }

  void LoadCookiesForKey(const std::string& key,
                         const LoadedCallback& loaded_callback) override {
    loaded_callback_ = loaded_callback;
  }

  void AddCookie(const net::CanonicalCookie& cc) override {}
  void UpdateCookieAccessTime(const net::CanonicalCookie& cc) override {}
  void DeleteCookie(const net::CanonicalCookie& cc) override {}
  void SetForceKeepSessionState() override {}
  void Flush(const base::Closure& callback) override { flushed_ = true; }

 private:
  ~TestPersistentCookieStore() override {}

  const GURL kTestCookieURL;
  LoadedCallback loaded_callback_;
  bool flushed_;
};

// Helper callback to be passed to CookieStore::GetCookiesWithOptionsAsync().
class GetCookieCallback {
 public:
  GetCookieCallback() : did_run_(false) {}

  // Returns true if the callback has been run.
  bool did_run() { return did_run_; }

  // Returns the parameter of the callback.
  const std::string& cookie_line() { return cookie_line_; }

  void Run(const std::string& cookie_line) {
    ASSERT_FALSE(did_run_);
    did_run_ = true;
    cookie_line_ = cookie_line;
  }

 private:
  bool did_run_;
  std::string cookie_line_;
};

// Helper callback to be passed to CookieStore::GetAllCookiesForURLAsync().
class GetAllCookiesCallback {
 public:
  GetAllCookiesCallback() : did_run_(false) {}

  // Returns true if the callback has been run.
  bool did_run() { return did_run_; }

  // Returns the parameter of the callback.
  const net::CookieList& cookie_list() { return cookie_list_; }

  void Run(const net::CookieList& cookie_list) {
    ASSERT_FALSE(did_run_);
    did_run_ = true;
    cookie_list_ = cookie_list;
  }

 private:
  bool did_run_;
  net::CookieList cookie_list_;
};

namespace {

void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies,
                         std::vector<bool>* out_removes,
                         const net::CanonicalCookie& cookie,
                         bool removed) {
  DCHECK(out_cookies);
  out_cookies->push_back(cookie);
  if (out_removes)
    out_removes->push_back(removed);
}

void IgnoreBoolean(bool ignored) {
}

void IgnoreString(const std::string& ignored) {
}

}  // namespace

class CookieStoreIOSWithBackend : public testing::Test {
 public:
  CookieStoreIOSWithBackend()
      : kTestCookieURL("http://foo.google.com/bar"),
        kTestCookieURL2("http://foo.google.com/baz"),
        kTestCookieURL3("http://foo.google.com"),
        kTestCookieURL4("http://bar.google.com/bar") {
    backend_ = new TestPersistentCookieStore;
    store_ = new net::CookieStoreIOS(backend_.get());
    net::CookieStoreIOS::SetCookiePolicy(net::CookieStoreIOS::ALLOW);
    cookie_changed_callback_ = store_->AddCallbackForCookie(
        kTestCookieURL, "abc",
        base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_));
  }

  ~CookieStoreIOSWithBackend() override {}

  // Gets the cookies. |callback| will be called on completion.
  void GetCookies(const net::CookieStore::GetCookiesCallback& callback) {
    net::CookieOptions options;
    options.set_include_httponly();
    store_->GetCookiesWithOptionsAsync(kTestCookieURL, options, callback);
  }

  // Sets a cookie.
  void SetCookie(const std::string& cookie_line) {
    net::CookieOptions options;
    options.set_include_httponly();
    store_->SetCookieWithOptionsAsync(kTestCookieURL, cookie_line, options,
                                      base::Bind(&IgnoreBoolean));
    net::CookieStoreIOS::NotifySystemCookiesChanged();
    // Wait until the flush is posted.
    base::MessageLoop::current()->RunUntilIdle();
  }

  void SetSystemCookie(const GURL& url,
                       const std::string& name,
                       const std::string& value) {
    NSHTTPCookieStorage* storage =
        [NSHTTPCookieStorage sharedHTTPCookieStorage];
    [storage setCookie:[NSHTTPCookie cookieWithProperties:@{
      NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()),
      NSHTTPCookieName : base::SysUTF8ToNSString(name),
      NSHTTPCookieValue : base::SysUTF8ToNSString(value),
      NSHTTPCookieDomain : base::SysUTF8ToNSString(url.host()),
    }]];
    net::CookieStoreIOS::NotifySystemCookiesChanged();
    base::MessageLoop::current()->RunUntilIdle();
  }

  void DeleteSystemCookie(const GURL& gurl, const std::string& name) {
    NSHTTPCookieStorage* storage =
        [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSURL* nsurl = net::NSURLWithGURL(gurl);
    NSArray* cookies = [storage cookiesForURL:nsurl];
    for (NSHTTPCookie* cookie in cookies) {
      if (cookie.name.UTF8String == name) {
        [storage deleteCookie:cookie];
        break;
      }
    }
    net::CookieStoreIOS::NotifySystemCookiesChanged();
    base::MessageLoop::current()->RunUntilIdle();
  }

 protected:
  const GURL kTestCookieURL;
  const GURL kTestCookieURL2;
  const GURL kTestCookieURL3;
  const GURL kTestCookieURL4;

  base::MessageLoop loop_;
  scoped_refptr<TestPersistentCookieStore> backend_;
  scoped_refptr<net::CookieStoreIOS> store_;
  scoped_ptr<net::CookieStore::CookieChangedSubscription>
      cookie_changed_callback_;
  std::vector<net::CanonicalCookie> cookies_changed_;
  std::vector<bool> cookies_removed_;
};

}  // namespace

namespace net {

TEST_F(CookieStoreIOSWithBackend, SetCookieCallsHookWhenNotSynchronized) {
  ClearCookies();
  SetCookie("abc=def");
  EXPECT_EQ(0U, cookies_changed_.size());
  EXPECT_EQ(0U, cookies_removed_.size());
  backend_->RunLoadedCallback();
  base::MessageLoop::current()->RunUntilIdle();
  EXPECT_EQ(1U, cookies_changed_.size());
  EXPECT_EQ(1U, cookies_removed_.size());
  EXPECT_EQ("abc", cookies_changed_[0].Name());
  EXPECT_EQ("def", cookies_changed_[0].Value());
  EXPECT_FALSE(cookies_removed_[0]);

  // Replacing an existing cookie is actually a two-phase delete + set
  // operation, so we get an extra notification.
  SetCookie("abc=ghi");
  EXPECT_EQ(3U, cookies_changed_.size());
  EXPECT_EQ(3U, cookies_removed_.size());
  EXPECT_EQ("abc", cookies_changed_[1].Name());
  EXPECT_EQ("def", cookies_changed_[1].Value());
  EXPECT_TRUE(cookies_removed_[1]);
  EXPECT_EQ("abc", cookies_changed_[2].Name());
  EXPECT_EQ("ghi", cookies_changed_[2].Value());
  EXPECT_FALSE(cookies_removed_[2]);

  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, SetCookieCallsHookWhenSynchronized) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  GetCookies(base::Bind(&IgnoreString));
  backend_->RunLoadedCallback();
  base::MessageLoop::current()->RunUntilIdle();
  ClearCookies();
  SetCookie("abc=def");
  EXPECT_EQ(1U, cookies_changed_.size());
  EXPECT_EQ(1U, cookies_removed_.size());
  EXPECT_EQ("abc", cookies_changed_[0].Name());
  EXPECT_EQ("def", cookies_changed_[0].Value());
  EXPECT_FALSE(cookies_removed_[0]);

  SetCookie("abc=ghi");
  EXPECT_EQ(3U, cookies_changed_.size());
  EXPECT_EQ(3U, cookies_removed_.size());
  EXPECT_EQ("abc", cookies_changed_[1].Name());
  EXPECT_EQ("def", cookies_changed_[1].Value());
  EXPECT_TRUE(cookies_removed_[1]);
  EXPECT_EQ("abc", cookies_changed_[2].Name());
  EXPECT_EQ("ghi", cookies_changed_[2].Value());
  EXPECT_FALSE(cookies_removed_[2]);
  DeleteSystemCookie(kTestCookieURL, "abc");

  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, DeleteCallsHook) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  GetCookies(base::Bind(&IgnoreString));
  backend_->RunLoadedCallback();
  base::MessageLoop::current()->RunUntilIdle();
  ClearCookies();
  SetCookie("abc=def");
  EXPECT_EQ(1U, cookies_changed_.size());
  EXPECT_EQ(1U, cookies_removed_.size());
  store_->DeleteCookieAsync(kTestCookieURL, "abc",
                            base::Bind(&IgnoreBoolean, false));
  CookieStoreIOS::NotifySystemCookiesChanged();
  base::MessageLoop::current()->RunUntilIdle();
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, SameValueDoesNotCallHook) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  GetCookieCallback callback;
  GetCookies(base::Bind(&IgnoreString));
  backend_->RunLoadedCallback();
  base::MessageLoop::current()->RunUntilIdle();
  ClearCookies();
  SetCookie("abc=def");
  EXPECT_EQ(1U, cookies_changed_.size());
  SetCookie("abc=def");
  EXPECT_EQ(1U, cookies_changed_.size());
  store_->UnSynchronize();
}

TEST(CookieStoreIOS, GetAllCookiesForURLAsync) {
  base::MessageLoop loop;
  const GURL kTestCookieURL("http://foo.google.com/bar");
  ClearCookies();
  scoped_refptr<CookieStoreIOS> cookie_store(new CookieStoreIOS(nullptr));
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, cookie_store.get());
  // Add a cookie.
  net::CookieOptions options;
  options.set_include_httponly();
  cookie_store->SetCookieWithOptionsAsync(
      kTestCookieURL, "a=b", options, net::CookieStore::SetCookiesCallback());
  // Disallow cookies.
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK);
  // No cookie in the system store.
  NSHTTPCookieStorage* system_store =
      [NSHTTPCookieStorage sharedHTTPCookieStorage];
  EXPECT_EQ(0u, [[system_store cookies] count]);
  // Flushing should not have any effect.
  cookie_store->Flush(base::Closure());
  // Check we can get the cookie even though cookies are disabled.
  GetAllCookiesCallback callback;
  cookie_store->GetAllCookiesForURLAsync(
      kTestCookieURL,
      base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback)));
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ(1u, callback.cookie_list().size());
  net::CanonicalCookie cookie = callback.cookie_list()[0];
  EXPECT_EQ("a", cookie.Name());
  EXPECT_EQ("b", cookie.Value());
  // Re-enable cookies.
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW);
  // Cookie is back in the system store.
  EXPECT_EQ(1u, [[system_store cookies] count]);
  cookie_store->UnSynchronize();
}

// Tests that cookies can be read before the backend is loaded.
TEST_F(CookieStoreIOSWithBackend, NotSynchronized) {
  // Start fetching the cookie.
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  // Backend loading completes.
  backend_->RunLoadedCallback();
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
}

// Tests that cookies can be read before synchronization is complete.
TEST_F(CookieStoreIOSWithBackend, Synchronizing) {
  // Start synchronization.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  // Backend loading completes (end of synchronization).
  backend_->RunLoadedCallback();
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  store_->UnSynchronize();
}

// Tests that cookies can be read before synchronization is complete, when
// triggered by a change in cookie policy.
TEST_F(CookieStoreIOSWithBackend, SynchronizingAfterPolicyChange) {
  ClearCookies();
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK);
  // SwitchSynchronizedStore() does nothing when cookies are blocked.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  // Start synchronization by allowing cookies.
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW);
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  // Backend loading completes (end of synchronization).
  backend_->RunLoadedCallback();
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  store_->UnSynchronize();
}

// Tests that Synchronization can be "aborted" (i.e. the cookie store is
// unsynchronized while synchronization is in progress).
TEST_F(CookieStoreIOSWithBackend, SyncThenUnsync) {
  ClearCookies();
  scoped_refptr<CookieStoreIOS> dummy_store = new CookieStoreIOS(nullptr);
  // Switch back and forth before synchronization can complete.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get());
  backend_->RunLoadedCallback();
  // No cookie leak in the system store.
  NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  EXPECT_EQ(0u, [[store cookies] count]);
  // No cookie lost.
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  dummy_store->UnSynchronize();
}

// Tests that Synchronization can be "aborted" while there are pending tasks
// (i.e. the cookie store is unsynchronized while synchronization is in progress
// and there are pending tasks).
TEST_F(CookieStoreIOSWithBackend, SyncThenUnsyncWithPendingTasks) {
  ClearCookies();
  scoped_refptr<CookieStoreIOS> dummy_store = new CookieStoreIOS(nullptr);
  // Start synchornization.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  // Create a pending task while synchronization is in progress.
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  // Cancel the synchronization.
  CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get());
  // Synchronization completes after being cancelled.
  backend_->RunLoadedCallback();
  // The task is not lost.
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  dummy_store->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, ChangePolicyOnceDuringSynchronization) {
  // Start synchronization.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  // Toggle cookie policy to trigger another synchronization while the first one
  // is still in progress.
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK);
  // Backend loading completes (end of synchronization).
  backend_->RunLoadedCallback();
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW);
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend,
       ChangePolicyDuringSynchronizationWithPendingTask) {
  // Start synchronization.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  // Create a pending task while synchronization is in progress.
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  // Toggle cookie policy to trigger another synchronization while the first one
  // is still in progress.
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK);
  // Backend loading completes (end of synchronization).
  backend_->RunLoadedCallback();
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, ChangePolicyTwiceDuringSynchronization) {
  // Start synchronization.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  // Toggle cookie policy to trigger another synchronization while the first one
  // is still in progress.
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK);
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW);
  // Backend loading completes (end of synchronization).
  backend_->RunLoadedCallback();
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, UnSynchronizeBeforeLoadComplete) {
  ClearCookies();
  // Switch back and forth before synchronization can complete.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  store_->UnSynchronize();
  backend_->RunLoadedCallback();
  // No cookie lost.
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
}

TEST_F(CookieStoreIOSWithBackend, UnSynchronize) {
  ClearCookies();
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  store_->UnSynchronize();
  // No cookie lost.
  GetCookieCallback callback;
  GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
  EXPECT_TRUE(callback.did_run());
  EXPECT_EQ("a=b", callback.cookie_line());
}

TEST_F(CookieStoreIOSWithBackend, FlushOnUnSynchronize) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  EXPECT_FALSE(backend_->flushed());
  store_->UnSynchronize();
  EXPECT_TRUE(backend_->flushed());
}

TEST_F(CookieStoreIOSWithBackend, FlushOnSwitch) {
  scoped_refptr<CookieStoreIOS> dummy_store = new CookieStoreIOS(nullptr);
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  EXPECT_FALSE(backend_->flushed());
  CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get());
  EXPECT_TRUE(backend_->flushed());
  dummy_store->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, FlushOnCookieChanged) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  store_->set_flush_delay_for_testing(base::TimeDelta());
  backend_->RunLoadedCallback();
  EXPECT_FALSE(backend_->flushed());

  // Set a cookie an check that it triggers a flush.
  SetCookie("x=y");
  EXPECT_TRUE(backend_->flushed());

  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, ManualFlush) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  EXPECT_FALSE(backend_->flushed());

  // The store should be flushed even if it is not dirty.
  store_->Flush(base::Closure());
  EXPECT_TRUE(backend_->flushed());

  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, FlushOnPolicyChange) {
  // Start synchronization.
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  // Toggle cookie policy to trigger a flush.
  EXPECT_FALSE(backend_->flushed());
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK);
  EXPECT_TRUE(backend_->flushed());
  store_->UnSynchronize();
  CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW);
}

TEST_F(CookieStoreIOSWithBackend, NoInitialNotifyWithNoCookie) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  std::vector<net::CanonicalCookie> cookies;
  store_->AddCallbackForCookie(
      kTestCookieURL, "abc",
      base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, NoInitialNotifyWithSystemCookie) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  SetSystemCookie(kTestCookieURL, "abc", "def");
  std::vector<net::CanonicalCookie> cookies;
  store_->AddCallbackForCookie(
      kTestCookieURL, "abc",
      base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, NotifyOnAdd) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  std::vector<bool> removes;
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL, "abc",
          base::Bind(&RecordCookieChanges, &cookies, &removes));
  EXPECT_EQ(0U, cookies.size());
  EXPECT_EQ(0U, removes.size());
  SetSystemCookie(kTestCookieURL, "abc", "def");
  EXPECT_EQ(1U, cookies.size());
  EXPECT_EQ(1U, removes.size());
  EXPECT_EQ("abc", cookies[0].Name());
  EXPECT_EQ("def", cookies[0].Value());
  EXPECT_FALSE(removes[0]);

  SetSystemCookie(kTestCookieURL, "ghi", "jkl");
  EXPECT_EQ(1U, cookies.size());
  EXPECT_EQ(1U, removes.size());

  DeleteSystemCookie(kTestCookieURL, "abc");
  DeleteSystemCookie(kTestCookieURL, "ghi");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, NotifyOnChange) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  std::vector<bool> removes;
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL, "abc",
          base::Bind(&RecordCookieChanges, &cookies, &removes));
  EXPECT_EQ(0U, cookies.size());
  SetSystemCookie(kTestCookieURL, "abc", "def");
  EXPECT_EQ(1U, cookies.size());
  SetSystemCookie(kTestCookieURL, "abc", "ghi");
  EXPECT_EQ(3U, cookies.size());
  EXPECT_EQ(3U, removes.size());
  EXPECT_EQ("abc", cookies[1].Name());
  EXPECT_EQ("def", cookies[1].Value());
  EXPECT_TRUE(removes[1]);
  EXPECT_EQ("abc", cookies[2].Name());
  EXPECT_EQ("ghi", cookies[2].Value());
  EXPECT_FALSE(removes[2]);

  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, NotifyOnDelete) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  std::vector<bool> removes;
  SetSystemCookie(kTestCookieURL, "abc", "def");
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL, "abc",
          base::Bind(&RecordCookieChanges, &cookies, &removes));
  EXPECT_EQ(0U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  EXPECT_EQ(1U, cookies.size());
  EXPECT_EQ(1U, removes.size());
  EXPECT_TRUE(removes[0]);
  SetSystemCookie(kTestCookieURL, "abc", "def");
  EXPECT_EQ(2U, cookies.size());
  EXPECT_EQ(2U, removes.size());
  EXPECT_FALSE(removes[1]);
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, NoNotifyOnNoChange) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL, "abc",
          base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  SetSystemCookie(kTestCookieURL, "abc", "def");
  EXPECT_EQ(1U, cookies.size());
  SetSystemCookie(kTestCookieURL, "abc", "def");
  EXPECT_EQ(1U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, MultipleNotifies) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  std::vector<net::CanonicalCookie> cookies2;
  std::vector<net::CanonicalCookie> cookies3;
  std::vector<net::CanonicalCookie> cookies4;
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL, "abc",
          base::Bind(&RecordCookieChanges, &cookies, nullptr));
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle2 =
      store_->AddCallbackForCookie(
          kTestCookieURL2, "abc",
          base::Bind(&RecordCookieChanges, &cookies2, nullptr));
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle3 =
      store_->AddCallbackForCookie(
          kTestCookieURL3, "abc",
          base::Bind(&RecordCookieChanges, &cookies3, nullptr));
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle4 =
      store_->AddCallbackForCookie(
          kTestCookieURL4, "abc",
          base::Bind(&RecordCookieChanges, &cookies4, nullptr));
  SetSystemCookie(kTestCookieURL, "abc", "def");
  SetSystemCookie(kTestCookieURL2, "abc", "def");
  SetSystemCookie(kTestCookieURL3, "abc", "def");
  SetSystemCookie(kTestCookieURL4, "abc", "def");
  EXPECT_EQ(2U, cookies.size());
  EXPECT_EQ(2U, cookies2.size());
  EXPECT_EQ(1U, cookies3.size());
  EXPECT_EQ(1U, cookies4.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  DeleteSystemCookie(kTestCookieURL2, "abc");
  DeleteSystemCookie(kTestCookieURL3, "abc");
  DeleteSystemCookie(kTestCookieURL4, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, LessSpecificNestedCookie) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  SetSystemCookie(kTestCookieURL2, "abc", "def");
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL2, "abc",
          base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  SetSystemCookie(kTestCookieURL3, "abc", "ghi");
  EXPECT_EQ(1U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, MoreSpecificNestedCookie) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  SetSystemCookie(kTestCookieURL3, "abc", "def");
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL2, "abc",
          base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  SetSystemCookie(kTestCookieURL2, "abc", "ghi");
  EXPECT_EQ(1U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, MoreSpecificNestedCookieWithSameValue) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  SetSystemCookie(kTestCookieURL3, "abc", "def");
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL2, "abc",
          base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  SetSystemCookie(kTestCookieURL2, "abc", "def");
  EXPECT_EQ(1U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

TEST_F(CookieStoreIOSWithBackend, RemoveCallback) {
  CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get());
  backend_->RunLoadedCallback();
  std::vector<net::CanonicalCookie> cookies;
  SetSystemCookie(kTestCookieURL, "abc", "def");
  scoped_ptr<net::CookieStore::CookieChangedSubscription> handle =
      store_->AddCallbackForCookie(
          kTestCookieURL, "abc",
          base::Bind(&RecordCookieChanges, &cookies, nullptr));
  EXPECT_EQ(0U, cookies.size());
  SetSystemCookie(kTestCookieURL, "abc", "ghi");
  EXPECT_EQ(2U, cookies.size());
  // this deletes the callback
  handle.reset();
  SetSystemCookie(kTestCookieURL, "abc", "jkl");
  EXPECT_EQ(2U, cookies.size());
  DeleteSystemCookie(kTestCookieURL, "abc");
  store_->UnSynchronize();
}

}  // namespace net