2
* Copyright © 2015 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
19
#include "mir_toolkit/mir_client_library.h"
20
#include "mir/cookie_factory.h"
22
#include "mir_test_framework/headless_test.h"
23
#include "mir_test_framework/connected_client_with_a_surface.h"
24
#include "mir/test/doubles/wrap_shell_to_track_latest_surface.h"
25
#include "mir/shell/shell_wrapper.h"
26
#include "mir/test/validity_matchers.h"
27
#include "mir/test/wait_condition.h"
29
#include "boost/throw_exception.hpp"
31
#include <gtest/gtest.h>
33
namespace mtf = mir_test_framework;
34
namespace mtd = mir::test::doubles;
35
namespace msh = mir::shell;
37
TEST(MirCookieFactory, attests_real_timestamp)
39
std::vector<uint8_t> secret{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xde, 0x01 };
40
auto factory = mir::cookie::CookieFactory::create_from_secret(secret);
42
uint64_t mock_timestamp{0x322322322332};
44
auto cookie = factory->timestamp_to_cookie(mock_timestamp);
46
EXPECT_TRUE(factory->attest_timestamp(cookie));
49
TEST(MirCookieFactory, doesnt_attest_faked_timestamp)
51
std::vector<uint8_t> secret{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xde, 0x01 };
52
auto factory = mir::cookie::CookieFactory::create_from_secret(secret);
54
MirCookie bad_client_no_biscuit{ 0x33221100, 0x33221100 };
56
EXPECT_FALSE(factory->attest_timestamp(bad_client_no_biscuit));
59
TEST(MirCookieFactory, timestamp_trusted_with_different_secret_doesnt_attest)
61
std::vector<uint8_t> alice{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xde, 0x01 };
62
std::vector<uint8_t> bob{ 0x01, 0x02, 0x44, 0xd8, 0xee, 0x0f, 0xde, 0x01 };
64
auto alices_factory = mir::cookie::CookieFactory::create_from_secret(alice);
65
auto bobs_factory = mir::cookie::CookieFactory::create_from_secret(bob);
67
uint64_t mock_timestamp{0x01020304};
69
auto alices_cookie = alices_factory->timestamp_to_cookie(mock_timestamp);
70
auto bobs_cookie = bobs_factory->timestamp_to_cookie(mock_timestamp);
72
EXPECT_FALSE(alices_factory->attest_timestamp(bobs_cookie));
73
EXPECT_FALSE(bobs_factory->attest_timestamp(alices_cookie));
76
TEST(MirCookieFactory, throw_when_secret_size_to_small)
78
std::vector<uint8_t> bob(mir::cookie::CookieFactory::minimum_secret_size - 1);
80
auto factory = mir::cookie::CookieFactory::create_from_secret(bob);
84
TEST(MirCookieFactory, saves_a_secret)
86
using namespace testing;
87
std::vector<uint8_t> secret;
89
mir::cookie::CookieFactory::create_saving_secret(secret);
91
EXPECT_THAT(secret.size(), Ge(mir::cookie::CookieFactory::minimum_secret_size));
94
TEST(MirCookieFactory, timestamp_trusted_with_saved_secret_does_attest)
96
uint64_t timestamp = 23;
97
std::vector<uint8_t> secret;
99
auto source_factory = mir::cookie::CookieFactory::create_saving_secret(secret);
100
auto sink_factory = mir::cookie::CookieFactory::create_from_secret(secret);
101
auto cookie = source_factory->timestamp_to_cookie(timestamp);
103
EXPECT_TRUE(sink_factory->attest_timestamp(cookie));
106
TEST(MirCookieFactory, internally_generated_secret_has_optimum_size)
108
using namespace testing;
109
std::vector<uint8_t> secret;
111
mir::cookie::CookieFactory::create_saving_secret(secret);
113
EXPECT_THAT(secret.size(), Eq(mir::cookie::CookieFactory::optimal_secret_size()));
116
TEST(MirCookieFactory, optimal_secret_size_is_larger_than_minimum_size)
118
using namespace testing;
120
EXPECT_THAT(mir::cookie::CookieFactory::optimal_secret_size(),
121
Ge(mir::cookie::CookieFactory::minimum_secret_size));