Skip to content

Commit

Permalink
Additional NKey Tests (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored May 29, 2024
1 parent 4b05797 commit 8a1111c
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/test/java/io/nats/client/NKeyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -587,12 +589,43 @@ public void testEquals() throws Exception {
}

@Test
public void testClear() throws Exception {
public void testClear() {
assertThrows(IllegalArgumentException.class, () -> {
NKey key = NKey.createServer(null);
key.clear();
key.getPrivateKey();

}, "Invalid encoding");
}

@Test
public void testPublicKeyFromSeed() throws Exception {
// using nsc generated seeds for testing
NKey pk = NKey.fromSeed("SOAELH6NJCEK4HST5644G4HK7TOAFZGRRJHNM4EUKUY7PPNDLIKO5IH4JM".toCharArray());
assertEquals("ODPWIBQJVIQ42462QAFI2RKJC4RZHCQSIVPRDDHWFCJAP52NRZK6Z2YC", new String(pk.getPublicKey()));

pk = NKey.fromSeed("SAANWFZ3JINNPERWT3ALE45U7GYT2ZDW6GJUIVPDKUF6GKAX6AISZJMAS4".toCharArray());
assertEquals("AATEJXG7UX4HFJ6ZPRTP22P6OYZER36YYD3GVBOVW7QHLU32P4QFFTZJ", new String(pk.getPublicKey()));

pk = NKey.fromSeed("SUAGDLNBWI2SGHDRYBHD63NH5FGZSVJUW2J7GAJZXWANQFLDW6G5SXZESU".toCharArray());
assertEquals("UBICBTHDKQRB4LIYA6BMIJ7EA2G7YS7FIWMMVKZJE6M3HS5IVCOLKDY2", new String(pk.getPublicKey()));
}

@Test
public void testFromPublicKey() throws Exception {
_testFromPublicKey("SUAHBVFYZF3DIEO4UIHIZMJICVLURLBM5JJPK7GSVGP2QUC3NZ323BRE6A", "UCM5BG6AAZSEGREBCLG7PG4GFQNJABSAVIXC6VWS7TDHZFPIYFVYHIDG");
_testFromPublicKey("SAADARCQJ3JA737Z443YNAZBNJNTFP7YNAF4QFUXKTBFBS4KAVK55DGSOQ", "AD2HQTUKOPBUGOPHA6KFRE6ZW5TH43D7P7E56OAQBZQLW2ECMNML6MVA");
_testFromPublicKey("SNAH645525YA4PNXHWWS46VNXXQTYAXOPKGHXYAHXZZ43XTDDG2ZQAX7LY", "NBZCD2OSMSDRVYCAI77HUN6A2WNDWNT2DMVVEW66DHNWCDXVOUWRCCK7");
_testFromPublicKey("SOAF5OP7UPK6XJCMNRYEJRET6YQSOE3FD4I4ERSN6WKHLYUC5AQDCOAFVY", "OA6SJACXYP2QGNLU4QYLJTVRVZPCZEEUNO2UQOVNGXYUPUJJHCVZIZQ2");
_testFromPublicKey("SCAP4LGVURDWVL37AZIM5O47UKANFI6FKBY77HMYF55CKW2XFKLNUBTTFE", "CAO36T42KFA2LMIZ6YHJKPQEJWT5ULYSV633FWBCEJ7MREZPHHC56BSC");
}

private static void _testFromPublicKey(String userEncodedSeed, String userEncodedPubKey) throws GeneralSecurityException, IOException {
NKey fromSeed = NKey.fromSeed(userEncodedSeed.toCharArray());
NKey fromKey = NKey.fromPublicKey(fromSeed.getPublicKey());

assertArrayEquals(fromSeed.getPublicKey(), fromKey.getPublicKey());
assertArrayEquals(userEncodedPubKey.toCharArray(), fromSeed.getPublicKey());
assertArrayEquals(userEncodedPubKey.toCharArray(), fromKey.getPublicKey());
}
}

0 comments on commit 8a1111c

Please sign in to comment.