summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/sp43/init.c
blob: 56c39859956b3308d4add17af0eb24b7f52ba9ab (plain) (blame)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 *  Exercise Object Manager Services
 *
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define TEST_INIT

#define CONFIGURE_INIT
#include "system.h"

#include <rtems/score/objectimpl.h>

const char rtems_test_name[] = "SP 43";

/* These functions have both macro and function incarnations */
#undef rtems_build_id
extern rtems_id rtems_build_id(int api,int class,int node,int index);
#undef rtems_build_name
extern rtems_name rtems_build_name(char C1,char C2,char C3,char C4);
#undef rtems_object_id_api_maximum
extern int rtems_object_id_api_maximum(void);
#undef rtems_object_id_api_minimum
extern int rtems_object_id_api_minimum(void);
#undef rtems_object_id_get_api
extern int rtems_object_id_get_api(rtems_id id);
#undef rtems_object_id_get_class
extern int rtems_object_id_get_class(rtems_id id);
#undef rtems_object_id_get_index
extern int rtems_object_id_get_index(rtems_id id);
#undef rtems_object_id_get_node
extern int rtems_object_id_get_node(rtems_id id);

void print_class_info(
  int                                 api,
  int                                 class,
  rtems_object_api_class_information *info
);

void change_name(
  rtems_id    id,
  const char *newName,
  bool        printable
);

rtems_id         main_task;
rtems_name       main_name;

void print_class_info(
  int                                 api,
  int                                 class,
  rtems_object_api_class_information *info
)
{
  printf(
    "%s API %s Information\n"
    "    minimum id  : 0x%08" PRIxrtems_id
      " maximum id: 0x%08" PRIxrtems_id "\n"
    "    maximum     :    %7" PRIu32 " available : %" PRIu32 "\n"
    "    auto_extend : %s\n",
    rtems_object_get_api_name(api),
    rtems_object_get_api_class_name(api, class),
    info->minimum_id,
    info->maximum_id,
    info->maximum,
    info->unallocated,
    ((info->auto_extend) ? "yes" : "no")
  );
}

void change_name(
  rtems_id    id,
  const char *newName,
  bool        printable
)
{
  rtems_status_code    sc;
  char                 name[ 5 ];
  char                *ptr;
  const char          *c;

  printf( "rtems_object_set_name - change name of init task to " );
  if ( printable )
    printf( "(%s)\n", newName );
  else {
    printf( "(" );
    for (c=newName ; *c ; ) {
       if (isprint((unsigned char)*c)) printf( "%c", *c );
       else                            printf( "0x%02x", *c );
       c++;
       if ( *c )
         printf( "-" );
    }
    printf( ")\n" );
  }

  sc = rtems_object_set_name( id, newName );
  directive_failed( sc, "rtems_object_set_name" );

  sc = rtems_object_get_classic_name( id, &main_name );
  directive_failed( sc, "rtems_object_get_classic_name" );
  put_name( main_name, FALSE );
  puts( " - name returned by rtems_object_get_classic_name" );

  ptr = rtems_object_get_name( id, 5, name );
  rtems_test_assert(ptr != NULL);
  printf( "rtems_object_get_name returned (%s) for init task\n", ptr );
}

