| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package logs |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"reflect" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/etix/mirrorbits/core" |
| 16 |
"github.com/etix/mirrorbits/filesystem" |
| 17 |
"github.com/etix/mirrorbits/mirrors" |
| 18 |
"github.com/etix/mirrorbits/network" |
| 19 |
"github.com/op/go-logging" |
| 20 |
) |
| 21 |
|
| 22 |
type CloseTester struct { |
| 23 |
closed bool |
| 24 |
} |
| 25 |
|
| 26 |
func (c *CloseTester) Write(p []byte) (n int, err error) { |
| 27 |
return 0, err |
| 28 |
} |
| 29 |
|
| 30 |
func (c *CloseTester) Close() error { |
| 31 |
c.closed = true |
| 32 |
return nil |
| 33 |
} |
| 34 |
|
| 35 |
func TestDownloadsLogger_Close(t *testing.T) { |
| 36 |
f := &CloseTester{} |
| 37 |
|
| 38 |
dlogger.f = f |
| 39 |
|
| 40 |
if f.closed == true { |
| 41 |
t.Fatalf("Precondition failed") |
| 42 |
} |
| 43 |
|
| 44 |
dlogger.Close() |
| 45 |
|
| 46 |
if f.closed == false { |
| 47 |
t.Fatalf("Should be closed") |
| 48 |
} |
| 49 |
|
| 50 |
if dlogger.l != nil || dlogger.f != nil { |
| 51 |
t.Fatalf("Should be nil") |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestIsTerminal(t *testing.T) { |
| 56 |
stat, _ := os.Stdout.Stat() |
| 57 |
if (stat.Mode() & os.ModeCharDevice) == 0 { |
| 58 |
t.Skip("Cannot test without a valid terminal") |
| 59 |
} |
| 60 |
|
| 61 |
if !isTerminal(os.Stdout) { |
| 62 |
t.Fatalf("The current terminal is supposed to support colors") |
| 63 |
} |
| 64 |
|
| 65 |
f, err := os.CreateTemp("", "mirrorbits-tests") |
| 66 |
if err != nil { |
| 67 |
t.Errorf("Unable to create a temporary file: %s", err.Error()) |
| 68 |
return |
| 69 |
} |
| 70 |
defer os.Remove(f.Name()) |
| 71 |
|
| 72 |
if isTerminal(f) { |
| 73 |
t.Fatalf("The given file cannot be a terminal") |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestReloadRuntimeLogs(t *testing.T) { |
| 78 |
rlogger.f = nil |
| 79 |
|
| 80 |
ReloadRuntimeLogs() |
| 81 |
|
| 82 |
if rlogger.f == nil { |
| 83 |
t.Fatalf("The logger output must be setup") |
| 84 |
} |
| 85 |
if rlogger.f != os.Stderr { |
| 86 |
t.Fatalf("The logger output is expected to be Stderr") |
| 87 |
} |
| 88 |
if logging.GetLevel("main") != logging.INFO { |
| 89 |
t.Fatalf("Log level is supposed to be INFO by default") |
| 90 |
} |
| 91 |
|
| 92 |
ptr := reflect.ValueOf(rlogger.f).Pointer() |
| 93 |
ReloadRuntimeLogs() |
| 94 |
if reflect.ValueOf(rlogger.f).Pointer() != ptr { |
| 95 |
t.Fatalf("The logger must not be reloaded when writing on Stderr") |
| 96 |
} |
| 97 |
|
| 98 |
/* */ |
| 99 |
core.RunLog = "/" |
| 100 |
ReloadRuntimeLogs() |
| 101 |
if rlogger.f != os.Stderr { |
| 102 |
t.Fatalf("Opening an invalid file must fallback to Stderr") |
| 103 |
} |
| 104 |
|
| 105 |
/* */ |
| 106 |
f, err := os.CreateTemp("", "mirrorbits-tests") |
| 107 |
if err != nil { |
| 108 |
t.Errorf("Unable to create a temporary file: %s", err.Error()) |
| 109 |
return |
| 110 |
} |
| 111 |
defer os.Remove(f.Name()) |
| 112 |
|
| 113 |
core.RunLog = f.Name() |
| 114 |
core.Debug = true |
| 115 |
|
| 116 |
ReloadRuntimeLogs() |
| 117 |
|
| 118 |
if logging.GetLevel("main") != logging.DEBUG { |
| 119 |
t.Fatalf("Log level is supposed to be DEBUG") |
| 120 |
} |
| 121 |
|
| 122 |
if rlogger.f == os.Stderr { |
| 123 |
t.Fatalf("The output is expected to be a file, not Stderr") |
| 124 |
} |
| 125 |
|
| 126 |
/* */ |
| 127 |
testString := "Testing42" |
| 128 |
log.Error(testString) |
| 129 |
|
| 130 |
buf, _ := io.ReadAll(f) |
| 131 |
if !strings.Contains(string(buf), testString) { |
| 132 |
t.Fatalf("The log doesn't contain the string %s", testString) |
| 133 |
} |
| 134 |
|
| 135 |
/* */ |
| 136 |
core.RunLog = "" |
| 137 |
ReloadRuntimeLogs() |
| 138 |
|
| 139 |
if rlogger.f != os.Stderr { |
| 140 |
t.Fatalf("The output is expected to be Stderr") |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
func TestOpenLogFile(t *testing.T) { |
| 146 |
path := t.TempDir() |
| 147 |
|
| 148 |
f, newfile, err := openLogFile(path + "/test1.log") |
| 149 |
if err != nil { |
| 150 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 151 |
} |
| 152 |
|
| 153 |
if newfile == false { |
| 154 |
t.Fatalf("Expected new file") |
| 155 |
} |
| 156 |
|
| 157 |
content := []byte("It works!") |
| 158 |
n, err := f.Write(content) |
| 159 |
if err != nil { |
| 160 |
t.Fatalf("Unexpected write error: %s", err.Error()) |
| 161 |
} |
| 162 |
if n != len(content) { |
| 163 |
t.Fatalf("Invalid number of bytes written") |
| 164 |
} |
| 165 |
f.Close() |
| 166 |
|
| 167 |
/* Reopen file to check newfile */ |
| 168 |
f, newfile, err = openLogFile(path + "/test1.log") |
| 169 |
if err != nil { |
| 170 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 171 |
} |
| 172 |
|
| 173 |
if newfile == true { |
| 174 |
t.Fatalf("Expected newfile to be false") |
| 175 |
} |
| 176 |
f.Close() |
| 177 |
|
| 178 |
/* Open invalid file */ |
| 179 |
f, _, err = openLogFile("") |
| 180 |
if err == nil { |
| 181 |
t.Fatalf("Error expected while opening invalid file") |
| 182 |
} |
| 183 |
f.Close() |
| 184 |
} |
| 185 |
|
| 186 |
func TestSetDownloadLogWriter(t *testing.T) { |
| 187 |
if dlogger.l != nil || dlogger.f != nil { |
| 188 |
t.Fatalf("Precondition failed") |
| 189 |
} |
| 190 |
|
| 191 |
var buf bytes.Buffer |
| 192 |
|
| 193 |
setDownloadLogWriter(&buf, true) |
| 194 |
|
| 195 |
if dlogger.l == nil { |
| 196 |
t.Fatalf("Logger not created") |
| 197 |
} |
| 198 |
|
| 199 |
if buf.Len() == 0 { |
| 200 |
t.Fatalf("Buffer empty, expected header") |
| 201 |
} |
| 202 |
|
| 203 |
if !strings.HasPrefix(buf.String(), "#") { |
| 204 |
t.Fatalf("Header doesn't starts with '#'") |
| 205 |
} |
| 206 |
|
| 207 |
buf.Reset() |
| 208 |
|
| 209 |
/* */ |
| 210 |
|
| 211 |
setDownloadLogWriter(&buf, false) |
| 212 |
|
| 213 |
if buf.Len() != 0 { |
| 214 |
t.Fatalf("Expected no content") |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestReloadDownloadLogs(t *testing.T) { |
| 219 |
// Not implemented because of GetConfig() |
| 220 |
// TODO need abstraction for GetConfig() |
| 221 |
} |
| 222 |
|
| 223 |
//type xResults struct { |
| 224 |
// FileInfo filesystem.FileInfo |
| 225 |
// MapURL string `json:"-"` |
| 226 |
// IP string |
| 227 |
// ClientInfo network.GeoIPRecord |
| 228 |
// MirrorList Mirrors |
| 229 |
// ExcludedList Mirrors `json:",omitempty"` |
| 230 |
// Fallback bool `json:",omitempty"` |
| 231 |
//} |
| 232 |
|
| 233 |
func TestLogDownload(t *testing.T) { |
| 234 |
var buf bytes.Buffer |
| 235 |
|
| 236 |
dlogger.Close() |
| 237 |
|
| 238 |
// The next line isn't supposed to crash. |
| 239 |
LogDownload("", "GET", 500, nil, nil) |
| 240 |
|
| 241 |
setDownloadLogWriter(&buf, true) |
| 242 |
|
| 243 |
buf.Reset() |
| 244 |
|
| 245 |
// The next few lines arent't supposed to crash. |
| 246 |
LogDownload("", "GET", 200, nil, nil) |
| 247 |
LogDownload("", "GET", 302, nil, nil) |
| 248 |
LogDownload("", "GET", 404, nil, nil) |
| 249 |
LogDownload("", "GET", 500, nil, nil) |
| 250 |
LogDownload("", "GET", 501, nil, nil) |
| 251 |
|
| 252 |
if c := strings.Count(buf.String(), "\n"); c != 5 { |
| 253 |
t.Fatalf("Invalid number of lines, got %d, expected 5", c) |
| 254 |
} |
| 255 |
|
| 256 |
buf.Reset() |
| 257 |
|
| 258 |
/* Test a log line with 200 status code */ |
| 259 |
p := &mirrors.Results{ |
| 260 |
FileInfo: filesystem.FileInfo{ |
| 261 |
Path: "/test/file.tgz", |
| 262 |
}, |
| 263 |
MirrorList: mirrors.Mirrors{ |
| 264 |
mirrors.Mirror{ |
| 265 |
ID: 1, |
| 266 |
Name: "m1", |
| 267 |
Asnum: 444, |
| 268 |
Distance: 99, |
| 269 |
CountryFields: []string{"FR", "UK", "DE"}, |
| 270 |
}, |
| 271 |
mirrors.Mirror{ |
| 272 |
ID: 2, |
| 273 |
Name: "m2", |
| 274 |
}, |
| 275 |
}, |
| 276 |
IP: "192.168.0.1", |
| 277 |
ClientInfo: network.GeoIPRecord{ |
| 278 |
ASNum: 444, |
| 279 |
}, |
| 280 |
Fallback: true, |
| 281 |
} |
| 282 |
|
| 283 |
LogDownload("JSON", "GET", 200, p, nil) |
| 284 |
|
| 285 |
expected := "JSON 200 GET \"/test/file.tgz\" ip:192.168.0.1 mirror:m1 fallback:true sameasn:444 distance:99.00km countries:FR,UK,DE\n" |
| 286 |
if !strings.HasSuffix(buf.String(), expected) { |
| 287 |
t.Fatalf("Invalid log line:\nGot:\n%#vs\nExpected:\n%#v", buf.String(), expected) |
| 288 |
} |
| 289 |
|
| 290 |
buf.Reset() |
| 291 |
|
| 292 |
/* Test a log line with 404 status code */ |
| 293 |
p = &mirrors.Results{ |
| 294 |
FileInfo: filesystem.FileInfo{ |
| 295 |
Path: "/test/file.tgz", |
| 296 |
}, |
| 297 |
IP: "192.168.0.1", |
| 298 |
} |
| 299 |
|
| 300 |
LogDownload("JSON", "GET", 404, p, nil) |
| 301 |
|
| 302 |
expected = "JSON 404 GET \"/test/file.tgz\" ip:192.168.0.1\n" |
| 303 |
if !strings.HasSuffix(buf.String(), expected) { |
| 304 |
t.Fatalf("Invalid log line:\nGot:\n%#vs\nExpected:\n%#v", buf.String(), expected) |
| 305 |
} |
| 306 |
|
| 307 |
buf.Reset() |
| 308 |
|
| 309 |
/* Test a log line with 500 status code */ |
| 310 |
p = &mirrors.Results{ |
| 311 |
MirrorList: mirrors.Mirrors{ |
| 312 |
mirrors.Mirror{ |
| 313 |
ID: 1, |
| 314 |
Name: "m1", |
| 315 |
}, |
| 316 |
mirrors.Mirror{ |
| 317 |
ID: 2, |
| 318 |
Name: "m2", |
| 319 |
}, |
| 320 |
}, |
| 321 |
} |
| 322 |
|
| 323 |
LogDownload("JSON", "GET", 500, p, errors.New("test error")) |
| 324 |
|
| 325 |
expected = "JSON 500 GET \"\" ip: mirror:m1 error:test error\n" |
| 326 |
if !strings.HasSuffix(buf.String(), expected) { |
| 327 |
t.Fatalf("Invalid log line:\nGot:\n%#vs\nExpected:\n%#v", buf.String(), expected) |
| 328 |
} |
| 329 |
|
| 330 |
buf.Reset() |
| 331 |
|
| 332 |
/* Test a log line with 501 status code */ |
| 333 |
p = &mirrors.Results{ |
| 334 |
FileInfo: filesystem.FileInfo{ |
| 335 |
Path: "/test/file.tgz", |
| 336 |
}, |
| 337 |
IP: "192.168.0.1", |
| 338 |
} |
| 339 |
|
| 340 |
LogDownload("JSON", "GET", 501, p, errors.New("test error")) |
| 341 |
|
| 342 |
expected = "JSON 501 GET \"/test/file.tgz\" ip:192.168.0.1 error:test error\n" |
| 343 |
if !strings.HasSuffix(buf.String(), expected) { |
| 344 |
t.Fatalf("Invalid log line:\nGot:\n%#vs\nExpected:\n%#v", buf.String(), expected) |
| 345 |
} |
| 346 |
|
| 347 |
buf.Reset() |
| 348 |
|
| 349 |
/* Make sure we don't trip when there's a %s (aka a "verb") in the request */ |
| 350 |
p = &mirrors.Results{ |
| 351 |
FileInfo: filesystem.FileInfo{ |
| 352 |
Path: "/test/%s/hacked", |
| 353 |
}, |
| 354 |
IP: "192.168.0.1", |
| 355 |
} |
| 356 |
|
| 357 |
LogDownload("JSON", "GET", 404, p, nil) |
| 358 |
|
| 359 |
expected = "JSON 404 GET \"/test/%s/hacked\" ip:192.168.0.1\n" |
| 360 |
if !strings.HasSuffix(buf.String(), expected) { |
| 361 |
t.Fatalf("Invalid log line:\nGot:\n%#vs\nExpected:\n%#v", buf.String(), expected) |
| 362 |
} |
| 363 |
|
| 364 |
buf.Reset() |
| 365 |
} |