/*_############################################################################
  _## 
  _##  SNMP4J - TestUSM.java  
  _## 
  _##  Copyright (C) 2003-2026  Frank Fock (SNMP4J.org)
  _##  
  _##  Licensed 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.snmp4j.security;

import junit.framework.TestCase;
import org.snmp4j.smi.OctetString;

public class TestUSM extends TestCase {

    public void testAddUser() throws Exception {
        USM usm = new USM();
        SecurityProtocols.getInstance().addDefaultProtocols();
        OctetString userName = new OctetString("user1");
        UsmUser usmUser = new UsmUser(userName, AuthHMAC128SHA224.ID, new OctetString("auth12345678"),
                PrivAES128.ID, new OctetString("priv123445678"));
        UsmUserEntry userEntry1 = usm.getUser(new OctetString(), userName);
        assertNull(userEntry1);

        //we add the user, but the engineId is not known yet
        usm.addUser(usmUser);
        UsmUserEntry userEntry2 = usm.getUser(new OctetString(), userName);
        assertNotNull(userEntry2);

        OctetString engineId = new OctetString("someEngineID");
        // localisation should take place now
        UsmUserEntry userEntry3 = usm.getUser(engineId, userName);
        assertNotNull(userEntry3);
        assertNotNull(userEntry3.getAuthenticationKey());
        assertNotNull(userEntry3.getPrivacyKey());
    }

    public void testUsmUserLocalizationGrant() throws Exception {
        USM usm = new USM();
        SecurityProtocols.getInstance().addDefaultProtocols();
        OctetString userName = new OctetString("user1");
        UsmUser usmUser = new UsmUser(userName, AuthHMAC128SHA224.ID, new OctetString("auth12345678"),
                PrivAES128.ID, new OctetString("priv123445678"), UsmUser.LocalizationGrant.outgoing);
        usm.addUser(usmUser);
        OctetString engineId = new OctetString("someEngineID");
        UsmUserEntry userEntry1 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.incoming);
        assertNull(userEntry1);
        UsmUserEntry userEntry2 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.never);
        assertNull(userEntry2);
        UsmUserEntry userEntry3 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.outgoing);
        assertNotNull(userEntry3);
        assertNotSame(usmUser, userEntry3.getUsmUser());
        assertEquals(engineId, userEntry3.getEngineID());
        assertEquals(userName, userEntry3.getUsmUser().getSecurityName());
        UsmUserEntry userEntry4 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.any);
        assertNotNull(userEntry4);
        assertNotSame(usmUser, userEntry4.getUsmUser());
        assertEquals(engineId, userEntry4.getEngineID());
        assertNotSame(usmUser, userEntry4.getUsmUser());
        assertEquals(usmUser.getSecurityName(), userEntry4.getUsmUser().getSecurityName());
        assertTrue(userEntry4.getUsmUser().isLocalized());
    }

    public void testUsmUserLocalizationGrantNoAuth() throws Exception {
        USM usm = new USM();
        SecurityProtocols.getInstance().addDefaultProtocols();
        OctetString userName = new OctetString("user1");
        UsmUser usmUser = new UsmUser(userName, null, new OctetString(),
                null, null, UsmUser.LocalizationGrant.outgoing);
        usm.addUser(usmUser);
        OctetString engineId = new OctetString("someEngineID");
        UsmUserEntry userEntry1 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.incoming);
        assertNotNull(userEntry1);
        assertNotSame(usmUser, userEntry1.getUsmUser());
        assertEquals(engineId, userEntry1.getEngineID());
        assertEquals(userName, userEntry1.getUsmUser().getSecurityName());
        UsmUserEntry userEntry2 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.never);
        assertNotNull(userEntry2);
        assertNotSame(usmUser, userEntry2.getUsmUser());
        assertEquals(engineId, userEntry2.getEngineID());
        assertEquals(userName, userEntry2.getUsmUser().getSecurityName());
        UsmUserEntry userEntry3 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.outgoing);
        assertNotNull(userEntry3);
        assertNotSame(usmUser, userEntry3.getUsmUser());
        assertEquals(engineId, userEntry3.getEngineID());
        assertEquals(userName, userEntry3.getUsmUser().getSecurityName());
        UsmUserEntry userEntry4 = usm.getUser(engineId, userName, UsmUser.LocalizationGrant.any);
        assertNotNull(userEntry4);
        assertNotSame(usmUser, userEntry4.getUsmUser());
        assertEquals(engineId, userEntry4.getEngineID());
        assertNotSame(usmUser, userEntry4.getUsmUser());
        assertEquals(usmUser.getSecurityName(), userEntry4.getUsmUser().getSecurityName());
        assertTrue(userEntry4.getUsmUser().isLocalized());
    }

}
