summaryrefslogtreecommitdiffstats
path: root/remoting/base/capabilities_unittest.cc
blob: 162c96d7f5c0b64de854df5d41e70b7718e38a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2013 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 <algorithm>
#include <vector>

#include "base/strings/string_util.h"
#include "remoting/base/capabilities.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

struct HasCapabilityTestData {
  const char* capabilities;
  const char* key;
  bool result;
};

struct IntersectTestData {
  const char* left;
  const char* right;
  const char* result;
};

}  // namespace

namespace remoting {

TEST(CapabilitiesTest, Empty) {
  // Expect that nothing can be found in an empty set.
  EXPECT_FALSE(HasCapability("", "a"));
  EXPECT_FALSE(HasCapability(" ", "a"));
  EXPECT_FALSE(HasCapability("  ", "a"));

  // Expect that nothing can be found in an empty set, event when the key is
  // empty.
  EXPECT_FALSE(HasCapability("", ""));
  EXPECT_FALSE(HasCapability(" ", ""));
  EXPECT_FALSE(HasCapability("  ", ""));
}

TEST(CapabilitiesTest, HasCapability) {
  HasCapabilityTestData data[] = {
    { "", "", false },
    { "a", "", false },
    { "a", "a", true },
    { "a a", "", false },
    { "a a", "a", true },
    { "a a", "z", false },
    { "a b", "", false },
    { "a b", "a", true },
    { "a b", "b", true },
    { "a b", "z", false },
    { "a b c", "", false },
    { "a b c", "a", true },
    { "a b c", "b", true },
    { "a b c", "z", false }
  };

  // Verify that HasCapability(|capabilities|, |key|) returns |result|.
  // |result|.
  for (size_t i = 0; i < arraysize(data); ++i) {
    std::vector<std::string> caps;
    Tokenize(data[i].capabilities, " ", &caps);

    do {
      EXPECT_EQ(data[i].result,
                HasCapability(JoinString(caps, " "), data[i].key));
    } while (std::next_permutation(caps.begin(), caps.end()));
  }
}

TEST(CapabilitiesTest, Intersect) {
  EXPECT_EQ(IntersectCapabilities("a", "a"), "a");

  IntersectTestData data[] = {
    { "", "", "" },
    { "a", "", "" },
    { "a", "a", "a" },
    { "a", "b", "" },
    { "a b", "", "" },
    { "a b", "a", "a" },
    { "a b", "b", "b" },
    { "a b", "z", "" },
    { "a b c", "a", "a" },
    { "a b c", "b", "b" },
    { "a b c", "a b", "a b" },
    { "a b c", "b a", "a b" },
    { "a b c", "z", "" }
  };

  // Verify that intersection of |right| with all permutations of |left| yields
  // |result|.
  for (size_t i = 0; i < arraysize(data); ++i) {
    std::vector<std::string> caps;
    Tokenize(data[i].left, " ", &caps);

    do {
      EXPECT_EQ(data[i].result,
                IntersectCapabilities(JoinString(caps, " "), data[i].right));
    } while (std::next_permutation(caps.begin(), caps.end()));
  }
}

}  // namespace remoting