1+ #pragma once
2+
3+ #include " pch.h"
4+
5+ #include < util.h>
6+
7+ using namespace std ;
8+
9+ namespace test_built_in_functions
10+ {
11+ TEST_CLASS (TestBuiltInFunctions)
12+ {
13+ public:
14+ void TestKeyMatch (string key1, string key2, bool res) {
15+ Scope scope = InitializeScope ();
16+ PushStringValue (scope, key1);
17+ PushStringValue (scope, key2);
18+
19+ KeyMatch (scope);
20+ bool my_res = GetBoolean (scope);
21+
22+ Assert::AreEqual (res, my_res);
23+ }
24+
25+ TEST_METHOD (TestKeyMatch) {
26+ TestKeyMatch (" /foo" , " /foo" , true );
27+ TestKeyMatch (" /foo" , " /foo*" , true );
28+ TestKeyMatch (" /foo" , " /foo/*" , false );
29+ TestKeyMatch (" /foo/bar" , " /foo" , false );
30+ TestKeyMatch (" /foo/bar" , " /foo*" , true );
31+ TestKeyMatch (" /foo/bar" , " /foo/*" , true );
32+ TestKeyMatch (" /foobar" , " /foo" , false );
33+ TestKeyMatch (" /foobar" , " /foo*" , true );
34+ TestKeyMatch (" /foobar" , " /foo/*" , false );
35+ }
36+
37+ void TestKeyMatch2 (string key1, string key2, bool res) {
38+ Scope scope = InitializeScope ();
39+ PushStringValue (scope, key1);
40+ PushStringValue (scope, key2);
41+
42+ KeyMatch2 (scope);
43+ bool my_res = GetBoolean (scope);
44+
45+ Assert::AreEqual (res, my_res);
46+ }
47+
48+ TEST_METHOD (TestKeyMatch2){
49+ TestKeyMatch2 (" /foo" , " /foo" , true );
50+ TestKeyMatch2 (" /foo" , " /foo*" , true );
51+ TestKeyMatch2 (" /foo" , " /foo/*" , false );
52+ TestKeyMatch2 (" /foo/bar" , " /foo" , false );
53+ TestKeyMatch2 (" /foo/bar" , " /foo*" , false ); // different with KeyMatch.
54+ TestKeyMatch2 (" /foo/bar" , " /foo/*" , true );
55+ TestKeyMatch2 (" /foobar" , " /foo" , false );
56+ TestKeyMatch2 (" /foobar" , " /foo*" , false ); // different with KeyMatch.
57+ TestKeyMatch2 (" /foobar" , " /foo/*" , false );
58+
59+ TestKeyMatch2 (" /" , " /:resource" , false );
60+ TestKeyMatch2 (" /resource1" , " /:resource" , true );
61+ TestKeyMatch2 (" /myid" , " /:id/using/:resId" , false );
62+ TestKeyMatch2 (" /myid/using/myresid" , " /:id/using/:resId" , true );
63+
64+ TestKeyMatch2 (" /proxy/myid" , " /proxy/:id/*" , false );
65+ TestKeyMatch2 (" /proxy/myid/" , " /proxy/:id/*" , true );
66+ TestKeyMatch2 (" /proxy/myid/res" , " /proxy/:id/*" , true );
67+ TestKeyMatch2 (" /proxy/myid/res/res2" , " /proxy/:id/*" , true );
68+ TestKeyMatch2 (" /proxy/myid/res/res2/res3" , " /proxy/:id/*" , true );
69+ TestKeyMatch2 (" /proxy/" , " /proxy/:id/*" , false );
70+
71+ TestKeyMatch2 (" /alice" , " /:id" , true );
72+ TestKeyMatch2 (" /alice/all" , " /:id/all" , true );
73+ TestKeyMatch2 (" /alice" , " /:id/all" , false );
74+ TestKeyMatch2 (" /alice/all" , " /:id" , false );
75+
76+ TestKeyMatch2 (" /alice/all" , " /:/all" , false );
77+ }
78+
79+ void TestKeyMatch3 (string key1, string key2, bool res) {
80+ Scope scope = InitializeScope ();
81+ PushStringValue (scope, key1);
82+ PushStringValue (scope, key2);
83+
84+ KeyMatch3 (scope);
85+ bool my_res = GetBoolean (scope);
86+
87+ Assert::AreEqual (res, my_res);
88+ }
89+
90+ TEST_METHOD (TestKeyMatch3){
91+ // keyMatch3() is similar with KeyMatch2(), except using "/proxy/{id}" instead of "/proxy/:id".
92+ TestKeyMatch3 (" /foo" , " /foo" , true );
93+ TestKeyMatch3 (" /foo" , " /foo*" , true );
94+ TestKeyMatch3 (" /foo" , " /foo/*" , false );
95+ TestKeyMatch3 (" /foo/bar" , " /foo" , false );
96+ TestKeyMatch3 (" /foo/bar" , " /foo*" , false );
97+ TestKeyMatch3 (" /foo/bar" , " /foo/*" , true );
98+ TestKeyMatch3 (" /foobar" , " /foo" , false );
99+ TestKeyMatch3 (" /foobar" , " /foo*" , false );
100+ TestKeyMatch3 (" /foobar" , " /foo/*" , false );
101+
102+ TestKeyMatch3 (" /" , " /{resource}" , false );
103+ TestKeyMatch3 (" /resource1" , " /{resource}" , true );
104+ TestKeyMatch3 (" /myid" , " /{id}/using/{resId}" , false );
105+ TestKeyMatch3 (" /myid/using/myresid" , " /{id}/using/{resId}" , true );
106+
107+ TestKeyMatch3 (" /proxy/myid" , " /proxy/{id}/*" , false );
108+ TestKeyMatch3 (" /proxy/myid/" , " /proxy/{id}/*" , true );
109+ TestKeyMatch3 (" /proxy/myid/res" , " /proxy/{id}/*" , true );
110+ TestKeyMatch3 (" /proxy/myid/res/res2" , " /proxy/{id}/*" , true );
111+ TestKeyMatch3 (" /proxy/myid/res/res2/res3" , " /proxy/{id}/*" , true );
112+ TestKeyMatch3 (" /proxy/" , " /proxy/{id}/*" , false );
113+
114+ TestKeyMatch3 (" /myid/using/myresid" , " /{id/using/{resId}" , false );
115+ }
116+
117+ void TestRegexMatch (string key1, string key2, bool res) {
118+ Scope scope = InitializeScope ();
119+ PushStringValue (scope, key1);
120+ PushStringValue (scope, key2);
121+
122+ RegexMatch (scope);
123+ bool my_res = GetBoolean (scope);
124+
125+ Assert::AreEqual (res, my_res);
126+ }
127+
128+ TEST_METHOD (TestRegexMatch) {
129+ TestRegexMatch (" /topic/create" , " /topic/create" , true );
130+ TestRegexMatch (" /topic/create/123" , " /topic/create" , false );
131+ TestRegexMatch (" /topic/delete" , " /topic/create" , false );
132+ TestRegexMatch (" /topic/edit" , " /topic/edit/[0-9]+" , false );
133+ TestRegexMatch (" /topic/edit/123" , " /topic/edit/[0-9]+" , true );
134+ TestRegexMatch (" /topic/edit/abc" , " /topic/edit/[0-9]+" , false );
135+ TestRegexMatch (" /foo/delete/123" , " /topic/delete/[0-9]+" , false );
136+ TestRegexMatch (" /topic/delete/0" , " /topic/delete/[0-9]+" , true );
137+ TestRegexMatch (" /topic/edit/123s" , " /topic/delete/[0-9]+" , false );
138+ }
139+
140+ void TestIPMatch (string ip1, string ip2, bool res) {
141+ Scope scope = InitializeScope ();
142+ PushStringValue (scope, ip1);
143+ PushStringValue (scope, ip2);
144+
145+ IPMatch (scope);
146+ bool my_res = GetBoolean (scope);
147+
148+ Assert::AreEqual (res, my_res);
149+ }
150+
151+ TEST_METHOD (TestIPMatch) {
152+ TestIPMatch (" 192.168.2.123" , " 192.168.2.0/24" , true );
153+ TestIPMatch (" 192.168.2.123" , " 192.168.3.0/24" , false );
154+ TestIPMatch (" 192.168.2.123" , " 192.168.2.0/16" , true );
155+ TestIPMatch (" 192.168.2.123" , " 192.168.2.123/32" , true );
156+ TestIPMatch (" 10.0.0.11" , " 10.0.0.0/8" , true );
157+ TestIPMatch (" 11.0.0.123" , " 10.0.0.0/8" , false );
158+ }
159+ };
160+ }
0 commit comments