1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #include "mongo/platform/basic.h"
32 
33 #include "mongo/db/matcher/expression.h"
34 #include "mongo/db/matcher/schema/expression_internal_schema_max_properties.h"
35 #include "mongo/db/matcher/schema/expression_internal_schema_min_properties.h"
36 #include "mongo/unittest/unittest.h"
37 
38 namespace mongo {
39 
40 namespace {
41 
TEST(InternalSchemaMaxPropertiesMatchExpression,RejectsObjectsWithTooManyElements)42 TEST(InternalSchemaMaxPropertiesMatchExpression, RejectsObjectsWithTooManyElements) {
43     InternalSchemaMaxPropertiesMatchExpression maxProperties;
44     ASSERT_OK(maxProperties.init(0));
45 
46     ASSERT_FALSE(maxProperties.matchesBSON(BSON("b" << 21)));
47     ASSERT_FALSE(maxProperties.matchesBSON(BSON("b" << 21 << "c" << 3)));
48 }
49 
TEST(InternalSchemaMaxPropertiesMatchExpression,AcceptsObjectWithLessThanOrEqualToMaxElements)50 TEST(InternalSchemaMaxPropertiesMatchExpression, AcceptsObjectWithLessThanOrEqualToMaxElements) {
51     InternalSchemaMaxPropertiesMatchExpression maxProperties;
52     ASSERT_OK(maxProperties.init(2));
53 
54     ASSERT_TRUE(maxProperties.matchesBSON(BSONObj()));
55     ASSERT_TRUE(maxProperties.matchesBSON(BSON("b" << BSONNULL)));
56     ASSERT_TRUE(maxProperties.matchesBSON(BSON("b" << 21)));
57     ASSERT_TRUE(maxProperties.matchesBSON(BSON("b" << 21 << "c" << 3)));
58 }
59 
TEST(InternalSchemaMaxPropertiesMatchExpression,MaxPropertiesZeroAllowsEmptyObjects)60 TEST(InternalSchemaMaxPropertiesMatchExpression, MaxPropertiesZeroAllowsEmptyObjects) {
61     InternalSchemaMaxPropertiesMatchExpression maxProperties;
62     ASSERT_OK(maxProperties.init(0));
63 
64     ASSERT_TRUE(maxProperties.matchesBSON(BSONObj()));
65 }
66 
TEST(InternalSchemaMaxPropertiesMatchExpression,NestedObjectsAreNotUnwound)67 TEST(InternalSchemaMaxPropertiesMatchExpression, NestedObjectsAreNotUnwound) {
68     InternalSchemaMaxPropertiesMatchExpression maxProperties;
69     ASSERT_OK(maxProperties.init(1));
70 
71     ASSERT_TRUE(maxProperties.matchesBSON(BSON("b" << BSON("c" << 2 << "d" << 3))));
72 }
73 
TEST(InternalSchemaMaxPropertiesMatchExpression,EquivalentFunctionIsAccurate)74 TEST(InternalSchemaMaxPropertiesMatchExpression, EquivalentFunctionIsAccurate) {
75     InternalSchemaMaxPropertiesMatchExpression maxProperties1;
76     InternalSchemaMaxPropertiesMatchExpression maxProperties2;
77     InternalSchemaMaxPropertiesMatchExpression maxProperties3;
78     ASSERT_OK(maxProperties1.init(1));
79     ASSERT_OK(maxProperties2.init(1));
80     ASSERT_OK(maxProperties3.init(2));
81 
82     ASSERT_TRUE(maxProperties1.equivalent(&maxProperties1));
83     ASSERT_TRUE(maxProperties1.equivalent(&maxProperties2));
84     ASSERT_FALSE(maxProperties1.equivalent(&maxProperties3));
85 }
86 
TEST(InternalSchemaMaxPropertiesMatchExpression,NestedArraysAreNotUnwound)87 TEST(InternalSchemaMaxPropertiesMatchExpression, NestedArraysAreNotUnwound) {
88     InternalSchemaMaxPropertiesMatchExpression maxProperties;
89     ASSERT_OK(maxProperties.init(2));
90 
91     ASSERT_TRUE(maxProperties.matchesBSON(BSON("a" << (BSON("b" << 2 << "c" << 3 << "d" << 4)))));
92 }
93 
TEST(InternalSchemaMaxPropertiesMatchExpression,MinPropertiesNotEquivalentToMaxProperties)94 TEST(InternalSchemaMaxPropertiesMatchExpression, MinPropertiesNotEquivalentToMaxProperties) {
95     InternalSchemaMaxPropertiesMatchExpression maxProperties;
96     InternalSchemaMinPropertiesMatchExpression minProperties;
97     ASSERT_OK(maxProperties.init(5));
98     ASSERT_OK(minProperties.init(5));
99 
100     ASSERT_FALSE(maxProperties.equivalent(&minProperties));
101 }
102 
103 }  // namespace
104 }  // namespace mongo
105