sexp-format-test.c 3.45 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
#include "testutils.h"
#include "sexp.h"

#include "buffer.h"
#include "bignum.h"

int
test_main(void)
{
  struct nettle_buffer buffer;

12
13
  {
    const uint8_t e[] = "(3:foo(3:bar17:xxxxxxxxxxxxxxxxx))";
Niels Möller's avatar
Niels Möller committed
14

15
    nettle_buffer_init(&buffer);
16
    ASSERT(sexp_format(&buffer, "(%0s(%0s%0s))",
17
18
19
		       "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
20
    ASSERT(sexp_format(NULL, "(%0s(%0s%0s))",
21
22
23
24
25
26
27
28
29
30
		       "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
  {
    const uint8_t e[] = "{KDM6Zm9vKDM6YmFyMTc6eHh4eHh4eHh4eHh4eHh4eHgpKQ==}";

    nettle_buffer_init(&buffer);
31
    ASSERT(sexp_transport_format(&buffer, "(%0s(%0s%0s))",
32
33
34
		       "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
35
    ASSERT(sexp_transport_format(NULL, "(%0s(%0s%0s))",
36
37
38
39
40
41
42
				 "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
  {
43
    const uint8_t e[] = "1:\0""1:a2:bc3:def4:ghij5:\x00\xDE\xAD\xBE\xEF";
44
45

    nettle_buffer_init(&buffer);  
46
47
48
    ASSERT(sexp_format(&buffer, "%i%i%i%i%i%i",
		       0, 0x61, 0x6263, 0x646566, 0x6768696a, 0xDEADBEEF)
	   == LLENGTH(e));
49
    
50
    ASSERT(buffer.size == LLENGTH(e));
51
52
53
54
55
56
57
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }

  {
    const uint8_t e[] = "(3:foo(4:bar))";
    
    nettle_buffer_init(&buffer);  
58
    ASSERT(sexp_format(&buffer, "(%0s%l)",
59
60
61
62
63
64
		       "foo", 7, "(4:bar)")
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
65
66
67
68
69
70
71
72
73
74
75

  {
    const uint8_t e[] = "([1:t]3:foo3:bar[6:gazonk]3:baz1:q)";

    nettle_buffer_init(&buffer);
    ASSERT(sexp_format(&buffer, "(%0t%0s%0s%0t%0s%0t%0s)",
		       "t", "foo", "bar", "gazonk", "baz", NULL, "q")
	   == strlen(e));

    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
76
77
78
79
80
81
82
83
84
85

  /* Try literals */
  {
    const uint8_t e[] = "(3:foo(3:bar17:xxxxxxxxxxxxxxxxx))";

    nettle_buffer_init(&buffer);
    ASSERT(sexp_format(&buffer, "(%0s(bar%0s))",
		       "foo", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    ASSERT(sexp_format(NULL, "(%0s(bar %0s))",
		       "foo", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
  {
    const uint8_t e[] = "(3:foo(3:bar17:xxxxxxxxxxxxxxxxx))";

    nettle_buffer_init(&buffer);
    ASSERT(sexp_format(&buffer, "(%0s(bar xxxxxxxxxxxxxxxxx))",
		       "foo")
	   == strlen(e));
    
    ASSERT(sexp_format(NULL, "(%0s(bar xxxxxxxxxxxxxxxxx))",
		       "foo")
103
104
105
106
107
108
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }

Niels Möller's avatar
Niels Möller committed
109
110
111
#if HAVE_LIBGMP
  {
    mpz_t x;
112
113
114
115
116
    mpz_t y;
    mpz_t z;
    
    const uint8_t e[] =
      "(3:foo(3:bar1:\xff""11:abcdefghijk13:\0\x81""abcdefghijk))";
117
118

    nettle_buffer_clear(&buffer);
119
120
121
122

    mpz_init_set_si(x, -1);
    nettle_mpz_init_set_str_256_u(y, 11, "abcdefghijk");
    nettle_mpz_init_set_str_256_u(z, 12, "\x81""abcdefghijk");
Niels Möller's avatar
Niels Möller committed
123
124
    nettle_buffer_init(&buffer);

125
    ASSERT(sexp_format(&buffer, "(%0s(%0s%b%b%b))",
126
127
		     "foo", "bar", x, y, z)
	   == LLENGTH(e));
Niels Möller's avatar
Niels Möller committed
128

129
    ASSERT(sexp_format(NULL, "(%0s(%0s%b%b%b))",
130
131
		     "foo", "bar", x, y, z)
	   == LLENGTH(e));
132
    
133
    ASSERT(buffer.size == LLENGTH(e));
134
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
Niels Möller's avatar
Niels Möller committed
135
136
137
138
139
140
141
142

    nettle_buffer_clear(&buffer);
    mpz_clear(x);
  }
#endif /* HAVE_LIBGMP */

  SUCCESS();
}