File: test_token_authority.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (178 lines) | stat: -rw-r--r-- 5,244 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mir/glib_main_loop.h"
#include "mir/main_loop.h"
#include "mir/shell/token_authority.h"
#include "mir/test/auto_unblock_thread.h"
#include "mir/test/spin_wait.h"
#include "mir/time/steady_clock.h"
#include <algorithm>
#include <chrono>
#include <gtest/gtest.h>
#include <iterator>
#include <memory>
#include <string>
#include <unistd.h>
#include <uuid.h>

struct TestTokenAuthority : testing::Test
{
    TestTokenAuthority() :
        clock{std::make_shared<mir::time::SteadyClock>()},
        main_loop{std::make_shared<mir::GLibMainLoop>(clock)},
        token_authority{std::shared_ptr<mir::MainLoop>(main_loop)},
        main_loop_thread{
            [this]()
            {
                main_loop->stop();
            },
            [this]()
            {
                main_loop->run();
            }}
    {
    }

    std::shared_ptr<mir::time::SteadyClock> const clock;
    std::shared_ptr<mir::MainLoop> const main_loop;
    mir::shell::TokenAuthority token_authority;
    mir::test::AutoUnblockThread main_loop_thread;
};

TEST_F(TestTokenAuthority, can_issue_tokens)
{
    auto const token = token_authority.issue_token({});

    // Make sure the generated token is a valid UUID
    uuid_t uuid;
    auto const token_string = std::string(token);
    auto ret = uuid_parse(token_string.c_str(), uuid);
    ASSERT_EQ(ret, 0);
}

TEST_F(TestTokenAuthority, tokens_are_revoked_in_time)
{
    using namespace std::chrono;

    auto constexpr revocation_time = milliseconds(3000);
    auto constexpr epsilon = milliseconds(50);

    bool revoked = false;
    auto const issuance_time = clock->now();

    auto const token = token_authority.issue_token(
        [&revoked, this, issuance_time, revocation_time, epsilon](auto&)
        {
            revoked = true;
            auto const delta_ms = duration_cast<milliseconds>(clock->now() - issuance_time);

            // The revocation time should be about 3 seconds, give or take a few ms
            ASSERT_LT(delta_ms - revocation_time, epsilon);
        });

    mir::test::spin_wait_for_condition_or_timeout(
        [&revoked]
        {
            return revoked;
        },
        revocation_time + epsilon);

    auto empty_token_opt = token_authority.get_token_for_string(std::string(token));

    // Test if the token authority has actually removed the token
    ASSERT_FALSE(empty_token_opt.has_value());
}

TEST_F(TestTokenAuthority, manual_revocation_works_correctly)
{
    bool revoked = false;
    auto const token = token_authority.issue_token(
        [&revoked](auto&)
        {
            revoked = true;
        });

    ASSERT_FALSE(revoked);

    token_authority.revoke_token(token);

    auto empty_token_opt = token_authority.get_token_for_string(std::string(token));
    ASSERT_FALSE(empty_token_opt.has_value());
    ASSERT_TRUE(revoked);
}

TEST_F(TestTokenAuthority, manual_revocation_doesnt_revoke_other_tokens)
{
    bool revoked1 = false, revoked2 = false;
    auto const token1 = token_authority.issue_token(
        [&revoked1](auto&)
        {
            revoked1 = true;
        });
    auto const token2 = token_authority.issue_token(
        [&revoked2](auto&)
        {
            revoked2 = true;
        });

    ASSERT_FALSE(revoked1);
    ASSERT_FALSE(revoked2);

    token_authority.revoke_token(token1);

    auto empty_token_opt = token_authority.get_token_for_string(std::string(token1));
    ASSERT_FALSE(empty_token_opt.has_value());
    ASSERT_TRUE(revoked1);
    ASSERT_FALSE(revoked2);
}

TEST_F(TestTokenAuthority, revoke_all_actually_revokes_all)
{
    std::vector<mir::shell::TokenAuthority::Token> tokens;
    std::ranges::generate_n(
        std::back_inserter(tokens),
        10,
        [this]()
        {
            return token_authority.issue_token({});
        });

    auto const token_valid = [this](auto const& token)
    {
        return token_authority.get_token_for_string(std::string(token)).has_value();
    };

    ASSERT_TRUE(std::ranges::all_of(tokens, token_valid));

    token_authority.revoke_all_tokens();

    ASSERT_TRUE(std::ranges::none_of(tokens, token_valid));
}

TEST_F(TestTokenAuthority, bogus_token_is_actually_bogus)
{
    auto const bogus_token = token_authority.get_bogus_token();

    // Make sure the generated bogus token is a valid UUID
    uuid_t uuid;
    auto const bogus_string = std::string(bogus_token);
    auto ret = uuid_parse(bogus_string.c_str(), uuid);
    ASSERT_EQ(ret, 0);

    // Mustn't be registered in the token authority as a genuine token
    ASSERT_FALSE(token_authority.get_token_for_string(bogus_string).has_value());
}