|
1 | 1 | package proof |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
4 | 9 | "testing" |
5 | 10 |
|
6 | 11 | "github.com/stretchr/testify/require" |
7 | 12 | ) |
8 | 13 |
|
| 14 | +var ( |
| 15 | + // proofInvalidJsonHexFileName is the name of the file that contains the |
| 16 | + // hex proof data for a proof where the meta type is declared as JSON |
| 17 | + // but the data is not valid JSON. |
| 18 | + proofInvalidJsonHexFileName = filepath.Join( |
| 19 | + testDataFileName, "proof-invalid-json-meta-reveal.hex", |
| 20 | + ) |
| 21 | +) |
| 22 | + |
9 | 23 | func TestValidateMetaReveal(t *testing.T) { |
10 | 24 | t.Parallel() |
11 | 25 |
|
@@ -78,3 +92,26 @@ func TestValidateMetaReveal(t *testing.T) { |
78 | 92 | }) |
79 | 93 | } |
80 | 94 | } |
| 95 | + |
| 96 | +// TestProofInvalidJsonMetaReveal tests that a proof with a meta reveal that |
| 97 | +// is declared as JSON but is not valid JSON will return the correct error when |
| 98 | +// trying to decode the decimal display. |
| 99 | +func TestProofInvalidJsonMetaReveal(t *testing.T) { |
| 100 | + proofHex, err := os.ReadFile(proofInvalidJsonHexFileName) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + proofBytes, err := hex.DecodeString( |
| 104 | + strings.Trim(string(proofHex), "\n"), |
| 105 | + ) |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + p := &Proof{} |
| 109 | + err = p.Decode(bytes.NewReader(proofBytes)) |
| 110 | + require.NoError(t, err) |
| 111 | + |
| 112 | + require.NotNil(t, p.MetaReveal) |
| 113 | + |
| 114 | + _, decDisplay, err := p.MetaReveal.GetDecDisplay() |
| 115 | + require.ErrorIs(t, err, ErrInvalidJSON) |
| 116 | + require.Zero(t, decDisplay) |
| 117 | +} |
0 commit comments