summaryrefslogtreecommitdiffstats
path: root/src/windows/identity/kconfig/test/utiltest.c
blob: f999dfa7109d8733ddbdc4dad51503e4619c41e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<stdio.h>
#include<kconfig.h>
#include<strsafe.h>

struct string_pair {
  wchar_t * ms;
  wchar_t * csv;
};

struct string_pair strings[] = {
  {L"foo\0bar\0baz,quux\0ab\"cd\0", L"foo,bar,\"baz,quux\",\"ab\"\"cd\""},
  {L"a\0b\0c\0d\0e\0", L"a,b,c,d,e"},
  {L"1\0", L"1"},
  {L"\0", L""},
  {L"b\0a\0", L"b,a"},
  {L"c\0a\0b\0", L"c,a,b"},
  {L"c\0a\0B\0", L"c,a,B"},
  {L"sdf\0Bar\0Foo\0BBB\0", L"sdf,Bar,Foo,BBB"}
};

int n_strings = ARRAYLENGTH(strings);

void print_ms(wchar_t * ms) {
  wchar_t * s;
  size_t cch;

  s = ms;
  while(*s) {
    printf("%S\\0", s);
    StringCchLength(s, 512, &cch);
    s += cch + 1;
  }
}

int ms_to_csv_test(void) {
  wchar_t wbuf[512];
  int i;
  khm_int32 code = 0;
  size_t cbbuf;
  size_t cbr;
  size_t cbnull;

  printf("khc_multi_string_to_csv() test:\n");

  for(i=0; i<n_strings; i++) {
    cbbuf = sizeof(wbuf);
    printf("Multi string:[");
    print_ms(strings[i].ms);
    printf("]->");
    code = khc_multi_string_to_csv(NULL, &cbnull, strings[i].ms);
    code = khc_multi_string_to_csv(wbuf, &cbbuf, strings[i].ms);
    if(code) {
      printf(" returned %d\n", code);
      return code;
    }
    printf("CSV[%S]", wbuf);
    if(wcscmp(wbuf, strings[i].csv)) {
      printf(" MISMATCH!");
      return 1;
    }

    StringCbLength(wbuf, sizeof(wbuf), &cbr);
    cbr+= sizeof(wchar_t);

    if(cbr != cbbuf) {
      printf(" Length mismatch");
      return 1;
    }

    if(cbnull != cbr) {
      printf(" NULL length mismatch");
      return 1;
    }

    printf("\n");
  }

  return code;
}

int csv_to_ms_test(void) {
  wchar_t wbuf[512];
  int i;
  khm_int32 code = 0;
  size_t cbbuf;
  size_t cbr;
  size_t cbnull;

  printf("khc_csv_to_multi_string() test:\n");

  for(i=0; i<n_strings; i++) {
    cbbuf = sizeof(wbuf);
    printf("CSV:[%S]->", strings[i].csv);
    code = khc_csv_to_multi_string(NULL, &cbnull, strings[i].csv);
    code = khc_csv_to_multi_string(wbuf, &cbbuf, strings[i].csv);
    if(code) {
      printf(" returned %d\n", code);
      return code;
    }
    printf("MS[");
    print_ms(wbuf);
    printf("]");

    if(cbnull != cbbuf) {
      printf(" NULL length mismatch");
      return 1;
    }

    printf("\n");

    printf("  Byte length:%d\n", cbbuf);
  }

  return code;
}

int ms_append_test(void)
{
  wchar_t wbuf[512];
  size_t cbbuf;
  khm_int32 code;
  int i;

  printf("khc_multi_string_append() test:\n");

  for(i=0; i<n_strings; i++) {
    cbbuf = sizeof(wbuf);
    khc_csv_to_multi_string(wbuf, &cbbuf, strings[i].csv);

    printf("MS[");
    print_ms(wbuf);
    printf("] + [foo]=[");
  
    cbbuf = sizeof(wbuf);
    code = khc_multi_string_append(wbuf, &cbbuf, L"foo");

    if(code) {
      printf(" returned %d\n", code);
      return code;
    }

    print_ms(wbuf);
    printf("]\n");

    printf("  byte length: %d\n", cbbuf);
  }
  return code;
}

int ms_delete_test(void)
{
  int code = 0;
  wchar_t wbuf[512];
  int i;
  size_t cbs;

  printf("khc_multi_string_delete() test:\n");
  for(i=0; i<n_strings; i++) {
    cbs = sizeof(wbuf);
    khc_csv_to_multi_string(wbuf, &cbs, strings[i].csv);

    printf("MS[");
    print_ms(wbuf);
    printf("] - [b]=[");

    printf("cs:");
    code = khc_multi_string_delete(wbuf, L"b", KHC_CASE_SENSITIVE);
    if(code) {
      printf("ci:");
      code = khc_multi_string_delete(wbuf, L"b", 0);
    }
    if(code) {
      printf("pcs:");
      code = khc_multi_string_delete(wbuf, L"b", KHC_CASE_SENSITIVE | KHC_PREFIX);
    }
    if(code) {
      printf("pci:");
      code = khc_multi_string_delete(wbuf, L"b", KHC_PREFIX);
    }

    if(!code)
      print_ms(wbuf);
    else
      printf(" returned %d\n", code);

    printf("]\n");
  }

  return code;
}

int main(int argc, char ** argv) {

  if(ms_to_csv_test())
    return 1;

  if(csv_to_ms_test())
    return 1;

  if(ms_append_test())
    return 1;

  if(ms_delete_test())
    return 1;

  return 0;
}