dial spec 1.7

This commit is contained in:
jcli
2015-10-20 16:26:28 -07:00
commit 3454a59e99
41 changed files with 6174 additions and 0 deletions

18
server/tests/makefile Normal file
View File

@@ -0,0 +1,18 @@
CC=$(TARGET)gcc
.PHONY: clean
.DEFAULT_GOAL=test
OBJS := test_dial_data.o test_url_lib.o ../url_lib.o ../dial_data.o run_tests.o
HEADERS := $(wildcard ../*.h)
%.c: $(HEADERS)
%.o: %.c $(HEADERS)
$(CC) -Wall -Werror -g -std=gnu99 $(CFLAGS) -c $*.c -o $*.o
test: $(OBJS)
$(CC) -Wall -Werror -g $(OBJS) -ldl -lpthread -o run_tests
clean:
rm -f *.o run_tests

42
server/tests/run_tests.c Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2014 Netflix, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETFLIX, INC. AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NETFLIX OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "test_dial_data.h"
#include "test_url_lib.h"
#include <stdio.h>
int main(int argc, char** argv) {
printf("====\n");
test_smartstrcat();
test_urldecode();
test_parse_app_name();
test_parse_params();
printf("====\n");
test_read_dial_data();
test_write_dial_data();
printf("====\n");
return 0;
}

53
server/tests/test.h Normal file
View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2014 Netflix, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETFLIX, INC. AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NETFLIX OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Macros to simplify testing functions.
#ifndef SRC_SERVER_TESTS_TEST_H_
#define SRC_SERVER_TESTS_TEST_H_
#define EXPECT(a, m) \
do { \
if (!a) { \
printf("[%s] failed: %s\n", #a, m); \
printf("%s -> FAILED\n", __func__); \
return; \
} \
} while (0)
#define EXPECT_STREQ(a, b) \
do { \
if (strcmp(a, b)) { \
printf("expected [%s == %s]\n", #a, #b); \
printf(" a = \"%s\"\n", a); \
printf(" b = \"%s\"\n", b); \
printf("%s -> FAILED\n", __func__); \
return; \
} \
} while (0)
#define DONE() \
printf("%s -> OK\n", __func__)
#endif /* SRC_SERVER_TESTS_TEST_H_ */

View File

@@ -0,0 +1,57 @@
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>DIAL Security Test CORS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function(){
var makePost=function(app){
console.log("make dial post...");
var ip = $("#ipAddress").val();
var port = $("#dialPort").val();
var urlStr = "http://"+ip+":"+port+"/apps/"+app;
console.log(urlStr);
$("#status").text("posted to "+urlStr);
$.ajax({
type: "POST",
url: urlStr,
data: "v=QH2-TGUlwu4",
dataType: "text/plain; charset=\"utf-8\""
});
};
$( "#testNetflix" ).click(function(){
console.log("testing Netflix");
makePost("Netflix");
});
$( "#testYoutube" ).click(function(){
console.log("testing Youtube");
makePost("Youtube");
});
});
</script>
</head>
<body>
<input id="ipAddress" type="text" name="ip" value="192.168.1.100">
<input id="dialPort" type="text" name="port" value="8060">
<p>
<button id="testNetflix">Test Netflix</button>
<button id="testYoutube">Test Youtube</button>
</p>
<p>
<div id="status"></div>
</p>
</body>
</html>

71
server/tests/test_cors.sh Executable file
View File

