Home | History | Annotate | Download | only in base
      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 <algorithm>
      6 #include <vector>
      7 
      8 #include "base/strings/string_util.h"
      9 #include "remoting/base/capabilities.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace {
     13 
     14 struct HasCapabilityTestData {
     15   const char* capabilities;
     16   const char* key;
     17   bool result;
     18 };
     19 
     20 struct IntersectTestData {
     21   const char* left;
     22   const char* right;
     23   const char* result;
     24 };
     25 
     26 }  // namespace
     27 
     28 namespace remoting {
     29 
     30 TEST(CapabilitiesTest, Empty) {
     31   // Expect that nothing can be found in an empty set.
     32   EXPECT_FALSE(HasCapability("", "a"));
     33   EXPECT_FALSE(HasCapability(" ", "a"));
     34   EXPECT_FALSE(HasCapability("  ", "a"));
     35 
     36   // Expect that nothing can be found in an empty set, event when the key is
     37   // empty.
     38   EXPECT_FALSE(HasCapability("", ""));
     39   EXPECT_FALSE(HasCapability(" ", ""));
     40   EXPECT_FALSE(HasCapability("  ", ""));
     41 }
     42 
     43 TEST(CapabilitiesTest, HasCapability) {
     44   HasCapabilityTestData data[] = {
     45     { "", "", false },
     46     { "a", "", false },
     47     { "a", "a", true },
     48     { "a a", "", false },
     49     { "a a", "a", true },
     50     { "a a", "z", false },
     51     { "a b", "", false },
     52     { "a b", "a", true },
     53     { "a b", "b", true },
     54     { "a b", "z", false },
     55     { "a b c", "", false },
     56     { "a b c", "a", true },
     57     { "a b c", "b", true },
     58     { "a b c", "z", false }
     59   };
     60 
     61   // Verify that HasCapability(|capabilities|, |key|) returns |result|.
     62   // |result|.
     63   for (size_t i = 0; i < arraysize(data); ++i) {
     64     std::vector<std::string> caps;
     65     Tokenize(data[i].capabilities, " ", &caps);
     66 
     67     do {
     68       EXPECT_EQ(HasCapability(JoinString(caps, " "), data[i].key),
     69                 data[i].result);
     70     } while (std::next_permutation(caps.begin(), caps.end()));
     71   }
     72 }
     73 
     74 TEST(CapabilitiesTest, Intersect) {
     75   EXPECT_EQ(IntersectCapabilities("a", "a"), "a");
     76 
     77   IntersectTestData data[] = {
     78     { "", "", "" },
     79     { "a", "", "" },
     80     { "a", "a", "a" },
     81     { "a", "b", "" },
     82     { "a b", "", "" },
     83     { "a b", "a", "a" },
     84     { "a b", "b", "b" },
     85     { "a b", "z", "" },
     86     { "a b c", "a", "a" },
     87     { "a b c", "b", "b" },
     88     { "a b c", "a b", "a b" },
     89     { "a b c", "b a", "a b" },
     90     { "a b c", "z", "" }
     91   };
     92 
     93   // Verify that intersection of |right| with all permutations of |left| yields
     94   // |result|.
     95   for (size_t i = 0; i < arraysize(data); ++i) {
     96     std::vector<std::string> caps;
     97     Tokenize(data[i].left, " ", &caps);
     98 
     99     do {
    100       EXPECT_EQ(IntersectCapabilities(JoinString(caps, " "), data[i].right),
    101                 data[i].result);
    102     } while (std::next_permutation(caps.begin(), caps.end()));
    103   }
    104 }
    105 
    106 }  // namespace remoting
    107