sexp-format-test.c 3.83 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
#include "testutils.h"
#include "sexp.h"

#include "buffer.h"
5
6
7
8

#if HAVE_LIBGMP
# include "bignum.h"
#endif
Niels Möller's avatar
Niels Möller committed
9
10
11
12
13
14

int
test_main(void)
{
  struct nettle_buffer buffer;

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

18
    nettle_buffer_init(&buffer);
19
    ASSERT(sexp_format(&buffer, "(%0s(%0s%0s))",
20
21
22
		       "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
23
    ASSERT(sexp_format(NULL, "(%0s(%0s%0s))",
24
25
26
27
28
29
30
31
32
33
		       "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);
34
    ASSERT(sexp_transport_format(&buffer, "(%0s(%0s%0s))",
35
36
37
		       "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
38
    ASSERT(sexp_transport_format(NULL, "(%0s(%0s%0s))",
39
40
41
42
43
44
45
				 "foo", "bar", "xxxxxxxxxxxxxxxxx")
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }
  {
46
    const uint8_t e[] = "1:\0""1:a2:bc3:def4:ghij5:\x00\xDE\xAD\xBE\xEF";
47
48

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

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

  {
    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));
  }
79
80
81
82
83
84
85
86
87
88

  /* 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));
    
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    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")
106
107
108
109
110
111
	   == strlen(e));
    
    ASSERT(buffer.size == strlen(e));
    ASSERT(MEMEQ(buffer.size, buffer.contents, e));
  }

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  /* 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
127
128
129
#if HAVE_LIBGMP
  {
    mpz_t x;
130
131
132
133
134
    mpz_t y;
    mpz_t z;
    
    const uint8_t e[] =
      "(3:foo(3:bar1:\xff""11:abcdefghijk13:\0\x81""abcdefghijk))";
135
136

    nettle_buffer_clear(&buffer);
137
138
139
140

    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
141
142
    nettle_buffer_init(&buffer);

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

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

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

  SUCCESS();
}