File: ct_test.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (96 lines) | stat: -rw-r--r-- 2,213 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tlog

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"testing"
)

func TestCertificateTransparency(t *testing.T) {
	// Test that we can verify actual Certificate Transparency proofs.
	// (The other tests check that we can verify our own proofs;
	// this is a test that the two are compatible.)

	if testing.Short() {
		t.Skip("skipping in -short mode")
	}

	var root ctTree
	httpGET(t, "http://ct.googleapis.com/logs/argon2020/ct/v1/get-sth", &root)

	var leaf ctEntries
	httpGET(t, "http://ct.googleapis.com/logs/argon2020/ct/v1/get-entries?start=10000&end=10000", &leaf)
	hash := RecordHash(leaf.Entries[0].Data)

	var rp ctRecordProof
	httpGET(t, "http://ct.googleapis.com/logs/argon2020/ct/v1/get-proof-by-hash?tree_size="+fmt.Sprint(root.Size)+"&hash="+url.QueryEscape(hash.String()), &rp)

	err := CheckRecord(rp.Proof, root.Size, root.Hash, 10000, hash)
	if err != nil {
		t.Fatal(err)
	}

	var tp ctTreeProof
	httpGET(t, "http://ct.googleapis.com/logs/argon2020/ct/v1/get-sth-consistency?first=3654490&second="+fmt.Sprint(root.Size), &tp)

	oh, _ := ParseHash("AuIZ5V6sDUj1vn3Y1K85oOaQ7y+FJJKtyRTl1edIKBQ=")
	err = CheckTree(tp.Proof, root.Size, root.Hash, 3654490, oh)
	if err != nil {
		t.Fatal(err)
	}
}

type ctTree struct {
	Size int64 `json:"tree_size"`
	Hash Hash  `json:"sha256_root_hash"`
}

type ctEntries struct {
	Entries []*ctEntry
}

type ctEntry struct {
	Data []byte `json:"leaf_input"`
}

type ctRecordProof struct {
	Index int64       `json:"leaf_index"`
	Proof RecordProof `json:"audit_path"`
}

type ctTreeProof struct {
	Proof TreeProof `json:"consistency"`
}

func httpGET(t *testing.T, url string, targ interface{}) {
	if testing.Verbose() {
		println()
		println(url)
	}
	resp, err := http.Get(url)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}
	if testing.Verbose() {
		os.Stdout.Write(data)
	}
	err = json.Unmarshal(data, targ)
	if err != nil {
		println(url)
		os.Stdout.Write(data)
		t.Fatal(err)
	}
}