This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
main.go
120 lines (104 loc) · 2.84 KB
/
main.go
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
111
112
113
114
115
116
117
118
119
120
// Copyright 2017-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main defines the end-to-end compatibility tests.
//
// Every implementation of ChainScript needs to generate the same test suite
// to test that encoding/decoding works across all implementations.
// When a new version of ChainScript is released:
// * this test suite should be updated to cover the new features
// * snapshot encoded bytes of the previous version should be added to the
// tests in https://github.com/stratumn/chainscript/samples.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
var (
// TestCases included in the compatibility test suite.
TestCases = map[string]TestCase{
"simple-segment": NewSimpleSegmentTest(),
"segment-references": NewReferencesTest(),
"segment-evidences": NewEvidencesTest(),
"segment-signatures": NewSignaturesTest(),
}
)
func main() {
if len(os.Args) < 3 {
panic("Some arguments are missing. Please provide the action requested and the file path.")
}
action := os.Args[1]
path := os.Args[2]
switch action {
case "generate":
generate(path)
case "validate":
validate(path)
default:
panic(fmt.Sprintf("Unknown action %s", action))
}
}
// generate encoded test segments and save them at the specified path.
func generate(path string) {
var results []TestData
for id, t := range TestCases {
results = append(results, TestData{
ID: id,
Data: t.Generate(),
})
}
b, err := json.Marshal(results)
if err != nil {
panic(err)
}
fmt.Printf("Saving encoded segments to %s...\n", path)
err = ioutil.WriteFile(path, b, os.ModePerm)
if err != nil {
panic(err)
}
fmt.Println("Saved.")
}
// validate encoded test segments from the specified path.
func validate(path string) {
fmt.Printf("Loading encoded segments from %s...\n", path)
b, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
var testData []TestData
err = json.Unmarshal(b, &testData)
if err != nil {
panic(err)
}
failed := false
for _, t := range testData {
tt, ok := TestCases[t.ID]
if !ok {
fmt.Printf("Unkown test case: %s\n", t.ID)
failed = true
continue
}
err = tt.Validate(t.Data)
if err != nil {
fmt.Printf("[%s] FAILED: %s\n", t.ID, err.Error())
failed = true
} else {
fmt.Printf("[%s] SUCCESS\n", t.ID)
}
}
if failed {
os.Exit(1)
}
}