| 1 | package main |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // Los orígenes son exactos: el nombre actual, los dos anteriores mientras dure |
| 6 | // el cambio de dominio y localhost de desarrollo. Nada por sufijo. |
| 7 | func TestAllowedOrigins(t *testing.T) { |
| 8 | cases := []struct { |
| 9 | origin string |
| 10 | want bool |
| 11 | }{ |
| 12 | {"https://code.elffuss.com", true}, |
| 13 | {"https://code.elffuss.utopiaia.com", true}, |
| 14 | {"https://elffuss-code.utopiaia.com", true}, |
| 15 | {"http://localhost:8799", true}, |
| 16 | {"http://127.0.0.1:8799", true}, |
| 17 | |
| 18 | {"", false}, |
| 19 | {"null", false}, |
| 20 | {"http://code.elffuss.com", false}, |
| 21 | {"https://code.elffuss.com:443", false}, |
| 22 | {"https://code.elffuss.com/", false}, |
| 23 | {"https://claw.elffuss.com", false}, |
| 24 | {"https://elffuss.com", false}, |
| 25 | {"https://git.elffuss.com", false}, |
| 26 | {"https://evil.code.elffuss.com", false}, |
| 27 | {"https://code.elffuss.com.evil.net", false}, |
| 28 | {"https://claw.elffuss.utopiaia.com", false}, |
| 29 | {"https://elffuss-claw.utopiaia.com", false}, |
| 30 | {"https://utopiaia.com", false}, |
| 31 | {"https://otra.utopiaia.com", false}, |
| 32 | {"http://localhost:8642", false}, |
| 33 | {"http://localhost", false}, |
| 34 | } |
| 35 | for _, c := range cases { |
| 36 | if got := allowedOrigins[c.origin]; got != c.want { |
| 37 | t.Errorf("allowedOrigins[%q] = %v, want %v", c.origin, got, c.want) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestHostIsLoopback(t *testing.T) { |
| 43 | cases := []struct { |
| 44 | host string |
| 45 | want bool |
| 46 | }{ |
| 47 | {"127.0.0.1:8765", true}, |
| 48 | {"localhost:8765", true}, |
| 49 | {"localhost", true}, |
| 50 | {"[::1]:8765", true}, |
| 51 | {"127.0.0.2:8765", true}, |
| 52 | |
| 53 | {"code.elffuss.com", false}, |
| 54 | {"evil.example:8765", false}, |
| 55 | {"0.0.0.0:8765", false}, |
| 56 | {"192.168.1.10:8765", false}, |
| 57 | {"", false}, |
| 58 | } |
| 59 | for _, c := range cases { |
| 60 | if got := hostIsLoopback(c.host); got != c.want { |
| 61 | t.Errorf("hostIsLoopback(%q) = %v, want %v", c.host, got, c.want) |
| 62 | } |
| 63 | } |
| 64 | } |