You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2019/05/20 13:22:39 UTC

[directory-ldap-api] branch master updated: Added the missing NisNetGroupTriple syntax checker (DIRAPI-343, DIRSERVER-2264)

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 2501a91  Added the missing NisNetGroupTriple syntax checker (DIRAPI-343, DIRSERVER-2264)
2501a91 is described below

commit 2501a91cd39448451235f2b80c4dec36dc852282
Author: emmanuel lecharny <el...@apache.org>
AuthorDate: Mon May 20 15:22:34 2019 +0200

    Added the missing NisNetGroupTriple syntax checker (DIRAPI-343,
    DIRSERVER-2264)
---
 .../api/ldap/model/constants/SchemaConstants.java  |   4 +
 .../NisNetgroupTripleSyntaxChecker.java            | 265 +++++++++++++++++++++
 .../NisNetgroupTripleSyntaxCheckerTest.java        |  75 ++++++
 3 files changed, 344 insertions(+)

diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/constants/SchemaConstants.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/constants/SchemaConstants.java
index 2133dee..25c10a9 100644
--- a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/constants/SchemaConstants.java
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/constants/SchemaConstants.java
@@ -1753,6 +1753,10 @@ public final class SchemaConstants
     //-------------------------------------------------------------------------
     // ---- Syntaxes ----------------------------------------------------------
     //-------------------------------------------------------------------------
