xref: /aosp_15_r20/external/grpc-grpc/src/python/grpcio_tests/tests/unit/_credentials_test.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests of credentials."""
15
16import logging
17import unittest
18
19import grpc
20
21
22class CredentialsTest(unittest.TestCase):
23    def test_call_credentials_composition(self):
24        first = grpc.access_token_call_credentials("abc")
25        second = grpc.access_token_call_credentials("def")
26        third = grpc.access_token_call_credentials("ghi")
27
28        first_and_second = grpc.composite_call_credentials(first, second)
29        first_second_and_third = grpc.composite_call_credentials(
30            first, second, third
31        )
32
33        self.assertIsInstance(first_and_second, grpc.CallCredentials)
34        self.assertIsInstance(first_second_and_third, grpc.CallCredentials)
35
36    def test_channel_credentials_composition(self):
37        first_call_credentials = grpc.access_token_call_credentials("abc")
38        second_call_credentials = grpc.access_token_call_credentials("def")
39        third_call_credentials = grpc.access_token_call_credentials("ghi")
40        channel_credentials = grpc.ssl_channel_credentials()
41
42        channel_and_first = grpc.composite_channel_credentials(
43            channel_credentials, first_call_credentials
44        )
45        channel_first_and_second = grpc.composite_channel_credentials(
46            channel_credentials, first_call_credentials, second_call_credentials
47        )
48        channel_first_second_and_third = grpc.composite_channel_credentials(
49            channel_credentials,
50            first_call_credentials,
51            second_call_credentials,
52            third_call_credentials,
53        )
54
55        self.assertIsInstance(channel_and_first, grpc.ChannelCredentials)
56        self.assertIsInstance(channel_first_and_second, grpc.ChannelCredentials)
57        self.assertIsInstance(
58            channel_first_second_and_third, grpc.ChannelCredentials
59        )
60
61    def test_invalid_string_certificate(self):
62        self.assertRaises(
63            TypeError,
64            grpc.ssl_channel_credentials,
65            root_certificates="A Certificate",
66            private_key=None,
67            certificate_chain=None,
68        )
69
70
71if __name__ == "__main__":
72    logging.basicConfig()
73    unittest.main(verbosity=2)
74