~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/tests/test_cfgfile.c

  • Committer: Scott James Remnant
  • Date: 2007-01-09 16:43:58 UTC
  • Revision ID: scott@netsplit.com-20070109164358-uskg7l84rzn2kyxe
* init/cfgfile.c (cfg_stanza_start, cfg_stanza_stop): Disallow
duplicate values for the script.
* init/tests/test_cfgfile.c (test_stanza_start, test_stanza_stop):
Test cases for those two functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1178
1178
}
1179
1179
 
1180
1180
void
 
1181
test_stanza_start (void)
 
1182
{
 
1183
        Job   *job;
 
1184
        Event *event;
 
1185
        FILE  *jf, *output;
 
1186
        char   filename[PATH_MAX];
 
1187
 
 
1188
        TEST_FUNCTION ("cfg_stanza_start");
 
1189
        program_name = "test";
 
1190
        output = tmpfile ();
 
1191
 
 
1192
        TEST_FILENAME (filename);
 
1193
 
 
1194
 
 
1195
        /* Check that a start stanza with an on argument followed by an
 
1196
         * event name results in the named event being added to the
 
1197
         * start events list.
 
1198
         */
 
1199
        TEST_FEATURE ("with on and single argument");
 
1200
        jf = fopen (filename, "w");
 
1201
        fprintf (jf, "exec /sbin/daemon\n");
 
1202
        fprintf (jf, "start on wibble\n");
 
1203
        fclose (jf);
 
1204
 
 
1205
        job = cfg_read_job (NULL, filename, "test");
 
1206
 
 
1207
        TEST_ALLOC_SIZE (job, sizeof (Job));
 
1208
        TEST_LIST_NOT_EMPTY (&job->start_events);
 
1209
 
 
1210
        event = (Event *)job->start_events.next;
 
1211
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1212
        TEST_EQ_STR (event->name, "wibble");
 
1213
 
 
1214
        nih_list_free (&job->entry);
 
1215
 
 
1216
 
 
1217
        /* Check that a start stanza with a script argument begins a
 
1218
         * block which is stored in the start_script member of the job.
 
1219
         */
 
1220
        TEST_FEATURE ("with script and block");
 
1221
        jf = fopen (filename, "w");
 
1222
        fprintf (jf, "exec /sbin/daemon\n");
 
1223
        fprintf (jf, "start script\n");
 
1224
        fprintf (jf, "    echo\n");
 
1225
        fprintf (jf, "end script\n");
 
1226
        fclose (jf);
 
1227
 
 
1228
        job = cfg_read_job (NULL, filename, "test");
 
1229
 
 
1230
        TEST_ALLOC_SIZE (job, sizeof (Job));
 
1231
 
 
1232
        TEST_ALLOC_PARENT (job->start_script, job);
 
1233
        TEST_EQ_STR (job->start_script, "echo\n");
 
1234
 
 
1235
        nih_list_free (&job->entry);
 
1236
 
 
1237
 
 
1238
        /* Check that repeated start on stanzas are permitted, each appending
 
1239
         * to the last.
 
1240
         */
 
1241
        TEST_FEATURE ("with multiple on stanzas");
 
1242
        jf = fopen (filename, "w");
 
1243
        fprintf (jf, "exec /sbin/daemon\n");
 
1244
        fprintf (jf, "start on wibble\n");
 
1245
        fprintf (jf, "start on wobble\n");
 
1246
        fprintf (jf, "start on waggle\n");
 
1247
        fclose (jf);
 
1248
 
 
1249
        job = cfg_read_job (NULL, filename, "test");
 
1250
 
 
1251
        TEST_ALLOC_SIZE (job, sizeof (Job));
 
1252
        TEST_LIST_NOT_EMPTY (&job->start_events);
 
1253
 
 
1254
        event = (Event *)job->start_events.next;
 
1255
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1256
        TEST_EQ_STR (event->name, "wibble");
 
1257
 
 
1258
        event = (Event *)event->entry.next;
 
1259
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1260
        TEST_EQ_STR (event->name, "wobble");
 
1261
 
 
1262
        event = (Event *)event->entry.next;
 
1263
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1264
        TEST_EQ_STR (event->name, "waggle");
 
1265
 
 
1266
        nih_list_free (&job->entry);
 
1267
 
 
1268
 
 
1269
        /* Check that multiple start script stanzas results in a
 
1270
         * syntax error.
 
1271
         */
 
1272
        TEST_FEATURE ("with script and block");
 
1273
        jf = fopen (filename, "w");
 
1274
        fprintf (jf, "exec /sbin/daemon\n");
 
1275
        fprintf (jf, "start script\n");
 
1276
        fprintf (jf, "    echo\n");
 
1277
        fprintf (jf, "end script\n");
 
1278
        fprintf (jf, "start script\n");
 
1279
        fprintf (jf, "    ls\n");
 
1280
        fprintf (jf, "end script\n");
 
1281
        fclose (jf);
 
1282
 
 
1283
        TEST_DIVERT_STDERR (output) {
 
1284
                job = cfg_read_job (NULL, filename, "test");
 
1285
        }
 
1286
        rewind (output);
 
1287
 
 
1288
        TEST_EQ_P (job, NULL);
 
1289
 
 
1290
        TEST_ERROR_EQ (output, "6: Duplicate value\n");
 
1291
        TEST_FILE_END (output);
 
1292
 
 
1293
        TEST_FILE_RESET (output);
 
1294
 
 
1295
 
 
1296
        /* Check that a start stanza without a second-level argument results
 
1297
         * in a syntax error.
 
1298
         */
 
1299
        TEST_FEATURE ("with missing argument");
 
1300
        jf = fopen (filename, "w");
 
1301
        fprintf (jf, "exec /sbin/daemon\n");
 
1302
        fprintf (jf, "start\n");
 
1303
        fclose (jf);
 
1304
 
 
1305
        TEST_DIVERT_STDERR (output) {
 
1306
                job = cfg_read_job (NULL, filename, "test");
 
1307
        }
 
1308
        rewind (output);
 
1309
 
 
1310
        TEST_EQ_P (job, NULL);
 
1311
 
 
1312
        TEST_ERROR_EQ (output, "2: Expected token\n");
 
1313
        TEST_FILE_END (output);
 
1314
 
 
1315
        TEST_FILE_RESET (output);
 
1316
 
 
1317
 
 
1318
        /* Check that a start stanza with an unknown second-level argument
 
1319
         * results in a syntax error.
 
1320
         */
 
1321
        TEST_FEATURE ("with unknown argument");
 
1322
        jf = fopen (filename, "w");
 
1323
        fprintf (jf, "exec /sbin/daemon\n");
 
1324
        fprintf (jf, "start foo\n");
 
1325
        fclose (jf);
 
1326
 
 
1327
        TEST_DIVERT_STDERR (output) {
 
1328
                job = cfg_read_job (NULL, filename, "test");
 
1329
        }
 
1330
        rewind (output);
 
1331
 
 
1332
        TEST_EQ_P (job, NULL);
 
1333
 
 
1334
        TEST_ERROR_EQ (output, "2: Unknown stanza\n");
 
1335
        TEST_FILE_END (output);
 
1336
 
 
1337
        TEST_FILE_RESET (output);
 
1338
 
 
1339
 
 
1340
        /* Check that a start on stanza without an argument results in a
 
1341
         * syntax error.
 
1342
         */
 
1343
        TEST_FEATURE ("with on and missing argument");
 
1344
        jf = fopen (filename, "w");
 
1345
        fprintf (jf, "exec /sbin/daemon\n");
 
1346
        fprintf (jf, "start on\n");
 
1347
        fclose (jf);
 
1348
 
 
1349
        TEST_DIVERT_STDERR (output) {
 
1350
                job = cfg_read_job (NULL, filename, "test");
 
1351
        }
 
1352
        rewind (output);
 
1353
 
 
1354
        TEST_EQ_P (job, NULL);
 
1355
 
 
1356
        TEST_ERROR_EQ (output, "2: Expected token\n");
 
1357
        TEST_FILE_END (output);
 
1358
 
 
1359
        TEST_FILE_RESET (output);
 
1360
 
 
1361
 
 
1362
        /* Check that a start on stanza with an extra argument results in a
 
1363
         * syntax error.
 
1364
         */
 
1365
        TEST_FEATURE ("with extra argument");
 
1366
        jf = fopen (filename, "w");
 
1367
        fprintf (jf, "exec /sbin/daemon\n");
 
1368
        fprintf (jf, "start on foo bar\n");
 
1369
        fclose (jf);
 
1370
 
 
1371
        TEST_DIVERT_STDERR (output) {
 
1372
                job = cfg_read_job (NULL, filename, "test");
 
1373
        }
 
1374
        rewind (output);
 
1375
 
 
1376
        TEST_EQ_P (job, NULL);
 
1377
 
 
1378
        TEST_ERROR_EQ (output, "2: Unexpected token\n");
 
1379
        TEST_FILE_END (output);
 
1380
 
 
1381
        TEST_FILE_RESET (output);
 
1382
 
 
1383
 
 
1384
        /* Check that a start script stanza with an extra argument results
 
1385
         * in a syntax error.
 
1386
         */
 
1387
        TEST_FEATURE ("with extra argument");
 
1388
        jf = fopen (filename, "w");
 
1389
        fprintf (jf, "exec /sbin/daemon\n");
 
1390
        fprintf (jf, "start script foo\n");
 
1391
        fclose (jf);
 
1392
 
 
1393
        TEST_DIVERT_STDERR (output) {
 
1394
                job = cfg_read_job (NULL, filename, "test");
 
1395
        }
 
1396
        rewind (output);
 
1397
 
 
1398
        TEST_EQ_P (job, NULL);
 
1399
 
 
1400
        TEST_ERROR_EQ (output, "2: Unexpected token\n");
 
1401
        TEST_FILE_END (output);
 
1402
 
 
1403
        TEST_FILE_RESET (output);
 
1404
 
 
1405
 
 
1406
        fclose (output);
 
1407
}
 
