oauth2_id_token_decoder_unittest.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2017 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. #include "google_apis/gaia/oauth2_id_token_decoder.h"
  5. #include <string>
  6. #include <vector>
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace {
  9. const char kIdTokenInvalidJwt[] =
  10. "dummy-header."
  11. "..."
  12. ".dummy-signature";
  13. const char kIdTokenInvalidJson[] =
  14. "dummy-header."
  15. "YWJj" // payload: abc
  16. ".dummy-signature";
  17. const char kIdTokenEmptyServices[] =
  18. "dummy-header."
  19. "eyAic2VydmljZXMiOiBbXSB9" // payload: { "services": [] }
  20. ".dummy-signature";
  21. const char kIdTokenEmptyServicesHeaderSignature[] =
  22. "."
  23. "eyAic2VydmljZXMiOiBbXSB9" // payload: { "services": [] }
  24. ".";
  25. const char kIdTokenMissingServices[] =
  26. "dummy-header."
  27. "eyAiYWJjIjogIiJ9" // payload: { "abc": ""}
  28. ".dummy-signature";
  29. const char kIdTokenNotChildAccount[] =
  30. "dummy-header."
  31. "eyAic2VydmljZXMiOiBbImFiYyJdIH0=" // payload: { "services": ["abc"] }
  32. ".dummy-signature";
  33. const char kIdTokenChildAccount[] =
  34. "dummy-header."
  35. "eyAic2VydmljZXMiOiBbInVjYSJdIH0=" // payload: { "services": ["uca"] }
  36. ".dummy-signature";
  37. const char kIdTokenAdvancedProtectionAccount[] =
  38. "dummy-header."
  39. "eyAic2VydmljZXMiOiBbInRpYSJdIH0=" // payload: { "services": ["tia"] }
  40. ".dummy-signature";
  41. const char kIdTokenChildAndAdvancedProtectionAccount[] =
  42. "dummy-header."
  43. "eyAic2VydmljZXMiOiBbInRpYSIsICJ1Y2EiXSB9"
  44. ".dummy-signature"; // payload: { "services": ["tia", "uca"] }
  45. class OAuth2IdTokenDecoderTest : public testing::Test {};
  46. TEST_F(OAuth2IdTokenDecoderTest, Invalid) {
  47. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenInvalidJwt).is_child_account);
  48. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenInvalidJson).is_child_account);
  49. EXPECT_FALSE(
  50. gaia::ParseServiceFlags(kIdTokenMissingServices).is_child_account);
  51. }
  52. TEST_F(OAuth2IdTokenDecoderTest, NotChild) {
  53. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenEmptyServices).is_child_account);
  54. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenEmptyServicesHeaderSignature)
  55. .is_child_account);
  56. EXPECT_FALSE(
  57. gaia::ParseServiceFlags(kIdTokenNotChildAccount).is_child_account);
  58. }
  59. TEST_F(OAuth2IdTokenDecoderTest, Child) {
  60. EXPECT_TRUE(gaia::ParseServiceFlags(kIdTokenChildAccount).is_child_account);
  61. }
  62. TEST_F(OAuth2IdTokenDecoderTest, NotAdvancedProtection) {
  63. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenEmptyServices)
  64. .is_under_advanced_protection);
  65. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenEmptyServicesHeaderSignature)
  66. .is_under_advanced_protection);
  67. EXPECT_FALSE(gaia::ParseServiceFlags(kIdTokenChildAccount)
  68. .is_under_advanced_protection);
  69. }
  70. TEST_F(OAuth2IdTokenDecoderTest, AdvancedProtection) {
  71. EXPECT_TRUE(gaia::ParseServiceFlags(kIdTokenAdvancedProtectionAccount)
  72. .is_under_advanced_protection);
  73. gaia::TokenServiceFlags service_flags =
  74. gaia::ParseServiceFlags(kIdTokenChildAndAdvancedProtectionAccount);
  75. EXPECT_TRUE(service_flags.is_child_account);
  76. EXPECT_TRUE(service_flags.is_under_advanced_protection);
  77. }
  78. } // namespace