/ common / fingerprints / parser / parser_test.go
parser_test.go
  1  // Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved.
  2  //
  3  // Licensed under the Apache License, Version 2.0 (the "License");
  4  // you may not use this file except in compliance with the License.
  5  // You may obtain a copy of the License at
  6  //
  7  //     http://www.apache.org/licenses/LICENSE-2.0
  8  //
  9  // Unless required by applicable law or agreed to in writing, software
 10  // distributed under the License is distributed on an "AS IS" BASIS,
 11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  // See the License for the specific language governing permissions and
 13  // limitations under the License.
 14  //
 15  // Requirement: Any integration or derivative work must explicitly attribute
 16  // Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its
 17  // documentation or user interface, as detailed in the NOTICE file.
 18  
 19  package parser
 20  
 21  import (
 22  	"github.com/Tencent/AI-Infra-Guard/pkg/httpx"
 23  	"github.com/projectdiscovery/fastdialer/fastdialer"
 24  	"github.com/stretchr/testify/assert"
 25  	"testing"
 26  	"time"
 27  )
 28  
 29  func TestSingleRule(t *testing.T) {
 30  	rule := "body~=\"123123\" && (body == \"title\" || header=\"X-Powered-By: Express\")"
 31  	config := &Config{
 32  		Body:   "1111231232233",
 33  		Header: "Server: nginx\r\nX-Powered-By: Express\r\n",
 34  		Icon:   23333,
 35  	}
 36  	tokens, err := ParseTokens(rule)
 37  	if err != nil {
 38  		t.Fatal(err)
 39  	}
 40  	if err = CheckBalance(tokens); err != nil {
 41  		t.Fatal(err)
 42  	}
 43  	dsl, err := TransFormExp(tokens)
 44  	if err != nil {
 45  		t.Fatal(err)
 46  	}
 47  	assert.True(t, dsl.Eval(config))
 48  }
 49  
 50  func TestSingleRuleForParse(t *testing.T) {
 51  	dialer, err := fastdialer.NewDialer(fastdialer.DefaultOptions)
 52  	assert.NoError(t, err)
 53  	httpOptions := &httpx.HTTPOptions{
 54  		Timeout:          time.Duration(30) * time.Second,
 55  		RetryMax:         3,
 56  		FollowRedirects:  false,
 57  		HTTPProxy:        "",
 58  		Unsafe:           false,
 59  		DefaultUserAgent: httpx.GetRandomUserAgent(),
 60  		Dialer:           dialer,
 61  	}
 62  	hp, err := httpx.NewHttpx(httpOptions)
 63  	assert.NoError(t, err)
 64  	resp, err := hp.Get("https://security.tencent.com/index.php", nil)
 65  	config := &Config{
 66  		Body:   resp.DataStr,
 67  		Header: resp.GetHeaderRaw(),
 68  		Icon:   3444,
 69  	}
 70  	rule := "header=\"nginx\" || header=\"X-Powered-By: Express\""
 71  	fp, err := transfromRule(rule)
 72  	assert.NoError(t, err)
 73  	x := fp.Eval(config)
 74  	t.Log(x)
 75  }
 76  
 77  func TestParseAdvisorTokens(t *testing.T) {
 78  	tokens, err := ParseAdvisorTokens(`version > "1.2.3" && version < "2.3.dev"`)
 79  	assert.NoError(t, err)
 80  	err = CheckBalance(tokens)
 81  	assert.NoError(t, err)
 82  	dsl, err := TransFormExp(tokens)
 83  	assert.NoError(t, err)
 84  	config := &AdvisoryConfig{
 85  		Version: "1.3",
 86  	}
 87  	b := dsl.AdvisoryEval(config)
 88  	t.Log(b)
 89  	//assert.Equal(t, dsl.AdvisoryEval(config), true)
 90  }
 91  
 92  func TestParseAdvisorLatestTokens(t *testing.T) {
 93  	tokens, err := ParseAdvisorTokens(`version > "0" && version < "latest"`)
 94  	assert.NoError(t, err)
 95  	err = CheckBalance(tokens)
 96  	assert.NoError(t, err)
 97  	dsl, err := TransFormExp(tokens)
 98  	assert.NoError(t, err)
 99  	config := &AdvisoryConfig{
100  		Version: "1.3",
101  	}
102  	b := dsl.AdvisoryEval(config)
103  	t.Log(b)
104  	//assert.Equal(t, dsl.AdvisoryEval(config), true)
105  }