[ 
https://issues.apache.org/jira/browse/STORM-1233?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15189435#comment-15189435
 ] 

ASF GitHub Bot commented on STORM-1233:
---------------------------------------

Github user redsanket commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1191#discussion_r55696301
  
    --- Diff: 
storm-core/test/jvm/org/apache/storm/security/auth/AuthUtilsTest.java ---
    @@ -0,0 +1,240 @@
    +/**
    + * 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.storm.security.auth;
    +
    +import java.io.IOException;
    +import java.io.File;
    +import java.security.MessageDigest;
    +import java.security.NoSuchAlgorithmException;
    +import java.util.Arrays;
    +import java.util.Collection;
    +import java.util.HashMap;
    +import java.util.Map;
    +
    +import javax.security.auth.login.AppConfigurationEntry;
    +import javax.security.auth.login.Configuration;
    +import javax.security.auth.Subject;
    +
    +import org.apache.commons.codec.binary.Hex;
    +import org.apache.storm.Config;
    +import org.junit.Assert;
    +import org.junit.Rule;
    +import org.junit.rules.TemporaryFolder;
    +import org.junit.Test;
    +import org.mockito.Mockito;
    +
    +public class AuthUtilsTest {
    +
    +    @Test(expected=IOException.class)
    +    public void getOptionsThrowsOnMissingSectionTest() throws IOException {
    +        Configuration mockConfig = Mockito.mock(Configuration.class);
    +        AuthUtils.get(mockConfig, "bogus-section", "");
    +    }
    +
    +    @Test
    +    public void getNonExistentSectionTest() throws IOException {
    +        Map<String, String> optionMap = new HashMap<String, String>();
    +        AppConfigurationEntry entry = 
Mockito.mock(AppConfigurationEntry.class);
    +        
    +        Mockito.<Map<String, 
?>>when(entry.getOptions()).thenReturn(optionMap);
    +        String section = "bogus-section";
    +        Configuration mockConfig = Mockito.mock(Configuration.class);
    +        Mockito.when(mockConfig.getAppConfigurationEntry(section))
    +               .thenReturn(new AppConfigurationEntry[] {entry});
    +        Assert.assertNull(
    +                AuthUtils.get(mockConfig, section, "nonexistent-key"));
    +    }
    +
    +    @Test
    +    public void getFirstValueForValidKeyTest() throws IOException {
    +        String k = "the-key";
    +        String expected = "good-value";
    +
    +        Map<String, String> optionMap = new HashMap<String, String>();
    +        optionMap.put(k, expected);
    +
    +        Map<String, String> badOptionMap = new HashMap<String, String>();
    +        badOptionMap.put(k, "bad-value");
    +
    +        AppConfigurationEntry emptyEntry = 
Mockito.mock(AppConfigurationEntry.class);
    +        AppConfigurationEntry badEntry = 
Mockito.mock(AppConfigurationEntry.class);
    +        AppConfigurationEntry goodEntry = 
Mockito.mock(AppConfigurationEntry.class);
    +        
    +        Mockito.<Map<String, 
?>>when(emptyEntry.getOptions()).thenReturn(new HashMap<String, String>());
    +        Mockito.<Map<String, 
?>>when(badEntry.getOptions()).thenReturn(badOptionMap);
    +        Mockito.<Map<String, 
?>>when(goodEntry.getOptions()).thenReturn(optionMap);
    +
    +        String section = "bogus-section";
    +        Configuration mockConfig = Mockito.mock(Configuration.class);
    +        Mockito.when(mockConfig.getAppConfigurationEntry(section))
    +               .thenReturn(new AppConfigurationEntry[] {emptyEntry, 
goodEntry, badEntry});
    +
    +        Assert.assertEquals(
    +                AuthUtils.get(mockConfig, section, k), expected);
    +    }
    +
    +    @Test
    +    public void objGettersReturnNullWithNullConfigTest() throws 
IOException {
    +        Assert.assertNull(AuthUtils.pullConfig(null, "foo"));
    +        Assert.assertNull(AuthUtils.get(null, "foo", "bar"));
    +
    +        Map emptyMap = new HashMap<String, String>();
    +        Assert.assertNull(AuthUtils.GetConfiguration(emptyMap));
    +    }
    +
    +    @Test
    +    public void getAutoCredentialsTest() {
    +        Map emptyMap = new HashMap<String, String>();
    +        Map<String, Collection<String>> map = new HashMap<String, 
Collection<String>>();
    +        map.put(Config.TOPOLOGY_AUTO_CREDENTIALS, 
    +                Arrays.asList(new 
String[]{"org.apache.storm.security.auth.AuthUtilsTestMock"}));
    +
    +        
Assert.assertTrue(AuthUtils.GetAutoCredentials(emptyMap).isEmpty());
    +        Assert.assertEquals(AuthUtils.GetAutoCredentials(map).size(), 1);
    +    }
    +
    +    @Test
    +    public void getNimbusAutoCredPluginTest() {
    +        Map emptyMap = new HashMap<String, String>();
    +        Map<String, Collection<String>> map = new HashMap<String, 
Collection<String>>();
    +        map.put(Config.NIMBUS_AUTO_CRED_PLUGINS, 
    +                Arrays.asList(new 
String[]{"org.apache.storm.security.auth.AuthUtilsTestMock"}));
    +
    +        
Assert.assertTrue(AuthUtils.getNimbusAutoCredPlugins(emptyMap).isEmpty());
    +        
Assert.assertEquals(AuthUtils.getNimbusAutoCredPlugins(map).size(), 1);
    +    }
    +
    +    @Test
    +    public void GetCredentialRenewersTest() {
    +        Map emptyMap = new HashMap<String, String>();
    +        Map<String, Collection<String>> map = new HashMap<String, 
Collection<String>>();
    +        map.put(Config.NIMBUS_CREDENTIAL_RENEWERS, 
    +                Arrays.asList(new 
String[]{"org.apache.storm.security.auth.AuthUtilsTestMock"}));
    +
    +        
Assert.assertTrue(AuthUtils.GetCredentialRenewers(emptyMap).isEmpty());
    +        Assert.assertEquals(AuthUtils.GetCredentialRenewers(map).size(), 
1);
    +    }
    +
    +    @Test
    +    public void populateSubjectTest() {
    +        AuthUtilsTestMock autoCred = Mockito.mock(AuthUtilsTestMock.class);
    +        Subject subject = new Subject();
    +        Map<String, String> cred = new HashMap<String, String>();
    +        Collection<IAutoCredentials> autos = Arrays.asList(new 
IAutoCredentials[]{autoCred}); 
    +        AuthUtils.populateSubject(subject, autos, cred);
    +        Mockito.verify(autoCred, 
Mockito.times(1)).populateSubject(subject, cred); 
    +    }
    +
    +    @Test
    +    public void makeDigestPayloadTest() throws NoSuchAlgorithmException { 
    +        String section = "user-pass-section";
    +        Map<String, String> optionMap = new HashMap<String, String>();
    +        String user = "user";
    +        String pass = "pass";
    +        optionMap.put("username", user);
    +        optionMap.put("password", pass);
    +        AppConfigurationEntry entry = 
Mockito.mock(AppConfigurationEntry.class);
    +        
    +        Mockito.<Map<String, 
?>>when(entry.getOptions()).thenReturn(optionMap);
    +        Configuration mockConfig = Mockito.mock(Configuration.class);
    +        Mockito.when(mockConfig.getAppConfigurationEntry(section))
    +               .thenReturn(new AppConfigurationEntry[] {entry});
    +
    +        MessageDigest digest = MessageDigest.getInstance("SHA-512");
    +        byte[] output = digest.digest((user + ":" + pass).getBytes());
    +        String sha = Hex.encodeHexString(output);
    +
    +        // previous code used this method to generate the string, ensure 
the two match
    +        StringBuilder builder = new StringBuilder();        
    +        for(byte b : output) {     
    +            builder.append(String.format("%02x", b));      
    +        }      
    +        String stringFormatMethod = builder.toString();
    +
    +        Assert.assertEquals(
    +            AuthUtils.makeDigestPayload(mockConfig, "user-pass-section"),
    +            sha);
    +
    +        Assert.assertEquals(sha, stringFormatMethod); 
    +    }
    +
    +    @Test(expected=RuntimeException.class)
    --- End diff --
    
    same here


> port backtype.storm.security.auth.AuthUtils-test to java
> --------------------------------------------------------
>
>                 Key: STORM-1233
>                 URL: https://issues.apache.org/jira/browse/STORM-1233
>             Project: Apache Storm
>          Issue Type: New Feature
>            Reporter: Robert Joseph Evans
>            Assignee: Alessandro Bellina
>              Labels: java-migration, jstorm-merger
>
> Just a test moving to junit



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to