Repository: jchambers/pushy
Pull request #797 (2020-07-15 08:45:04)

changed sh to bash in generate-certs.sh
error message when executing with sh
```
~/git/pushy/pushy/script$ ./generate-certs.sh 
Generating a RSA private key
......................................................................................................................................+++++
.....................................................................................+++++
writing new private key to 'ca.key'
-----
x509: Use -help for summary.
Generating a RSA private key
................+++++
.+++++
writing new private key to 'server-key.pem'
-----
x509: Use -help for summary.
Generating a RSA private key
.........................................................+++++
............................................+++++
writing new private key to 'single-topic-client.key'
-----
x509: Use -help for summary.
Generating a RSA private key
.........................................+++++
.....+++++
writing new private key to 'multi-topic-client.key'
-----
Can't open server-certs.pem for reading, No such file or directory
140657904542144:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('server-certs.pem','r')
140657904542144:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79:
Can't open single-topic-client.pem for reading, No such file or directory
140689287942592:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('single-topic-client.pem','r')
140689287942592:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79:
Can't open multi-topic-client.pem for reading, No such file or directory
139761754120640:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('multi-topic-client.pem','r')
139761754120640:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79:
Can't open single-topic-client.pem for reading, No such file or directory
140532233011648:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('single-topic-client.pem','r')
140532233011648:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79:
Keystore multiple-keys.jks wird in multiple-keys.p12 importiert...
Eintrag für Alias pair1 erfolgreich importiert.
Eintrag für Alias pair2 erfolgreich importiert.
Eintrag für Alias pair3 erfolgreich importiert.
Eintrag für Alias pair4 erfolgreich importiert.
Importbefehl abgeschlossen: 4 Einträge erfolgreich importiert, 0 Einträge nicht erfolgreich oder abgebrochen
rm: das Entfernen von 'server.key' ist nicht möglich: Datei oder Verzeichnis nicht gefunden
read EC key
writing EC key
```


jchambers: This works fine for me as-is, and it's not clear why switching from `/bin/sh` to `/bin/bash` changes anything. Can you say more about what you think is going wrong and why switching to bash helps?
b0rengar: on Ubuntu 18.04 sh is linked to dash which causes the error messages from above

ls -la /bin/sh 
lrwxrwxrwx 1 root root 4 Feb 12 16:43 /bin/sh -> dash

jchambers: I'm sorry, but I'm still not following. Why, then, does `dash` struggle with this script? What about `dash` is preventing the files from being found?
b0rengar: To be honest i don't have clue. Switching to bash solved the problem for me and just wanted to share it.
jchambers: Well, first and foremost, thank you for the contribution! The instinct to give back and share what you've learned is very welcome and greatly appreciated.

At the same time, I think we want to understand the problem in a little more detail before we commit changes to fix it (i.e. we want to be able to explain _why_ a change fixes the problem rather than just asserting that it _does_). It sounds like you've found a way forward for yourself; I'm going to close this for now, but will happily reopen it if we can get a more complete understanding of the problem and why this is the right solution.

Cheers!
eager: Via [`checkbashisms`](https://linux.die.net/man/1/checkbashisms), I suspect the issue is the use of [`$RANDOM`](https://tldp.org/LDP/abs/html/randomvar.html).
Reamer: Thanks to @eager to find the source of the problem and @b0rengar to provide a PR
@jchambers Can we merge this into a master?

jchambers: I think @eager is right (though I'm still confused about why it doesn't fail locally), but I think that also opens the door 

Patch:
diff --git a/pushy/script/generate-certs.sh b/pushy/script/generate-certs.sh
index 24483a8b..ddc19bb9 100755
--- a/pushy/script/generate-certs.sh
+++ b/pushy/script/generate-certs.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 # Generate a new, self-signed root CA
 openssl req -config openssl-custom.cnf -extensions v3_ca -new -x509 -days 36500 -nodes -subj "/CN=PushyTestRoot" -newkey rsa:2048 -sha512 -out ca.pem -keyout ca.key
diff --git a/pushy/src/main/java/com/eatthepath/json/JsonParser.java b/pushy/src/main/java/com/eatthepath/json/JsonParser.java
index 892c143e..eaf82fd0 100644
--- a/pushy/src/main/java/com/eatthepath/json/JsonParser.java
+++ b/pushy/src/main/java/com/eatthepath/json/JsonParser.java
@@ -250,7 +250,7 @@ public class JsonParser {
                 if (stringBuilder.length() == 0) {
                     unescapedString = jsonString.substring(start, position);
                 } else {
-                    stringBuilder.append(jsonString, start, jsonString.length() - 1);
+                    stringBuilder.append(jsonString, start, position);
                     unescapedString = stringBuilder.toString();
                 }
 


Test patch:
diff --git a/pushy/src/test/java/com/eatthepath/json/JsonParserTest.java b/pushy/src/test/java/com/eatthepath/json/JsonParserTest.java
index 4667c3fd..5067ad0d 100644
--- a/pushy/src/test/java/com/eatthepath/json/JsonParserTest.java
+++ b/pushy/src/test/java/com/eatthepath/json/JsonParserTest.java
@@ -124,13 +124,18 @@ class JsonParserTest {
         nestedMap.put("b", Collections.emptyList());
         nestedMap.put("c", false);
 
+        final Map<String, Object> multipleEscapeMap = new HashMap<>();
+        multipleEscapeMap.put("key1", "value/1");
+        multipleEscapeMap.put("key2", "value/2");
+
         return Stream.of(
                 arguments("{}", Collections.emptyMap()),
                 arguments("{\"moose\": 12}", Collections.singletonMap("moose", 12L)),
                 arguments("{\"moose\": 12, \"porcupine\": true}", multipleEntryMap),
                 arguments("{\"moose\": {\"porcupine\": true}}",
                         Collections.singletonMap("moose", Collections.singletonMap("porcupine", true))),
-                arguments("{\"a\": {}, \"b\": [], \"c\": false}", nestedMap));
+                arguments("{\"a\": {}, \"b\": [], \"c\": false}", nestedMap),
+                arguments("{\"key1\":\"value\\/1\",\"key2\":\"value\\/2\"}", multipleEscapeMap));
     }
 
     @ParameterizedTest