summaryrefslogtreecommitdiffstats
path: root/source/tests/basic.c
blob: 4f34ba4b1fbb55ff0896afcd7605f84a59497c4c (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
#define ET_TEST_IMPL
#include "../test.h"
#include <inttypes.h>
#include <stdio.h>

#define TEST_ITERATIONS 16

struct test_with_iterations {
  struct et_test_state_private_base base;
  int iteration;
};

static int init_test_with_iterations(const struct et_state *state, void *data, et_state_init_data init_data) {
  struct test_with_iterations *priv = data;
  priv->iteration = 0;
  return 0;
}

static int check_id_tick(const struct et_state *state, void *data, struct et_next_state *next_state) {
  struct test_with_iterations *priv = data;
  struct et_test_plan *test_plan = priv->base.plan;
  struct et_test_plan_entry *cur = test_current(test_plan);
  et_test_status status = TEST_PASS;
  u16 command = 0x9000;

  if ((status = test_exi_readwrite(test_plan, &command, sizeof(command), TEST_EPRIV_BASE)) != TEST_PASS) {
    cur->status = status;
    return test_next(test_plan, next_state);
  }

  if (command != 0x0470) {
    test_plan_entry_set_error(cur, "bad identifier from gecko: expected 0x0470, got 0x%04" PRIx16, command);
    status = TEST_FAIL;
  }

  cur->status = status;

  if (status != TEST_PASS || ++priv->iteration >= TEST_ITERATIONS) {
    return test_next(test_plan, next_state);
  }

  return 0;
}

TEST_DEF(check_id, "Identify Gecko (0x09)", TEST_PAUSE_ON_ERROR, init_test_with_iterations, check_id_tick, NULL, struct test_with_iterations);

static int bad_commands_tick(const struct et_state *state, void *data, struct et_next_state *next_state) {
  struct test_with_iterations *priv = data;
  struct et_test_plan *test_plan = priv->base.plan;
  struct et_test_plan_entry *cur = test_current(test_plan);
  et_test_status status = TEST_PASS;

  static const u16 bad_commands[] = { 0x0000, 0x1000, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0xe000, 0xf000 };

  for (int i = 0; i < ARRLEN(bad_commands); ++i) {
    u16 orig_cmd = bad_commands[i];
    u16 cmd = orig_cmd;
    if ((status = test_exi_readwrite(test_plan, &cmd, sizeof(cmd), TEST_EPRIV_BASE)) != TEST_PASS) {
      goto test_fail;
    }

    if (cmd != 0x0000) {
      test_plan_entry_set_error(cur, "got bogus response from invalid command 0x%04" PRIx16 ": 0x%04" PRIx16, orig_cmd, cmd);
      status = TEST_FAIL;
      break;
    }
  }

test_fail:
  cur->status = status;

  if (status != TEST_PASS || ++priv->iteration >= TEST_ITERATIONS) {
    return test_next(test_plan, next_state);
  }

  return 0;
}

TEST_DEF(bad_commands, "Bad commands", 0, init_test_with_iterations, bad_commands_tick, NULL, struct test_with_iterations);