rtems_task Init(
  rtems_task_argument argument
)
{
  rtems_status_code                   sc;
  rtems_id                            tmpId;
  rtems_name                          tmpName;
  char                                name[5];
  char                               *ptr;
  const char                          newName[5] = "New1";
  char                                tmpNameString[5];
  int                                 part;
  rtems_object_api_class_information  info;

  TEST_BEGIN();

  printf( "RTEMS Version: %s\n", rtems_get_version_string() );

  main_task = rtems_task_self();

  puts( "rtems_object_get_classic_name - INVALID_ADDRESS" );
  sc = rtems_object_get_classic_name( main_task, NULL );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ADDRESS,
    "rtems_object_get_classic_name #1"
  );

  puts( "rtems_object_get_classic_name - INVALID_ID (bad index)" );
  sc = rtems_object_get_classic_name( main_task + 5, &main_name );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ID,
    "rtems_object_get_classic_name #2"
  );

  puts( "rtems_object_get_classic_name - INVALID_ID (unallocated index)" );
  sc = rtems_object_get_classic_name( main_task + 1, &main_name );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ID,
    "rtems_object_get_classic_name #4"
  );

  puts( "rtems_object_get_classic_name - INVALID_ID (bad API)" );
  tmpId = rtems_build_id( 0xff, OBJECTS_RTEMS_TASKS, 1, 1 ),
  sc = rtems_object_get_classic_name( tmpId, &main_name );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ID,
    "rtems_object_get_classic_name #5"
  );

  sc = rtems_object_get_classic_name( main_task, &main_name );
  directive_failed( sc, "rtems_object_get_classic_name" );
  put_name( main_name, FALSE );
  puts( " - name returned by rtems_object_get_classic_name for Init task id" );

  sc = rtems_object_get_classic_name( RTEMS_SELF, &main_name );
  directive_failed( sc, "rtems_object_get_classic_name" );
  put_name( main_name, FALSE );
  puts( " - name returned by rtems_object_get_classic_name for RTEMS_SELF" );

  tmpName = rtems_build_name( 'T', 'E', 'M', 'P' );
  put_name( tmpName, FALSE );
  puts( " - rtems_build_name for TEMP" );


  /*
   * rtems_object_get_name - cases
   */
  puts( "rtems_object_get_name - bad id for class with instances" );
  ptr = rtems_object_get_name( main_task + 5, 5, name );
  rtems_test_assert(ptr == NULL);

  puts( "rtems_object_get_name - bad id for class without instances" );
  ptr = rtems_object_get_name(
    rtems_build_id( OBJECTS_CLASSIC_API, OBJECTS_RTEMS_BARRIERS, 1, 1 ),
    5,
    name
  );
  rtems_test_assert(ptr == NULL);

  puts( "rtems_object_get_name - bad length" );
  ptr = rtems_object_get_name( main_task, 0, name );
  rtems_test_assert(ptr == NULL);

  puts( "rtems_object_get_name - bad pointer" );
  ptr = rtems_object_get_name( main_task, 5, NULL );
  rtems_test_assert(ptr == NULL);

  ptr = rtems_object_get_name( main_task, 5, name );
  rtems_test_assert(ptr != NULL);
  printf( "rtems_object_get_name returned (%s) for init task id\n", ptr );

  ptr = rtems_object_get_name( RTEMS_SELF, 5, name );
  rtems_test_assert(ptr != NULL);
  printf( "rtems_object_get_name returned (%s) for RTEMS_SELF\n", ptr );

  /*
   * rtems_object_set_name - errors
   */

  puts( "rtems_object_set_name - INVALID_ADDRESS" );
  sc = rtems_object_set_name( tmpId, NULL );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ADDRESS,
    "rtems_object_set_name INVALID_ADDRESS"
  );

  puts( "rtems_object_set_name - INVALID_ID (bad API)" );
  tmpId = rtems_build_id( 0xff, OBJECTS_RTEMS_TASKS, 1, 1 ),
  sc = rtems_object_set_name( tmpId, newName );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ID,
    "rtems_object_set_name #1"
  );

  puts( "rtems_object_set_name - INVALID_ID (bad index)" );
  sc = rtems_object_set_name( main_task + 10, newName );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ID,
    "rtems_object_set_name #2"
  );

  /*
   * rtems_object_set_name - change name of init task in various ways.
   *
   * This is strange but pushes the SuperCore code to do different things.
   */

  change_name( main_task,  "New1", TRUE );
  change_name( main_task, "Ne1", TRUE );
  change_name( main_task, "N1", TRUE );
  change_name( main_task, "N", TRUE );
  change_name( main_task, "", TRUE );
  tmpNameString[0] = 'N';
  tmpNameString[1] = 0x07;
  tmpNameString[2] = 0x09;
  tmpNameString[3] = '1';
  tmpNameString[4] = '\0';
  change_name( main_task, tmpNameString, FALSE );

  /*
   * Change object name using SELF ID
   */

  change_name( RTEMS_SELF,  "SELF", TRUE );

  ptr = rtems_object_get_name( main_task, 5, name );
  rtems_test_assert(ptr != NULL);
  printf( "rtems_object_get_name returned (%s) for init task id\n", ptr );


  /*
   * Exercise id build and extraction routines
   */

  puts( "rtems_build_id - build an id to match init task" );
  tmpId = rtems_build_id( OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TASKS, 1, 1 );
  rtems_test_assert( tmpId == main_task );

  puts( "rtems_object_id_get_api - OK" );
  part = rtems_object_id_get_api( main_task );
  rtems_test_assert( part == OBJECTS_CLASSIC_API );

  puts( "rtems_object_id_get_class - OK" );
  part = rtems_object_id_get_class( main_task );
  rtems_test_assert( part == OBJECTS_RTEMS_TASKS );

  puts( "rtems_object_id_get_node - OK" );
  part = rtems_object_id_get_node( main_task );
  rtems_test_assert( part == 1 );

  puts( "rtems_object_id_get_index - OK" );
  part = rtems_object_id_get_index( main_task );
  rtems_test_assert( part == 1 );

  /*
   * Start another screen and do the API/Class min/max routines
   */
  rtems_test_pause();

  printf( "rtems_object_id_api_minimum returned %d\n",
          rtems_object_id_api_minimum() );
  printf( "rtems_object_id_api_maximum returned %d\n",
          rtems_object_id_api_maximum() );

  printf( "rtems_object_api_minimum_class(0) returned %d\n",
          rtems_object_api_minimum_class(0) );
  printf( "rtems_object_api_maximum_class(0) returned %d\n",
          rtems_object_api_maximum_class(0) );

  printf( "rtems_object_api_minimum_class(0) returned %d\n",
          rtems_object_api_minimum_class(0) );
  printf( "rtems_object_api_maximum_class(0) returned %d\n",
          rtems_object_api_maximum_class(0) );
  printf( "rtems_object_api_minimum_class(255) returned %d\n",
          rtems_object_api_minimum_class(255) );
  printf( "rtems_object_api_maximum_class(255) returned %d\n",
          rtems_object_api_maximum_class(255) );

  printf( "rtems_object_api_minimum_class(OBJECTS_INTERNAL_API) returned %d\n",
          rtems_object_api_minimum_class(OBJECTS_INTERNAL_API) );
  printf( "rtems_object_api_maximum_class(OBJECTS_INTERNAL_API) returned %d\n",
          rtems_object_api_maximum_class(OBJECTS_INTERNAL_API) );

  printf( "rtems_object_api_minimum_class(OBJECTS_CLASSIC_API) returned %d\n",
          rtems_object_api_minimum_class(OBJECTS_CLASSIC_API) );
  printf( "rtems_object_api_maximum_class(OBJECTS_CLASSIC_API) returned %d\n",
          rtems_object_api_maximum_class(OBJECTS_CLASSIC_API) );

  /*
   *  Another screen break for the API and class name tests
   */
  rtems_test_pause();

  printf( "rtems_object_get_api_name(0) = %s\n", rtems_object_get_api_name(0) );
  printf( "rtems_object_get_api_name(255) = %s\n",
    rtems_object_get_api_name(255));

  printf( "rtems_object_get_api_name(INTERNAL_API) = %s\n",
     rtems_object_get_api_name(OBJECTS_INTERNAL_API) );
  printf( "rtems_object_get_api_name(CLASSIC_API) = %s\n",
     rtems_object_get_api_name(OBJECTS_CLASSIC_API) );

  printf( "rtems_object_get_api_class_name(0, RTEMS_TASKS) = %s\n",
    rtems_object_get_api_class_name( 0, OBJECTS_RTEMS_TASKS ) );
  printf( "rtems_object_get_api_class_name(CLASSIC_API, 0) = %s\n",
    rtems_object_get_api_class_name( OBJECTS_CLASSIC_API, 0 ) );
  printf("rtems_object_get_api_class_name(INTERNAL_API, MUTEXES) = %s\n",
    rtems_object_get_api_class_name(
       OBJECTS_INTERNAL_API, OBJECTS_INTERNAL_MUTEXES));
  printf("rtems_object_get_api_class_name(CLASSIC_API, RTEMS_BARRIERS) = %s\n",
    rtems_object_get_api_class_name(
       OBJECTS_CLASSIC_API, OBJECTS_RTEMS_BARRIERS));

  /*
   *  Another screen break for the information
   */

  rtems_test_pause();

  puts( "rtems_object_get_class_information - INVALID_ADDRESS" );
  sc = rtems_object_get_class_information(
             OBJECTS_INTERNAL_API, OBJECTS_INTERNAL_THREADS, NULL );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_ADDRESS,
    "rtems_object_get_class_information"
  );

  puts( "rtems_object_get_class_information - INVALID_NUMBER (bad API)" );
  sc =
    rtems_object_get_class_information(0, OBJECTS_INTERNAL_THREADS, &info);
  fatal_directive_status(
    sc,
    RTEMS_INVALID_NUMBER,
    "rtems_object_get_class_information (API)"
  );

  puts( "rtems_object_get_class_information - INVALID_NUMBER (api=0xff)" );
  sc = rtems_object_get_class_information( 0xff, 1, &info );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_NUMBER,
    "rtems_object_get_class_information (api=0xff)"
  );

  puts( "rtems_object_get_class_information - INVALID_NUMBER (class=0)" );
  sc = rtems_object_get_class_information(
    OBJECTS_INTERNAL_API, 0, &info );
  fatal_directive_status(
    sc,
    RTEMS_INVALID_NUMBER,
    "rtems_object_get_class_information (class=0)"
  );

  puts(
    "rtems_object_get_class_information - INVALID_NUMBER (class too high)" );
  sc = rtems_object_get_class_information(
    OBJECTS_INTERNAL_API, 0xff, &info);
  fatal_directive_status(
    sc,
    RTEMS_INVALID_NUMBER,
    "rtems_object_get_class_information (class #2)"
  );

  puts( "rtems_object_get_class_information - Classic Tasks - OK" );
  sc = rtems_object_get_class_information(
             OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TASKS, &info );
  directive_failed( sc, "rtems_object_get_class_information" );
  print_class_info( OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TASKS, &info );

  puts( "rtems_object_get_class_information - Classic Timers - OK" );
  sc = rtems_timer_create(0, NULL);
  fatal_directive_status(
    sc,
    RTEMS_INVALID_NAME,
    "rtems_timer_create"
  );
  sc = rtems_object_get_class_information(
             OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TIMERS, &info );
  directive_failed( sc, "rtems_object_get_class_information" );
  print_class_info( OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TIMERS, &info );

  /*
   *  Ugly hack to force a weird error.
   *
   *  The weird case is that we need to look up a thread Id where the
   *  thread classes' object information table pointer is NULL.  Probably
   *  impossible to really hit until registration is completely dynamically
   *  configurable.
   */
  {
    rtems_task_priority              old_priority;
    void                            *tmp;
    int                              class;
    int                              api;

    class = OBJECTS_INTERNAL_API;
    api   = OBJECTS_INTERNAL_THREADS;

    puts( "rtems_task_set_priority - use valid Idle thread id" );
    sc = rtems_task_set_priority(
      rtems_build_id( class, api, 1, 1 ),
      RTEMS_CURRENT_PRIORITY,
      &old_priority
    );
    directive_failed( sc, "rtems_task_set_priority" );

    /* destroy internal API thread class pointer */
    puts( "rtems_task_set_priority - clobber internal thread class info" );
    tmp = _Objects_Information_table[ api ][ class ];
    _Objects_Information_table[ api ][ class ] = NULL;

    puts( "rtems_task_set_priority - use valid Idle thread id again" );
    sc = rtems_task_set_priority(
      rtems_build_id( class, api, 1, 1 ),
      RTEMS_CURRENT_PRIORITY,
      &old_priority
    );
    fatal_directive_status( sc, RTEMS_INVALID_ID, "rtems_task_set_priority" );

    puts( "rtems_task_set_priority - use valid Idle thread id again" );
    sc = rtems_task_set_priority(
      rtems_build_id( class, api, 1, 1 ),
      RTEMS_CURRENT_PRIORITY,
      &old_priority
    );
    fatal_directive_status( sc, RTEMS_INVALID_ID, "rtems_task_set_priority" );

    /* restore pointer */
    puts( "rtems_task_set_priority - restore internal thread class info" );
    _Objects_Information_table[ api ][ class ] = tmp;
  }

  /*
   *  Bad Id on an object which disables interrupts as part of translating
   *  the Id into an object pointer.  Semaphore is the only object that
   *  needs this. This is a "good" Id in that is it in range, but bad in
   *  that it has not been allocated so the local_table pointer is NULL.
   */
  puts( "rtems_semaphore_obtain - good but uncreated ID - INVALID_ID - OK" );
  sc = rtems_semaphore_obtain(
    rtems_build_id(
      OBJECTS_CLASSIC_API,
      OBJECTS_RTEMS_SEMAPHORES,
      1,
      rtems_configuration_get_maximum_semaphores()
    ),
    RTEMS_DEFAULT_OPTIONS,
    0
  );
  fatal_directive_status( sc, RTEMS_INVALID_ID, "rtems_semaphore_obtain" );

  TEST_END();
  rtems_test_exit( 0 );
}