Home | History | Annotate | Download | only in prototype
      1 // Copyright 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 "cloud_print/gcp20/prototype/x_privet_token.h"
      6 
      7 #include <stdio.h>
      8 
      9 #include "base/base64.h"
     10 #include "base/basictypes.h"
     11 #include "base/format_macros.h"
     12 #include "base/logging.h"
     13 #include "base/sha1.h"
     14 #include "base/strings/string_number_conversions.h"
     15 #include "base/strings/stringprintf.h"
     16 #include "base/time/time.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 TEST(XPrivetTokenTest, Generation) {
     20   std::string secret = "E3E92296-E290-4E77-B678-6AEF256C30C8";
     21   uint64 gen_time = 1372444784;
     22   uint64 issue_time = gen_time;
     23 
     24   XPrivetToken xtoken(secret, base::Time::FromTimeT(gen_time));
     25 
     26   std::string issue_time_str = base::StringPrintf("%" PRIu64, issue_time);
     27   std::string sha1_val = base::SHA1HashString(secret + ":" + issue_time_str);
     28 
     29   ASSERT_STRCASEEQ("2216828f9eefc3931c1b9a110dcca3dbec23571d",
     30                    base::HexEncode(sha1_val.data(), sha1_val.size()).c_str());
     31 
     32   std::string base64_val;
     33   base::Base64Encode(sha1_val, &base64_val);
     34   std::string token = base64_val + ":" + issue_time_str;
     35 
     36   ASSERT_EQ(token, xtoken.GenerateXTokenWithTime(issue_time));
     37 
     38   EXPECT_NE(xtoken.GenerateXTokenWithTime(issue_time),
     39             xtoken.GenerateXTokenWithTime(issue_time + 1));
     40 }
     41 
     42 TEST(XPrivetTokenTest, CheckingValid) {
     43   base::Time gen_time = base::Time::FromTimeT(1372444784);
     44   XPrivetToken xtoken("9CEEA1AD-BC24-4D5A-8AB4-A6CE3E0CC4CD", gen_time);
     45 
     46   std::string token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT());
     47   EXPECT_TRUE(xtoken.CheckValidXToken(token));
     48 
     49   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 1);
     50   EXPECT_TRUE(xtoken.CheckValidXToken(token));
     51 
     52   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 55);
     53   EXPECT_TRUE(xtoken.CheckValidXToken(token));
     54 
     55   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 60*60 - 5);
     56   EXPECT_TRUE(xtoken.CheckValidXToken(token));
     57 
     58   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 60*60 + 10);
     59   EXPECT_TRUE(xtoken.CheckValidXToken(token));
     60 
     61   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 24*60*60 - 1);
     62   EXPECT_TRUE(xtoken.CheckValidXToken(token));
     63 }
     64 
     65 TEST(XPrivetTokenTest, CheckingInvalid) {
     66   base::Time gen_time = base::Time::FromTimeT(1372444784);
     67   XPrivetToken xtoken("9CEEA1AD-BC24-4D5A-8AB4-A6CE3E0CC4CD", gen_time);
     68 
     69   // Meaningless tokens:
     70 
     71   std::string token = "CEEA1AD9CEEA1AD9CEEA1AD9CEEA1AD";
     72   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     73 
     74   base::Base64Encode("CEEA1AD9CEEA1AD9CEEA1AD9CEEA1AD", &token);
     75   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     76 
     77   base::Base64Encode("CEEA1AD9CEEA:1AD9CEEA1AD9CEEA1AD", &token);
     78   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     79 
     80   base::Base64Encode("CEEA1AD9CEEA:1AD9CEEA1AD9:CEEA1AD", &token);
     81   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     82 
     83   base::Base64Encode("CEEA1AD9CEEA:123456", &token);
     84   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     85 
     86   base::Base64Encode("CEEA1AD9CEEA:", &token);
     87   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     88 
     89   base::Base64Encode("CEEA1AD9CEEA:1372444784", &token);
     90   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     91 
     92   EXPECT_FALSE(xtoken.CheckValidXToken(""));
     93 
     94   // Expired tokens:
     95 
     96   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 24*60*60 + 1);
     97   EXPECT_FALSE(xtoken.CheckValidXToken(token));
     98 
     99   token = xtoken.GenerateXTokenWithTime(gen_time.ToTimeT() + 7*24*60*60 - 1023);
    100   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    101 
    102   // Tokens with different secret:
    103 
    104   XPrivetToken another("6F02AC4E-6F37-4078-AF42-5EE5D8180284", gen_time);
    105 
    106   token = another.GenerateXTokenWithTime(gen_time.ToTimeT() - 24*60*60 - 1);
    107   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    108 
    109   token = another.GenerateXTokenWithTime(gen_time.ToTimeT() - 24*60*60 + 1);
    110   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    111 
    112   token = another.GenerateXTokenWithTime(gen_time.ToTimeT());
    113   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    114 
    115   token = another.GenerateXTokenWithTime(gen_time.ToTimeT() + 1);
    116   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    117 
    118   token = another.GenerateXTokenWithTime(gen_time.ToTimeT() + 24*60*60 - 1);
    119   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    120 
    121   token = another.GenerateXTokenWithTime(gen_time.ToTimeT() + 24*60*60 + 1);
    122   EXPECT_FALSE(xtoken.CheckValidXToken(token));
    123 }
    124 
    125