Home | History | Annotate | Download | only in identity
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
      6 
      7 #include <vector>
      8 
      9 #include "testing/gmock/include/gmock/gmock.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 using extensions::IdentityMintRequestQueue;
     13 
     14 namespace {
     15 
     16 class MockRequest : public extensions::IdentityMintRequestQueue::Request {
     17  public:
     18   MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType));
     19 };
     20 
     21 }  // namespace
     22 
     23 TEST(IdentityMintQueueTest, SerialRequests) {
     24   IdentityMintRequestQueue::MintType type =
     25       IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE;
     26   IdentityMintRequestQueue queue;
     27   std::string extension_id("ext_id");
     28   MockRequest request1;
     29   MockRequest request2;
     30 
     31   EXPECT_CALL(request1, StartMintToken(type)).Times(1);
     32   queue.RequestStart(type, extension_id, std::set<std::string>(), &request1);
     33   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request1);
     34 
     35   EXPECT_CALL(request2, StartMintToken(type)).Times(1);
     36   queue.RequestStart(type, extension_id, std::set<std::string>(), &request2);
     37   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request2);
     38 }
     39 
     40 TEST(IdentityMintQueueTest, InteractiveType) {
     41   IdentityMintRequestQueue::MintType type =
     42       IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE;
     43   IdentityMintRequestQueue queue;
     44   std::string extension_id("ext_id");
     45   MockRequest request1;
     46 
     47   EXPECT_CALL(request1, StartMintToken(type)).Times(1);
     48   queue.RequestStart(type, extension_id, std::set<std::string>(), &request1);
     49   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request1);
     50 }
     51 
     52 TEST(IdentityMintQueueTest, ParallelRequests) {
     53   IdentityMintRequestQueue::MintType type =
     54       IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE;
     55   IdentityMintRequestQueue queue;
     56   std::string extension_id("ext_id");
     57   MockRequest request1;
     58   MockRequest request2;
     59   MockRequest request3;
     60 
     61   EXPECT_CALL(request1, StartMintToken(type)).Times(1);
     62   queue.RequestStart(type, extension_id, std::set<std::string>(), &request1);
     63   queue.RequestStart(type, extension_id, std::set<std::string>(), &request2);
     64   queue.RequestStart(type, extension_id, std::set<std::string>(), &request3);
     65 
     66   EXPECT_CALL(request2, StartMintToken(type)).Times(1);
     67   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request1);
     68 
     69   EXPECT_CALL(request3, StartMintToken(type)).Times(1);
     70   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request2);
     71 
     72   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request3);
     73 }
     74 
     75 TEST(IdentityMintQueueTest, ParallelRequestsFromTwoExtensions) {
     76   IdentityMintRequestQueue::MintType type =
     77       IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE;
     78   IdentityMintRequestQueue queue;
     79   std::string extension_id1("ext_id_1");
     80   std::string extension_id2("ext_id_2");
     81   MockRequest request1;
     82   MockRequest request2;
     83 
     84   EXPECT_CALL(request1, StartMintToken(type)).Times(1);
     85   EXPECT_CALL(request2, StartMintToken(type)).Times(1);
     86   queue.RequestStart(type, extension_id1, std::set<std::string>(), &request1);
     87   queue.RequestStart(type, extension_id2, std::set<std::string>(), &request2);
     88 
     89   queue.RequestComplete(type, extension_id1,
     90                         std::set<std::string>(), &request1);
     91   queue.RequestComplete(type, extension_id2,
     92                         std::set<std::string>(), &request2);
     93 }
     94 
     95 TEST(IdentityMintQueueTest, ParallelRequestsForDifferentScopes) {
     96   IdentityMintRequestQueue::MintType type =
     97       IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE;
     98   IdentityMintRequestQueue queue;
     99   std::string extension_id("ext_id");
    100   MockRequest request1;
    101   MockRequest request2;
    102   std::set<std::string> scopes1;
    103   std::set<std::string> scopes2;
    104 
    105   scopes1.insert("a");
    106   scopes1.insert("b");
    107   scopes2.insert("a");
    108 
    109   EXPECT_CALL(request1, StartMintToken(type)).Times(1);
    110   EXPECT_CALL(request2, StartMintToken(type)).Times(1);
    111   queue.RequestStart(type, extension_id, scopes1, &request1);
    112   queue.RequestStart(type, extension_id, scopes2, &request2);
    113 
    114   queue.RequestComplete(type, extension_id, scopes1, &request1);
    115   queue.RequestComplete(type, extension_id, scopes2, &request2);
    116 }
    117 
    118 TEST(IdentityMintQueueTest, KeyComparisons) {
    119   std::string extension_id1("ext_id_1");
    120   std::string extension_id2("ext_id_2");
    121   std::set<std::string> scopes1;
    122   std::set<std::string> scopes2;
    123   std::set<std::string> scopes3;
    124 
    125   scopes1.insert("a");
    126   scopes1.insert("b");
    127   scopes2.insert("a");
    128 
    129   std::vector<std::string> ids;
    130   ids.push_back(extension_id1);
    131   ids.push_back(extension_id2);
    132 
    133   std::vector<std::set<std::string> > scopesets;
    134   scopesets.push_back(scopes1);
    135   scopesets.push_back(scopes2);
    136   scopesets.push_back(scopes3);
    137 
    138   std::vector<IdentityMintRequestQueue::RequestKey> keys;
    139   typedef std::vector<
    140     IdentityMintRequestQueue::RequestKey>::const_iterator
    141       RequestKeyIterator;
    142 
    143   std::vector<std::string>::const_iterator id_it;
    144   std::vector<std::set<std::string> >::const_iterator scope_it;
    145 
    146   for (id_it = ids.begin(); id_it != ids.end(); ++id_it) {
    147     for (scope_it = scopesets.begin(); scope_it != scopesets.end();
    148          ++scope_it) {
    149       keys.push_back(IdentityMintRequestQueue::RequestKey(
    150           IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE, *id_it,
    151           *scope_it));
    152       keys.push_back(IdentityMintRequestQueue::RequestKey(
    153           IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE, *id_it, *scope_it));
    154     }
    155   }
    156 
    157   // keys should not be less than themselves
    158   for (RequestKeyIterator it = keys.begin(); it != keys.end(); ++it) {
    159     EXPECT_FALSE(*it < *it);
    160   }
    161 
    162   // keys should not equal different keys
    163   for (RequestKeyIterator it1 = keys.begin(); it1 != keys.end(); ++it1) {
    164     RequestKeyIterator it2 = it1;
    165     for (++it2; it2 != keys.end(); ++it2) {
    166       EXPECT_TRUE(*it1 < *it2 || *it2 < *it1);
    167       EXPECT_FALSE(*it1 < *it2 && *it2 < *it1);
    168     }
    169   }
    170 }
    171 
    172 TEST(IdentityMintQueueTest, Empty) {
    173   IdentityMintRequestQueue::MintType type =
    174       IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE;
    175   IdentityMintRequestQueue queue;
    176   std::string extension_id("ext_id");
    177   MockRequest request1;
    178 
    179   EXPECT_TRUE(queue.empty(type, extension_id, std::set<std::string>()));
    180   EXPECT_CALL(request1, StartMintToken(type)).Times(1);
    181   queue.RequestStart(type, extension_id, std::set<std::string>(), &request1);
    182   EXPECT_FALSE(queue.empty(type, extension_id, std::set<std::string>()));
    183   queue.RequestComplete(type, extension_id, std::set<std::string>(), &request1);
    184   EXPECT_TRUE(queue.empty(type, extension_id, std::set<std::string>()));
    185 }
    186