github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/librclone/ctest/ctest.c (about)

     1  /*
     2  This is a very simple test/demo program for librclone's C interface
     3  */
     4  #include <stdio.h>
     5  #include <stdlib.h>
     6  #include <string.h>
     7  #include <dlfcn.h>
     8  #include "librclone.h"
     9  
    10  void testRPC(char *method, char *in) {
    11      struct RcloneRPCResult out = RcloneRPC(method, in);
    12      printf("status: %d\n", out.Status);
    13      printf("output: %s\n", out.Output);
    14      free(out.Output);
    15  }
    16  
    17  // noop command
    18  void testNoOp() {
    19      printf("test rc/noop\n");
    20      struct RcloneRPCResult out = RcloneRPC("rc/noop", "{"
    21              " \"p1\": [1,\"2\",null,4],"
    22              " \"p2\": { \"a\":1, \"b\":2 } "
    23              "}");
    24      printf("status: %d\n", out.Status);
    25      printf("output: %s\n", out.Output);
    26      const char *expected =
    27          "{\n"
    28          "\t\"p1\": [\n"
    29          "\t\t1,\n"
    30          "\t\t\"2\",\n"
    31          "\t\tnull,\n"
    32          "\t\t4\n"
    33          "\t],\n"
    34          "\t\"p2\": {\n"
    35          "\t\t\"a\": 1,\n"
    36          "\t\t\"b\": 2\n"
    37          "\t}\n"
    38          "}\n";
    39      if (strcmp(expected, out.Output) != 0) {
    40          fprintf(stderr, "Wrong output.\nWant:\n%s\nGot:\n%s\n", expected, out.Output);
    41          exit(EXIT_FAILURE);
    42      }
    43      if (out.Status != 200) {
    44          fprintf(stderr, "Wrong status: want: %d: got: %d\n", 200, out.Status);
    45          exit(EXIT_FAILURE);
    46      }
    47      free(out.Output);
    48  }
    49  
    50  // error command
    51  void testError() {
    52      printf("test rc/error\n");
    53      struct RcloneRPCResult out = RcloneRPC("rc/error",
    54              "{"
    55              " \"p1\": [1,\"2\",null,4],"
    56              " \"p2\": { \"a\":1, \"b\":2 } "
    57              "}");
    58      printf("status: %d\n", out.Status);
    59      printf("output: %s\n", out.Output);
    60      const char *expected =
    61          "{\n"
    62          "\t\"error\": \"arbitrary error on input map[p1:[1 2 \\u003cnil\\u003e 4] p2:map[a:1 b:2]]\",\n"
    63          "\t\"input\": {\n"
    64          "\t\t\"p1\": [\n"
    65          "\t\t\t1,\n"
    66          "\t\t\t\"2\",\n"
    67          "\t\t\tnull,\n"
    68          "\t\t\t4\n"
    69          "\t\t],\n"
    70          "\t\t\"p2\": {\n"
    71          "\t\t\t\"a\": 1,\n"
    72          "\t\t\t\"b\": 2\n"
    73          "\t\t}\n"
    74          "\t},\n"
    75          "\t\"path\": \"rc/error\",\n"
    76          "\t\"status\": 500\n"
    77          "}\n";
    78      if (strcmp(expected, out.Output) != 0) {
    79          fprintf(stderr, "Wrong output.\nWant:\n%s\nGot:\n%s\n", expected, out.Output);
    80          exit(EXIT_FAILURE);
    81      }
    82      if (out.Status != 500) {
    83          fprintf(stderr, "Wrong status: want: %d: got: %d\n", 500, out.Status);
    84          exit(EXIT_FAILURE);
    85      }
    86      free(out.Output);
    87  }
    88  
    89  // copy file using "operations/copyfile" command
    90  void testCopyFile() {
    91      printf("test operations/copyfile\n");
    92      testRPC("operations/copyfile",
    93              "{"
    94                  "\"srcFs\": \"/tmp\","
    95                  "\"srcRemote\": \"tmpfile\","
    96                  "\"dstFs\": \"/tmp\","
    97                  "\"dstRemote\": \"tmpfile2\""
    98              "}");
    99  }
   100  
   101  // list the remotes
   102  void testListRemotes() {
   103      printf("test operations/listremotes\n");
   104      testRPC("config/listremotes", "{}");
   105  }
   106  
   107  int main(int argc, char** argv) {
   108      printf("c main begin\n");
   109      RcloneInitialize();
   110  
   111      testNoOp();
   112      testError();
   113      /* testCopyFile(); */
   114      /* testListRemotes(); */
   115  
   116      /* testRPC("config/setpath", "{\"path\":\"/tmp/rclone.conf\"}"); */
   117      /* testRPC("config/listremotes", "{}"); */
   118  
   119      RcloneFinalize();
   120      return EXIT_SUCCESS;
   121  }