Skip to content

Commit

Permalink
Modify tests to explicitly test for situation mentioned in #961
Browse files Browse the repository at this point in the history
add() is used instead of the previous replace(), but behaviour should still be the same.
  • Loading branch information
lhy-hoyin committed Oct 19, 2024
1 parent fd8dc5e commit 0c15467
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ class DefaultCollectionMutatorTest {
assertEquals 'bar', ((IdentifiableObject) m.collection.toArray()[0]).obj
}

@Test
void addSecureDigestAlgorithmWithSameIdReplacesExisting() {
Class<?> c = Class.forName("io.jsonwebtoken.impl.security.DefaultMacAlgorithm")
Constructor<?> ctor = c.getDeclaredConstructor(String.class, String.class, int.class)
ctor.setAccessible(true)
MacAlgorithm custom = (MacAlgorithm) ctor.newInstance('HS512', 'HmacSHA512', 80)

m.add(Jwts.SIG.HS512)
m.add(custom)
assertEquals 2, changeCount // replace is count as one change
assertEquals 1, m.getCollection().size() // existing is removed as part of replacement
assertEquals 80, ((MacAlgorithm) m.getCollection().toArray()[0]).getKeyBitLength()
}

@Test
void remove() {
m.add('hello').add('world')
Expand All @@ -131,16 +145,14 @@ class DefaultCollectionMutatorTest {

@Test
void replace() {
Class<?> c = Class.forName("io.jsonwebtoken.impl.security.DefaultMacAlgorithm")
Constructor<?> ctor = c.getDeclaredConstructor(String.class, String.class, int.class)
ctor.setAccessible(true)
MacAlgorithm custom = (MacAlgorithm) ctor.newInstance('HS512', 'HmacSHA512', 80)
def e1 = new IdentifiableObject('sameId', 'e1')
def e2 = new IdentifiableObject('sameId', 'e2')

m.add(Jwts.SIG.HS512)
m.replace(Jwts.SIG.HS512, custom)
m.add(e1)
m.replace(e1, e2)
assertEquals 2, changeCount // replace is count as one change
assertEquals 1, m.getCollection().size() // existing is removed as part of replacement
assertEquals 80, ((MacAlgorithm) m.getCollection().toArray()[0]).getKeyBitLength()
assertEquals 'e2', ((IdentifiableObject) m.getCollection().toArray()[0]).obj
}

@Test
Expand Down Expand Up @@ -175,9 +187,9 @@ class DefaultCollectionMutatorTest {

private class IdentifiableObject implements Identifiable {
String id
String obj
Object obj

IdentifiableObject(String id, String obj) {
IdentifiableObject(String id, Object obj) {
this.id = id
this.obj = obj
}
Expand Down

0 comments on commit 0c15467

Please sign in to comment.