~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to tests/unit-tests/test_mir_cookie.cpp

  • Committer: Package Import Robot
  • Author(s): Alexandros Frantzis
  • Date: 2015-10-08 16:12:19 UTC
  • mto: This revision was merged to the branch mainline in revision 109.
  • Revision ID: package-import@ubuntu.com-20151008161219-emk4a1ys51yy0wjb
Tags: upstream-0.17.0+15.10.20151008.2
Import upstream version 0.17.0+15.10.20151008.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir_toolkit/mir_client_library.h"
 
20
#include "mir/cookie_factory.h"
 
21
 
 
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"
 
28
 
 
29
#include "boost/throw_exception.hpp"
 
30
 
 
31
#include <gtest/gtest.h>
 
32
 
 
33
namespace mtf = mir_test_framework;
 
34
namespace mtd = mir::test::doubles;
 
35
namespace msh = mir::shell;
 
36
 
 
37
TEST(MirCookieFactory, attests_real_timestamp)
 
38
{
 
39
    std::vector<uint8_t> secret{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xde, 0x01 };
 
40
    auto factory = mir::cookie::CookieFactory::create_from_secret(secret);
 
41
 
 
42
    uint64_t mock_timestamp{0x322322322332};
 
43
 
 
44
    auto cookie = factory->timestamp_to_cookie(mock_timestamp);
 
45
 
 
46
    EXPECT_TRUE(factory->attest_timestamp(cookie));
 
47
}
 
48
 
 
49
TEST(MirCookieFactory, doesnt_attest_faked_timestamp)
 
50
{
 
51
    std::vector<uint8_t> secret{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xde, 0x01 };
 
52
    auto factory = mir::cookie::CookieFactory::create_from_secret(secret);
 
53
 
 
54
    MirCookie bad_client_no_biscuit{ 0x33221100, 0x33221100 };
 
55
 
 
56
    EXPECT_FALSE(factory->attest_timestamp(bad_client_no_biscuit));
 
57
}
 
58
 
 
59
TEST(MirCookieFactory, timestamp_trusted_with_different_secret_doesnt_attest)
 
60
{
 
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 };
 
63
 
 
64
    auto alices_factory = mir::cookie::CookieFactory::create_from_secret(alice);
 
65
    auto bobs_factory   = mir::cookie::CookieFactory::create_from_secret(bob);
 
66
 
 
67
    uint64_t mock_timestamp{0x01020304};
 
68
 
 
69
    auto alices_cookie = alices_factory->timestamp_to_cookie(mock_timestamp);
 
70
    auto bobs_cookie = bobs_factory->timestamp_to_cookie(mock_timestamp);
 
71
 
 
72
    EXPECT_FALSE(alices_factory->attest_timestamp(bobs_cookie));
 
73
    EXPECT_FALSE(bobs_factory->attest_timestamp(alices_cookie));
 
74
}
 
75
 
 
76
TEST(MirCookieFactory, throw_when_secret_size_to_small)
 
77
{
 
78
    std::vector<uint8_t> bob(mir::cookie::CookieFactory::minimum_secret_size - 1);
 
79
    EXPECT_THROW({
 
80
        auto factory = mir::cookie::CookieFactory::create_from_secret(bob);
 
81
    }, std::logic_error);
 
82
}
 
83
 
 
84
TEST(MirCookieFactory, saves_a_secret)
 
85
{
 
86
    using namespace testing;
 
87
    std::vector<uint8_t> secret;
 
88
 
 
89
    mir::cookie::CookieFactory::create_saving_secret(secret);
 
90
 
 
91
    EXPECT_THAT(secret.size(), Ge(mir::cookie::CookieFactory::minimum_secret_size));
 
92
}
 
93
 
 
94
TEST(MirCookieFactory, timestamp_trusted_with_saved_secret_does_attest)
 
95
{
 
96
    uint64_t timestamp   = 23;
 
97
    std::vector<uint8_t> secret;
 
98
 
 
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);
 
102
 
 
103
    EXPECT_TRUE(sink_factory->attest_timestamp(cookie));
 
104
}
 
105
 
 
106
TEST(MirCookieFactory, internally_generated_secret_has_optimum_size)
 
107
{
 
108
    using namespace testing;
 
109
    std::vector<uint8_t> secret;
 
110
 
 
111
    mir::cookie::CookieFactory::create_saving_secret(secret);
 
112
 
 
113
    EXPECT_THAT(secret.size(), Eq(mir::cookie::CookieFactory::optimal_secret_size()));
 
114
}
 
115
 
 
116
TEST(MirCookieFactory, optimal_secret_size_is_larger_than_minimum_size)
 
117
{
 
118
    using namespace testing;
 
119
 
 
120
    EXPECT_THAT(mir::cookie::CookieFactory::optimal_secret_size(),
 
121
        Ge(mir::cookie::CookieFactory::minimum_secret_size));
 
122
}