github.com/TeaOSLab/EdgeNode@v1.3.8/internal/iplibrary/ip_list_sqlite_test.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package iplibrary_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
     7  	"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
     8  	_ "github.com/iwind/TeaGo/bootstrap"
     9  	"github.com/iwind/TeaGo/logs"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestSQLiteIPList_AddItem(t *testing.T) {
    15  	db, err := iplibrary.NewSQLiteIPList()
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer func() {
    20  		_ = db.Close()
    21  	}()
    22  
    23  	err = db.AddItem(&pb.IPItem{
    24  		Id:                            1,
    25  		IpFrom:                        "192.168.1.101",
    26  		IpTo:                          "",
    27  		Version:                       1024,
    28  		ExpiredAt:                     time.Now().Unix() + 3600,
    29  		Reason:                        "",
    30  		ListId:                        2,
    31  		IsDeleted:                     false,
    32  		Type:                          "ipv4",
    33  		EventLevel:                    "error",
    34  		ListType:                      "black",
    35  		IsGlobal:                      true,
    36  		CreatedAt:                     0,
    37  		NodeId:                        11,
    38  		ServerId:                      22,
    39  		SourceNodeId:                  0,
    40  		SourceServerId:                0,
    41  		SourceHTTPFirewallPolicyId:    0,
    42  		SourceHTTPFirewallRuleGroupId: 0,
    43  		SourceHTTPFirewallRuleSetId:   0,
    44  		SourceServer:                  nil,
    45  		SourceHTTPFirewallPolicy:      nil,
    46  		SourceHTTPFirewallRuleGroup:   nil,
    47  		SourceHTTPFirewallRuleSet:     nil,
    48  	})
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	err = db.Close()
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	t.Log("ok")
    59  }
    60  
    61  func TestSQLiteIPList_ReadItems(t *testing.T) {
    62  	db, err := iplibrary.NewSQLiteIPList()
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	defer func() {
    67  		_ = db.Close()
    68  	}()
    69  
    70  	defer func() {
    71  		_ = db.Close()
    72  	}()
    73  
    74  	items, goNext, err := db.ReadItems(0, 2)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	t.Log("goNext:", goNext)
    79  	logs.PrintAsJSON(items, t)
    80  }
    81  
    82  func TestSQLiteIPList_ReadMaxVersion(t *testing.T) {
    83  	db, err := iplibrary.NewSQLiteIPList()
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	defer func() {
    88  		_ = db.Close()
    89  	}()
    90  	t.Log(db.ReadMaxVersion())
    91  }
    92  
    93  func TestSQLiteIPList_UpdateMaxVersion(t *testing.T) {
    94  	db, err := iplibrary.NewSQLiteIPList()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	defer func() {
    99  		_ = db.Close()
   100  	}()
   101  
   102  	err = db.UpdateMaxVersion(1027)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	t.Log(db.ReadMaxVersion())
   107  }