Index: chrome/browser/chromeos/login/users/affiliation_unittest.cc |
diff --git a/chrome/browser/chromeos/login/users/affiliation_unittest.cc b/chrome/browser/chromeos/login/users/affiliation_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6aa0a347f5fa3028d6c919b41da80528607306f6 |
--- /dev/null |
+++ b/chrome/browser/chromeos/login/users/affiliation_unittest.cc |
@@ -0,0 +1,108 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <set> |
+#include <string> |
+ |
+#include "chrome/browser/chromeos/login/users/affiliation.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace chromeos { |
+ |
+TEST(AffiliationTest, HaveCommonElementEmptySet) { |
+ // Empty sets don't have common elements. |
+ EXPECT_FALSE( |
+ HaveCommonElement(std::set<std::string>(), std::set<std::string>())); |
+ |
+ std::set<std::string> not_empty_set; |
+ not_empty_set.insert("a"); |
+ |
+ // Only first set is empty. No common elements and no crash. |
+ EXPECT_FALSE(HaveCommonElement(std::set<std::string>(), not_empty_set)); |
+ // Now the second set is empty. |
+ EXPECT_FALSE(HaveCommonElement(not_empty_set, std::set<std::string>())); |
+} |
+ |
+TEST(AffiliationTest, HaveCommonElementNoOverlap) { |
+ std::set<std::string> set1; |
+ std::set<std::string> set2; |
+ |
+ // No common elements. |
+ set1.insert("a"); |
+ set1.insert("b"); |
+ set1.insert("c"); |
+ |
+ set2.insert("d"); |
+ set2.insert("e"); |
+ set2.insert("f"); |
+ EXPECT_FALSE(HaveCommonElement(set1, set2)); |
+} |
+ |
+TEST(AffiliationTest, HaveCommonElementFirstAndLastIsCommon) { |
+ std::set<std::string> set1; |
+ std::set<std::string> set2; |
+ |
+ // The common element is last in set1 and first in set2. |
+ set1.insert("a"); |
+ set1.insert("b"); |
+ set1.insert("c"); |
+ set1.insert("d"); // String "d" is common. |
+ |
+ set2.insert("d"); |
+ set2.insert("e"); |
+ set2.insert("f"); |
+ |
+ EXPECT_TRUE(HaveCommonElement(set1, set2)); |
+ EXPECT_TRUE(HaveCommonElement(set2, set1)); |
+} |
+ |
+TEST(AffiliationTest, HaveCommonElementCommonInTheMiddle) { |
+ std::set<std::string> set1; |
+ std::set<std::string> set2; |
+ |
+ // The common element is in the middle of the two sets. |
+ set1.insert("b"); |
+ set1.insert("f"); // String "f" is common. |
+ set1.insert("k"); |
+ |
+ set2.insert("c"); |
+ set2.insert("f"); |
+ set2.insert("j"); |
+ |
+ EXPECT_TRUE(HaveCommonElement(set1, set2)); |
+ EXPECT_TRUE(HaveCommonElement(set2, set1)); |
+} |
+ |
+TEST(AffiliationTest, Generic) { |
+ std::set<std::string> user_ids; // User affiliation IDs. |
+ std::set<std::string> device_ids; // Device affiliation IDs. |
+ |
+ EXPECT_FALSE(IsUserAffiliated(user_ids, device_ids, "", "")); |
+ |
+ EXPECT_FALSE(IsUserAffiliated(user_ids, device_ids, "user", "")); |
+ |
+ // Not valid email. |
+ EXPECT_FALSE(IsUserAffiliated(user_ids, device_ids, "user", "user")); |
+ |
+ EXPECT_FALSE(IsUserAffiliated(user_ids, device_ids, "user@notmanaged.com", |
+ "managed.com")); |
+ |
+ EXPECT_TRUE(IsUserAffiliated(user_ids, device_ids, "user@managed.com", |
+ "managed.com")); |
+ |
+ user_ids.insert("aaaa"); // Device IDs are empty -> compare email and domain. |
+ EXPECT_TRUE(IsUserAffiliated(user_ids, device_ids, "user@managed.com", |
+ "managed.com")); |
+ |
+ device_ids.insert("bbbb"); // Device and user IDs do not overlap. |
+ EXPECT_FALSE(IsUserAffiliated(user_ids, device_ids, "user@managed.com", |
+ "managed.com")); |
+ |
+ user_ids.insert("cccc"); // Device and user IDs do overlap. |
+ device_ids.insert("cccc"); |
+ EXPECT_TRUE(IsUserAffiliated(user_ids, device_ids, "user@notmanaged.com", |
+ "managed.com")); |
+} |
+ |
+} // namespace chromeos |