Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add semantic hash checking #24

Merged
merged 6 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/dhall_clj/binary.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

(def protocol-version
"The currently supported version for the binary protocol"
"1.1")
"3.0.0")

(def supported-versions
"A list of versions that will be accepted for deserialization"
["1.1" "1.0"])
["3.0.0"])

(defn encode
"Encode `e` (which should be Dhall AST) into its binary form.
Expand Down
10 changes: 10 additions & 0 deletions src/dhall_clj/fail.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
(ex/derive ::missing-file ::imports)
(ex/derive ::missing-imports ::imports)
(ex/derive ::cyclic-import ::imports)
(ex/derive ::hash-mismatch ::imports)

(defn missing-keyword!
"Throws an ex-info from the `missing` keyword"
Expand Down Expand Up @@ -88,6 +89,15 @@
{:type ::cyclic-import
:import import}))

(defn hash-mismatch!
"Throws an ex-info on hash protection mismatch"
[import actual-hash expression]
(throw-data
"Hash mismatch"
{:type ::hash-mismatch
:import import
:actual-hash actual-hash
:expression expression}))

;;
;; Serialization
Expand Down
23 changes: 18 additions & 5 deletions src/dhall_clj/import.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
[digest :refer [sha-256]]
[me.raynes.fs :as fs]
[clojure.java.io :as io]
[clojure.string :as str]
[dhall-clj.parse :refer [parse expr]]
[dhall-clj.state :as state]
[dhall-clj.fail :as fail]
[clojure.string :as str]
[dhall-clj.alpha-normalize :refer [alpha-normalize]]
[dhall-clj.beta-normalize :refer [beta-normalize]]
[dhall-clj.typecheck :refer [typecheck]]
[dhall-clj.binary :as binary]
[dhall-clj.state :as s])
(:import [dhall_clj.ast Import Local Remote Env Missing]))

Expand Down Expand Up @@ -134,9 +137,19 @@
(expr-from-import data mode))
resolved-expr (resolve-imports
dynamic-expr
(update state :stack conj data))]
;; TODO: typecheck + normalize
resolved-expr))))
(update state :stack conj data))
_ (typecheck resolved-expr {})
normalized (beta-normalize resolved-expr)]
(if-not hash?
normalized
(let [expected-hash (str/lower-case hash?)
actual-hash (-> normalized
alpha-normalize
binary/encode
sha-256)]
(if (= expected-hash actual-hash)
normalized
(fail/hash-mismatch! this actual-hash normalized))))))))

dhall_clj.ast.ImportAlt
(resolve-imports [this state]
Expand Down
4 changes: 1 addition & 3 deletions src/dhall_clj/parse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,7 @@
ch
(-> ch :c first))))
(apply str))]
(if quoted?
(str "`" str-label "`")
str-label)))
str-label))

(defmethod expr :non-empty-list-literal [e]
;; Here we always pass the type as nil, because it's a non empty list,
Expand Down
44 changes: 44 additions & 0 deletions test/dhall_clj/import_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns dhall-clj.import-test
(:require [clojure.test :refer :all]
[medley.core :refer [map-vals]]
[dhall-clj.ast :refer :all]
[dhall-clj.parse :refer [parse expr]]
[dhall-clj.import :refer [resolve-imports]]
[dhall-clj.typecheck :refer [typecheck]]
[dhall-clj.state :as s]
[dhall-clj.test-utils :refer :all]
[me.raynes.fs :as fs]))


;; Simple tests

(def success-cases
[["./dhall-haskell/Prelude/package.dhall sha256:b575f038399d47f033b63d6e29ceb8e7778b45765778026c9015ef1d28655cc4"
"./dhall-haskell/Prelude/package.dhall"]])

(def failure-cases
[["./dhall-haskell/Prelude/package.dhall sha256:b575f038399d47f033b63d6e29ceb8e7778b45765778026c9015ef1d28655cc3"
"Hash mismatch"]])

(deftest import-success-suite
(let [import-cache (s/new)
f #(-> %
parse
expr
(resolve-imports import-cache))]
(doseq [[actual expected] success-cases]
(println "TESTCASE import: " actual)
(testing actual
(is (= (f actual) (f expected)))))))

(deftest import-failure-suite
(let [import-cache (s/new)
f #(-> %
parse
expr
(resolve-imports import-cache))]
(doseq [[failure message] failure-cases]
(println "TESTCASE import: " failure)
(testing failure
(is (thrown-with-msg? clojure.lang.ExceptionInfo (re-pattern message)
(f failure)))))))
8 changes: 4 additions & 4 deletions test/dhall_clj/parse_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@
(->RecordLit {})]

["{fo-o=1, `Text`=2}"
(->RecordLit {"`Text`" (->NaturalLit 2)
"fo-o" (->NaturalLit 1)})]
(->RecordLit {"Text" (->NaturalLit 2)
"fo-o" (->NaturalLit 1)})]

["{ _bar = 4 }"
(->RecordLit {"_bar" (->NaturalLit 4)})]

["{fo-o : Natural, `Text` : Text}"
(->RecordT {"`Text`" (->TextT)
"fo-o" (->NaturalT)})]
(->RecordT {"Text" (->TextT)
"fo-o" (->NaturalT)})]

["{ _bar : Natural }"
(->RecordT {"_bar" (->NaturalT)})]
Expand Down
1 change: 1 addition & 0 deletions test/dhall_clj/typecheck_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
(resolve-imports (s/new))
(typecheck {})))
nil)]
(println "TESTCASE" testcase)
(testing testcase
(is (= nil (run)))))))

Expand Down