57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
// Copyright 2022 The Ip2Region Authors. All rights reserved.
|
|
// Use of this source code is governed by a Apache2.0-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package xdb
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNewHeaderShortInput(t *testing.T) {
|
|
// input shorter than 20 bytes should be rejected
|
|
short := make([]byte, 16)
|
|
_, err := NewHeader(short)
|
|
if err == nil {
|
|
t.Fatal("NewHeader with 16 bytes should fail")
|
|
}
|
|
|
|
// 20 bytes should be accepted
|
|
valid := make([]byte, 20)
|
|
_, err = NewHeader(valid)
|
|
if err != nil {
|
|
t.Fatalf("NewHeader with 20 bytes should succeed: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestNewHeaderParsing(t *testing.T) {
|
|
// construct a known 20-byte header
|
|
input := []byte{
|
|
0x03, 0x00, // Version = 3 (LE)
|
|
0x01, 0x00, // IndexPolicy = 1 (LE)
|
|
0x00, 0x00, 0x00, 0x00, // CreatedAt = 0
|
|
0x00, 0x00, 0x00, 0x00, // StartIndexPtr = 0
|
|
0x00, 0x00, 0x00, 0x00, // EndIndexPtr = 0
|
|
0x04, 0x00, // IPVersion = 4
|
|
0x04, 0x00, // RuntimePtrBytes = 4
|
|
}
|
|
|
|
h, err := NewHeader(input)
|
|
if err != nil {
|
|
t.Fatalf("NewHeader failed: %s", err)
|
|
}
|
|
|
|
if h.Version != 3 {
|
|
t.Errorf("Version = %d, want 3", h.Version)
|
|
}
|
|
if h.IndexPolicy != 1 {
|
|
t.Errorf("IndexPolicy = %d, want 1", h.IndexPolicy)
|
|
}
|
|
if h.IPVersion != 4 {
|
|
t.Errorf("IPVersion = %d, want 4", h.IPVersion)
|
|
}
|
|
if h.RuntimePtrBytes != 4 {
|
|
t.Errorf("RuntimePtrBytes = %d, want 4", h.RuntimePtrBytes)
|
|
}
|
|
}
|