-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTLVSerialize.fs
110 lines (88 loc) · 3.93 KB
/
TLVSerialize.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module TLVSerialize
open System
open System.IO
open System.Text.Json
open Expecto
open DotNetLightning.Serialization
[<Tests>]
let bigSizeVarIntTests =
let hex = NBitcoin.DataEncoders.HexEncoder()
let dataPath1 =
Path.Join(
AppDomain.CurrentDomain.BaseDirectory,
"../../..",
("Data/bolt1-bigsize.json")
)
let testData1 = dataPath1 |> File.ReadAllText |> JsonDocument.Parse
testList
"BigSize encoding tests"
[
let successTestCases =
testData1.RootElement.EnumerateArray()
|> Seq.choose(fun x ->
match x.TryGetProperty("exp_error") with
| true, _ -> None
| false, _ -> Some x
)
for v in successTestCases do
yield
testCase(v.GetProperty("name").GetString())
<| fun _ ->
use wms = new MemoryStream()
use writer = new LightningWriterStream(wms)
let value = v.GetProperty("value").GetUInt64()
let b =
v.GetProperty("bytes").GetString() |> hex.DecodeData
let actualBytes =
writer.WriteBigSize(value)
wms.ToArray()
Expect.equal (actualBytes) b "failed to write"
use rms = new MemoryStream(b)
use reader = new LightningReaderStream(rms)
let actualValue = reader.ReadBigSize()
Expect.equal actualValue value "failed to read"
let failureTestCases =
testData1.RootElement.EnumerateArray()
|> Seq.choose(fun x ->
match x.TryGetProperty("exp_error") with
| true, _ -> Some(x)
| false, _ -> None
)
for v in failureTestCases do
yield
testCase(v.GetProperty("name").GetString())
<| fun _ ->
use wms = new MemoryStream()
use writer = new LightningWriterStream(wms)
let b =
v.GetProperty("bytes").GetString() |> hex.DecodeData
use rms = new MemoryStream(b)
use reader = new LightningReaderStream(rms)
let isEOFError =
v
.GetProperty("exp_error")
.GetString()
.Contains("EOF")
if isEOFError then
Expect.throwsT<System.IO.EndOfStreamException>
(fun _ -> reader.ReadBigSize() |> ignore)
"should throw EOF exception"
else
Expect.throwsT<FormatException>
(fun _ -> reader.ReadBigSize() |> ignore)
"should throw Format exception"
yield
testProperty "Should encode-decode"
<| fun (v: uint64) ->
use wms = new MemoryStream()
use writer = new LightningWriterStream(wms)
writer.WriteBigSize(v)
use rms = new MemoryStream(wms.ToArray())
use reader = new LightningReaderStream(rms)
let actual = reader.ReadBigSize()
Expect.equal actual v ""
]
let bolt4Tests2 =
// let dataPath1 = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "../../..", ("Data/bolt04/onion-test-multi-frame.json"))
// let testData1 = dataPath1 |> File.ReadAllText |> JsonDocument.Parse
testList "bolt04 test vectors" [ testCase "" <| ignore ]