# SPDX-License-Identifier: MIT
import hashlib, sys, tempfile, unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]; sys.path.insert(0,str(ROOT/'tools'))
from check import CANONICAL_MIT_BYTES, LICENSE_SHA256, verify_license
from conform import Failure

class LicenseBindingTests(unittest.TestCase):
 def assert_license_failure(self, root, digest=LICENSE_SHA256):
  with self.assertRaises(Failure) as got: verify_license(root,digest)
  self.assertEqual(got.exception.code,'VF3-LICENSE-001')
 def test_selected_root_license_passes(self):
  self.assertEqual(verify_license(ROOT.parent,LICENSE_SHA256),LICENSE_SHA256)
  self.assertEqual(hashlib.sha256(CANONICAL_MIT_BYTES).hexdigest(),LICENSE_SHA256)
 def test_missing_license_fails_closed(self):
  with tempfile.TemporaryDirectory() as d: self.assert_license_failure(d)
 def test_modified_license_fails_closed(self):
  with tempfile.TemporaryDirectory() as d:
   Path(d,'LICENSE').write_bytes(CANONICAL_MIT_BYTES+b'\nmodified\n'); self.assert_license_failure(d)
 def test_wrong_license_fails_closed(self):
  with tempfile.TemporaryDirectory() as d:
   Path(d,'LICENSE').write_text('GNU GENERAL PUBLIC LICENSE\n'); self.assert_license_failure(d)
 def test_stale_declared_digest_fails_closed(self):
  with tempfile.TemporaryDirectory() as d:
   Path(d,'LICENSE').write_bytes(CANONICAL_MIT_BYTES); self.assert_license_failure(d,'0'*64)
if __name__=='__main__': unittest.main()
