1/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 *------------------------------------------------------------------
17 * pot_test.c - test harness for pot plugin
18 *------------------------------------------------------------------
19 */
20
21#include <vat/vat.h>
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24
25#include <vppinfra/error.h>
26
27#define __plugin_msg_base pot_test_main.msg_id_base
28#include <vlibapi/vat_helper_macros.h>
29
30/* Declare message IDs */
31#include <ioam/lib-pot/pot.api_enum.h>
32#include <ioam/lib-pot/pot.api_types.h>
33
34typedef struct {
35    /* API message ID base */
36    u16 msg_id_base;
37    vat_main_t *vat_main;
38} pot_test_main_t;
39
40pot_test_main_t pot_test_main;
41
42static int api_pot_profile_add (vat_main_t *vam)
43{
44#define MAX_BITS 64
45    unformat_input_t *input = vam->input;
46    vl_api_pot_profile_add_t *mp;
47    u8 *name = NULL;
48    u64 prime = 0;
49    u64 secret_share = 0;
50    u64 secret_key = 0;
51    u32  bits = MAX_BITS;
52    u64 lpc = 0, poly2 = 0;
53    u8 id = 0;
54    int rv = 0;
55    int ret;
56
57    while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
58      {
59        if (unformat(input, "name %s", &name))
60	  ;
61	else if(unformat(input, "id %d", &id))
62	  ;
63        else if (unformat(input, "validator-key 0x%Lx", &secret_key))
64          ;
65        else if (unformat(input, "prime-number 0x%Lx", &prime))
66          ;
67        else if (unformat(input, "secret-share 0x%Lx", &secret_share))
68          ;
69        else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
70          ;
71        else if (unformat(input, "lpc 0x%Lx", &lpc))
72          ;
73        else if (unformat(input, "bits-in-random %u", &bits))
74	  {
75	    if (bits > MAX_BITS)
76	      bits = MAX_BITS;
77	  }
78        else
79  	break;
80      }
81
82    if (!name)
83      {
84        errmsg ("name required\n");
85        rv = -99;
86        goto OUT;
87      }
88
89    M2(POT_PROFILE_ADD, mp, sizeof(vl_api_string_t) + vec_len(name));
90
91    vl_api_vec_to_api_string(name, &mp->list_name);
92    mp->secret_share = clib_host_to_net_u64(secret_share);
93    mp->polynomial_public = clib_host_to_net_u64(poly2);
94    mp->lpc = clib_host_to_net_u64(lpc);
95    mp->prime = clib_host_to_net_u64(prime);
96    if (secret_key != 0)
97      {
98        mp->secret_key = clib_host_to_net_u64(secret_key);
99        mp->validator = 1;
100      }
101    else
102      {
103	mp->validator = 0;
104      }
105    mp->id = id;
106    mp->max_bits = bits;
107
108    S(mp);
109    W (ret);
110    return ret;
111
112OUT:
113    vec_free(name);
114    return(rv);
115}
116
117static int api_pot_profile_activate (vat_main_t *vam)
118{
119#define MAX_BITS 64
120    unformat_input_t *input = vam->input;
121    vl_api_pot_profile_activate_t *mp;
122    u8 *name = NULL;
123    u8 id = 0;
124    int rv = 0;
125    int ret;
126
127    while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
128      {
129        if (unformat(input, "name %s", &name))
130	  ;
131	else if(unformat(input, "id %d", &id))
132	  ;
133        else
134  	break;
135      }
136
137    if (!name)
138      {
139        errmsg ("name required\n");
140        rv = -99;
141        goto OUT;
142      }
143
144    M2(POT_PROFILE_ACTIVATE, mp, sizeof(vl_api_string_t) + vec_len(name));
145    vl_api_vec_to_api_string(name, &mp->list_name);
146    mp->id = id;
147
148    S(mp);
149    W (ret);
150    return ret;
151
152OUT:
153    vec_free(name);
154    return(rv);
155}
156
157
158static int api_pot_profile_del (vat_main_t *vam)
159{
160    vl_api_pot_profile_del_t *mp;
161    int ret;
162
163    M(POT_PROFILE_DEL, mp);
164    S(mp);
165    W (ret);
166    return ret;
167}
168
169static int api_pot_profile_show_config_dump (vat_main_t *vam)
170{
171    unformat_input_t *input = vam->input;
172    vl_api_pot_profile_show_config_dump_t *mp;
173    u8 id = 0;
174    int ret;
175
176    while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
177    {
178      if(unformat(input,"id %d",&id));
179      else
180        break;
181    }
182    M(POT_PROFILE_SHOW_CONFIG_DUMP, mp);
183
184    mp->id = id;
185
186    S(mp);
187    W (ret);
188    return ret;
189}
190static int vl_api_pot_profile_show_config_details_t_handler (vat_main_t *vam)
191{
192  return -1;
193}
194
195/* Override generated plugin register symbol */
196#define vat_plugin_register pot_vat_plugin_register
197#include <ioam/lib-pot/pot.api_test.c>
198