mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
[file/bandwidth] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases [file/perm] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases - ADD function to parse form "rwx-wxr-x" or "-rwx-w-r-x" - ADD function to ParseFileMode to convert os.FileMode to file.Perm [file/progress] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases [ioutils/...] - UPDATE documentation: update enhanced README and TESTING guidelines - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases [logger/...] - UPDATE documentation: update enhanced README and TESTING guidelines - ADD documentation: add enhanced README and TESTING guidelines for sub packages - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases - UPDATE config: remove FileBufferSize from OptionFile (rework hookfile) - UPDATE fields: expose Store function in interface - REWORK hookfile: rework package, use aggregator to allow multi write and single file - FIX hookstderr: fix bug with NonColorable - FIX hookstdout: fix bug with NonColorable - FIX hookwriter: fix bug with NonColorable [network/protocol] - ADD function IsTCP, IsUDP, IsUnixLike to check type of protocol [runner] - FIX typo [socket] - UPDATE documentation: update enhanced README and TESTING guidelines - ADD documentation: add enhanced README and TESTING guidelines for sub packages - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases - REWORK server: use context compatible io.reader, io.writer, io.closer instead of reader / writer - REWORK server: simplify, optimize server - REMOVE reader, writer type - ADD context: add new interface in root socket interface to expose context interface that extend context, io reader/writer/closer, dediacted function to server (IsConnected, ...)
363 lines
8.6 KiB
Go
363 lines
8.6 KiB
Go
//go:build linux || darwin
|
|
|
|
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*
|
|
*/
|
|
|
|
package unixgram_test
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
|
|
libprm "github.com/nabbar/golib/file/perm"
|
|
libsck "github.com/nabbar/golib/socket"
|
|
scksrv "github.com/nabbar/golib/socket/server/unixgram"
|
|
)
|
|
|
|
var _ = Describe("Unix Datagram Server Basic Operations", func() {
|
|
var (
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
sockPath string
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
ctx, cancel = context.WithCancel(testCtx)
|
|
})
|
|
|
|
AfterEach(func() {
|
|
if cancel != nil {
|
|
cancel()
|
|
}
|
|
cleanupSocketFile(sockPath)
|
|
time.Sleep(50 * time.Millisecond) // Allow cleanup
|
|
})
|
|
|
|
Describe("Server Construction", func() {
|
|
Context("with valid configuration", func() {
|
|
It("should create server successfully", func() {
|
|
cfg := createBasicConfig()
|
|
handler := func(ctx libsck.Context) {}
|
|
|
|
srv, err := scksrv.New(nil, handler, cfg)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(srv).ToNot(BeNil())
|
|
})
|
|
|
|
It("should accept nil UpdateConn callback", func() {
|
|
cfg := createBasicConfig()
|
|
handler := func(ctx libsck.Context) {}
|
|
|
|
srv, err := scksrv.New(nil, handler, cfg)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(srv).ToNot(BeNil())
|
|
})
|
|
|
|
It("should accept custom UpdateConn callback", func() {
|
|
cfg := createBasicConfig()
|
|
handler := func(ctx libsck.Context) {}
|
|
updateConn := func(conn net.Conn) {}
|
|
|
|
srv, err := scksrv.New(updateConn, handler, cfg)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(srv).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with invalid configuration", func() {
|
|
It("should return error with nil handler", func() {
|
|
cfg := createBasicConfig()
|
|
|
|
srv, err := scksrv.New(nil, nil, cfg)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(srv).To(BeNil())
|
|
})
|
|
|
|
It("should return error with excessive GID", func() {
|
|
cfg := createBasicConfig()
|
|
cfg.GroupPerm = 99999 // Exceeds MaxGID
|
|
handler := func(ctx libsck.Context) {}
|
|
|
|
srv, err := scksrv.New(nil, handler, cfg)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(srv).To(BeNil())
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("Server State Management", func() {
|
|
var srv scksrv.ServerUnixGram
|
|
|
|
BeforeEach(func() {
|
|
handler := newTestHandler(false)
|
|
var err error
|
|
srv, sockPath, err = createServerWithHandler(handler.handler)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
if srv != nil && srv.IsRunning() {
|
|
_ = srv.Close()
|
|
}
|
|
})
|
|
|
|
Context("initial state", func() {
|
|
It("should not be running", func() {
|
|
Expect(srv.IsRunning()).To(BeFalse())
|
|
})
|
|
|
|
It("should be gone", func() {
|
|
Expect(srv.IsGone()).To(BeTrue())
|
|
})
|
|
|
|
It("should have zero connections", func() {
|
|
Expect(srv.OpenConnections()).To(Equal(int64(0)))
|
|
})
|
|
})
|
|
|
|
Context("after starting", func() {
|
|
BeforeEach(func() {
|
|
startServer(srv, ctx)
|
|
})
|
|
|
|
AfterEach(func() {
|
|
stopServer(srv, cancel)
|
|
})
|
|
|
|
It("should be running", func() {
|
|
Expect(srv.IsRunning()).To(BeTrue())
|
|
})
|
|
|
|
It("should not be gone", func() {
|
|
Expect(srv.IsGone()).To(BeFalse())
|
|
})
|
|
|
|
It("should have zero connections (datagram is stateless)", func() {
|
|
Expect(srv.OpenConnections()).To(Equal(int64(0)))
|
|
})
|
|
})
|
|
|
|
Context("after stopping", func() {
|
|
BeforeEach(func() {
|
|
startServer(srv, ctx)
|
|
stopServer(srv, cancel)
|
|
})
|
|
|
|
It("should not be running", func() {
|
|
Expect(srv.IsRunning()).To(BeFalse())
|
|
})
|
|
|
|
It("should be gone", func() {
|
|
Eventually(func() bool {
|
|
return srv.IsGone()
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
})
|
|
|
|
It("should have zero connections", func() {
|
|
Expect(srv.OpenConnections()).To(Equal(int64(0)))
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("Server Lifecycle", func() {
|
|
var srv scksrv.ServerUnixGram
|
|
|
|
BeforeEach(func() {
|
|
handler := newTestHandler(false)
|
|
var err error
|
|
srv, sockPath, err = createServerWithHandler(handler.handler)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
if srv != nil && srv.IsRunning() {
|
|
_ = srv.Close()
|
|
}
|
|
})
|
|
|
|
It("should start successfully", func() {
|
|
go func() {
|
|
_ = srv.Listen(ctx)
|
|
}()
|
|
|
|
Eventually(func() bool {
|
|
return srv.IsRunning()
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
|
|
cancel()
|
|
})
|
|
|
|
It("should stop via context cancellation", func() {
|
|
startServer(srv, ctx)
|
|
|
|
cancel()
|
|
|
|
Eventually(func() bool {
|
|
return !srv.IsRunning()
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
})
|
|
|
|
It("should stop via Shutdown method", func() {
|
|
startServer(srv, ctx)
|
|
|
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer shutdownCancel()
|
|
|
|
err := srv.Shutdown(shutdownCtx)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(srv.IsRunning()).To(BeFalse())
|
|
Expect(srv.IsGone()).To(BeTrue())
|
|
})
|
|
|
|
It("should stop via Close method", func() {
|
|
startServer(srv, ctx)
|
|
|
|
err := srv.Close()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Eventually(func() bool {
|
|
return !srv.IsRunning()
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
})
|
|
|
|
It("should allow multiple Close calls", func() {
|
|
startServer(srv, ctx)
|
|
|
|
err1 := srv.Close()
|
|
Expect(err1).ToNot(HaveOccurred())
|
|
|
|
Eventually(func() bool {
|
|
return srv.IsGone()
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
|
|
err2 := srv.Close()
|
|
Expect(err2).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("should create socket file on start", func() {
|
|
startServer(srv, ctx)
|
|
|
|
Eventually(func() bool {
|
|
return fileExists(sockPath)
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
|
|
cancel()
|
|
})
|
|
|
|
It("should remove socket file on stop", func() {
|
|
startServer(srv, ctx)
|
|
|
|
Eventually(func() bool {
|
|
return fileExists(sockPath)
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
|
|
stopServer(srv, cancel)
|
|
|
|
Eventually(func() bool {
|
|
return !fileExists(sockPath)
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
})
|
|
})
|
|
|
|
Describe("Interface Implementation", func() {
|
|
var srv scksrv.ServerUnixGram
|
|
|
|
BeforeEach(func() {
|
|
handler := newTestHandler(false)
|
|
var err error
|
|
srv, sockPath, err = createServerWithHandler(handler.handler)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("should implement Server interface", func() {
|
|
var _ libsck.Server = srv
|
|
})
|
|
|
|
It("should provide IsRunning method", func() {
|
|
Expect(srv.IsRunning()).To(BeFalse())
|
|
})
|
|
|
|
It("should provide IsGone method", func() {
|
|
Expect(srv.IsGone()).To(BeTrue())
|
|
})
|
|
|
|
It("should provide OpenConnections method", func() {
|
|
Expect(srv.OpenConnections()).To(Equal(int64(0)))
|
|
})
|
|
|
|
It("should provide RegisterSocket method", func() {
|
|
cfg := createBasicConfig()
|
|
err := srv.RegisterSocket(cfg.Address, libprm.Perm(0600), -1)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("should provide SetTLS method (no-op for Unix sockets)", func() {
|
|
err := srv.SetTLS(true, nil)
|
|
Expect(err).ToNot(HaveOccurred()) // Always nil for Unix sockets
|
|
})
|
|
})
|
|
|
|
Describe("Socket File Permissions", func() {
|
|
var srv scksrv.ServerUnixGram
|
|
|
|
AfterEach(func() {
|
|
if srv != nil && srv.IsRunning() {
|
|
_ = srv.Close()
|
|
}
|
|
})
|
|
|
|
It("should apply configured file permissions", func() {
|
|
cfg := createBasicConfig()
|
|
cfg.PermFile = libprm.Perm(0660)
|
|
sockPath = cfg.Address
|
|
|
|
handler := func(ctx libsck.Context) {}
|
|
var err error
|
|
srv, err = scksrv.New(nil, handler, cfg)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
startServer(srv, ctx)
|
|
|
|
Eventually(func() bool {
|
|
perm, err := checkFilePermissions(sockPath)
|
|
return err == nil && perm == 0660
|
|
}, 2*time.Second, 10*time.Millisecond).Should(BeTrue())
|
|
|
|
cancel()
|
|
})
|
|
})
|
|
})
|