sexp-format-test.c 3.8 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));
  }

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  /* Literal parenthesis */
  {
    const uint8_t e[] = ")3:foo(3:bar";

    nettle_buffer_init(&buffer);
    ASSERT(sexp_format(&buffer, "%)foo%(%s", 3, "bar")
	   == strlen(e));
    
    ASSERT(sexp_format(NULL, "%)foo%(%s", 3, "bar")
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
  
Niels Möller's avatar
Niels Möller committed
124
125
126
#if HAVE_LIBGMP
  {
    mpz_t x;
127
128
129
130
131
    mpz_t y;
    mpz_t z;
    
    const uint8_t e[] =
      "(3:foo(3:bar1:\xff""11:abcdefghijk13:\0\x81""abcdefghijk))";
132
133

    nettle_buffer_clear(&buffer);
134
135
136
137

    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
138
139
    nettle_buffer_init(&buffer);

140
    ASSERT(sexp_format(&buffer, "(%0s(%0s%b%b%b))",
141
142
		     "foo", "bar", x, y, z)
	   == LLENGTH(e));
Niels Möller's avatar
Niels Möller committed
143

144
    ASSERT(sexp_format(NULL, "(%0s(%0s%b%b%b))",
145
146
		     "foo", "bar", x, y, z)
	   == LLENGTH(e));
147
    
148
    ASSERT(buffer.size == LLENGTH(e));
149
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
Niels Möller's avatar
Niels Möller committed
150
151
152
153
154
155
156
157

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

  SUCCESS();
}