+    // RFC 2307
+    public static final String NIS_NETGROUP_TRIPLE_SYNTAX = "1.3.6.1.1.1.0.0";
+    public static final String BOOT_PARAMETER_SYNTAX = "1.3.6.1.1.1.0.1";
+    
     public static final String NAME_OR_NUMERIC_ID_SYNTAX = "1.3.6.1.4.1.18060.0.4.0.0.0";
 
     public static final String OBJECT_CLASS_TYPE_SYNTAX = "1.3.6.1.4.1.18060.0.4.0.0.1";
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/NisNetgroupTripleSyntaxChecker.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/NisNetgroupTripleSyntaxChecker.java
new file mode 100644
index 0000000..76cc45c
--- /dev/null
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/NisNetgroupTripleSyntaxChecker.java
@@ -0,0 +1,265 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
+
+
+import org.apache.directory.api.i18n.I18n;
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
+import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
+import org.apache.directory.api.util.Strings;
+
+
+/**
+ * A SyntaxChecker which verifies that a value is a NisNetGroupTriple according to 
+ * RFC 2307 :
+ * <pre>
+ * nisnetgrouptriple = "(" hostname "," username "," domainname ")"
+ *      hostname          = "" / "-" / keystring
+ *      username          = "" / "-" / keystring
+ *      domainname        = "" / "-" / keystring
+ * </pre>
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@SuppressWarnings("serial")
+public final class NisNetgroupTripleSyntaxChecker extends SyntaxChecker
+{
+    /**
+     * A static instance of NisNetGroupTripleChecker
+     */
+    public static final NisNetgroupTripleSyntaxChecker INSTANCE = 
+        new NisNetgroupTripleSyntaxChecker( SchemaConstants.NIS_NETGROUP_TRIPLE_SYNTAX );
+    
+    /**
+     * A static Builder for this class
+     */
+    public static final class Builder extends SCBuilder<NisNetgroupTripleSyntaxChecker>
+    {
+        /**
+         * The Builder constructor
+         */
+        private Builder()
+        {
+            super( SchemaConstants.NIS_NETGROUP_TRIPLE_SYNTAX );
+        }
+        
+        
+        /**
+         * Create a new instance of NisNetgroupTripleSyntaxChecker
+         * @return A new instance of NisNetgroupTripleSyntaxChecker
+         */
+        @Override
+        public NisNetgroupTripleSyntaxChecker build()
+        {
+            return new NisNetgroupTripleSyntaxChecker( oid );
+        }
+    }
+
+    
+    /**
+     * Creates a new instance of NisNetgroupTripleSyntaxChecker.
+     * 
+     * @param oid The OID to use for this SyntaxChecker
+     */
+    private NisNetgroupTripleSyntaxChecker( String oid )
+    {
+        super( oid );
+    }
+
+    
+    /**
+     * @return An instance of the Builder for this class
+     */
+    public static Builder builder()
+    {
+        return new Builder();
+    }
+    
+    
+    private int parseKeyString( String strValue, int pos )
+    {
+        char c = strValue.charAt( pos );
+        
+        // The end of the keyString
+        if ( ( c == ',' ) || ( c == ')' ) )
+        {
+            return pos;
+        }
+        
+        // We must have a first alphabetic char
+        if ( Character.isUpperCase( c ) || Character.isLowerCase( c ) )
+        {
+            pos++;
+        }
+        else
+        {
+            return -1;
+        }
+        
+        try 
+        { 
+            c = strValue.charAt( pos );
+            
+            while ( ( c != ',') && ( c != ')' ) )
+            {
+                if ( Character.isUpperCase( c ) || Character.isLowerCase( c ) || Character.isDigit( c ) ||
+                    ( c == '-' ) || ( c == ';' ) || ( c == '_' ) )
+                {
+                    pos++;
+                }
+                else
+                {
+                    return -1;
+                }
+
+                c = strValue.charAt( pos );
+            }
+            
+            return pos;
+        }
+        catch ( IndexOutOfBoundsException ioobe )
+        {
+            return -1;
+        }
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isValidSyntax( Object value )
+    {
+        String strValue;
+
+        if ( value == null )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
+            }
+            
+            return false;
+        }
+
+        if ( value instanceof String )
+        {
+            strValue = ( String ) value;
+        }
+        else if ( value instanceof byte[] )
+        {
+            strValue = Strings.utf8ToString( ( byte[] ) value );
+        }
+        else
+        {
+            strValue = value.toString();
+        }
+
+        // The  nisNetGroup must at least contain a '(', 2 ',' and a ')'
+        if ( strValue.length() < 4 )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+
+        // The hostname
+        int pos = parseKeyString( strValue, 1 );
+        
+        if ( pos == -1 )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+        
+        if ( strValue.charAt( pos ) != ',' )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+        else
+        {
+            pos++;
+        }
+
+        // The username
+        pos = parseKeyString( strValue, pos );
+        
+        if ( pos == -1 )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+        
+        if ( strValue.charAt( pos ) != ',' )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+        else
+        {
+            pos++;
+        }
+
+        // The domainname
+        pos = parseKeyString( strValue, pos );
+        
+        if ( pos == -1 )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+        
+        if ( strValue.charAt( pos ) != ')' )
+        {
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
+            }
+            
+            return false;
+        }
+
+        return true;
+    }
+}
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/NisNetgroupTripleSyntaxCheckerTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/NisNetgroupTripleSyntaxCheckerTest.java
new file mode 100644
index 0000000..8101a51
--- /dev/null
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/NisNetgroupTripleSyntaxCheckerTest.java
@@ -0,0 +1,75 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.api.ldap.model.schema.syntaxes;
+
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.directory.api.ldap.model.schema.syntaxCheckers.NisNetgroupTripleSyntaxChecker;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.api.parallel.ExecutionMode;
+
+/**
+ * Test cases for NisNetgroupTripleSyntaxChecker.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@Execution(ExecutionMode.CONCURRENT)
+public class NisNetgroupTripleSyntaxCheckerTest
+{
+    NisNetgroupTripleSyntaxChecker checker = NisNetgroupTripleSyntaxChecker.INSTANCE;
+
+
+    @Test
+    public void testNullString()
+    {
+        assertFalse( checker.isValidSyntax( null ) );
+    }
+
+
+    @Test
+    public void testEmptyString()
+    {
+        assertFalse( checker.isValidSyntax( "" ) );
+    }
+
+
+    @Test
+    public void testWrongCase()
+    {
+        assertFalse( checker.isValidSyntax( "(,," ) );
+        assertFalse( checker.isValidSyntax( ",,)" ) );
+        assertFalse( checker.isValidSyntax( "( , , )" ) );
+        assertFalse( checker.isValidSyntax( "(A,B,1)" ) );
+        assertFalse( checker.isValidSyntax( "(A_=v,b,D)" ) );
+        assertFalse( checker.isValidSyntax( "(a, ,c)" ) );
+    }
+
+
+    @Test
+    public void testCorrectCase()
+    {
+        assertTrue( checker.isValidSyntax( "(,,)" ) );
+        assertTrue( checker.isValidSyntax( "(a,,b)" ) );
+        assertTrue( checker.isValidSyntax( "(test,test,test)" ) );
+    }
+}