|
| 1 | +package io.github.hikingc.matrixsdk.api.identifiers; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.EmptySource; |
| 8 | +import org.junit.jupiter.params.provider.NullSource; |
| 9 | +import org.junit.jupiter.params.provider.ValueSource; |
| 10 | + |
| 11 | +class EventIDTest { |
| 12 | + |
| 13 | + @ParameterizedTest(name = "[{index}] \"{0}\"") |
| 14 | + @ValueSource( |
| 15 | + strings = { |
| 16 | + // v3+ reference-hash shape |
| 17 | + "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", |
| 18 | + "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI9dSaz3jRoiQ-fXE", |
| 19 | + "$acR1l0raRc2h8DzKlR4E9RAxwbrIY8v_4V-1kfBGCiA", |
| 20 | + "$LWXstUyAjMr8vBiVjTMH_hEcnKMhc0zVi52gxHYzc-4", |
| 21 | + "$1c-AYXvOG3AH0z9OTfHktZ4b6l3f1uK1Wv4h5CkQY9U", |
| 22 | + // legacy v1/v2 shape |
| 23 | + "$acR1l0raRc2h8DzKlR4E9RAxwbrIY8v_4V-1kfBGCiA:matrix.org", |
| 24 | + "$event1:example.com", |
| 25 | + "$143273582443PhrSn:example.org", |
| 26 | + // opaque content is allowed to contain "unusual" characters per spec — |
| 27 | + // clients must not impose structure beyond the sigil |
| 28 | + "$acR1l0raRc2h8DzKlR4E9RAxwbrIY8v/4V+1kfBGCiA", // non-base64url chars, still opaque |
| 29 | + "$has spaces in it", |
| 30 | + "$has\nnewline", |
| 31 | + "$emoji🎉event", |
| 32 | + "$has\"quote", |
| 33 | + "$ " // single space after sigil is still non-empty content |
| 34 | + }) |
| 35 | + void withValidStrings_ReturnEventID(String id) { |
| 36 | + assertDoesNotThrow(() -> EventID.parse(id), "Exception not expected for input: " + id); |
| 37 | + } |
| 38 | + |
| 39 | + @ParameterizedTest(name = "[{index}] \"{0}\"") |
| 40 | + @ValueSource( |
| 41 | + strings = { |
| 42 | + "acR1l0raRc2h8DzKlR4E9RAxwbrIY8v_4V-1kfBGCiA", // missing sigil |
| 43 | + "@acR1l0raRc2h8DzKlR4E9RAxwbrIY8v_4V-1kfBGCiA", // wrong sigil (User ID) |
| 44 | + "!acR1l0raRc2h8DzKlR4E9RAxwbrIY8v_4V-1kfBGCiA", // wrong sigil (Room ID) |
| 45 | + "#acR1l0raRc2h8DzKlR4E9RAxwbrIY8v_4V-1kfBGCiA", // wrong sigil (Room Alias) |
| 46 | + " ", // no sigil at all, just whitespace |
| 47 | + "$" // sigil present but zero content after it |
| 48 | + }) |
| 49 | + void withInvalidStrings_ThrowsException(String id) { |
| 50 | + assertThrows( |
| 51 | + IllegalArgumentException.class, |
| 52 | + () -> EventID.parse(id), |
| 53 | + "Exception expected for input: " + id); |
| 54 | + } |
| 55 | + |
| 56 | + @ParameterizedTest |
| 57 | + @NullSource |
| 58 | + void withNull_ThrowsException(String id) { |
| 59 | + assertThrows(NullPointerException.class, () -> EventID.parse(id)); |
| 60 | + } |
| 61 | + |
| 62 | + @ParameterizedTest |
| 63 | + @EmptySource |
| 64 | + void withEmpty_ThrowsException(String id) { |
| 65 | + assertThrows(IllegalArgumentException.class, () -> EventID.parse(id)); |
| 66 | + } |
| 67 | +} |
0 commit comments