Skip to content

Commit

Permalink
update tests, pkghash
Browse files Browse the repository at this point in the history
  • Loading branch information
renerocksai committed Apr 22, 2023
1 parent fed4220 commit b7f3ae0
Show file tree
Hide file tree
Showing 7 changed files with 865 additions and 111 deletions.
25 changes: 21 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub fn build(b: *std.build.Builder) !void {
});

facil_lib.linkLibrary(facil_dep.artifact("facil.io"));
facil_lib.install();
// facil_lib.install();
const unused_facil_install_step = b.addInstallArtifact(facil_lib);
_ = unused_facil_install_step;

inline for ([_]struct {
name: []const u8,
Expand Down Expand Up @@ -73,10 +75,10 @@ pub fn build(b: *std.build.Builder) !void {
});

example.linkLibrary(facil_dep.artifact("facil.io"));

example.addModule("zap", zap_module);

const example_run = example.run();
// const example_run = example.run();
const example_run = b.addRunArtifact(example);
example_run_step.dependOn(&example_run.step);

// install the artifact - depending on the "example"
Expand All @@ -89,6 +91,7 @@ pub fn build(b: *std.build.Builder) !void {
//

// authenticating http client for internal testing
// (facil.io based, not used anymore)
//
var http_client_exe = b.addExecutable(.{
.name = "http_client",
Expand Down Expand Up @@ -128,6 +131,20 @@ pub fn build(b: *std.build.Builder) !void {
auth_tests.addModule("zap", zap_module);
auth_tests.step.dependOn(&http_client_runner_build_step.step);

const run_auth_tests = b.addRunArtifact(auth_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&auth_tests.step);
test_step.dependOn(&run_auth_tests.step);

// pkghash
//
var pkghash_exe = b.addExecutable(.{
.name = "pkghash",
.root_source_file = .{ .path = "./tools/pkghash.zig" },
.target = target,
.optimize = optimize,
});
var pkghash_step = b.step("pkghash", "Build pkghash");
const pkghash_build_step = b.addInstallArtifact(pkghash_exe);
pkghash_step.dependOn(&pkghash_build_step.step);
}
12 changes: 10 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

.dependencies = .{
.@"facil.io" = .{
.url = "https://github.com/zigzap/facil.io/archive/64a3fb6d66225d3ff6194e84623eb9d48bc3b6fb.tar.gz",
.hash = "1220da26a9450eb75ecdb93b5dd3dabfea53053734cb68c748f0426d445179bc7c92",
// temp workaround until zig's fetch is fixed, supporting GH's redirects
.url = "http://localhost:8000/zap-0.0.7.tar.gz",

// this is how it should be:
//.url = "https://github.com/zigzap/facil.io/archive/refs/tags/zap-0.0.7.tar.gz",
.hash = "1220d03e0579bbb726efb8224ea289b26227bc421158b45c1b16a60b31bfa400ab33",
// our tool says:
//.hash = "1220bbc4738c846f3253ae98e1848514f2ed1f02ecc1c7a62076b6508f449e9b5f66",


}
}
}
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 76 additions & 99 deletions src/test_auth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,66 +122,44 @@ fn endpoint_http_unauthorized(e: *Endpoints.SimpleEndpoint, r: zap.SimpleRequest
//
// http client code for in-process sending of http request
//
fn setHeader(h: [*c]fio.http_s, name: []const u8, value: []const u8) !void {
const hname: fio.fio_str_info_s = .{
.data = util.toCharPtr(name),
.len = name.len,
.capa = name.len,
};

const vname: fio.fio_str_info_s = .{
.data = util.toCharPtr(value),
.len = value.len,
.capa = value.len,
};
const ret = fio.http_set_header2(h, hname, vname);

if (ret == 0) return;
return zap.HttpError.HttpSetHeader;
}

fn sendRequest() void {
const ret = zap.http_connect("http://127.0.0.1:3000/test", null, .{
.on_response = on_response,
.on_request = null,
.on_upgrade = null,
.on_finish = null,
.udata = null,
.public_folder = null,
.public_folder_length = 0,
.max_header_size = 32 * 1024,
.max_body_size = 500 * 1024,
.max_clients = 1,
.tls = null,
.reserved1 = 0,
.reserved2 = 0,
.reserved3 = 0,
.ws_max_msg_size = 0,
.timeout = 5,
.ws_timeout = 0,
.log = 0,
.is_client = 1,
});
// _ = ret;
std.debug.print("\nret = {d}\n", .{ret});
zap.fio_start(.{ .threads = 1, .workers = 1 });
}
const ClientAuthReqHeaderFields = struct {
auth: Authenticators.AuthScheme,
token: []const u8,
};

fn on_response(r: [*c]fio.http_s) callconv(.C) void {
// the first time around, we need to complete the request. E.g. set headers.
if (r.*.status_str == zap.FIOBJ_INVALID) {
setHeader(r, "Authorization", "Bearer ABCDEFG") catch return;
zap.http_finish(r);
return;
}
const response = zap.http_req2str(r);
if (zap.fio2str(response)) |body| {
std.debug.print("{s}\n", .{body});
} else {
std.debug.print("Oops\n", .{});
fn makeRequest(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeaderFields) !void {
const uri = try std.Uri.parse(url);

var h = std.http.Headers{ .allocator = a };
defer h.deinit();

if (auth) |auth_fields| {
const authstring = try std.fmt.allocPrint(a, "{s}{s}", .{ auth_fields.auth.str(), auth_fields.token });
defer a.free(authstring);
try h.append(auth_fields.auth.headerFieldStrHeader(), authstring);
}

var http_client: std.http.Client = .{ .allocator = a };
defer http_client.deinit();

var req = try http_client.request(.GET, uri, h, .{});
defer req.deinit();

try req.start();
try req.do();
// var br = std.io.bufferedReaderSize(std.crypto.tls.max_ciphertext_record_len, req.reader());
// var buffer: [1024]u8 = undefined;
// we know we won't receive a lot
// const len = try br.reader().readAll(&buffer);
// std.debug.print("RESPONSE:\n{s}\n", .{buffer[0..len]});
zap.fio_stop();
}

fn makeRequestThread(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeaderFields) !std.Thread {
return try std.Thread.spawn(.{}, makeRequest, .{ a, url, auth });
}

//
// end of http client code
//
Expand All @@ -190,32 +168,6 @@ test "BearerAuthSingle authenticateRequest OK" {
const a = std.testing.allocator;
const token = "ABCDEFG";

//
// Unfortunately, spawning a child process confuses facilio:
//
// 1. attempt: spawn curl process before we start facilio threads
// this doesn't work: facilio doesn't start up if we spawn a child process
// var p = std.ChildProcess.init(&.{
// "bash",
// "-c",
// "sleep 10; curl -H \"Authorization: Bearer\"" ++ token ++ " http://localhost:3000/test -v",
// }, a);
// try p.spawn();

// 2. attempt:
// our custom client doesn't work either
// var p = std.ChildProcess.init(&.{
// "bash",
// "-c",
// "sleep 3; ./zig-out/bin/http_client &",
// }, a);
// try p.spawn();
// std.debug.print("done spawning\n", .{});

// 3. attempt: sending the request in-process
// this doesn't work either because facilio wants to be either server or client, gets confused, at least when we're doing it this way
// sendRequest();

// setup listener
var listener = zap.SimpleEndpointListener.init(
a,
Expand Down Expand Up @@ -248,10 +200,13 @@ test "BearerAuthSingle authenticateRequest OK" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("\n\n*******************************************\n", .{});
std.debug.print("\n\nPlease run the following:\n", .{});
std.debug.print("./zig-out/bin/http_client_runner\n", .{});
std.debug.print("\n\n*******************************************\n", .{});
// std.debug.print("\n\n*******************************************\n", .{});
// std.debug.print("\n\nPlease run the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client_runner\n", .{});
// std.debug.print("\n\n*******************************************\n", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = token });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -304,8 +259,11 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Bearer invalid\r", .{});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Bearer invalid\r", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = "invalid" });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -352,8 +310,11 @@ test "BearerAuthMulti authenticateRequest OK" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client_runner http://127.0.0.1:3000/test Bearer invalid\r", .{});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client_runner http://127.0.0.1:3000/test Bearer " ++ token ++ "\r", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = token });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -400,8 +361,11 @@ test "BearerAuthMulti authenticateRequest test-unauthorized" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client_runner http://127.0.0.1:3000/test Bearer invalid\r", .{});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client_runner http://127.0.0.1:3000/test Bearer invalid\r", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = "invalid" });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -453,8 +417,11 @@ test "BasicAuth Token68 authenticateRequest" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic " ++ token ++ "\r", .{});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic " ++ token ++ "\r", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = token });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -506,8 +473,11 @@ test "BasicAuth Token68 authenticateRequest test-unauthorized" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic " ++ "invalid\r", .{});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic " ++ "invalid\r", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = "invalid" });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -569,8 +539,11 @@ test "BasicAuth UserPass authenticateRequest" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic {s}\r", .{encoded});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic {s}\r", .{encoded});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = encoded });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down Expand Up @@ -619,6 +592,7 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" {
var encoder = std.base64.url_safe.Encoder;
var buffer: [256]u8 = undefined;
const encoded = encoder.encode(&buffer, token);
_ = encoded;

// create authenticator
const Authenticator = Authenticators.BasicAuth(Map, .UserPass);
Expand All @@ -632,8 +606,11 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" {
try listener.addEndpoint(auth_ep.getEndpoint());

listener.listen() catch {};
std.debug.print("Waiting for the following:\n", .{});
std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic {s}-invalid\r", .{encoded});
// std.debug.print("Waiting for the following:\n", .{});
// std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic invalid\r", .{});

const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = "invalid" });
defer thread.join();

// start worker threads
zap.start(.{
Expand Down
Loading

0 comments on commit b7f3ae0

Please sign in to comment.