summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/tests/encodeinttest.c
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /security/nss/cmd/tests/encodeinttest.c
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'security/nss/cmd/tests/encodeinttest.c')
-rw-r--r--security/nss/cmd/tests/encodeinttest.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/security/nss/cmd/tests/encodeinttest.c b/security/nss/cmd/tests/encodeinttest.c
new file mode 100644
index 000000000..f0062ea5e
--- /dev/null
+++ b/security/nss/cmd/tests/encodeinttest.c
@@ -0,0 +1,62 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include <stdio.h>
+
+#include "secasn1.h"
+
+struct TestCase {
+ long value;
+ unsigned char data[5];
+ unsigned int len;
+};
+
+static struct TestCase testCase[] = {
+/* XXX NSS doesn't generate the shortest encoding for negative values. */
+#if 0
+ { -128, { 0x80 }, 1 },
+ { -129, { 0xFF, 0x7F }, 2 },
+#endif
+
+ { 0, { 0x00 }, 1 },
+ { 127, { 0x7F }, 1 },
+ { 128, { 0x00, 0x80 }, 2 },
+ { 256, { 0x01, 0x00 }, 2 },
+ { 32768, { 0x00, 0x80, 0x00 }, 3 }
+};
+
+int
+main()
+{
+ PRBool failed = PR_FALSE;
+ unsigned int i;
+ unsigned int j;
+
+ for (i = 0; i < sizeof(testCase) / sizeof(testCase[0]); i++) {
+ SECItem encoded;
+ if (SEC_ASN1EncodeInteger(NULL, &encoded, testCase[i].value) == NULL) {
+ fprintf(stderr, "SEC_ASN1EncodeInteger failed\n");
+ failed = PR_TRUE;
+ continue;
+ }
+ if (encoded.len != testCase[i].len ||
+ memcmp(encoded.data, testCase[i].data, encoded.len) != 0) {
+ fprintf(stderr, "Encoding of %ld is incorrect:",
+ testCase[i].value);
+ for (j = 0; j < encoded.len; j++) {
+ fprintf(stderr, " 0x%02X", (unsigned int)encoded.data[j]);
+ }
+ fputs("\n", stderr);
+ failed = PR_TRUE;
+ }
+ PORT_Free(encoded.data);
+ }
+
+ if (failed) {
+ fprintf(stderr, "FAIL\n");
+ return 1;
+ }
+ printf("PASS\n");
+ return 0;
+}