2022-01-10 15:32:56 +00:00
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/manifest
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
|
|
|
|
suite "Manifest":
|
|
|
|
test "Should produce valid tree hash checksum":
|
2022-03-17 13:56:46 +00:00
|
|
|
var manifest = Manifest.new(
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks = @[
|
2022-03-18 19:50:53 +00:00
|
|
|
Block.new("Block 1".toBytes).tryGet().cid,
|
|
|
|
Block.new("Block 2".toBytes).tryGet().cid,
|
|
|
|
Block.new("Block 3".toBytes).tryGet().cid,
|
|
|
|
Block.new("Block 4".toBytes).tryGet().cid,
|
|
|
|
Block.new("Block 5".toBytes).tryGet().cid,
|
|
|
|
Block.new("Block 6".toBytes).tryGet().cid,
|
|
|
|
Block.new("Block 7".toBytes).tryGet().cid,
|
2022-03-17 13:56:46 +00:00
|
|
|
]).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
let
|
2022-04-05 00:46:13 +00:00
|
|
|
encoded = @[byte 18, 32, 227, 176, 196, 66, 152,
|
2022-01-11 03:04:00 +00:00
|
|
|
252, 28, 20, 154, 251, 244, 200, 153,
|
|
|
|
111, 185, 36, 39, 174, 65, 228, 100,
|
|
|
|
155, 147, 76, 164, 149, 153, 27, 120,
|
|
|
|
82, 184, 85]
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
var mh: MultiHash
|
2022-04-05 00:46:13 +00:00
|
|
|
check MultiHash.decode(encoded, mh).tryGet() > 0
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
let encodedCid = Cid.init(manifest.version, manifest.codec, mh).tryGet()
|
|
|
|
check:
|
|
|
|
encodedCid == manifest.cid.tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
test "Should encode/decode to/from manifest":
|
|
|
|
let
|
2022-01-11 02:25:13 +00:00
|
|
|
blocks = (0..<1000).mapIt(
|
2022-03-18 19:50:53 +00:00
|
|
|
Block.new(("Block " & $it).toBytes).tryGet().cid
|
2022-01-11 02:25:13 +00:00
|
|
|
)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
var
|
2022-04-05 00:46:13 +00:00
|
|
|
manifest = Manifest.new(blocks).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
let
|
2022-04-05 00:46:13 +00:00
|
|
|
e = manifest.encode().tryGet()
|
|
|
|
decoded = Manifest.decode(e).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
check:
|
|
|
|
decoded.blocks == blocks
|
|
|
|
decoded.protected == false
|
|
|
|
|
|
|
|
test "Should produce a protected manifest":
|
|
|
|
let
|
|
|
|
blocks = (0..<333).mapIt(
|
|
|
|
Block.new(("Block " & $it).toBytes).tryGet().cid
|
|
|
|
)
|
|
|
|
manifest = Manifest.new(blocks).tryGet()
|
|
|
|
protected = Manifest.new(manifest, 2, 2).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
protected.originalCid == manifest.cid.tryGet()
|
|
|
|
protected.blocks[0..<333] == manifest.blocks
|
|
|
|
protected.protected == true
|
|
|
|
protected.originalLen == manifest.len
|
|
|
|
|
|
|
|
# fill up with empty Cid's
|
|
|
|
for i in protected.rounded..<protected.len:
|
|
|
|
protected.blocks[i] = EmptyCid[manifest.version]
|
|
|
|
.catch
|
|
|
|
.get()[manifest.hcodec]
|
|
|
|
.catch
|
|
|
|
.get()
|
|
|
|
|
|
|
|
var
|
|
|
|
encoded = protected.encode().tryGet()
|
|
|
|
decoded = Manifest.decode(encoded).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
decoded.protected == true
|
|
|
|
decoded.originalLen == manifest.len
|
|
|
|
|
|
|
|
decoded.K == protected.K
|
|
|
|
decoded.M == protected.M
|
|
|
|
|
|
|
|
decoded.originalCid == protected.originalCid
|
|
|
|
decoded.originalCid == manifest.cid.tryGet()
|
|
|
|
|
|
|
|
decoded.blocks == protected.blocks
|
|
|
|
decoded.blocks[0..<333] == manifest.blocks
|