1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import os
1615import pytest
1716import requests
1817from google import showcase
1918
19+
2020@pytest .fixture
2121def run_pqc_test (use_mtls ):
2222 if not use_mtls :
2323 pytest .skip ("PQC integration test requires mTLS (--mtls flag) to be enabled." )
2424
25+
2526@pytest .mark .parametrize (
2627 "transport_fixture" ,
2728 ["intercepted_echo_grpc" , "intercepted_echo_rest" ]
2829)
2930def test_pqc_negotiated_group (run_pqc_test , request , transport_fixture ):
30- """Verifies that the generated client library negotiates PQC with the Showcase server."""
31+ """Verifies that the generated client library negotiates PQC (X25519MLKEM768) with Showcase server."""
3132 client , interceptor = request .getfixturevalue (transport_fixture )
32-
33- # Make secure call using the standard client library fixture
33+
34+ # Make secure call using standard GAPIC client library fixture
3435 response = client .echo (request = showcase .EchoRequest (content = "Verify PQC connection." ))
3536 assert response .content == "Verify PQC connection."
36-
37+
3738 # Extract negotiated group and supported groups from response headers
3839 negotiated_group = None
3940 supported_groups = None
@@ -42,59 +43,56 @@ def test_pqc_negotiated_group(run_pqc_test, request, transport_fixture):
4243 negotiated_group = value
4344 elif key .lower () == "x-showcase-tls-client-supported-groups" :
4445 supported_groups = value
45-
46+
4647 assert negotiated_group is not None , "Failed: Showcase server did not return negotiated TLS group header."
4748 assert supported_groups is not None , "Failed: Showcase server did not return client advertised supported groups."
48-
49+
4950 print (f"\n [PQC Verification] ({ transport_fixture } ) Negotiated TLS Group: { negotiated_group } " )
5051 print (f"[PQC Verification] ({ transport_fixture } ) Client Advertised Supported Groups: { supported_groups } " )
51-
52- # Enforce PQC compliance (this will fail if not using MLKEM/ Kyber)
52+
53+ # Enforce PQC compliance (X25519MLKEM768 or Kyber)
5354 assert "MLKEM" in negotiated_group or "Kyber" in negotiated_group , \
5455 f"Failed: { transport_fixture } Connection is NOT PQC-compliant! Negotiated: { negotiated_group } "
5556
5657
5758def test_google_auth_transport_pqc (run_pqc_test ):
58- """Verifies that the google-auth HTTP transport adapter negotiates PQC with the Showcase server."""
59+ """Verifies that google-auth transport adapter negotiates PQC (X25519MLKEM768) with Showcase server."""
5960 import google .auth .transport .requests
60- from google .protobuf .json_format import MessageToJson
6161 from conftest import HostNameIgnoringAdapter
6262
63- # 1. Initialize a standard requests Session with mTLS certs
63+ # 1. Initialize requests Session with mTLS certs
6464 session = requests .Session ()
6565 cert_path = "/usr/local/google/home/omairn/git/googleapis/google-cloud-python-dev2/packages/gapic-generator/tests/cert/mtls.crt"
6666 key_path = "/usr/local/google/home/omairn/git/googleapis/google-cloud-python-dev2/packages/gapic-generator/tests/cert/mtls.key"
6767
6868 session .verify = cert_path
6969 session .cert = (cert_path , key_path )
70-
71- # Bypass localhost hostname mismatch
7270 session .mount ("https://" , HostNameIgnoringAdapter ())
7371
74- # 2. Wrap it in google-auth's Transport Request adapter
72+ # 2. Wrap session in google-auth transport adapter
7573 auth_transport = google .auth .transport .requests .Request (session = session )
7674
77- # 3. Serialize the request body using the official protobuf JSON serializer
75+ # 3. Serialize request body
7876 req = showcase .EchoRequest (content = "Verify google-auth transport PQC connection." )
79- body = MessageToJson (req , including_default_value_fields = True ).encode ("utf-8" )
77+ body = showcase . EchoRequest . to_json (req , including_default_value_fields = False ).encode ("utf-8" )
8078
81- # 4. Make secure call using the google-auth transport adapter
79+ # 4. Execute request through google-auth's transport layer
8280 url = "https://localhost:7469/v1beta1/echo:echo"
83- method = "POST"
84- headers = {"Content-Type" : "application/json" }
81+ headers = {
82+ "Content-Type" : "application/json" ,
83+ "x-goog-api-client" : "gapic/1.0 rest/1.0" ,
84+ }
8585
86- # Execute the request through google-auth's transport layer
87- response = auth_transport (url = url , method = method , body = body , headers = headers )
88- assert response .status == 200
86+ response = auth_transport (url = url , method = "POST" , body = body , headers = headers )
87+ assert response .status == 200 , f"Failed: status={ response .status } , body={ response .data } "
8988
90- # 5. Extract TLS group from response headers returned by Showcase
89+ # 5. Extract and verify negotiated TLS group returned by Showcase
9190 negotiated_group = response .headers .get ("x-showcase-tls-group" )
9291 supported_groups = response .headers .get ("x-showcase-tls-client-supported-groups" )
9392
9493 assert negotiated_group is not None , "Failed: Showcase server did not return negotiated TLS group header."
9594 print (f"\n [google-auth Transport PQC] Negotiated TLS Group: { negotiated_group } " )
9695 print (f"[google-auth Transport PQC] Client Advertised Supported Groups: { supported_groups } " )
9796
98- # Assert PQC compliance
9997 assert "MLKEM" in negotiated_group or "Kyber" in negotiated_group , \
10098 f"Failed: google-auth Transport is NOT PQC-compliant! Negotiated: { negotiated_group } "
0 commit comments