Author: vlendec
Date: 2004-08-31 21:21:22 +0000 (Tue, 31 Aug 2004)
New Revision: 2148

WebSVN: 
http://websvn.samba.org/websvn/changeset.php?rep=samba&path=/trunk/source&rev=2148&nolog=1

Log:
ASN1 ist really interesting :-)

This now survives the test cases Love has kindly given me. -1 was still
failing...

Volker


Added:
   trunk/source/torture/t_asn1.c
Modified:
   trunk/source/Makefile.in
   trunk/source/libsmb/asn1.c


Changeset:
Modified: trunk/source/Makefile.in
===================================================================
--- trunk/source/Makefile.in    2004-08-31 20:36:55 UTC (rev 2147)
+++ trunk/source/Makefile.in    2004-08-31 21:21:22 UTC (rev 2148)
@@ -1278,6 +1278,9 @@
 bin/[EMAIL PROTECTED]@: bin/[EMAIL PROTECTED]@ torture/t_push_ucs2.o
        $(CC) $(FLAGS) -o $@ $(LIBS) torture/t_push_ucs2.o -L ./bin -lbigballofmud
 
+bin/[EMAIL PROTECTED]@: bin/[EMAIL PROTECTED]@ torture/t_asn1.o
+       $(CC) $(FLAGS) -o $@ $(LIBS) torture/t_asn1.o -L ./bin -lbigballofmud
+
 bin/[EMAIL PROTECTED]@: lib/snprintf.c
        $(CC) $(FLAGS) -o $@ -DTEST_SNPRINTF lib/snprintf.c -lm
 install: installbin installman installscripts installdat installswat installmodules 
@INSTALLCLIENT@
@@ -1569,4 +1572,4 @@
 # These are called by the test suite and need to be built before
 # running it.  For the time being we don't build all of BIN_PROGS,
 # because they're not all needed.
-check-programs: bin/t_strcmp bin/t_strstr bin/t_push_ucs2 bin/smbcontrol 
bin/t_snprintf
+check-programs: bin/t_strcmp bin/t_strstr bin/t_push_ucs2 bin/smbcontrol 
bin/t_snprintf bin/t_asn1

Modified: trunk/source/libsmb/asn1.c
===================================================================
--- trunk/source/libsmb/asn1.c  2004-08-31 20:36:55 UTC (rev 2147)
+++ trunk/source/libsmb/asn1.c  2004-08-31 21:21:22 UTC (rev 2148)
@@ -151,7 +151,15 @@
 BOOL asn1_write_Integer(ASN1_DATA *data, int i)
 {
        if (!asn1_push_tag(data, ASN1_INTEGER)) return False;
-       push_int_bigendian(data, i, i<0);
+       if (i == -1) {
+               /* -1 is special as it consists of all-0xff bytes. In
+                    push_int_bigendian this is the only case that is not
+                    properly handled, as all 0xff bytes would be handled as
+                    leading ones to be ignored. */
+               asn1_write_uint8(data, 0xff);
+       } else {
+               push_int_bigendian(data, i, i<0);
+       }
        return asn1_pop_tag(data);
 }
 

Added: trunk/source/torture/t_asn1.c
===================================================================
--- trunk/source/torture/t_asn1.c       2004-08-31 20:36:55 UTC (rev 2147)
+++ trunk/source/torture/t_asn1.c       2004-08-31 21:21:22 UTC (rev 2148)
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2004 by Volker Lendecke
+ *
+ * Test harness for asn1_write_*, inspired by Love Hornquist Astrand
+ */
+
+#include "includes.h"
+
+static DATA_BLOB tests[] = {
+        {"\x02\x01\x00", 3, NULL},
+        {"\x02\x01\x7f", 3, NULL},
+        {"\x02\x02\x00\x80", 4, NULL},
+        {"\x02\x02\x01\x00", 4, NULL},
+        {"\x02\x01\x80", 3, NULL},
+        {"\x02\x02\xff\x7f", 4, NULL},
+        {"\x02\x01\xff", 3, NULL},
+        {"\x02\x02\xff\x01", 4, NULL},
+        {"\x02\x02\x00\xff", 4, NULL},
+        {"\x02\x04\x80\x00\x00\x00", 6, NULL},
+        {"\x02\x04\x7f\xff\xff\xff", 6, NULL},
+       {NULL, 0, NULL}
+};
+
+static int values[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
+                      0x80000000, 0x7fffffff};
+
+int main(void)
+{
+       int i = 0;
+       int val;
+       BOOL ok = True;
+
+       for (i=0; tests[i].data != NULL; i++) {
+               ASN1_DATA data;
+               DATA_BLOB blob;
+
+               ZERO_STRUCT(data);
+               asn1_write_Integer(&data, values[i]);
+
+               if ((data.length != tests[i].length) ||
+                   (memcmp(data.data, tests[i].data, data.length) != 0)) {
+                       printf("Test for %d failed\n", values[i]);
+                       ok = False;
+               }
+
+               blob.data = data.data;
+               blob.length = data.length;
+               asn1_load(&data, blob);
+               if (!asn1_read_Integer(&data, &val)) {
+                       printf("Could not read our own Integer for %d\n",
+                              values[i]);
+                       ok = False;
+               }
+               if (val != values[i]) {
+                       printf("%d -> ASN -> Int %d\n", values[i], val);
+                       ok = False;
+               }
+       }
+
+       return ok ? 0 : 1;
+}

Reply via email to