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 // Unit tests for implementation of google_api_keys namespace. | 5 // Unit tests for implementation of google_api_keys namespace. |
6 // | 6 // |
7 // Because the file deals with a lot of preprocessor defines and | 7 // Because the file deals with a lot of preprocessor defines and |
8 // optionally includes an internal header, the way we test is by | 8 // optionally includes an internal header, the way we test is by |
9 // including the .cc file multiple times with different defines set. | 9 // including the .cc file multiple times with different defines set. |
10 // This is a little unorthodox, but it lets us test the behavior as | 10 // This is a little unorthodox, but it lets us test the behavior as |
11 // close to unmodified as possible. | 11 // close to unmodified as possible. |
12 | 12 |
13 #include "google_apis/google_api_keys.h" | 13 #include "google_apis/google_api_keys_unittest.h" |
14 | |
15 #include <memory> | |
16 | 14 |
17 #include "base/macros.h" | 15 #include "base/macros.h" |
18 #include "build/build_config.h" | 16 #include "build/build_config.h" |
19 #include "google_apis/gaia/gaia_switches.h" | 17 #include "google_apis/gaia/gaia_switches.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | 18 |
22 // The Win builders fail (with a linker crash) when trying to link | 19 // The Win builders fail (with a linker crash) when trying to link |
23 // unit_tests, and the Android builders complain about multiply | 20 // unit_tests, and the Android builders complain about multiply |
24 // defined symbols (likely they don't do name decoration as well as | 21 // defined symbols (likely they don't do name decoration as well as |
25 // the Mac and Linux linkers). Therefore these tests are only built | 22 // the Mac and Linux linkers). Therefore these tests are only built |
26 // and run on Mac and Linux, which should provide plenty of coverage | 23 // and run on Mac and Linux, which should provide plenty of coverage |
27 // since there are no platform-specific bits in this code. | 24 // since there are no platform-specific bits in this code. |
28 #if defined(OS_LINUX) || defined(OS_MACOSX) | 25 #if defined(OS_LINUX) || defined(OS_MACOSX) |
29 | 26 |
30 // We need to include everything included by google_api_keys.cc once | 27 // We need to include everything included by google_api_keys.cc once |
31 // at global scope so that things like STL and classes from base don't | 28 // at global scope so that things like STL and classes from base don't |
32 // get defined when we re-include the google_api_keys.cc file | 29 // get defined when we re-include the google_api_keys.cc file |
33 // below. We used to include that file in its entirety here, but that | 30 // below. We used to include that file in its entirety here, but that |
34 // can cause problems if the linker decides the version of symbols | 31 // can cause problems if the linker decides the version of symbols |
35 // from that file included here is the "right" version. | 32 // from that file included here is the "right" version. |
36 | 33 |
37 #include <stddef.h> | 34 #include <stddef.h> |
38 | 35 |
39 #include <string> | 36 #include <string> |
40 #include "base/command_line.h" | 37 #include "base/command_line.h" |
41 #include "base/environment.h" | |
42 #include "base/lazy_instance.h" | 38 #include "base/lazy_instance.h" |
43 #include "base/logging.h" | 39 #include "base/logging.h" |
44 #include "base/strings/stringize_macros.h" | 40 #include "base/strings/stringize_macros.h" |
45 | 41 |
| 42 #if defined(OS_MACOSX) |
| 43 #include "google_apis/google_api_keys_mac.h" |
| 44 #endif |
| 45 |
| 46 GoogleAPIKeysTest::GoogleAPIKeysTest() : env_(base::Environment::Create()) { |
| 47 static_assert(11 == 3 + 2 * google_apis::CLIENT_NUM_ITEMS, |
| 48 "Unexpected number of key entries."); |
| 49 env_cache_[0].variable_name = "GOOGLE_API_KEY"; |
| 50 env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN"; |
| 51 env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN"; |
| 52 env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT"; |
| 53 env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT"; |
| 54 env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING"; |
| 55 env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING"; |
| 56 env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST"; |
| 57 env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST"; |
| 58 env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID"; |
| 59 env_cache_[10].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET"; |
| 60 } |
| 61 |
| 62 GoogleAPIKeysTest::~GoogleAPIKeysTest() {} |
| 63 |
| 64 void GoogleAPIKeysTest::SetUp() { |
| 65 // Unset all environment variables that can affect these tests, |
| 66 // for the duration of the tests. |
| 67 for (size_t i = 0; i < arraysize(env_cache_); ++i) { |
| 68 EnvironmentCache& cache = env_cache_[i]; |
| 69 cache.was_set = env_->HasVar(cache.variable_name); |
| 70 cache.value.clear(); |
| 71 if (cache.was_set) { |
| 72 env_->GetVar(cache.variable_name, &cache.value); |
| 73 env_->UnSetVar(cache.variable_name); |
| 74 } |
| 75 } |
| 76 } |
| 77 |
| 78 void GoogleAPIKeysTest::TearDown() { |
| 79 // Restore environment. |
| 80 for (size_t i = 0; i < arraysize(env_cache_); ++i) { |
| 81 EnvironmentCache& cache = env_cache_[i]; |
| 82 if (cache.was_set) { |
| 83 env_->SetVar(cache.variable_name, cache.value); |
| 84 } |
| 85 } |
| 86 } |
| 87 |
46 // This is the default baked-in value for OAuth IDs and secrets. | 88 // This is the default baked-in value for OAuth IDs and secrets. |
47 static const char kDummyToken[] = "dummytoken"; | 89 static const char kDummyToken[] = "dummytoken"; |
48 | 90 |
49 struct EnvironmentCache { | |
50 public: | |
51 EnvironmentCache() : variable_name(NULL), was_set(false) {} | |
52 | |
53 const char* variable_name; | |
54 bool was_set; | |
55 std::string value; | |
56 }; | |
57 | |
58 class GoogleAPIKeysTest : public testing::Test { | |
59 public: | |
60 GoogleAPIKeysTest() : env_(base::Environment::Create()) { | |
61 env_cache_[0].variable_name = "GOOGLE_API_KEY"; | |
62 env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN"; | |
63 env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN"; | |
64 env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT"; | |
65 env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT"; | |
66 env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING"; | |
67 env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING"; | |
68 env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST"; | |
69 env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST"; | |
70 env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID"; | |
71 env_cache_[10].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET"; | |
72 } | |
73 | |
74 void SetUp() override { | |
75 // Unset all environment variables that can affect these tests, | |
76 // for the duration of the tests. | |
77 for (size_t i = 0; i < arraysize(env_cache_); ++i) { | |
78 EnvironmentCache& cache = env_cache_[i]; | |
79 cache.was_set = env_->HasVar(cache.variable_name); | |
80 cache.value.clear(); | |
81 if (cache.was_set) { | |
82 env_->GetVar(cache.variable_name, &cache.value); | |
83 env_->UnSetVar(cache.variable_name); | |
84 } | |
85 } | |
86 } | |
87 | |
88 void TearDown() override { | |
89 // Restore environment. | |
90 for (size_t i = 0; i < arraysize(env_cache_); ++i) { | |
91 EnvironmentCache& cache = env_cache_[i]; | |
92 if (cache.was_set) { | |
93 env_->SetVar(cache.variable_name, cache.value); | |
94 } | |
95 } | |
96 } | |
97 | |
98 private: | |
99 std::unique_ptr<base::Environment> env_; | |
100 | |
101 // Why 3? It is for GOOGLE_API_KEY, GOOGLE_DEFAULT_CLIENT_ID and | |
102 // GOOGLE_DEFAULT_CLIENT_SECRET. | |
103 // | |
104 // Why 2 times CLIENT_NUM_ITEMS? This is the number of different | |
105 // clients in the OAuth2Client enumeration, and for each of these we | |
106 // have both an ID and a secret. | |
107 EnvironmentCache env_cache_[3 + 2 * google_apis::CLIENT_NUM_ITEMS]; | |
108 }; | |
109 | |
110 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) | 91 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) |
111 // Test official build behavior, since we are in a checkout where this | 92 // Test official build behavior, since we are in a checkout where this |
112 // is possible. | 93 // is possible. |
113 namespace official_build { | 94 namespace official_build { |
114 | 95 |
115 // We start every test by creating a clean environment for the | 96 // We start every test by creating a clean environment for the |
116 // preprocessor defines used in google_api_keys.cc | 97 // preprocessor defines used in google_api_keys.cc |
117 #undef DUMMY_API_TOKEN | 98 #undef DUMMY_API_TOKEN |
118 #undef GOOGLE_API_KEY | 99 #undef GOOGLE_API_KEY |
119 #undef GOOGLE_CLIENT_ID_MAIN | 100 #undef GOOGLE_CLIENT_ID_MAIN |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 EXPECT_EQ("env-SECRET_MAIN", secret_main); | 475 EXPECT_EQ("env-SECRET_MAIN", secret_main); |
495 EXPECT_EQ("env-ID_CLOUD_PRINT", id_cloud_print); | 476 EXPECT_EQ("env-ID_CLOUD_PRINT", id_cloud_print); |
496 EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print); | 477 EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print); |
497 EXPECT_EQ("env-ID_REMOTING", id_remoting); | 478 EXPECT_EQ("env-ID_REMOTING", id_remoting); |
498 EXPECT_EQ("env-SECRET_REMOTING", secret_remoting); | 479 EXPECT_EQ("env-SECRET_REMOTING", secret_remoting); |
499 EXPECT_EQ("env-ID_REMOTING_HOST", id_remoting_host); | 480 EXPECT_EQ("env-ID_REMOTING_HOST", id_remoting_host); |
500 EXPECT_EQ("env-SECRET_REMOTING_HOST", secret_remoting_host); | 481 EXPECT_EQ("env-SECRET_REMOTING_HOST", secret_remoting_host); |
501 } | 482 } |
502 | 483 |
503 #endif // defined(OS_LINUX) || defined(OS_MACOSX) | 484 #endif // defined(OS_LINUX) || defined(OS_MACOSX) |
OLD | NEW |