cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

append.c (2544B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include <CUnit/CUnit.h>
     21#include <guacamole/parser.h>
     22
     23#include <stdio.h>
     24#include <stdlib.h>
     25#include <unistd.h>
     26
     27/**
     28 * Test which verifies that guac_parser correctly parses Guacamole instructions
     29 * from arbitrary blocks of data passed to guac_parser_append().
     30 */
     31void test_parser__append() {
     32
     33    /* Allocate parser */
     34    guac_parser* parser = guac_parser_alloc();
     35    CU_ASSERT_PTR_NOT_NULL_FATAL(parser);
     36
     37    /* Instruction input */
     38    char buffer[] = "4.test,8.testdata,5.zxcvb,13.guacamoletest;XXXXXXXXXXXXXXXXXX";
     39    char* current = buffer;
     40
     41    /* While data remains */
     42    int remaining = sizeof(buffer)-1;
     43    while (remaining > 18) {
     44
     45        /* Parse more data */
     46        int parsed = guac_parser_append(parser, current, remaining);
     47        if (parsed == 0)
     48            break;
     49
     50        current += parsed;
     51        remaining -= parsed;
     52
     53    }
     54
     55    /* Parse of instruction should be complete */
     56    CU_ASSERT_EQUAL(remaining, 18);
     57    CU_ASSERT_EQUAL(parser->state, GUAC_PARSE_COMPLETE);
     58
     59    /* Parse is complete - no more data should be read */
     60    CU_ASSERT_EQUAL(guac_parser_append(parser, current, 18), 0);
     61    CU_ASSERT_EQUAL(parser->state, GUAC_PARSE_COMPLETE);
     62
     63    /* Validate resulting structure */
     64    CU_ASSERT_EQUAL(parser->argc, 3);
     65    CU_ASSERT_PTR_NOT_NULL_FATAL(parser->opcode);
     66    CU_ASSERT_PTR_NOT_NULL_FATAL(parser->argv[0]);
     67    CU_ASSERT_PTR_NOT_NULL_FATAL(parser->argv[1]);
     68    CU_ASSERT_PTR_NOT_NULL_FATAL(parser->argv[2]);
     69
     70    /* Validate resulting content */
     71    CU_ASSERT_STRING_EQUAL(parser->opcode,  "test");
     72    CU_ASSERT_STRING_EQUAL(parser->argv[0], "testdata");
     73    CU_ASSERT_STRING_EQUAL(parser->argv[1], "zxcvb");
     74    CU_ASSERT_STRING_EQUAL(parser->argv[2], "guacamoletest");
     75
     76}
     77