| 1 | package protocol |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // Vaste vector: moet gelijk blijven aan internal/backup in BlueGit. |
| 11 | func TestSignatureVector(t *testing.T) { |
| 12 | sec := SigningSecret("bgbk_test") |
| 13 | if sec != "c04d0bdbac2260c010f61c9b063b28026eed1d2292c928708708821449e72c2a" { |
| 14 | t.Fatalf("geheim %s", sec) |
| 15 | } |
| 16 | if got := Signature(sec, "post", "/api/v1/notify", "1800000000", []byte("{}")); got != "51c5ec72528304632383637b99dfda1e14b68194bbec2d4faa36c9d5ef892548" { |
| 17 | t.Fatalf("handtekening %s", got) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestVerify(t *testing.T) { |
| 22 | sec := SigningSecret("bgbk_x") |
| 23 | now := time.Unix(1_800_000_000, 0) |
| 24 | body := []byte(`{"archive":"a"}`) |
| 25 | req, _ := http.NewRequest("POST", "http://b/api/v1/rollback", bytes.NewReader(body)) |
| 26 | Sign(req, sec, "i", body, now) |
| 27 | if err := Verify(req, sec, body, now); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if Verify(req, sec, []byte("{}"), now) == nil || Verify(req, sec, body, now.Add(6*time.Minute)) == nil || Verify(req, "x", body, now) == nil { |
| 31 | t.Fatal("vervalsing geaccepteerd") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestCleanLogical(t *testing.T) { |
| 36 | for _, p := range []string{"state/bluegit.db", "data/repos/a/b.git/HEAD", "state/ssh/k"} { |
| 37 | if _, ok := CleanLogical(p); !ok { |
| 38 | t.Error(p) |
| 39 | } |
| 40 | } |
| 41 | for _, p := range []string{"state/backup.json", "../x", "data/work/x", "data/releases/.incoming/a", "/x", "state/./bluegit.db"} { |
| 42 | if _, ok := CleanLogical(p); ok { |
| 43 | t.Error(p) |
| 44 | } |
| 45 | } |
| 46 | } |