| 1 | package maincmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestParseHostspec(t *testing.T) { |
| 9 | for _, tt := range []struct { |
| 10 | src string |
| 11 | parsingURL bool |
| 12 | wantHost string |
| 13 | wantPath string |
| 14 | wantPort int |
| 15 | }{ |
| 16 | { |
| 17 | src: "localhost", |
| 18 | parsingURL: true, |
| 19 | wantHost: "localhost", |
| 20 | wantPath: "", |
| 21 | wantPort: 0, |
| 22 | }, |
| 23 | |
| 24 | { |
| 25 | src: "localhost/path", |
| 26 | parsingURL: true, |
| 27 | wantHost: "localhost", |
| 28 | wantPath: "path", |
| 29 | wantPort: 0, |
| 30 | }, |
| 31 | |
| 32 | { |
| 33 | src: "user@localhost/path", |
| 34 | parsingURL: true, |
| 35 | wantHost: "user@localhost", |
| 36 | wantPath: "path", |
| 37 | wantPort: 0, |
| 38 | }, |
| 39 | |
| 40 | { |
| 41 | src: "user@[2001:db8::1]:23/path", |
| 42 | parsingURL: true, |
| 43 | wantHost: "user@2001:db8::1", |
| 44 | wantPath: "path", |
| 45 | wantPort: 23, |
| 46 | }, |
| 47 | |
| 48 | { |
| 49 | src: "localhost:881", |
| 50 | parsingURL: true, |
| 51 | wantHost: "localhost", |
| 52 | wantPath: "", |
| 53 | wantPort: 881, |
| 54 | }, |
| 55 | |
| 56 | { |
| 57 | src: "localhost:881/path", |
| 58 | parsingURL: true, |
| 59 | wantHost: "localhost", |
| 60 | wantPath: "path", |
| 61 | wantPort: 881, |
| 62 | }, |
| 63 | |
| 64 | { |
| 65 | src: "localhost:/path", |
| 66 | parsingURL: true, |
| 67 | wantHost: "localhost", |
| 68 | wantPath: "path", |
| 69 | wantPort: 0, |
| 70 | }, |
| 71 | |
| 72 | { |
| 73 | src: "localhost:", |
| 74 | parsingURL: false, |
| 75 | wantHost: "localhost", |
| 76 | wantPath: "", |
| 77 | wantPort: 0, |
| 78 | }, |
| 79 | |
| 80 | { |
| 81 | src: "localhost:path", |
| 82 | parsingURL: false, |
| 83 | wantHost: "localhost", |
| 84 | wantPath: "path", |
| 85 | wantPort: 0, |
| 86 | }, |
| 87 | |
| 88 | { |
| 89 | src: "localhost::path", |
| 90 | parsingURL: false, |
| 91 | wantHost: "localhost", |
| 92 | wantPath: ":path", |
| 93 | wantPort: 0, |
| 94 | }, |
| 95 | |
| 96 | { |
| 97 | src: "user@localhost::path", |
| 98 | parsingURL: false, |
| 99 | wantHost: "user@localhost", |
| 100 | wantPath: ":path", |
| 101 | wantPort: 0, |
| 102 | }, |
| 103 | } { |
| 104 | t.Run(fmt.Sprintf("src=%s, url=%v", tt.src, tt.parsingURL), func(t *testing.T) { |
| 105 | host, path, port, err := parseHostspec(tt.src, tt.parsingURL) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if host != tt.wantHost { |
| 110 | t.Errorf("unexpected host: got %q, want %q", host, tt.wantHost) |
| 111 | } |
| 112 | if path != tt.wantPath { |
| 113 | t.Errorf("unexpected path: got %q, want %q", path, tt.wantPath) |
| 114 | } |
| 115 | if port != tt.wantPort { |
| 116 | t.Errorf("unexpected port: got %d, want %d", port, tt.wantPort) |
| 117 | } |
| 118 | }) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestCheckForHostspec(t *testing.T) { |
| 123 | for _, tt := range []struct { |
| 124 | src string |
| 125 | wantHost string |
| 126 | wantPath string |
| 127 | wantPort int |
| 128 | }{ |
| 129 | { |
| 130 | src: "rsync://localhost", |
| 131 | wantHost: "localhost", |
| 132 | wantPath: "", |
| 133 | wantPort: -1, // daemon-accessing |
| 134 | }, |
| 135 | |
| 136 | { |
| 137 | src: "rsync://localhost/path", |
| 138 | wantHost: "localhost", |
| 139 | wantPath: "path", |
| 140 | wantPort: -1, // daemon-accessing |
| 141 | }, |
| 142 | |
| 143 | { |
| 144 | src: "rsync://user@localhost/path", |
| 145 | wantHost: "user@localhost", |
| 146 | wantPath: "path", |
| 147 | wantPort: -1, // daemon-accessing |
| 148 | }, |
| 149 | |
| 150 | { |
| 151 | src: "user@[2001:db8::1]:path", |
| 152 | wantHost: "user@2001:db8::1", |
| 153 | wantPath: "path", |
| 154 | wantPort: 0, // non-daemon-accessing |
| 155 | }, |
| 156 | |
| 157 | { |
| 158 | src: "localhost:path", |
| 159 | wantHost: "localhost", |
| 160 | wantPath: "path", |
| 161 | wantPort: 0, // non-daemon-accessing |
| 162 | }, |
| 163 | |
| 164 | { |
| 165 | src: "user@localhost:path", |
| 166 | wantHost: "user@localhost", |
| 167 | wantPath: "path", |
| 168 | wantPort: 0, // non-daemon-accessing |
| 169 | }, |
| 170 | |
| 171 | { |
| 172 | src: "localhost:/path", |
| 173 | wantHost: "localhost", |
| 174 | wantPath: "/path", |
| 175 | wantPort: 0, // non-daemon-accessing |
| 176 | }, |
| 177 | |
| 178 | { |
| 179 | src: "localhost:", |
| 180 | wantHost: "localhost", |
| 181 | wantPath: "", |
| 182 | wantPort: 0, // non-daemon-accessing |
| 183 | }, |
| 184 | |
| 185 | { |
| 186 | src: "localhost:path", |
| 187 | wantHost: "localhost", |
| 188 | wantPath: "path", |
| 189 | wantPort: 0, // non-daemon-accessing |
| 190 | }, |
| 191 | |
| 192 | { |
| 193 | src: "localhost::path", |
| 194 | wantHost: "localhost", |
| 195 | wantPath: "path", |
| 196 | wantPort: -1, // daemon-accessing |
| 197 | }, |
| 198 | |
| 199 | { |
| 200 | src: "user@localhost::path", |
| 201 | wantHost: "user@localhost", |
| 202 | wantPath: "path", |
| 203 | wantPort: -1, // daemon-accessing |
| 204 | }, |
| 205 | } { |
| 206 | t.Run(tt.src, func(t *testing.T) { |
| 207 | host, path, port, err := checkForHostspec(tt.src) |
| 208 | if err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | if host != tt.wantHost { |
| 212 | t.Errorf("unexpected host: got %q, want %q", host, tt.wantHost) |
| 213 | } |
| 214 | if path != tt.wantPath { |
| 215 | t.Errorf("unexpected path: got %q, want %q", path, tt.wantPath) |
| 216 | } |
| 217 | if port != tt.wantPort { |
| 218 | t.Errorf("unexpected port: got %d, want %d", port, tt.wantPort) |
| 219 | } |
| 220 | }) |
| 221 | } |
| 222 | } |