Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

View file

@ -0,0 +1,130 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "clar.h"
#include "console/cobs.h"
static CobsDecodeContext ctx;
static unsigned char out[1024];
static void decode_start(size_t length) {
cobs_streaming_decode_start(&ctx, out, length);
}
static void assert_decode_char_succeeds(char c) {
cl_assert(cobs_streaming_decode(&ctx, c));
}
static void assert_decode_succeeds(const char * restrict buf, size_t length) {
while (length--) {
assert_decode_char_succeeds(*buf++);
}
}
static void assert_decode_fails(const char * restrict buf, size_t length) {
bool result = true;
while (result && length--) {
result = result && cobs_streaming_decode(&ctx, *buf++);
}
cl_assert(result == false);
}
static void assert_buffer_not_touched(size_t index) {
cl_assert_equal_i(out[index], 0xcc);
}
static void assert_decode_completed(size_t expected_length) {
size_t decoded_length = cobs_streaming_decode_finish(&ctx);
cl_assert_equal_i(decoded_length, expected_length);
if (decoded_length != SIZE_MAX) {
assert_buffer_not_touched(decoded_length);
}
}
static void assert_output_equal(const void * restrict expected, size_t length) {
cl_assert(memcmp(out, expected, length) == 0);
}
void test_cobs_decode__initialize(void) {
memset(&ctx, 0xdd, sizeof(ctx));
memset(out, 0xcc, sizeof(out));
}
void test_cobs_decode__simple(void) {
const char vector[] = { 0x06, 'H', 'e', 'l', 'l', 'o' };
decode_start(5);
assert_decode_succeeds(vector, sizeof(vector));
assert_decode_completed(5);
assert_output_equal("Hello", 5);
}
void test_cobs_decode__zeroes(void) {
const char vector[] = { 0x01, 0x02, 'A', 0x01, 0x02, 'B', 0x01 };
decode_start(6);
assert_decode_succeeds(vector, sizeof(vector));
assert_decode_completed(6);
assert_output_equal("\0A\0\0B\0", 6);
}
void test_cobs_decode__max_length_block(void) {
decode_start(254);
assert_decode_char_succeeds(0xff);
for (int c = 0x01; c <= 0xfe; ++c) {
assert_decode_char_succeeds(c);
}
assert_decode_completed(254);
const unsigned char *iter = out;
for (int c = 0x01; c <= 0xfe; ++c) {
cl_assert_equal_i(*iter++, c);
}
}
void test_cobs_decode__empty_data(void) {
decode_start(100);
assert_decode_char_succeeds(0x01);
assert_decode_completed(0);
assert_buffer_not_touched(0);
}
void test_cobs_decode__output_too_small_1(void) {
const char vector[] = { 0x06, 'L', 'a', 'r', 'g', 'e', '.' };
decode_start(5);
assert_decode_fails(vector, sizeof(vector));
assert_decode_completed(SIZE_MAX);
assert_buffer_not_touched(6);
}
void test_cobs_decode__output_too_small_2(void) {
const char vector[] = { 0x05, 'a', 'b', 'c', 'd', 0x01, 0x01 };
decode_start(5);
assert_decode_fails(vector, sizeof(vector));
assert_decode_completed(SIZE_MAX);
assert_buffer_not_touched(6);
}
void test_cobs_decode__input_truncated(void) {
const char vector[] = { 0x05, 'a', 'b', 'c' };
decode_start(100);
assert_decode_succeeds(vector, sizeof(vector));
assert_decode_completed(SIZE_MAX);
}

View file

@ -0,0 +1,122 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "clar.h"
#include "console/cobs.h"
unsigned char out[1024];
static void assert_out_not_touched(size_t index) {
cl_assert_equal_i(out[index], 0xcc);
}
static void assert_out_equal(const void * restrict expected, size_t length) {
cl_assert(memcmp(out, expected, length) == 0);
}
static void assert_encode(const void * restrict src, size_t in_length,
size_t expected_length) {
size_t out_length = cobs_encode(out, src, in_length);
cl_assert_equal_i(out_length, expected_length);
if (expected_length != SIZE_MAX) {
assert_out_not_touched(expected_length);
}
}
void test_cobs_encode__initialize(void) {
memset(out, 0xcc, sizeof(out));
}
void test_cobs_encode__empty(void) {
assert_encode("", 0, 1);
assert_out_equal("\x01", 1);
}
void test_cobs_encode__zero(void) {
assert_encode("\0", 1, 2);
assert_out_equal("\x01\x01", 2);
}
void test_cobs_encode__simple(void) {
assert_encode("Hello", 5, 6);
assert_out_equal("\x06Hello", 6);
}
void test_cobs_encode__multiple_blocks(void) {
assert_encode("Hello\0w\0rld", 11, 12);
assert_out_equal("\x06Hello\x02w\x04rld", 12);
}
void test_cobs_encode__max_block_1(void) {
// vector[] = { 0x01, 0x02, ..., 0xfe };
uint8_t vector[254];
for (size_t i = 0; i < sizeof(vector); ++i) {
vector[i] = i + 1;
}
// expected[] = { 0xff, 0x01, 0x02, ..., 0xfe };
uint8_t expected[255];
expected[0] = 0xff;
for (size_t i = 1; i < sizeof(expected); ++i) {
expected[i] = i;
}
assert_encode(vector, sizeof(vector), sizeof(expected));
assert_out_equal(expected, sizeof(expected));
}
void test_cobs_encode__max_block_2(void) {
// vector[] = { 0x01, 0x02, ..., 0xfe, 0xff };
uint8_t vector[255];
for (size_t i = 0; i < sizeof(vector); ++i) {
vector[i] = i + 1;
}
// expected[] = { 0xff, 0x01, 0x02, ..., 0xfe, 0x02, 0xff };
uint8_t expected[257];
expected[0] = 0xff;
uint8_t c = 1;
for (int i = 1; i <= 255; ++i) {
expected[i] = c++;
}
expected[255] = 0x02;
expected[256] = 0xff;
assert_encode(vector, sizeof(vector), sizeof(expected));
assert_out_equal(expected, sizeof(expected));
}
void test_cobs_encode__max_block_3(void) {
// vector[] = { 0x00, 0x01, 0x02, 0x03, ..., 0xfe, 0xff };
uint8_t vector[256];
for (size_t i = 0; i < sizeof(vector); ++i) {
vector[i] = i;
}
// expected[] = { 0x01, 0xff, 0x01, 0x02, ..., 0xfe, 0x02, 0xff };
uint8_t expected[258];
expected[0] = 0x01;
expected[1] = 0xff;
uint8_t c = 1;
for (int i = 2; i <= 256; ++i) {
expected[i] = c++;
}
expected[256] = 0x02;
expected[257] = 0xff;
assert_encode(vector, sizeof(vector), sizeof(expected));
assert_out_equal(expected, sizeof(expected));
}

13
tests/fw/console/wscript Normal file
View file

@ -0,0 +1,13 @@
from waftools.pebble_test import clar
def build(ctx):
clar(ctx,
sources_ant_glob = "src/fw/console/cobs.c",
test_sources_ant_glob = "test_cobs_decode.c")
clar(ctx,
sources_ant_glob = "src/fw/console/cobs.c",
test_sources_ant_glob = "test_cobs_encode.c")
# vim:filetype=python