2022-03-30 01:46:21 +05:30
package variables
import (
"testing"
2022-04-07 07:53:01 +02:00
"time"
2022-03-30 01:46:21 +05:30
2025-02-11 04:31:37 +07:00
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
2022-03-30 01:46:21 +05:30
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestVariablesEvaluate ( t * testing . T ) {
2022-07-11 22:18:13 +05:30
data := ` a2 : "{{md5('test')}}"
2022-04-20 15:41:21 +05:30
a3 : "this_is_random_text"
2022-06-08 17:43:52 +03:00
a4 : "{{date_time('%Y-%M-%D')}}"
2022-04-20 15:41:21 +05:30
a5 : "{{reverse(hostname)}}"
a6 : "123456" `
2022-03-30 01:46:21 +05:30
variables := Variable { }
err := yaml . Unmarshal ( [ ] byte ( data ) , & variables )
require . NoError ( t , err , "could not unmarshal variables" )
result := variables . Evaluate ( map [ string ] interface { } { "hostname" : "google.com" } )
2022-04-07 07:53:01 +02:00
a4 := time . Now ( ) . Format ( "2006-01-02" )
2022-07-11 22:18:13 +05:30
require . Equal ( t , map [ string ] interface { } { "a2" : "098f6bcd4621d373cade4e832627b4f6" , "a3" : "this_is_random_text" , "a4" : a4 , "a5" : "moc.elgoog" , "a6" : "123456" } , result , "could not get correct elements" )
2023-02-22 11:15:55 +08:00
// json
data = ` {
"a2" : "{{md5('test')}}" ,
"a3" : "this_is_random_text" ,
"a4" : "{{date_time('%Y-%M-%D')}}" ,
"a5" : "{{reverse(hostname)}}" ,
"a6" : "123456"
} `
variables = Variable { }
err = json . Unmarshal ( [ ] byte ( data ) , & variables )
require . NoError ( t , err , "could not unmarshal json variables" )
result = variables . Evaluate ( map [ string ] interface { } { "hostname" : "google.com" } )
a4 = time . Now ( ) . Format ( "2006-01-02" )
require . Equal ( t , map [ string ] interface { } { "a2" : "098f6bcd4621d373cade4e832627b4f6" , "a3" : "this_is_random_text" , "a4" : a4 , "a5" : "moc.elgoog" , "a6" : "123456" } , result , "could not get correct elements" )
2022-03-30 01:46:21 +05:30
}