@@ -155,6 +155,125 @@ TEST(TestEnforcer, TestMapParams) {
155155 ASSERT_EQ (e.Enforce (params), true );
156156}
157157
158+ TEST (TestEnforcer, TestJsonData) {
159+ using json = nlohmann::json;
160+ casbin::Enforcer e (abac_rule_model_path, abac_rule_policy_path);
161+
162+ // Test with JSON object containing Age property
163+ // Policy: p, r.sub.Age > 18, /data1, read
164+ auto sub_json_adult = std::make_shared<json>(json{
165+ {" Name" , " alice" },
166+ {" Age" , 30 }
167+ });
168+
169+ casbin::DataMap params = {
170+ {" sub" , sub_json_adult},
171+ {" obj" , " /data1" },
172+ {" act" , " read" }
173+ };
174+
175+ // Should be allowed: Age 30 > 18
176+ ASSERT_EQ (e.Enforce (params), true );
177+
178+ // Test with minor (Age < 18)
179+ auto sub_json_minor = std::make_shared<json>(json{
180+ {" Name" , " bob" },
181+ {" Age" , 16 }
182+ });
183+
184+ params = {
185+ {" sub" , sub_json_minor},
186+ {" obj" , " /data1" },
187+ {" act" , " read" }
188+ };
189+
190+ // Should be denied: Age 16 < 18
191+ ASSERT_EQ (e.Enforce (params), false );
192+
193+ // Test policy: p, r.sub.Age < 60, /data2, write
194+ auto sub_json_young = std::make_shared<json>(json{
195+ {" Name" , " charlie" },
196+ {" Age" , 45 }
197+ });
198+
199+ params = {
200+ {" sub" , sub_json_young},
201+ {" obj" , " /data2" },
202+ {" act" , " write" }
203+ };
204+
205+ // Should be allowed: Age 45 < 60
206+ ASSERT_EQ (e.Enforce (params), true );
207+
208+ // Test with senior (Age >= 60)
209+ auto sub_json_senior = std::make_shared<json>(json{
210+ {" Name" , " dave" },
211+ {" Age" , 65 }
212+ });
213+
214+ params = {
215+ {" sub" , sub_json_senior},
216+ {" obj" , " /data2" },
217+ {" act" , " write" }
218+ };
219+
220+ // Should be denied: Age 65 >= 60
221+ ASSERT_EQ (e.Enforce (params), false );
222+ }
223+
224+ TEST (TestEnforcer, TestComplexJsonData) {
225+ using json = nlohmann::json;
226+
227+ // Create a custom model and policy for testing complex JSON structures
228+ // similar to the issue example
229+ casbin::Enforcer e (abac_rule_model_path, abac_rule_policy_path);
230+
231+ // Test with complex nested JSON object like in the issue
232+ auto complex_request = std::make_shared<json>(json{
233+ {" ID" , " zk" },
234+ {" proxy" , " vpn" },
235+ {" Department" , " nlp" },
236+ {" month" , " Jan" },
237+ {" week" , " Mon" },
238+ {" time" , " morning" },
239+ {" Longitude" , 123 },
240+ {" Latitude" , 456 },
241+ {" Altitude" , 789 },
242+ {" OS" , " HarmonyOS" },
243+ {" CPU" , " XeonPlatinum8480+" },
244+ {" NetworkType" , " WLan" },
245+ {" ProtocolType" , " Bluetooth" },
246+ {" EncryptionType" , " 3DES" },
247+ {" SecurityProtocol" , " HTTPS" },
248+ {" Age" , 25 } // For testing with existing policy
249+ });
250+
251+ casbin::DataMap params = {
252+ {" sub" , complex_request},
253+ {" obj" , " /data1" },
254+ {" act" , " read" }
255+ };
256+
257+ // Should work with complex JSON - Age 25 > 18
258+ ASSERT_EQ (e.Enforce (params), true );
259+
260+ // Test with object that also contains nested objects
261+ auto obj_json = std::make_shared<json>(json{
262+ {" SecurityLevel" , 3 },
263+ {" Source" , " ISS" },
264+ {" DistributionMethod" , " C" }
265+ });
266+
267+ params = {
268+ {" sub" , complex_request},
269+ {" obj" , obj_json},
270+ {" act" , " read" }
271+ };
272+
273+ // This should not crash even with JSON in multiple parameters
274+ ASSERT_NO_THROW (e.Enforce (params));
275+ }
276+
158277template <typename T>
159278void TestEnforceEx (casbin::Enforcer& e, T&& params, const bool expect_result, const std::vector<std::string>& expect_explain) {
160279 std::vector<std::string> actual_explain;
0 commit comments