func TestLZ4Compression(t *testing.T) { c := new(rawConnection) for i := 0; i < 10; i++ { dataLen := 150 + rand.Intn(150) data := make([]byte, dataLen) _, err := io.ReadFull(rand.Reader, data[100:]) if err != nil { t.Fatal(err) } comp, err := c.lz4Compress(data) if err != nil { t.Errorf("compressing %d bytes: %v", dataLen, err) continue } res, err := c.lz4Decompress(comp) if err != nil { t.Errorf("decompressing %d bytes to %d: %v", len(comp), dataLen, err) continue } if len(res) != len(data) { t.Errorf("Incorrect len %d != expected %d", len(res), len(data)) } if !bytes.Equal(data, res) { t.Error("Incorrect decompressed data") } t.Logf("OK #%d, %d -> %d -> %d", i, dataLen, len(comp), dataLen) } }
// addFiles adds files with random Sequence to the Sorter. func addFiles(n int, s IndexSorter) { for i := 0; i < n; i++ { rnd := rand.Int63() f := protocol.FileInfo{ Name: fmt.Sprintf("file-%d", rnd), Size: rand.Int63(), Permissions: uint32(rand.Intn(0777)), ModifiedS: rand.Int63(), ModifiedNs: int32(rand.Int63()), Sequence: rnd, Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: uint64(rand.Int63())}}}, Blocks: []protocol.BlockInfo{{ Size: int32(rand.Intn(128 << 10)), Hash: []byte(rand.String(32)), }}, } s.Append(f) } }
func basicAuthAndSessionMiddleware(cookieName string, cfg config.GUIConfiguration, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if cfg.IsValidAPIKey(r.Header.Get("X-API-Key")) { next.ServeHTTP(w, r) return } cookie, err := r.Cookie(cookieName) if err == nil && cookie != nil { sessionsMut.Lock() _, ok := sessions[cookie.Value] sessionsMut.Unlock() if ok { next.ServeHTTP(w, r) return } } httpl.Debugln("Sessionless HTTP request with authentication; this is expensive.") error := func() { time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond) w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"") http.Error(w, "Not Authorized", http.StatusUnauthorized) } hdr := r.Header.Get("Authorization") if !strings.HasPrefix(hdr, "Basic ") { error() return } hdr = hdr[6:] bs, err := base64.StdEncoding.DecodeString(hdr) if err != nil { error() return } fields := bytes.SplitN(bs, []byte(":"), 2) if len(fields) != 2 { error() return } // Check if the username is correct, assuming it was sent as UTF-8 username := string(fields[0]) if username == cfg.User { goto usernameOK } // ... check it again, converting it from assumed ISO-8859-1 to UTF-8 username = string(iso88591ToUTF8(fields[0])) if username == cfg.User { goto usernameOK } // Neither of the possible interpretations match the configured username emitLoginAttempt(false, username) error() return usernameOK: // Check password as given (assumes UTF-8 encoding) password := fields[1] if err := bcrypt.CompareHashAndPassword([]byte(cfg.Password), password); err == nil { goto passwordOK } // ... check it again, converting it from assumed ISO-8859-1 to UTF-8 password = iso88591ToUTF8(password) if err := bcrypt.CompareHashAndPassword([]byte(cfg.Password), password); err == nil { goto passwordOK } // Neither of the attempts to verify the password checked out emitLoginAttempt(false, username) error() return passwordOK: sessionid := rand.String(32) sessionsMut.Lock() sessions[sessionid] = true sessionsMut.Unlock() http.SetCookie(w, &http.Cookie{ Name: cookieName, Value: sessionid, MaxAge: 0, }) emitLoginAttempt(true, username) next.ServeHTTP(w, r) }) }