| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package runnertoken_test |
| 4 | |
| 5 | import ( |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/tenseleyFlow/shithub/internal/actions/runnertoken" |
| 12 | ) |
| 13 | |
| 14 | func TestNewAndHashOfRoundTrip(t *testing.T) { |
| 15 | encoded, hash, err := runnertoken.New() |
| 16 | if err != nil { |
| 17 | t.Fatalf("New: %v", err) |
| 18 | } |
| 19 | if len(encoded) != runnertoken.SizeBytes*2 { |
| 20 | t.Fatalf("encoded length: got %d, want %d", len(encoded), runnertoken.SizeBytes*2) |
| 21 | } |
| 22 | if _, err := hex.DecodeString(encoded); err != nil { |
| 23 | t.Fatalf("encoded token is not hex: %v", err) |
| 24 | } |
| 25 | |
| 26 | got, err := runnertoken.HashOf(encoded) |
| 27 | if err != nil { |
| 28 | t.Fatalf("HashOf: %v", err) |
| 29 | } |
| 30 | if !runnertoken.Equal(got, hash) { |
| 31 | t.Fatalf("HashOf did not reproduce stored hash") |
| 32 | } |
| 33 | if strings.Contains(hex.EncodeToString(hash), encoded) { |
| 34 | t.Fatalf("hash contains plaintext token") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestHashOfRejectsMalformedAndWrongSize(t *testing.T) { |
| 39 | if _, err := runnertoken.HashOf("not-hex"); !errors.Is(err, runnertoken.ErrMalformed) { |
| 40 | t.Fatalf("malformed: got %v, want ErrMalformed", err) |
| 41 | } |
| 42 | if _, err := runnertoken.HashOf("abcd"); !errors.Is(err, runnertoken.ErrWrongSize) { |
| 43 | t.Fatalf("wrong size: got %v, want ErrWrongSize", err) |
| 44 | } |
| 45 | } |
| 46 |