aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/ut/checker_ut.cpp
blob: 73f2b0dfdae52df99158812bd88bf0d41fb29f26 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
#include "common.h"

#include <library/cpp/tvmauth/client/mocked_updater.h>
#include <library/cpp/tvmauth/client/misc/checker.h>
#include <library/cpp/tvmauth/client/misc/getter.h>
#include <library/cpp/tvmauth/client/misc/api/threaded_updater.h>

#include <library/cpp/tvmauth/type.h>

#include <library/cpp/testing/unittest/registar.h>

using namespace NTvmAuth;

Y_UNIT_TEST_SUITE(ClientChecker) {
    static const TTvmId OK_CLIENT = 100500;
    static const TString PROD_TICKET = "3:user:CAsQ__________9_Gg4KAgh7EHsg0oXYzAQoAA:N8PvrDNLh-5JywinxJntLeQGDEHBUxfzjuvB8-_BEUv1x9CALU7do8irDlDYVeVVDr4AIpR087YPZVzWPAqmnBuRJS0tJXekmDDvrivLnbRrzY4IUXZ_fImB0fJhTyVetKv6RD11bGqnAJeDpIukBwPTbJc_EMvKDt8V490CJFw";
    static const TString TEST_TICKET = "3:user:CA0Q__________9_Gg4KAgh7EHsg0oXYzAQoAQ:FSADps3wNGm92Vyb1E9IVq5M6ZygdGdt1vafWWEhfDDeCLoVA-sJesxMl2pGW4OxJ8J1r_MfpG3ZoBk8rLVMHUFrPa6HheTbeXFAWl8quEniauXvKQe4VyrpA1SPgtRoFqi5upSDIJzEAe1YRJjq1EClQ_slMt8R0kA_JjKUX54";
    static const TString PROD_YATEAM_TICKET = "3:user:CAwQ__________9_Gg4KAgh7EHsg0oXYzAQoAg:M9dEFEWHLHXiL7brCsyfYlm254PE6VeshUjI62u2qMDRzt6-0jAoJTIdDiogerItht1YFYSn8fSqmMf23_rueGj-wkmvyNzbcBSk3jtK2U5sai_W0bK6OwukR9tzWzi1Gcgg9DrNEeIKFvs1EBqYCF4mPHWo5bgk0CR580Cgit4";
    static const TString TEST_YATEAM_TICKET = "3:user:CA4Q__________9_Gg4KAgh7EHsg0oXYzAQoAw:IlaV3htk3jYrviIOz3k3Dfwz7p-bYYpbrgdn53GiUrMGdrT9eobHeuzNvPLrWB0yuYZAD46C3MGxok4GGmHhT73mki4XOCX8yWT4jW_hzcHBik1442tjWwh8IWqV_7q5j5496suVuLWjnZORWbb7I-2iwdIlU1BUiDfhoAolCq8";
    static const TString STRESS_TICKET = "3:user:CA8Q__________9_Gg4KAgh7EHsg0oXYzAQoBA:GBuG_TLo6SL2OYFxp7Zly04HPNzmAF7Fu2E8E9SnwQDoxq9rf7VThSPtTmnBSAl5UVRRPkMsRtzzHZ87qtj6l-PvF0K7PrDu7-yS_xiFTgAl9sEfXAIHJVzZLoksGRgpoBtpBUg9vVaJsPns0kWFKJgq8M-Mk9agrSk7sb2VUeQ";
    static const TString SRV_TICKET = "3:serv:CBAQ__________9_IgYIexCUkQY:GioCM49Ob6_f80y6FY0XBVN4hLXuMlFeyMvIMiDuQnZkbkLpRpQOuQo5YjWoBjM0Vf-XqOm8B7xtrvxSYHDD7Q4OatN2l-Iwg7i71lE3scUeD36x47st3nd0OThvtjrFx_D8mw_c0GT5KcniZlqq1SjhLyAk1b_zJsx8viRAhCU";

    Y_UNIT_TEST(User) {
        NTvmApi::TClientSettings s{
            .DiskCacheDir = GetCachePath(),
            .SelfTvmId = OK_CLIENT,
            .CheckServiceTickets = true,
        };

        auto l = MakeIntrusive<TLogger>();
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            UNIT_ASSERT_EXCEPTION(TUserTicketChecker::Check("kek", u->GetCachedUserContext()), TBrokenTvmClientSettings);
        }
        UNIT_ASSERT_C(l->Stream.Str().find("was successfully fetched") == TString::npos, l->Stream.Str());

        s.CheckUserTicketsWithBbEnv = EBlackboxEnv::Prod;
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedUserContext({});
            UNIT_ASSERT(TUserTicketChecker::Check(PROD_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(STRESS_TICKET, c));

            c = u->GetCachedUserContext(EBlackboxEnv::ProdYateam);
            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_TICKET, c));
            UNIT_ASSERT(TUserTicketChecker::Check(PROD_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(STRESS_TICKET, c));

            UNIT_ASSERT_EXCEPTION(TUserTicketChecker::Check(PROD_TICKET, u->GetCachedUserContext(EBlackboxEnv::Stress)), TBrokenTvmClientSettings);
        }

        s.CheckUserTicketsWithBbEnv = EBlackboxEnv::Test;
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedUserContext();

            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_TICKET, c));
            UNIT_ASSERT(TUserTicketChecker::Check(TEST_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(STRESS_TICKET, c));
        }

        s.CheckUserTicketsWithBbEnv = EBlackboxEnv::ProdYateam;
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedUserContext();

            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_TICKET, c));
            UNIT_ASSERT(TUserTicketChecker::Check(PROD_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(STRESS_TICKET, c));
        }

        s.CheckUserTicketsWithBbEnv = EBlackboxEnv::TestYateam;
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedUserContext();

            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_YATEAM_TICKET, c));
            UNIT_ASSERT(TUserTicketChecker::Check(TEST_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(STRESS_TICKET, c));
        }

        s.CheckUserTicketsWithBbEnv = EBlackboxEnv::Stress;
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedUserContext();

            UNIT_ASSERT(TUserTicketChecker::Check(PROD_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(PROD_YATEAM_TICKET, c));
            UNIT_ASSERT(!TUserTicketChecker::Check(TEST_YATEAM_TICKET, c));
            UNIT_ASSERT(TUserTicketChecker::Check(STRESS_TICKET, c));
        }
    }

    Y_UNIT_TEST(Service) {
        NTvmApi::TClientSettings s{
            .DiskCacheDir = GetCachePath(),
            .SelfTvmId = OK_CLIENT,
            .CheckUserTicketsWithBbEnv = EBlackboxEnv::Stress,
        };

        auto l = MakeIntrusive<TLogger>();
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            UNIT_ASSERT_EXCEPTION(TServiceTicketChecker::Check(SRV_TICKET, u->GetCachedServiceContext()), TBrokenTvmClientSettings);
        }
        UNIT_ASSERT_C(l->Stream.Str().find("was successfully fetched") == TString::npos, l->Stream.Str());

        s.CheckServiceTickets = true;
        l = MakeIntrusive<TLogger>();
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedServiceContext();

            UNIT_ASSERT(TServiceTicketChecker::Check(SRV_TICKET, c));
            UNIT_ASSERT(!TServiceTicketChecker::Check(PROD_TICKET, c));
        }
        UNIT_ASSERT_C(l->Stream.Str().find("was successfully fetched") == TString::npos, l->Stream.Str());

        s.SelfTvmId = 17;
        l = MakeIntrusive<TLogger>();
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());

            auto c = u->GetCachedServiceContext();

            UNIT_ASSERT(!TServiceTicketChecker::Check(SRV_TICKET, c));
            UNIT_ASSERT(!TServiceTicketChecker::Check(PROD_TICKET, c));
        }
        UNIT_ASSERT_C(l->Stream.Str().find("was successfully fetched") == TString::npos, l->Stream.Str());
    }

    Y_UNIT_TEST(Tickets) {
        NTvmApi::TClientSettings s{
            .DiskCacheDir = GetCachePath(),
            .SelfTvmId = OK_CLIENT,
            .Secret = (TStringBuf) "qwerty",
            .FetchServiceTicketsForDstsWithAliases = {{"blackbox", 19}},
        };

        auto l = MakeIntrusive<TLogger>();
        {
            auto u = NTvmApi::TThreadedUpdater::Create(s, l);
            UNIT_ASSERT_VALUES_EQUAL(TClientStatus::Ok, u->GetStatus().GetCode());
            auto c = u->GetCachedServiceTickets();
            UNIT_ASSERT_VALUES_EQUAL("3:serv:CBAQ__________9_IgYIKhCUkQY:CX", TServiceTicketGetter::GetTicket("blackbox", c));
            UNIT_ASSERT_EXCEPTION_CONTAINS(TServiceTicketGetter::GetTicket("blackbox2", c),
                                           TBrokenTvmClientSettings,
                                           "Destination 'blackbox2' was not specified in settings. Check your settings (if you use Qloud/YP/tvmtool - check it's settings)");
        }
        UNIT_ASSERT_C(l->Stream.Str().find("was successfully fetched") == TString::npos, l->Stream.Str());
    }

    Y_UNIT_TEST(ErrorForDst) {
        TAsyncUpdaterPtr u = new TMockedUpdater();
        auto c = u->GetCachedServiceTickets();

        UNIT_ASSERT_VALUES_EQUAL(TMockedUpdater::TSettings::CreateDeafult().Backends.at(0).Value,
                                 TServiceTicketGetter::GetTicket("my_dest", c));
        UNIT_ASSERT_VALUES_EQUAL(TMockedUpdater::TSettings::CreateDeafult().Backends.at(0).Value,
                                 TServiceTicketGetter::GetTicket(42, c));
        UNIT_ASSERT_EXCEPTION_CONTAINS(TServiceTicketGetter::GetTicket("my_bad_dest", c),
                                       TMissingServiceTicket,
                                       "Failed to get ticket for 'my_bad_dest': Dst is not found");
        UNIT_ASSERT_EXCEPTION_CONTAINS(TServiceTicketGetter::GetTicket(43, c),
                                       TMissingServiceTicket,
                                       "Failed to get ticket for '43': Dst is not found");
    }
}