Changeset 19345 in josm for trunk/test


Ignore:
Timestamp:
2025-03-10T17:38:42+01:00 (10 days ago)
Author:
stoecker
Message:

don't send authentication oinformation to wrong server, fix #24149, patch by ssundell

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/io/auth/CredentialsManagerTest.java

    r18650 r19345  
    22package org.openstreetmap.josm.io.auth;
    33
     4import org.junit.jupiter.api.Assertions;
     5import org.junit.jupiter.api.Test;
    46import org.openstreetmap.josm.testutils.annotations.HTTP;
     7
     8import java.net.Authenticator;
     9import java.util.List;
    510
    611/**
     
    1318        return new CredentialsManager(new JosmPreferencesCredentialAgent());
    1419    }
     20
     21    @Test
     22    public void testMultipleUnsavedHostsLookup() throws CredentialsAgentException {
     23        final AbstractCredentialsAgent aca = new JosmPreferencesCredentialAgent();
     24        // A provider that mimics user giving the credentials and choosing not to store them in preferences.
     25        AbstractCredentialsAgent.setCredentialsProvider((requestorType, agent, response, username, password, host) -> {
     26            response.setUsername("user" + host);
     27            response.setPassword("password".toCharArray());
     28            response.setSaveCredentials(false);
     29            response.setCanceled(false);
     30        });
     31        final CredentialsManager agent = new CredentialsManager(aca);
     32
     33        String host1 = "example.com";
     34        String host2 = "example.org";
     35        for (String host : List.of(host1, host2)) {
     36            // Try to get credentials after "failure" => provider gives the credentials.
     37            agent.getCredentials(Authenticator.RequestorType.SERVER, host, true);
     38        }
     39        // Both hosts should receive their respective credentials.
     40        CredentialsAgentResponse response = agent.getCredentials(Authenticator.RequestorType.SERVER, host1, false);
     41        Assertions.assertEquals("user" + host1, response.getUsername());
     42        response = agent.getCredentials(Authenticator.RequestorType.SERVER, host2, false);
     43        Assertions.assertEquals("user" + host2, response.getUsername());
     44    }
    1545}
Note: See TracChangeset for help on using the changeset viewer.