~jamesodhunt/upstart/bug-530779-with-formatting-changes

« back to all changes in this revision

Viewing changes to init/tests/test_parse_job.c

  • Committer: Lukáš Nykrýn
  • Date: 2012-02-16 15:45:41 UTC
  • mto: This revision was merged to the branch mainline in revision 1358.
  • Revision ID: lnykryn@redhat.com-20120216154541-f1ngijj35s5dhgmi
Add usage stanza (Petr Lautrbach <plautrba@redhat.com>)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8197
8197
        nih_free (err);
8198
8198
}
8199
8199
 
 
8200
void
 
8201
test_stanza_usage (void)
 
8202
{
 
8203
        JobClass*job;
 
8204
        NihError *err;
 
8205
        size_t    pos, lineno;
 
8206
        char      buf[1024];
 
8207
 
 
8208
        TEST_FUNCTION ("stanza_usage");
 
8209
 
 
8210
        /* Check that a usage stanza with an argument results in it
 
8211
         * being stored in the job.
 
8212
         */
 
8213
        TEST_FEATURE ("with single argument");
 
8214
        strcpy (buf, "usage \"stanza usage test message\"\n");
 
8215
 
 
8216
        TEST_ALLOC_FAIL {
 
8217
                pos = 0;
 
8218
                lineno = 1;
 
8219
                job = parse_job (NULL, NULL, NULL, "test", buf, strlen (buf),
 
8220
                                 &pos, &lineno);
 
8221
 
 
8222
                if (test_alloc_failed) {
 
8223
                        TEST_EQ_P (job, NULL);
 
8224
 
 
8225
                        err = nih_error_get ();
 
8226
                        TEST_EQ (err->number, ENOMEM);
 
8227
                        nih_free (err);
 
8228
 
 
8229
                        continue;
 
8230
                }
 
8231
 
 
8232
                TEST_EQ (pos, strlen (buf));
 
8233
                TEST_EQ (lineno, 2);
 
8234
 
 
8235
                TEST_ALLOC_SIZE (job, sizeof (JobClass));
 
8236
 
 
8237
                TEST_ALLOC_PARENT (job->usage, job);
 
8238
                TEST_EQ_STR (job->usage, "stanza usage test message");
 
8239
 
 
8240
                nih_free (job);
 
8241
        }
 
8242
 
 
8243
 
 
8244
        /* Check that the last of multiple usage stanzas is used.
 
8245
         */
 
8246
        TEST_FEATURE ("with multiple stanzas");
 
8247
        strcpy (buf, "usage \"stanza usage original\"\n");
 
8248
        strcat (buf, "usage \"stanza usage test message\"\n");
 
8249
 
 
8250
        TEST_ALLOC_FAIL {
 
8251
                pos = 0;
 
8252
                lineno = 1;
 
8253
                job = parse_job (NULL, NULL, NULL, "test", buf, strlen (buf),
 
8254
                                 &pos, &lineno);
 
8255
 
 
8256
                if (test_alloc_failed) {
 
8257
                        TEST_EQ_P (job, NULL);
 
8258
 
 
8259
                        err = nih_error_get ();
 
8260
                        TEST_EQ (err->number, ENOMEM);
 
8261
                        nih_free (err);
 
8262
 
 
8263
                        continue;
 
8264
                }
 
8265
 
 
8266
                TEST_EQ (pos, strlen (buf));
 
8267
                TEST_EQ (lineno, 3);
 
8268
 
 
8269
                TEST_ALLOC_SIZE (job, sizeof (JobClass));
 
8270
 
 
8271
                TEST_ALLOC_PARENT (job->usage, job);
 
8272
                TEST_EQ_STR (job->usage, "stanza usage test message");
 
8273
 
 
8274
                nih_free (job);
 
8275
        }
 
8276
 
 
8277
 
 
8278
        /* Check that a usage stanza without an argument results in
 
8279
         * a syntax error.
 
8280
         */
 
8281
        TEST_FEATURE ("with missing argument");
 
8282
        strcpy (buf, "usage\n");
 
8283
 
 
8284
        pos = 0;
 
8285
        lineno = 1;
 
8286
        job = parse_job (NULL, NULL, NULL, "test", buf, strlen (buf), &pos, &lineno);
 
8287
 
 
8288
        TEST_EQ_P (job, NULL);
 
8289
 
 
8290
        err = nih_error_get ();
 
8291
        TEST_EQ (err->number, NIH_CONFIG_EXPECTED_TOKEN);
 
8292
        TEST_EQ (pos, 5);
 
8293
        TEST_EQ (lineno, 1);
 
8294
        nih_free (err);
 
8295
 
 
8296
 
 
8297
        /* Check that a usage stanza with an extra second argument
 
8298
         * results in a syntax error.
 
8299
         */
 
8300
        TEST_FEATURE ("with extra argument");
 
8301
        strcpy (buf, "usage stanza usage test message\n");
 
8302
 
 
8303
        pos = 0;
 
8304
        lineno = 1;
 
8305
        job = parse_job (NULL, NULL, NULL, "test", buf, strlen (buf), &pos, &lineno);
 
8306
 
 
8307
        TEST_EQ_P (job, NULL);
 
8308
 
 
8309
        err = nih_error_get ();
 
8310
        TEST_EQ (err->number, NIH_CONFIG_UNEXPECTED_TOKEN);
 
8311
        TEST_EQ (pos, 13);
 
8312
        TEST_EQ (lineno, 1);
 
8313
        nih_free (err);
 
8314
}
 
8315
 
8200
8316
int
8201
8317
main (int   argc,
8202
8318
      char *argv[])
8245
8361
        test_stanza_chdir ();
8246
8362
        test_stanza_setuid ();
8247
8363
        test_stanza_setgid ();
 
8364
        test_stanza_usage ();
8248
8365
 
8249
8366
        return 0;
8250
8367
}