aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/ut/settings_ut.cpp
blob: 726beaf9284a89260a702803f6e26689b7adec00 (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
#include "common.h"

#include <library/cpp/tvmauth/client/misc/api/settings.h>

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

using namespace NTvmAuth;

Y_UNIT_TEST_SUITE(ClientSettings) {
#if !defined(_win_)
    Y_UNIT_TEST(CheckValid) {
        struct TTestCase {
            TString Name;
            NTvmApi::TClientSettings Settings;
            TString Err;
        };
        std::vector<TTestCase> cases = {
            TTestCase{
                .Name = "default",
                .Settings = {},
                .Err = "Invalid settings: nothing to do",
            },
            TTestCase{
                .Name = "only secret",
                .Settings = {
                    .Secret = TStringBuf("foobar"),
                },
                .Err = "Secret is present but destinations list is empty. It makes no sense",
            },
            TTestCase{
                .Name = "only dsts",
                .Settings = {
                    .FetchServiceTicketsForDsts = {42},
                },
                .Err = "SelfTvmId cannot be 0 if fetching of Service Tickets required",
            },
            TTestCase{
                .Name = "dsts with selfTvmId",
                .Settings = {
                    .SelfTvmId = 43,
                    .FetchServiceTicketsForDsts = {42},
                },
                .Err = "Secret is required for fetching of Service Tickets",
            },
            TTestCase{
                .Name = "correct service tickets fetching",
                .Settings = {
                    .SelfTvmId = 43,
                    .Secret = TStringBuf("foobar"),
                    .FetchServiceTicketsForDsts = {42},
                },
                .Err = "",
            },
            TTestCase{
                .Name = "only check srv flag",
                .Settings = {
                    .CheckServiceTickets = true,
                },
                .Err = "SelfTvmId cannot be 0 if checking of Service Tickets required",
            },
            TTestCase{
                .Name = "tirole without disk cache",
                .Settings = {
                    .SelfTvmId = 43,
                    .Secret = TStringBuf("foobar"),
                    .FetchRolesForIdmSystemSlug = "kek",
                },
                .Err = "Disk cache must be enabled to use roles: they can be heavy",
            },
        };

        for (const TTestCase& c : cases) {
            if (c.Err) {
                UNIT_ASSERT_EXCEPTION_CONTAINS_C(
                    c.Settings.CheckValid(),
                    TBrokenTvmClientSettings,
                    c.Err,
                    c.Name);
            } else {
                UNIT_ASSERT_NO_EXCEPTION_C(c.Settings.CheckValid(), c.Name);
            }
        }

        NTvmApi::TClientSettings s{.DiskCacheDir = "/impossible/dir"};
        UNIT_ASSERT_EXCEPTION(s.CheckValid(), TPermissionDenied);
    }

    Y_UNIT_TEST(CloneNormalized) {
        NTvmApi::TClientSettings original;
        original.FetchServiceTicketsForDsts = {43};

        UNIT_ASSERT_EXCEPTION_CONTAINS(original.CloneNormalized(),
                                       TBrokenTvmClientSettings,
                                       "SelfTvmId cannot be 0 if fetching of Service Tickets required");
        original.SelfTvmId = 15;
        original.Secret = "bar";
        original.DiskCacheDir = "./";

        NTvmApi::TClientSettings::TDstVector expected = {43};
        UNIT_ASSERT_VALUES_EQUAL(expected, original.CloneNormalized().FetchServiceTicketsForDsts);

        original.FetchServiceTicketsForDstsWithAliases = {{"foo", 42}};
        expected = {42, 43};
        UNIT_ASSERT_VALUES_EQUAL(expected, original.CloneNormalized().FetchServiceTicketsForDsts);

        original.FetchRolesForIdmSystemSlug = "kek";
        expected = {42, 43, 2028120};
        UNIT_ASSERT_VALUES_EQUAL(expected, original.CloneNormalized().FetchServiceTicketsForDsts);

        original.FetchServiceTicketsForDsts.push_back(2028120);
        expected = {42, 43, 2028120};
        UNIT_ASSERT_VALUES_EQUAL(expected, original.CloneNormalized().FetchServiceTicketsForDsts);
    }

    Y_UNIT_TEST(NeedServiceTicketsFetching) {
        NTvmApi::TClientSettings s;

        UNIT_ASSERT(!s.NeedServiceTicketsFetching());

        s.FetchServiceTicketsForDsts = {42};
        UNIT_ASSERT(s.NeedServiceTicketsFetching());
        s.FetchServiceTicketsForDsts.clear();

        s.FetchServiceTicketsForDstsWithAliases = {{"foo", 42}};
        UNIT_ASSERT(s.NeedServiceTicketsFetching());
        s.FetchServiceTicketsForDstsWithAliases.clear();

        s.FetchRolesForIdmSystemSlug = "bar";
        UNIT_ASSERT(s.NeedServiceTicketsFetching());
        s.FetchRolesForIdmSystemSlug.clear();
    }

    Y_UNIT_TEST(permitions) {
        UNIT_ASSERT_EXCEPTION(NTvmApi::TClientSettings::CheckPermissions("/qwerty"), TPermissionDenied);

        const TString tmpDir = "./cache_dir";

        NFs::RemoveRecursive(tmpDir);
        NFs::MakeDirectory(tmpDir, NFs::FP_OWNER_WRITE | NFs::FP_GROUP_WRITE | NFs::FP_ALL_WRITE);
        UNIT_ASSERT_EXCEPTION(NTvmApi::TClientSettings::CheckPermissions(tmpDir), TPermissionDenied);

        NFs::RemoveRecursive(tmpDir);
        NFs::MakeDirectory(tmpDir, NFs::FP_OWNER_READ | NFs::FP_GROUP_READ | NFs::FP_ALL_READ);
        UNIT_ASSERT_EXCEPTION(NTvmApi::TClientSettings::CheckPermissions(tmpDir), TPermissionDenied);

        NFs::RemoveRecursive(tmpDir);
        NFs::MakeDirectory(tmpDir, NFs::FP_COMMON_FILE);
        UNIT_ASSERT_NO_EXCEPTION(NTvmApi::TClientSettings::CheckPermissions(tmpDir));
    }
#endif

    Y_UNIT_TEST(Dst) {
        UNIT_ASSERT_EXCEPTION_CONTAINS(NTvmApi::TClientSettings::TDst(0), yexception, "TvmId cannot be 0");
        UNIT_ASSERT_EXCEPTION_CONTAINS(NTvmApi::TClientSettings::TDstMap({{"blackbox", 0}}),
                                       TBrokenTvmClientSettings,
                                       "TvmId cannot be 0");
    }
}