|
| 1 | +module Test.Registry.SRIHash (spec) where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Monad.Except (ExceptT(..)) |
| 6 | +import Control.Monad.Except as Except |
| 7 | +import Data.Either (Either(..)) |
| 8 | +import Data.Maybe (Maybe(..)) |
| 9 | +import Data.String as String |
| 10 | +import Effect.Aff (Aff) |
| 11 | +import Node.ChildProcess as Process |
| 12 | +import Node.Path (FilePath) |
| 13 | +import Node.Path as Path |
| 14 | +import Registry.SRIHash as SRIHash |
| 15 | +import Sunde as Sunde |
| 16 | +import Test.Assert as Assert |
| 17 | +import Test.Spec as Spec |
| 18 | +import Test.Utils as Utils |
| 19 | + |
| 20 | +spec :: Spec.Spec Unit |
| 21 | +spec = do |
| 22 | + Spec.describe "Creates a valid sha256 SRI hash" do |
| 23 | + Spec.it "Hashes a spago.dhall file the same as openssl" do |
| 24 | + hash <- SRIHash.hashFile hooksSpago |
| 25 | + hash `Assert.shouldEqual` hooksSpagoHash |
| 26 | + |
| 27 | + Spec.it "Hashes a LICENSE file the same as openssl" do |
| 28 | + hash <- SRIHash.hashFile hooksLicense |
| 29 | + hash `Assert.shouldEqual` hooksLicenseHash |
| 30 | + |
| 31 | + Spec.it "spago.dhall hash matches the nix hash" do |
| 32 | + hash <- SRIHash.hashFile hooksSpago |
| 33 | + nix <- Except.runExceptT $ sha256Nix hooksSpago |
| 34 | + hash `Assert.shouldEqualRight` nix |
| 35 | + |
| 36 | + Spec.it "LICENSE hash matches the nix hash" do |
| 37 | + hash <- SRIHash.hashFile hooksLicense |
| 38 | + nix <- Except.runExceptT $ sha256Nix hooksLicense |
| 39 | + hash `Assert.shouldEqualRight` nix |
| 40 | + |
| 41 | + Spec.describe "Encodes and decodes from string" do |
| 42 | + Spec.it "Round-trips spago.dhall file" do |
| 43 | + hooksSpagoHash `Assert.shouldEqualRight` SRIHash.parse (SRIHash.print hooksSpagoHash) |
| 44 | + |
| 45 | + Spec.it "Round-trips LICENSE file" do |
| 46 | + hooksLicenseHash `Assert.shouldEqualRight` SRIHash.parse (SRIHash.print hooksLicenseHash) |
| 47 | + |
| 48 | +-- Test hash produced by `openssl`: |
| 49 | +-- openssl dgst -sha256 -binary < test/_fixtures/halogen-hooks/spago.dhall | openssl base64 -A |
| 50 | +hooksSpagoHash :: SRIHash.SRIHash |
| 51 | +hooksSpagoHash = Utils.fromRight "Failed to parse SRIHash" $ SRIHash.parse "sha256-fN9RUAzN21ZY4Y0UwqUSxwUPVz1g7/pcqoDvbJZoT04=" |
| 52 | + |
| 53 | +hooksSpago :: FilePath |
| 54 | +hooksSpago = Path.concat [ "test", "_fixtures", "halogen-hooks", "spago.dhall" ] |
| 55 | + |
| 56 | +-- Test hash produced by `openssl`: |
| 57 | +-- openssl dgst -sha256 -binary < test/_fixtures/LICENSE | openssl base64 -A |
| 58 | +hooksLicenseHash :: SRIHash.SRIHash |
| 59 | +hooksLicenseHash = Utils.fromRight "Failed to parse SRIHash" $ SRIHash.parse "sha256-wOzNcCq20TAL/LMT1lYIiaoEIFGDBw+yp14bj7qK9v4=" |
| 60 | + |
| 61 | +hooksLicense :: FilePath |
| 62 | +hooksLicense = Path.concat [ "test", "_fixtures", "halogen-hooks", "LICENSE" ] |
| 63 | + |
| 64 | +sha256Nix :: FilePath -> ExceptT String Aff SRIHash.SRIHash |
| 65 | +sha256Nix path = ExceptT do |
| 66 | + -- In Nix 2.4 this will become `nix hash file` |
| 67 | + let args = [ "hash-file", "--sri", path ] |
| 68 | + result <- Sunde.spawn { cmd: "nix", args, stdin: Nothing } Process.defaultSpawnOptions |
| 69 | + pure $ case result.exit of |
| 70 | + Process.Normally 0 -> do |
| 71 | + SRIHash.parse $ String.trim result.stdout |
| 72 | + _ -> |
| 73 | + Left result.stderr |
0 commit comments