You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
60 lines
1.8 KiB
package datekeys_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
datekeys "g.activething.com/go/DateKeys"
|
|
)
|
|
|
|
// The catalogue matches spec §69 exactly, in order.
|
|
func TestCatalogueMatchesSpec(t *testing.T) {
|
|
spec, err := os.ReadFile("spec/DateKeys_Protocol_Specification_v0.8.2.md")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := string(spec)
|
|
start := strings.Index(s, "## 69. Errores normativos")
|
|
end := strings.Index(s, "## 70.")
|
|
if start < 0 || end < start {
|
|
t.Fatal("section 69 not found")
|
|
}
|
|
var want []string
|
|
for _, line := range strings.Split(s[start:end], "\n") {
|
|
if line = strings.TrimSpace(line); strings.HasPrefix(line, "ERR_") {
|
|
want = append(want, line)
|
|
}
|
|
}
|
|
var got []string
|
|
for _, e := range datekeys.All() {
|
|
got = append(got, e.Code())
|
|
}
|
|
if strings.Join(got, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("catalogue\n got %v\nwant %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestCode(t *testing.T) {
|
|
err := fmt.Errorf("capsule: step 8: %w", fmt.Errorf("agewrap: %w", datekeys.ErrRoundMismatch))
|
|
if datekeys.Code(err) != "ERR_ROUND_MISMATCH" || !errors.Is(err, datekeys.ErrRoundMismatch) || errors.Is(err, datekeys.ErrIntegrity) {
|
|
t.Fatal("wrapping")
|
|
}
|
|
joined := fmt.Errorf("drand: %w: %w", datekeys.ErrReleaseUnavailable, errors.Join(errors.New("x"), datekeys.ErrReleaseInvalid))
|
|
if datekeys.Code(joined) != "ERR_RELEASE_UNAVAILABLE" || !errors.Is(joined, datekeys.ErrReleaseInvalid) {
|
|
t.Fatal("joined errors")
|
|
}
|
|
if datekeys.Code(errors.New("plain")) != "" || datekeys.Code(nil) != "" {
|
|
t.Fatal("non-DateKeys errors have no code")
|
|
}
|
|
if datekeys.ErrIntegrity.Error() != "ERR_INTEGRITY" {
|
|
t.Fatal("message")
|
|
}
|
|
data := fmt.Errorf("capsule: step 4: %w", datekeys.ErrExtensionDataInvalid)
|
|
if datekeys.Code(data) != "ERR_EXTENSION_DATA_INVALID" || errors.Is(data, datekeys.ErrExtensionCriticalUnknown) {
|
|
t.Fatal("ERR_EXTENSION_DATA_INVALID")
|
|
}
|
|
}
|