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

Make tests available as JSON and as CSV (RFC 4180) #79

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
all: check

# find out what has changed
check: tests.csv
ruby csv-to-json.rb $< | diff tests.json -
echo OK

# Call this explicitly to overwrite tests.json
tests.json: tests.csv
ruby csv-to-json.rb $< >[email protected]
mv $@ [email protected]
mv [email protected] $@

# Call this explicitly and then manually move to tests.csv
new-tests.csv: tests.json
ruby json-to-csv.rb $< >$@
-diff $@ tests.csv

21 changes: 21 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ Each test vector is organized as follows:
| `resolved-cri` | hex-encoded CBOR representation of the resolved CRI reference relative to the [base CRI](#bases) |
| `resolved-uri` | resolved URI relative to the [base URI](#bases) |

### Test Vector CSV Layout

The test vectors are assembled in a CSV (RFC 4180)
comma-separated-value file.

The columns are:

| Key Name | Description |
| --- | --- |
| `type` | kind of test: `ok`, `fail`; special value `base` |
| `uri` | a URI reference |
| `cri` | hex-encoded CBOR representation of the CRI reference corresponding to `uri` |
| `cri-diag` | `cri` in CBOR diagnostic format |
| `uri-from-cri` | the URI obtained translating the CRI reference `cri`, or "=" if identical to `uri` |
| `resolved-cri` | hex-encoded CBOR representation of the resolved CRI reference relative to the [base CRI](#bases) |
| `resolved-cri-diag` | `resolved-cri` in CBOR diagnostic format |
| `resolved-uri` | resolved URI relative to the [base URI](#bases) |

An initial line with type `base` gives the base URI and CRI applied, see above.


## Test Logics

### CBOR deserialization
Expand Down
53 changes: 53 additions & 0 deletions tests/csv-to-json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'cbor-diagnostic'
require 'treetop'
require 'cbor-diag-parser'
require 'sid-csv' # steal from sid-csv gem
include CSV::SID

class String
def hexi
bytes.map{|x| "%02x" % x}.join
end
def hexs
bytes.map{|x| "%02x" % x}.join(" ")
end
def xeh
gsub(/\s/, "").chars.each_slice(2).map{ |x| Integer(x.join, 16).chr("BINARY") }.join
end
end

def hex_to_cri(h)
CBOR::CRI.new(*CBOR.decode(h.xeh))
end

def hex_to_diag(h)
CBOR.decode(h.xeh).cbor_diagnostic
end

def diag_to_hex(d)
parser = CBOR_DIAGParser.new
parser.parse(d).to_rb.to_cbor.hexi
end

test_data = {}

tv = []

CSV.read("tests.csv").each do |row|
case row
in ["base", baseuri, basecri_diag]
test_data["base-uri"] = baseuri
test_data["base-cri"] = diag_to_hex(basecri_diag)
test_data["test-vectors"] = tv
in ["ok", uri_in, cri_in_diag, uri_from_cri_opt, resolved_cri_diag, resolved_uri]
tv << {
"uri" => uri_in,
"cri" => diag_to_hex(cri_in_diag),
"uri-from-cri" => (uri_from_cri_opt == "=" ? uri_in : uri_from_cri_opt),
"resolved-cri" => diag_to_hex(resolved_cri_diag),
"resolved-uri" => resolved_uri,
}
end
end

puts JSON.pretty_generate(test_data)
66 changes: 66 additions & 0 deletions tests/json-to-csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'cbor-diagnostic'
require 'sid-csv' # steal from sid-csv gem
include CSV::SID

class String
def hexi
bytes.map{|x| "%02x" % x}.join
end
def hexs
bytes.map{|x| "%02x" % x}.join(" ")
end
def xeh
gsub(/\s/, "").chars.each_slice(2).map{ |x| Integer(x.join, 16).chr("BINARY") }.join
end
end

def hex_to_diag(h)
CBOR.decode(h.xeh).cbor_diagnostic
end



test_data = JSON.load_file("tests.json")

baseuri = test_data["base-uri"]
basecri_hex = test_data["base-cri"]
basecri_diag = hex_to_diag(basecri_hex)

data = [
["base", baseuri, # basecri_hex,
basecri_diag]
]

tests = test_data["test-vectors"]
tests.each do |td|
uri_in = td["uri"]
cri_in = td["cri"]
uri_from_cri = td["uri-from-cri"]
resolved_cri = td["resolved-cri"]
resolved_uri = td["resolved-uri"]
data << [
"ok", uri_in, # cri_in,
hex_to_diag(cri_in),
(uri_from_cri == uri_in ? "=" : uri_from_cri),
# resolved_cri,
hex_to_diag(resolved_cri), resolved_uri,
]
end


=begin
| Key Name | Description |
| --- | --- |
| `type` | kind of test: `ok`, `fail`; special value `base` |
| `uri` | a URI reference |
| `cri` | hex-encoded CBOR representation of the CRI reference corresponding to `uri` |
| `cri-diag` | `cri` in CBOR diagnostic format |
| `uri-from-cri` | the URI obtained translating the CRI reference `cri`, or "=" if identical to `uri` |
| `resolved-cri` | hex-encoded CBOR representation of the resolved CRI reference relative to the [base CRI](#bases) |
| `resolved-cri-diag` | `resolved-cri` in CBOR diagnostic format |
| `resolved-uri` | resolved URI relative to the [base URI](#bases)

=end |


puts csv_generate(data)
102 changes: 102 additions & 0 deletions tests/tests.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
base,coaps://foo:4711/pa/th?query#frag,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]"
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,//a,"[null, [""a""]]",=,"[-2, [""a""]]",coaps://a
ok,//192.168.0.97,"[null, [h'C0A80061']]",=,"[-2, [h'C0A80061']]",coaps://192.168.0.97
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,/,"[true, [""""]]",=,"[-2, [""foo"", 4711], [""""]]",coaps://foo:4711/
ok,/a,"[true, [""a""]]",=,"[-2, [""foo"", 4711], [""a""]]",coaps://foo:4711/a
ok,?a,"[0, null, [""a""]]",=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""a""]]",coaps://foo:4711/pa/th?a
ok,#a,"[0, null, null, ""a""]",=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""a""]",coaps://foo:4711/pa/th?query#a
ok,a,"[1, [""a""]]",=,"[-2, [""foo"", 4711], [""pa"", ""a""]]",coaps://foo:4711/pa/a
ok,a/b,"[1, [""a"", ""b""]]",=,"[-2, [""foo"", 4711], [""pa"", ""a"", ""b""]]",coaps://foo:4711/pa/a/b
ok,a/./b,"[1, [""a"", ""b""]]",a/b,"[-2, [""foo"", 4711], [""pa"", ""a"", ""b""]]",coaps://foo:4711/pa/a/b
ok,./a/b,"[1, [""a"", ""b""]]",a/b,"[-2, [""foo"", 4711], [""pa"", ""a"", ""b""]]",coaps://foo:4711/pa/a/b
ok,../a,"[2, [""a""]]",=,"[-2, [""foo"", 4711], [""a""]]",coaps://foo:4711/a
ok,../a/b/../c/.,"[2, [""a"", ""c""]]",../a/c,"[-2, [""foo"", 4711], [""a"", ""c""]]",coaps://foo:4711/a/c
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,a://b,"[""a"", [""b""]]",=,"[""a"", [""b""]]",a://b
ok,a://192.168.0.98,"[""a"", [h'C0A80062']]",=,"[""a"", [h'C0A80062']]",a://192.168.0.98
ok,a:,"[""a""]",=,"[""a""]",a:
ok,a:b,"[""a"", true, [""b""]]",=,"[""a"", true, [""b""]]",a:b
ok,a:?b,"[""a"", null, null, [""b""]]",=,"[""a"", null, null, [""b""]]",a:?b
ok,a:/b,"[""a"", null, [""b""]]",=,"[""a"", null, [""b""]]",a:/b
ok,a:#b,"[""a"", null, null, null, ""b""]",=,"[""a"", null, null, null, ""b""]",a:#b
ok,//a:25186,"[null, [""a"", 25186]]",=,"[-2, [""a"", 25186]]",coaps://a:25186
ok,//a/,"[null, [""a""], [""""]]",=,"[-2, [""a""], [""""]]",coaps://a/
ok,//a/b,"[null, [""a""], [""b""]]",=,"[-2, [""a""], [""b""]]",coaps://a/b
ok,//a?b,"[null, [""a""], null, [""b""]]",=,"[-2, [""a""], null, [""b""]]",coaps://a?b
ok,//a#b,"[null, [""a""], null, null, ""b""]",=,"[-2, [""a""], null, null, ""b""]",coaps://a#b
ok,//192.168.0.97:25186,"[null, [h'C0A80061', 25186]]",=,"[-2, [h'C0A80061', 25186]]",coaps://192.168.0.97:25186
ok,//192.168.0.97/,"[null, [h'C0A80061'], [""""]]",=,"[-2, [h'C0A80061'], [""""]]",coaps://192.168.0.97/
ok,//192.168.0.97/b,"[null, [h'C0A80061'], [""b""]]",=,"[-2, [h'C0A80061'], [""b""]]",coaps://192.168.0.97/b
ok,//192.168.0.97?b,"[null, [h'C0A80061'], null, [""b""]]",=,"[-2, [h'C0A80061'], null, [""b""]]",coaps://192.168.0.97?b
ok,//192.168.0.97#b,"[null, [h'C0A80061'], null, null, ""b""]",=,"[-2, [h'C0A80061'], null, null, ""b""]",coaps://192.168.0.97#b
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,/?b,"[true, [""""], [""b""]]",=,"[-2, [""foo"", 4711], [""""], [""b""]]",coaps://foo:4711/?b
ok,/#b,"[true, [""""], null, ""b""]",=,"[-2, [""foo"", 4711], [""""], null, ""b""]",coaps://foo:4711/#b
ok,/a/,"[true, [""a"", """"]]",=,"[-2, [""foo"", 4711], [""a"", """"]]",coaps://foo:4711/a/
ok,/a/b,"[true, [""a"", ""b""]]",=,"[-2, [""foo"", 4711], [""a"", ""b""]]",coaps://foo:4711/a/b
ok,/a?b,"[true, [""a""], [""b""]]",=,"[-2, [""foo"", 4711], [""a""], [""b""]]",coaps://foo:4711/a?b
ok,/a#b,"[true, [""a""], null, ""b""]",=,"[-2, [""foo"", 4711], [""a""], null, ""b""]",coaps://foo:4711/a#b
ok,?a&b,"[0, null, [""a"", ""b""]]",=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""a"", ""b""]]",coaps://foo:4711/pa/th?a&b
ok,?a#b,"[0, null, [""a""], ""b""]",=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""a""], ""b""]",coaps://foo:4711/pa/th?a#b
ok,a://b:25443,"[""a"", [""b"", 25443]]",=,"[""a"", [""b"", 25443]]",a://b:25443
ok,a://b/,"[""a"", [""b""], [""""]]",=,"[""a"", [""b""], [""""]]",a://b/
ok,a://b/c,"[""a"", [""b""], [""c""]]",=,"[""a"", [""b""], [""c""]]",a://b/c
ok,a://b?c,"[""a"", [""b""], null, [""c""]]",=,"[""a"", [""b""], null, [""c""]]",a://b?c
ok,a://b#c,"[""a"", [""b""], null, null, ""c""]",=,"[""a"", [""b""], null, null, ""c""]",a://b#c
ok,a://192.168.0.98:25443,"[""a"", [h'C0A80062', 25443]]",=,"[""a"", [h'C0A80062', 25443]]",a://192.168.0.98:25443
ok,a://192.168.0.98/,"[""a"", [h'C0A80062'], [""""]]",=,"[""a"", [h'C0A80062'], [""""]]",a://192.168.0.98/
ok,a://192.168.0.98/c,"[""a"", [h'C0A80062'], [""c""]]",=,"[""a"", [h'C0A80062'], [""c""]]",a://192.168.0.98/c
ok,a://192.168.0.98?c,"[""a"", [h'C0A80062'], null, [""c""]]",=,"[""a"", [h'C0A80062'], null, [""c""]]",a://192.168.0.98?c
ok,a://192.168.0.98#c,"[""a"", [h'C0A80062'], null, null, ""c""]",=,"[""a"", [h'C0A80062'], null, null, ""c""]",a://192.168.0.98#c
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,a:?c,"[""a"", null, null, [""c""]]",=,"[""a"", null, null, [""c""]]",a:?c
ok,a:#c,"[""a"", null, null, null, ""c""]",=,"[""a"", null, null, null, ""c""]",a:#c
ok,a:b/,"[""a"", true, [""b"", """"]]",=,"[""a"", true, [""b"", """"]]",a:b/
ok,a:b/c,"[""a"", true, [""b"", ""c""]]",=,"[""a"", true, [""b"", ""c""]]",a:b/c
ok,a:b%3Fc,"[""a"", true, [""b?c""]]",=,"[""a"", true, [""b?c""]]",a:b%3Fc
ok,a:b?c,"[""a"", true, [""b""], [""c""]]",=,"[""a"", true, [""b""], [""c""]]",a:b?c
ok,a:b#c,"[""a"", true, [""b""], null, ""c""]",=,"[""a"", true, [""b""], null, ""c""]",a:b#c
ok,a:?b&c,"[""a"", null, null, [""b"", ""c""]]",=,"[""a"", null, null, [""b"", ""c""]]",a:?b&c
ok,a:?b#c,"[""a"", null, null, [""b""], ""c""]",=,"[""a"", null, null, [""b""], ""c""]",a:?b#c
ok,//a:25186/,"[null, [""a"", 25186], [""""]]",=,"[-2, [""a"", 25186], [""""]]",coaps://a:25186/
ok,//a:25186/c,"[null, [""a"", 25186], [""c""]]",=,"[-2, [""a"", 25186], [""c""]]",coaps://a:25186/c
ok,//a:25186?c,"[null, [""a"", 25186], null, [""c""]]",=,"[-2, [""a"", 25186], null, [""c""]]",coaps://a:25186?c
ok,//a:25186#c,"[null, [""a"", 25186], null, null, ""c""]",=,"[-2, [""a"", 25186], null, null, ""c""]",coaps://a:25186#c
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,//a/?c,"[null, [""a""], [""""], [""c""]]",=,"[-2, [""a""], [""""], [""c""]]",coaps://a/?c
ok,//a/#c,"[null, [""a""], [""""], null, ""c""]",=,"[-2, [""a""], [""""], null, ""c""]",coaps://a/#c
ok,//a/b/,"[null, [""a""], [""b"", """"]]",=,"[-2, [""a""], [""b"", """"]]",coaps://a/b/
ok,//a/b/c,"[null, [""a""], [""b"", ""c""]]",=,"[-2, [""a""], [""b"", ""c""]]",coaps://a/b/c
ok,//a/b?c,"[null, [""a""], [""b""], [""c""]]",=,"[-2, [""a""], [""b""], [""c""]]",coaps://a/b?c
ok,//a/b#c,"[null, [""a""], [""b""], null, ""c""]",=,"[-2, [""a""], [""b""], null, ""c""]",coaps://a/b#c
ok,//a?b&c,"[null, [""a""], null, [""b"", ""c""]]",=,"[-2, [""a""], null, [""b"", ""c""]]",coaps://a?b&c
ok,//a?b#c,"[null, [""a""], null, [""b""], ""c""]",=,"[-2, [""a""], null, [""b""], ""c""]",coaps://a?b#c
ok,//192.168.0.97:25186/,"[null, [h'C0A80061', 25186], [""""]]",=,"[-2, [h'C0A80061', 25186], [""""]]",coaps://192.168.0.97:25186/
ok,//192.168.0.97:25186/c,"[null, [h'C0A80061', 25186], [""c""]]",=,"[-2, [h'C0A80061', 25186], [""c""]]",coaps://192.168.0.97:25186/c
ok,//192.168.0.97:25186?c,"[null, [h'C0A80061', 25186], null, [""c""]]",=,"[-2, [h'C0A80061', 25186], null, [""c""]]",coaps://192.168.0.97:25186?c
ok,//192.168.0.97:25186#c,"[null, [h'C0A80061', 25186], null, null, ""c""]",=,"[-2, [h'C0A80061', 25186], null, null, ""c""]",coaps://192.168.0.97:25186#c
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,//192.168.0.97/?c,"[null, [h'C0A80061'], [""""], [""c""]]",=,"[-2, [h'C0A80061'], [""""], [""c""]]",coaps://192.168.0.97/?c
ok,//192.168.0.97/#c,"[null, [h'C0A80061'], [""""], null, ""c""]",=,"[-2, [h'C0A80061'], [""""], null, ""c""]",coaps://192.168.0.97/#c
ok,//192.168.0.97/b/,"[null, [h'C0A80061'], [""b"", """"]]",=,"[-2, [h'C0A80061'], [""b"", """"]]",coaps://192.168.0.97/b/
ok,//192.168.0.97/b/c,"[null, [h'C0A80061'], [""b"", ""c""]]",=,"[-2, [h'C0A80061'], [""b"", ""c""]]",coaps://192.168.0.97/b/c
ok,//192.168.0.97/b?c,"[null, [h'C0A80061'], [""b""], [""c""]]",=,"[-2, [h'C0A80061'], [""b""], [""c""]]",coaps://192.168.0.97/b?c
ok,//192.168.0.97/b#c,"[null, [h'C0A80061'], [""b""], null, ""c""]",=,"[-2, [h'C0A80061'], [""b""], null, ""c""]",coaps://192.168.0.97/b#c
ok,//192.168.0.97?b&c,"[null, [h'C0A80061'], null, [""b"", ""c""]]",=,"[-2, [h'C0A80061'], null, [""b"", ""c""]]",coaps://192.168.0.97?b&c
ok,//192.168.0.97?b#c,"[null, [h'C0A80061'], null, [""b""], ""c""]",=,"[-2, [h'C0A80061'], null, [""b""], ""c""]",coaps://192.168.0.97?b#c
ok,"",[0],=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""query""], ""frag""]",coaps://foo:4711/pa/th?query#frag
ok,/?b&c,"[true, [""""], [""b"", ""c""]]",=,"[-2, [""foo"", 4711], [""""], [""b"", ""c""]]",coaps://foo:4711/?b&c
ok,/?b#c,"[true, [""""], [""b""], ""c""]",=,"[-2, [""foo"", 4711], [""""], [""b""], ""c""]",coaps://foo:4711/?b#c
ok,/a//,"[true, [""a"", """", """"]]",=,"[-2, [""foo"", 4711], [""a"", """", """"]]",coaps://foo:4711/a//
ok,/a//c,"[true, [""a"", """", ""c""]]",=,"[-2, [""foo"", 4711], [""a"", """", ""c""]]",coaps://foo:4711/a//c
ok,/a/?c,"[true, [""a"", """"], [""c""]]",=,"[-2, [""foo"", 4711], [""a"", """"], [""c""]]",coaps://foo:4711/a/?c
ok,/a/#c,"[true, [""a"", """"], null, ""c""]",=,"[-2, [""foo"", 4711], [""a"", """"], null, ""c""]",coaps://foo:4711/a/#c
ok,/a/b/,"[true, [""a"", ""b"", """"]]",=,"[-2, [""foo"", 4711], [""a"", ""b"", """"]]",coaps://foo:4711/a/b/
ok,/a/b/c,"[true, [""a"", ""b"", ""c""]]",=,"[-2, [""foo"", 4711], [""a"", ""b"", ""c""]]",coaps://foo:4711/a/b/c
ok,/a/b?c,"[true, [""a"", ""b""], [""c""]]",=,"[-2, [""foo"", 4711], [""a"", ""b""], [""c""]]",coaps://foo:4711/a/b?c
ok,/a/b#c,"[true, [""a"", ""b""], null, ""c""]",=,"[-2, [""foo"", 4711], [""a"", ""b""], null, ""c""]",coaps://foo:4711/a/b#c
ok,/a?b&c,"[true, [""a""], [""b"", ""c""]]",=,"[-2, [""foo"", 4711], [""a""], [""b"", ""c""]]",coaps://foo:4711/a?b&c
ok,/a?b#c,"[true, [""a""], [""b""], ""c""]",=,"[-2, [""foo"", 4711], [""a""], [""b""], ""c""]",coaps://foo:4711/a?b#c
ok,?a&b&c,"[0, null, [""a"", ""b"", ""c""]]",=,"[-2, [""foo"", 4711], [""pa"", ""th""], [""a"", ""b"", ""c""]]",coaps://foo:4711/pa/th?a&b&c
4 changes: 2 additions & 2 deletions tests/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@
},
{
"uri": "a:b?c",
"cri": "846161F5816162816163",
"cri": "846161f5816162816163",
"uri-from-cri": "a:b?c",
"resolved-cri": "846161F5816162816163",
"resolved-cri": "846161f5816162816163",
"resolved-uri": "a:b?c"
},
{
Expand Down
Loading