2020-05-06 05:37:26 +03:00
|
|
|
"""
|
|
|
|
Validate HMAC-SHA256 implementation against the NIST test vectors.
|
|
|
|
|
|
|
|
The vectors are located in tests/validation/data/hmac_sha256_validation.json.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
2021-02-25 01:11:52 +03:00
|
|
|
|
2020-05-06 05:37:26 +03:00
|
|
|
import pytest
|
2021-02-25 01:11:52 +03:00
|
|
|
from google.cloud import bigquery
|
|
|
|
|
2020-10-07 01:30:59 +03:00
|
|
|
from bigquery_etl.routine.parse_routine import (
|
|
|
|
RawRoutine,
|
2021-02-25 01:11:52 +03:00
|
|
|
read_routine_dir,
|
2020-10-07 01:30:59 +03:00
|
|
|
routine_tests_sql,
|
|
|
|
)
|
2020-05-06 05:37:26 +03:00
|
|
|
|
|
|
|
validation_data_file = "tests/validation/data/hmac_sha256_validation.json"
|
|
|
|
|
|
|
|
|
|
|
|
def udfs():
|
|
|
|
"""Get all udfs and assertions."""
|
2020-10-07 03:47:15 +03:00
|
|
|
return read_routine_dir("tests/assert", "udf", "udf_js")
|
2020-05-06 05:37:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def load_data():
|
|
|
|
"""Load test data."""
|
|
|
|
with open(validation_data_file, "r") as f:
|
|
|
|
return json.load(f)["data"]
|
|
|
|
|
|
|
|
|
2020-10-07 01:27:17 +03:00
|
|
|
def generate_raw_routine(test_cases):
|
2020-05-06 05:37:26 +03:00
|
|
|
"""Generate a SQL test for each instance in hmac_sha256_validation.json."""
|
|
|
|
test_sql_fixture = (
|
|
|
|
"SELECT assert_equals("
|
|
|
|
"'{Mac}',"
|
|
|
|
"TO_HEX(SUBSTR("
|
|
|
|
"udf.hmac_sha256("
|
|
|
|
"FROM_HEX('{Key}'),"
|
|
|
|
"FROM_HEX('{Msg}')),"
|
|
|
|
"1,"
|
|
|
|
"{Tlen})));"
|
|
|
|
)
|
|
|
|
test_sql_stmnts = [test_sql_fixture.format(**test_case) for test_case in test_cases]
|
|
|
|
|
2020-11-17 21:57:48 +03:00
|
|
|
return RawRoutine.from_file(
|
|
|
|
from_text="\n".join(test_sql_stmnts),
|
|
|
|
path="udf" / "hmac_sha256" / "hmac_sha256" / "sql",
|
2020-05-06 05:37:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_sql():
|
|
|
|
"""Generate SQL statements to test."""
|
2020-10-07 01:27:17 +03:00
|
|
|
return routine_tests_sql(generate_raw_routine(load_data()), udfs())
|
2020-05-06 05:37:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("sql", generate_sql())
|
|
|
|
def test_validate_hmac_sha256(sql):
|
|
|
|
"""Validate hmac_sha256."""
|
|
|
|
job_config = bigquery.QueryJobConfig(use_legacy_sql=False)
|
|
|
|
job = bigquery.Client().query(sql, job_config=job_config)
|
|
|
|
job.result()
|