forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obscuroscan_test.go
67 lines (58 loc) · 2.19 KB
/
obscuroscan_test.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
package obscuroscan
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/obscuronet/go-obscuro/go/common"
"github.com/obscuronet/go-obscuro/go/common/httputil"
"github.com/obscuronet/go-obscuro/go/enclave/core"
"github.com/obscuronet/go-obscuro/go/enclave/crypto"
"github.com/obscuronet/go-obscuro/integration/datagenerator"
)
func TestCanDecryptTxBlob(t *testing.T) {
txs := []*common.L2Tx{datagenerator.CreateL2Tx(), datagenerator.CreateL2Tx()}
txsJSONBytes, err := decryptTxBlob(generateEncryptedTxBlob(txs))
if err != nil {
t.Fatalf("transaction blob decryption failed. Cause: %s", err)
}
expectedTxsJSONBytes, err := json.Marshal(txs)
if err != nil {
t.Fatalf("marshalling transactions to JSON failed. Cause: %s", err)
}
if string(expectedTxsJSONBytes) != string(txsJSONBytes) {
t.Fatalf("expected %s, got %s", string(expectedTxsJSONBytes), string(txsJSONBytes))
}
}
func TestThrowsIfEncryptedRollupIsInvalid(t *testing.T) {
_, err := decryptTxBlob([]byte("invalid_tx_blob"))
if err == nil {
t.Fatal("did not error on invalid transaction blob")
}
}
// Generates an encrypted transaction blob in Base64 encoding.
func generateEncryptedTxBlob(txs []*common.L2Tx) []byte {
rollup := core.Batch{Header: &common.BatchHeader{}, Transactions: txs}
txBlob := rollup.ToExtBatch(crypto.NewTransactionBlobCryptoImpl(nil)).EncryptedTxBlob
return []byte(base64.StdEncoding.EncodeToString(txBlob))
}
func TestObscuroscan_getRollupByNumOrTxHash(t *testing.T) {
logger := gethlog.Logger.New(gethlog.Root())
ob := NewObscuroscan("http://testnet.obscuroscan.io", logger)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
req.Method = http.MethodOptions
resp := httptest.NewRecorder()
ob.getRollupByNumOrTxHash(resp, req)
if resp.Header().Get(httputil.CorsAllowOrigin) != httputil.OriginAll {
t.Fatal("CORS Allow Origin not set.")
}
if resp.Header().Get(httputil.CorsAllowMethods) != httputil.ReqOptions {
t.Fatal("CORS Allow Methods not set.")
}
if resp.Header().Get(httputil.CorsAllowHeaders) != httputil.CorsHeaders {
t.Fatal("CORS Allow Headers not set.")
}
}