1408
 
 
1409
void
 
1410
test_stanza_stop (void)
 
1411
{
 
1412
        Job   *job;
 
1413
        Event *event;
 
1414
        FILE  *jf, *output;
 
1415
        char   filename[PATH_MAX];
 
1416
 
 
1417
        TEST_FUNCTION ("cfg_stanza_stop");
 
1418
        program_name = "test";
 
1419
        output = tmpfile ();
 
1420
 
 
1421
        TEST_FILENAME (filename);
 
1422
 
 
1423
 
 
1424
        /* Check that a stop stanza with an on argument followed by an
 
1425
         * event name results in the named event being added to the
 
1426
         * stop events list.
 
1427
         */
 
1428
        TEST_FEATURE ("with on and single argument");
 
1429
        jf = fopen (filename, "w");
 
1430
        fprintf (jf, "exec /sbin/daemon\n");
 
1431
        fprintf (jf, "stop on wibble\n");
 
1432
        fclose (jf);
 
1433
 
 
1434
        job = cfg_read_job (NULL, filename, "test");
 
1435
 
 
1436
        TEST_ALLOC_SIZE (job, sizeof (Job));
 
1437
        TEST_LIST_NOT_EMPTY (&job->stop_events);
 
1438
 
 
1439
        event = (Event *)job->stop_events.next;
 
1440
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1441
        TEST_EQ_STR (event->name, "wibble");
 
1442
 
 
1443
        nih_list_free (&job->entry);
 
1444
 
 
1445
 
 
1446
        /* Check that a stop stanza with a script argument begins a
 
1447
         * block which is stored in the stop_script member of the job.
 
1448
         */
 
1449
        TEST_FEATURE ("with script and block");
 
1450
        jf = fopen (filename, "w");
 
1451
        fprintf (jf, "exec /sbin/daemon\n");
 
1452
        fprintf (jf, "stop script\n");
 
1453
        fprintf (jf, "    echo\n");
 
1454
        fprintf (jf, "end script\n");
 
1455
        fclose (jf);
 
1456
 
 
1457
        job = cfg_read_job (NULL, filename, "test");
 
1458
 
 
1459
        TEST_ALLOC_SIZE (job, sizeof (Job));
 
1460
 
 
1461
        TEST_ALLOC_PARENT (job->stop_script, job);
 
1462
        TEST_EQ_STR (job->stop_script, "echo\n");
 
1463
 
 
1464
        nih_list_free (&job->entry);
 
1465
 
 
1466
 
 
1467
        /* Check that repeated stop on stanzas are permitted, each appending
 
1468
         * to the last.
 
1469
         */
 
1470
        TEST_FEATURE ("with multiple on stanzas");
 
1471
        jf = fopen (filename, "w");
 
1472
        fprintf (jf, "exec /sbin/daemon\n");
 
1473
        fprintf (jf, "stop on wibble\n");
 
1474
        fprintf (jf, "stop on wobble\n");
 
1475
        fprintf (jf, "stop on waggle\n");
 
1476
        fclose (jf);
 
1477
 
 
1478
        job = cfg_read_job (NULL, filename, "test");
 
1479
 
 
1480
        TEST_ALLOC_SIZE (job, sizeof (Job));
 
1481
        TEST_LIST_NOT_EMPTY (&job->stop_events);
 
1482
 
 
1483
        event = (Event *)job->stop_events.next;
 
1484
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1485
        TEST_EQ_STR (event->name, "wibble");
 
1486
 
 
1487
        event = (Event *)event->entry.next;
 
1488
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1489
        TEST_EQ_STR (event->name, "wobble");
 
1490
 
 
1491
        event = (Event *)event->entry.next;
 
1492
        TEST_ALLOC_SIZE (event, sizeof (Event));
 
1493
        TEST_EQ_STR (event->name, "waggle");
 
1494
 
 
1495
        nih_list_free (&job->entry);
 
1496
 
 
1497
 
 
1498
        /* Check that multiple stop script stanzas results in a
 
1499
         * syntax error.
 
1500
         */
 
1501
        TEST_FEATURE ("with script and block");
 
1502
        jf = fopen (filename, "w");
 
1503
        fprintf (jf, "exec /sbin/daemon\n");
 
1504
        fprintf (jf, "stop script\n");
 
1505
        fprintf (jf, "    echo\n");
 
1506
        fprintf (jf, "end script\n");
 
1507
        fprintf (jf, "stop script\n");
 
1508
        fprintf (jf, "    ls\n");
 
1509
        fprintf (jf, "end script\n");
 
1510
        fclose (jf);
 
1511
 
 
1512
        TEST_DIVERT_STDERR (output) {
 
1513
                job = cfg_read_job (NULL, filename, "test");
 
1514
        }
 
1515
        rewind (output);
 
1516
 
 
1517
        TEST_EQ_P (job, NULL);
 
1518
 
 
1519
        TEST_ERROR_EQ (output, "6: Duplicate value\n");
 
1520
        TEST_FILE_END (output);
 
1521
 
 
1522
        TEST_FILE_RESET (output);
 
1523
 
 
1524
 
 
1525
        /* Check that a stop stanza without a second-level argument results
 
1526
         * in a syntax error.
 
1527
         */
 
1528
        TEST_FEATURE ("with missing argument");
 
1529
        jf = fopen (filename, "w");
 
1530
        fprintf (jf, "exec /sbin/daemon\n");
 
1531
        fprintf (jf, "stop\n");
 
1532
        fclose (jf);
 
1533
 
 
1534
        TEST_DIVERT_STDERR (output) {
 
1535
                job = cfg_read_job (NULL, filename, "test");
 
1536
        }
 
1537
        rewind (output);
 
1538
 
 
1539
        TEST_EQ_P (job, NULL);
 
1540
 
 
1541
        TEST_ERROR_EQ (output, "2: Expected token\n");
 
1542
        TEST_FILE_END (output);
 
1543
 
 
1544
        TEST_FILE_RESET (output);
 
1545
 
 
1546
 
 
1547
        /* Check that a stop stanza with an unknown second-level argument
 
1548
         * results in a syntax error.
 
1549
         */
 
1550
        TEST_FEATURE ("with unknown argument");
 
1551
        jf = fopen (filename, "w");
 
1552
        fprintf (jf, "exec /sbin/daemon\n");
 
1553
        fprintf (jf, "stop foo\n");
 
1554
        fclose (jf);
 
1555
 
 
1556
        TEST_DIVERT_STDERR (output) {
 
1557
                job = cfg_read_job (NULL, filename, "test");
 
1558
        }
 
1559
        rewind (output);
 
1560
 
 
1561
        TEST_EQ_P (job, NULL);
 
1562
 
 
1563
        TEST_ERROR_EQ (output, "2: Unknown stanza\n");
 
1564
        TEST_FILE_END (output);
 
1565
 
 
1566
        TEST_FILE_RESET (output);
 
1567
 
 
1568
 
 
1569
        /* Check that a stop on stanza without an argument results in a
 
1570
         * syntax error.
 
1571
         */
 
1572
        TEST_FEATURE ("with on and missing argument");
 
1573
        jf = fopen (filename, "w");
 
1574
        fprintf (jf, "exec /sbin/daemon\n");
 
1575
        fprintf (jf, "stop on\n");
 
1576
        fclose (jf);
 
1577
 
 
1578
        TEST_DIVERT_STDERR (output) {
 
1579
                job = cfg_read_job (NULL, filename, "test");
 
1580
        }
 
1581
        rewind (output);
 
1582
 
 
1583
        TEST_EQ_P (job, NULL);
 
1584
 
 
1585
        TEST_ERROR_EQ (output, "2: Expected token\n");
 
1586
        TEST_FILE_END (output);
 
1587
 
 
1588
        TEST_FILE_RESET (output);
 
1589
 
 
1590
 
 
1591
        /* Check that a stop on stanza with an extra argument results in a
 
1592
         * syntax error.
 
1593
         */
 
1594
        TEST_FEATURE ("with extra argument");
 
1595
        jf = fopen (filename, "w");
 
1596
        fprintf (jf, "exec /sbin/daemon\n");
 
1597
        fprintf (jf, "stop on foo bar\n");
 
1598
        fclose (jf);
 
1599
 
 
1600
        TEST_DIVERT_STDERR (output) {
 
1601
                job = cfg_read_job (NULL, filename, "test");
 
1602
        }
 
1603
        rewind (output);
 
1604
 
 
1605
        TEST_EQ_P (job, NULL);
 
1606
 
 
1607
        TEST_ERROR_EQ (output, "2: Unexpected token\n");
 
1608
        TEST_FILE_END (output);
 
1609
 
 
1610
        TEST_FILE_RESET (output);
 
1611
 
 
1612
 
 
1613
        /* Check that a stop script stanza with an extra argument results
 
1614
         * in a syntax error.
 
1615
         */
 
1616
        TEST_FEATURE ("with extra argument");
 
1617
        jf = fopen (filename, "w");
 
1618
        fprintf (jf, "exec /sbin/daemon\n");
 
1619
        fprintf (jf, "stop script foo\n");
 
1620
        fclose (jf);
 
1621
 
 
1622
        TEST_DIVERT_STDERR (output) {
 
1623
                job = cfg_read_job (NULL, filename, "test");
 
1624
        }
 
1625
        rewind (output);
 
1626
 
 
1627
        TEST_EQ_P (job, NULL);
 
1628
 
 
1629
        TEST_ERROR_EQ (output, "2: Unexpected token\n");
 
1630
        TEST_FILE_END (output);
 
1631
 
 
1632
        TEST_FILE_RESET (output);
 
1633
 
 
1634
 
 
1635
        fclose (output);
 
1636
}
 
1637
 
 
1638
void
1181
1639
test_stanza_pid (void)
1182
1640
{
1183
1641
        Job  *job;
1979
2437
        test_stanza_author ();
1980
2438
        test_stanza_emits ();
1981
2439
        test_stanza_on ();
 
2440
        test_stanza_start ();
 
2441
        test_stanza_stop ();
1982
2442
        test_stanza_pid ();
1983
2443
        test_stanza_kill ();
1984
2444
        test_stanza_normalexit ();