@@ -0,0 +1,71 @@
#!/bin/bash
if [ $# -eq 0 ]; then
echo "usage: `basename $0`: <DIAL server ip address> <port> "
exit 1
fi
ip_address=$1
port=$2
#Testing all the positive cases
origins="http://www4.netflix.com http://1.netflix.com https://www.netflix.com https://www4.netflix.com ftp://this.is.fine"
for origin in $origins; do
curl --fail --silent --header "Origin:$origin" --data "v=QH2-TGUlwu4" http://$ip_address:$port/apps/Netflix || echo "failed: $origin should be accepted"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/Netflix || echo "failed: $origin should be accepted"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/Netflix/run || echo "failed: $origin should be accepted"
if [ $ip_address == "localhost" ];
then
echo "testing dial_data OPTIONS on $ip_address from origin $origin"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/Netflix/dial_data || echo "failed: $origin should be accepted"
fi
done
origins="http://www4.youtube.com http://1.youtube.com https://www.youtube.com https://www4.youtube.com ftp://this.is.fine"
for origin in $origins; do
curl --fail --silent --header "Origin:$origin" --data "v=QH2-TGUlwu4" http://$ip_address:$port/apps/YouTube || echo "failed: $origin should be accepted"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/YouTube || echo "failed: $origin should be accepted"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/YouTube/run || echo "failed: $origin should be accepted"
if [ $ip_address == "localhost" ];
then
echo "testing dial_data OPTIONS on $ip_address from origin $origin"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/YouTube/dial_data || echo "failed: $origin should be accepted"
fi
done
#Testing all the negative cases
origins="http://www.netflix-a.com http://www.netflix.com4 http://a-netflix.com https://ww.netflix-a.com https://www.netflix.com4 https://a-netflix.com http://netflix.com http://www.attack.com https://www.attack.com file://www.attack.com"
for origin in $origins; do
curl --fail --silent --header "Origin:$origin" --data "v=QH2-TGUlwu4" http://$ip_address:$port/apps/Netflix && echo "failed: $origin should be rejected"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/Netflix && echo "failed: $origin should be rejected"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/Netflix/run && echo "failed: $origin should be rejected"
if [ $ip_address == "localhost" ];
then
echo "testing dial_data OPTIONS on $ip_address from origin $origin"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/Netflix/dial_data && echo "failed: $origin should be rejected"
fi
done
origins="http://www.youtube-a.com http://www.youtube.com4 http://a-youtube.com https://ww.youtube-a.com https://www.youtube.com4 https://a-youtube.com http://youtube.com https://youtube.com http://www.attack.com https://www.attack.com file://www.attack.com"
for origin in $origins; do
curl --fail --silent --header "Origin:$origin" --data "v=QH2-TGUlwu4" http://$ip_address:$port/apps/YouTube && echo "failed: $origin should be rejected"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/YouTube && echo "failed: $origin should be rejected"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/YouTube/run && echo "failed: $origin should be rejected"
if [ $ip_address == "localhost" ];
then
echo "testing dial_data OPTIONS on $ip_address from origin $origin"
curl --fail --silent --header "Origin:$origin" -X OPTIONS http://$ip_address:$port/apps/YouTube/dial_data && echo "failed: $origin should be rejected"
fi
done
#Finally test with no header
curl --fail --silent --data "v=QH2-TGUlwu4" http://$ip_address:$port/apps/YouTube || echo "failed: request without an Origin should be accepted"
curl --fail --silent -X OPTIONS http://$ip_address:$port/apps/YouTube || echo "failed: request without an Origin should be accepted"
curl --fail --silent -X OPTIONS http://$ip_address:$port/apps/YouTube/run || echo "failed: request without an Origin should be accepted"
if [ $ip_address == "localhost" ];
then
echo "testing dial_data OPTIONS on $ip_address with no origin"
curl --fail --silent -X OPTIONS http://$ip_address:$port/apps/YouTube/dial_data || echo "failed: request without an Origin should be accepted"
fi
echo "Done."

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2014 Netflix, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETFLIX, INC. AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NETFLIX OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../dial_data.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "test.h"
int key_value_pairs = 3;
char *keys[] = {"key1", "key2", "key3"};
char *values[] = {"value1", "value2", "value3"};
void test_read_dial_data() {
DIALData *data = retrieve_dial_data("dial_data");
for (int i = 0; data != NULL; data = data->next, i++) {
EXPECT_STREQ(data->key, keys[2 - i]);
EXPECT_STREQ(data->value, values[2 - i]);
}
DONE();
}
void test_write_dial_data() {
DIALData *result = NULL;
for (int i = 0; i < key_value_pairs; ++i) {
DIALData *node = (DIALData *) malloc(sizeof(DIALData));
node->key = keys[i];
node->value = values[i];
node->next = result;
result = node;
}
store_dial_data("YouTube", result);
DIALData *readBack = retrieve_dial_data("YouTube");
for (int i = 0; readBack != NULL; readBack = readBack->next, i++) {
EXPECT_STREQ(readBack->key, keys[i]);
EXPECT_STREQ(readBack->value, values[i]);
}
DONE();
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2014 Netflix, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETFLIX, INC. AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NETFLIX OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SRC_SERVER_TESTS_TEST_DIAL_DATA_H_
#define SRC_SERVER_TESTS_TEST_DIAL_DATA_H_
void test_read_dial_data();
void test_write_dial_data();
#endif /* SRC_SERVER_TESTS_TEST_DIAL_DATA_H_ */

113
server/tests/test_url_lib.c Normal file
View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2014 Netflix, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETFLIX, INC. AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NETFLIX OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../url_lib.h"
#include "../dial_data.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "test.h"
void test_smartstrcat() {
char* src1 = "Hello ";
char* src2 = "world!";
char* src3 = " Trunc ated";
char dest[128] = {0, };
char* p = (char *) dest;
p = smartstrcat(p, src1, 128);
EXPECT_STREQ(dest, "Hello ");
p = smartstrcat(p, src2, dest + 128 - p);
EXPECT_STREQ(dest, "Hello world!");
p = smartstrcat(p, src3, 6);
EXPECT_STREQ(dest, "Hello world! Trunc");
DONE();
}
void test_urldecode() {
char* param = "%26bla+r";
char dest[128] = {0, };
EXPECT(urldecode(dest, param, 128), "Failed to decode.");
EXPECT_STREQ(dest, "&bla r");
DONE();
}
void test_parse_app_name() {
char *app_name;
EXPECT((app_name = parse_app_name(NULL)), "Failed to extract app_name");
EXPECT_STREQ(app_name, "unknown");
EXPECT((app_name = parse_app_name("")), "Failed to extract app_name");
EXPECT_STREQ(app_name, "unknown");
EXPECT((app_name = parse_app_name("/")), "Failed to extract app_name");
EXPECT_STREQ(app_name, "unknown");
EXPECT((app_name = parse_app_name("/apps/YouTube/DialData")),
"Failed to extract app_name");
EXPECT_STREQ(app_name, "YouTube");
EXPECT((app_name = parse_app_name("//")), "Failed to extract app_name");
EXPECT_STREQ(app_name, "");
EXPECT((app_name = parse_app_name("/invalid")),
"Failed to extract app_name");
EXPECT_STREQ(app_name, "unknown");
DONE();
}
void test_parse_params() {
EXPECT(!parse_params(""), "Empty query string should generate no params");
EXPECT(!parse_params(NULL), "Null query, should generate no params");
DIALData *result = parse_params("a=b");
EXPECT_STREQ(result->key, "a");
EXPECT_STREQ(result->value, "b");
result = parse_params("?a=b");
EXPECT_STREQ(result->key, "a");
EXPECT_STREQ(result->value, "b");
result = parse_params("?a=b&c=d");
EXPECT_STREQ(result->key, "c");
EXPECT_STREQ(result->value, "d");
EXPECT_STREQ(result->next->key, "a");
EXPECT_STREQ(result->next->value, "b");
char query_string[1024] = {0, };
char *current = query_string;
for (int i = 0; i < 25; ++i) {
current = smartstrcat(current, "a=b&", 256);
}
result = parse_params(query_string);
int length = 0;
for (; result != NULL; result = result->next) {
length++;
}
EXPECT((length == 25), "25 params should have been parsed");
DONE();
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2014 Netflix, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETFLIX, INC. AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NETFLIX OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SRC_SERVER_TESTS_TEST_URL_LIB_H_
#define SRC_SERVER_TESTS_TEST_URL_LIB_H_
void test_smartstrcat();
void test_urldecode();
void test_parse_app_name();
void test_parse_param();
void test_parse_params();
#endif /* SRC_SERVER_TESTS_TEST_URL_LIB_H_ */