OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/string_util.h" | 5 #include "base/string_util.h" |
6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/api/identity/identity_api.h" | 8 #include "chrome/browser/extensions/api/identity/identity_api.h" |
9 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" | 9 #include "chrome/browser/extensions/component_loader.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
11 #include "chrome/browser/extensions/extension_browsertest.h" | 11 #include "chrome/browser/extensions/extension_browsertest.h" |
12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" | 18 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
20 #include "content/public/test/test_utils.h" | 22 #include "content/public/test/test_utils.h" |
21 #include "extensions/common/id_util.h" | 23 #include "extensions/common/id_util.h" |
22 #include "google_apis/gaia/google_service_auth_error.h" | 24 #include "google_apis/gaia/google_service_auth_error.h" |
23 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 25 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
24 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 27 #include "grit/browser_resources.h" |
| 28 #include "net/test/spawned_test_server.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
27 | 31 |
28 using testing::_; | 32 using testing::_; |
29 using testing::Return; | 33 using testing::Return; |
30 using testing::ReturnRef; | 34 using testing::ReturnRef; |
31 | 35 |
32 namespace extensions { | 36 namespace extensions { |
33 | 37 |
34 namespace { | 38 namespace { |
(...skipping 875 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 IN_PROC_BROWSER_TEST_F(RemoveCachedAuthTokenFunctionTest, MatchingToken) { | 914 IN_PROC_BROWSER_TEST_F(RemoveCachedAuthTokenFunctionTest, MatchingToken) { |
911 IdentityTokenCacheValue token(kAccessToken, | 915 IdentityTokenCacheValue token(kAccessToken, |
912 base::TimeDelta::FromSeconds(3600)); | 916 base::TimeDelta::FromSeconds(3600)); |
913 SetCachedToken(token); | 917 SetCachedToken(token); |
914 EXPECT_TRUE(InvalidateDefaultToken()); | 918 EXPECT_TRUE(InvalidateDefaultToken()); |
915 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, | 919 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, |
916 GetCachedToken().status()); | 920 GetCachedToken().status()); |
917 } | 921 } |
918 | 922 |
919 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { | 923 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { |
| 924 public: |
| 925 void LoadComponent() { |
| 926 // Normally the ComponentLoader won't load the component in tests. |
| 927 ExtensionService* service = extensions::ExtensionSystem::Get( |
| 928 profile())->extension_service(); |
| 929 |
| 930 service->component_loader()->Add( |
| 931 IDR_IDENTITY_API_UI_MANIFEST, |
| 932 base::FilePath(FILE_PATH_LITERAL("identity_api_ui"))); |
| 933 } |
| 934 |
| 935 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 936 // Reduce performance test variance by disabling background networking. |
| 937 command_line->AppendSwitch(switches::kDisableBackgroundNetworking); |
| 938 } |
920 }; | 939 }; |
921 | 940 |
922 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, UserCloseWindow) { | 941 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, UserCloseWindow) { |
923 content::WindowedNotificationObserver observer( | 942 LoadComponent(); |
924 chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 943 net::SpawnedTestServer https_server( |
925 content::NotificationService::AllSources()); | 944 net::SpawnedTestServer::TYPE_HTTPS, |
| 945 net::SpawnedTestServer::kLocalhost, |
| 946 base::FilePath(FILE_PATH_LITERAL( |
| 947 "chrome/test/data/extensions/api_test/identity"))); |
| 948 ASSERT_TRUE(https_server.Start()); |
| 949 GURL auth_url(https_server.GetURL("files/interaction_required.html")); |
926 | 950 |
927 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( | 951 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( |
928 new IdentityLaunchWebAuthFlowFunction()); | 952 new IdentityLaunchWebAuthFlowFunction()); |
929 | |
930 RunFunctionAsync( | |
931 function, "[{\"interactive\": true, \"url\": \"data:text/html,auth\"}]"); | |
932 | |
933 observer.Wait(); | |
934 Browser* web_auth_flow_browser = | |
935 content::Source<Browser>(observer.source()).ptr(); | |
936 web_auth_flow_browser->window()->Close(); | |
937 | |
938 EXPECT_EQ(std::string(errors::kUserRejected), WaitForError(function)); | |
939 } | |
940 | |
941 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, InteractionRequired) { | |
942 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( | |
943 new IdentityLaunchWebAuthFlowFunction()); | |
944 scoped_refptr<Extension> empty_extension( | 953 scoped_refptr<Extension> empty_extension( |
945 utils::CreateEmptyExtension()); | 954 utils::CreateEmptyExtension()); |
946 function->set_extension(empty_extension.get()); | 955 function->set_extension(empty_extension.get()); |
947 | 956 |
948 std::string error = utils::RunFunctionAndReturnError( | 957 std::string args = "[{\"interactive\": true, \"url\": \"" + |
949 function, "[{\"interactive\": false, \"url\": \"data:text/html,auth\"}]", | 958 auth_url.spec() + "\"}]"; |
950 browser()); | 959 RunFunctionAsync(function, args); |
| 960 |
| 961 GURL last_loaded; |
| 962 content::NavigationController* web_auth_flow_controller; |
| 963 do { |
| 964 content::WindowedNotificationObserver observer( |
| 965 content::NOTIFICATION_LOAD_STOP, |
| 966 content::NotificationService::AllSources()); |
| 967 observer.Wait(); |
| 968 web_auth_flow_controller = |
| 969 content::Source<content::NavigationController>(observer.source()).ptr(); |
| 970 last_loaded = web_auth_flow_controller->GetWebContents()->GetURL(); |
| 971 } while (last_loaded != auth_url); |
| 972 if (web_auth_flow_controller->GetWebContents()->GetEmbedderWebContents()) |
| 973 web_auth_flow_controller->GetWebContents()->GetEmbedderWebContents()-> |
| 974 Close(); |
| 975 else |
| 976 web_auth_flow_controller->GetWebContents()->Close(); |
| 977 |
| 978 EXPECT_EQ(std::string(errors::kUserRejected), WaitForError(function)); |
| 979 } |
| 980 |
| 981 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, InteractionRequired) { |
| 982 LoadComponent(); |
| 983 net::SpawnedTestServer https_server( |
| 984 net::SpawnedTestServer::TYPE_HTTPS, |
| 985 net::SpawnedTestServer::kLocalhost, |
| 986 base::FilePath(FILE_PATH_LITERAL( |
| 987 "chrome/test/data/extensions/api_test/identity"))); |
| 988 ASSERT_TRUE(https_server.Start()); |
| 989 GURL auth_url(https_server.GetURL("files/interaction_required.html")); |
| 990 |
| 991 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( |
| 992 new IdentityLaunchWebAuthFlowFunction()); |
| 993 scoped_refptr<Extension> empty_extension( |
| 994 utils::CreateEmptyExtension()); |
| 995 function->set_extension(empty_extension.get()); |
| 996 |
| 997 std::string args = "[{\"interactive\": false, \"url\": \"" + |
| 998 auth_url.spec() + "\"}]"; |
| 999 std::string error = utils::RunFunctionAndReturnError(function, args, |
| 1000 browser()); |
951 | 1001 |
952 EXPECT_EQ(std::string(errors::kInteractionRequired), error); | 1002 EXPECT_EQ(std::string(errors::kInteractionRequired), error); |
953 } | 1003 } |
954 | 1004 |
955 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, NonInteractiveSuccess) { | 1005 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, NonInteractiveSuccess) { |
| 1006 LoadComponent(); |
956 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( | 1007 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( |
957 new IdentityLaunchWebAuthFlowFunction()); | 1008 new IdentityLaunchWebAuthFlowFunction()); |
958 scoped_refptr<Extension> empty_extension( | 1009 scoped_refptr<Extension> empty_extension( |
959 utils::CreateEmptyExtension()); | 1010 utils::CreateEmptyExtension()); |
960 function->set_extension(empty_extension.get()); | 1011 function->set_extension(empty_extension.get()); |
961 | 1012 |
962 function->InitFinalRedirectURLPrefixesForTest("abcdefghij"); | 1013 function->InitFinalRedirectURLPrefixesForTest("abcdefghij"); |
963 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 1014 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
964 function, | 1015 function, |
965 "[{\"interactive\": false," | 1016 "[{\"interactive\": false," |
966 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]", | 1017 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]", |
967 browser())); | 1018 browser())); |
968 | 1019 |
969 std::string url; | 1020 std::string url; |
970 EXPECT_TRUE(value->GetAsString(&url)); | 1021 EXPECT_TRUE(value->GetAsString(&url)); |
971 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), | 1022 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), |
972 url); | 1023 url); |
973 } | 1024 } |
974 | 1025 |
975 IN_PROC_BROWSER_TEST_F( | 1026 IN_PROC_BROWSER_TEST_F( |
976 LaunchWebAuthFlowFunctionTest, InteractiveFirstNavigationSuccess) { | 1027 LaunchWebAuthFlowFunctionTest, InteractiveFirstNavigationSuccess) { |
| 1028 LoadComponent(); |
977 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( | 1029 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( |
978 new IdentityLaunchWebAuthFlowFunction()); | 1030 new IdentityLaunchWebAuthFlowFunction()); |
979 scoped_refptr<Extension> empty_extension( | 1031 scoped_refptr<Extension> empty_extension( |
980 utils::CreateEmptyExtension()); | 1032 utils::CreateEmptyExtension()); |
981 function->set_extension(empty_extension.get()); | 1033 function->set_extension(empty_extension.get()); |
982 | 1034 |
983 function->InitFinalRedirectURLPrefixesForTest("abcdefghij"); | 1035 function->InitFinalRedirectURLPrefixesForTest("abcdefghij"); |
984 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 1036 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
985 function, | 1037 function, |
986 "[{\"interactive\": true," | 1038 "[{\"interactive\": true," |
987 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]", | 1039 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]", |
988 browser())); | 1040 browser())); |
989 | 1041 |
990 std::string url; | 1042 std::string url; |
991 EXPECT_TRUE(value->GetAsString(&url)); | 1043 EXPECT_TRUE(value->GetAsString(&url)); |
992 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), | 1044 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), |
993 url); | 1045 url); |
994 } | 1046 } |
995 | 1047 |
996 IN_PROC_BROWSER_TEST_F( | 1048 IN_PROC_BROWSER_TEST_F( |
997 LaunchWebAuthFlowFunctionTest, InteractiveSecondNavigationSuccess) { | 1049 LaunchWebAuthFlowFunctionTest, InteractiveSecondNavigationSuccess) { |
| 1050 LoadComponent(); |
| 1051 net::SpawnedTestServer https_server( |
| 1052 net::SpawnedTestServer::TYPE_HTTPS, |
| 1053 net::SpawnedTestServer::kLocalhost, |
| 1054 base::FilePath(FILE_PATH_LITERAL( |
| 1055 "chrome/test/data/extensions/api_test/identity"))); |
| 1056 ASSERT_TRUE(https_server.Start()); |
| 1057 GURL auth_url(https_server.GetURL("files/redirect_to_chromiumapp.html")); |
| 1058 |
998 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( | 1059 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( |
999 new IdentityLaunchWebAuthFlowFunction()); | 1060 new IdentityLaunchWebAuthFlowFunction()); |
1000 scoped_refptr<Extension> empty_extension( | 1061 scoped_refptr<Extension> empty_extension( |
1001 utils::CreateEmptyExtension()); | 1062 utils::CreateEmptyExtension()); |
1002 function->set_extension(empty_extension.get()); | 1063 function->set_extension(empty_extension.get()); |
1003 | 1064 |
1004 function->InitFinalRedirectURLPrefixesForTest("abcdefghij"); | 1065 function->InitFinalRedirectURLPrefixesForTest("abcdefghij"); |
| 1066 std::string args = "[{\"interactive\": true, \"url\": \"" + |
| 1067 auth_url.spec() + "\"}]"; |
1005 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 1068 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
1006 function, | 1069 function, args, browser())); |
1007 "[{\"interactive\": true," | |
1008 "\"url\": \"data:text/html,<script>window.location.replace('" | |
1009 "https://abcdefghij.chromiumapp.org/callback#test')</script>\"}]", | |
1010 browser())); | |
1011 | 1070 |
1012 std::string url; | 1071 std::string url; |
1013 EXPECT_TRUE(value->GetAsString(&url)); | 1072 EXPECT_TRUE(value->GetAsString(&url)); |
1014 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), | 1073 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), |
1015 url); | 1074 url); |
1016 } | 1075 } |
1017 | 1076 |
1018 } // namespace extensions | 1077 } // namespace extensions |
OLD | NEW |