1/* 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 */ 6 7// 8// Test for resource open before PPAPI initialization. 9// 10 11#include <stdio.h> 12#include <string.h> 13 14#include <string> 15#include <sstream> 16 17#include "native_client/src/untrusted/irt/irt.h" 18#include "native_client/src/untrusted/nacl/nacl_irt.h" 19 20#include "ppapi/cpp/instance.h" 21#include "ppapi/cpp/module.h" 22#include "ppapi/cpp/var.h" 23#include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" 24 25std::string str; 26 27void load_manifest(TYPE_nacl_irt_query *query_func) { 28 struct nacl_irt_resource_open nacl_irt_resource_open; 29 if (sizeof(nacl_irt_resource_open) != 30 (*query_func)( 31 NACL_IRT_RESOURCE_OPEN_v0_1, 32 &nacl_irt_resource_open, 33 sizeof(nacl_irt_resource_open))) { 34 str = "irt manifest api not found"; 35 return; 36 } 37 int desc; 38 int error; 39 error = nacl_irt_resource_open.open_resource("test_file", &desc); 40 if (0 != error) { 41 str = "Can't open file"; 42 printf("Can't open file, error=%d", error); 43 return; 44 } 45 46 str = "File Contents:\n"; 47 48 FILE *iob = fdopen(desc, "r"); 49 char buffer[4096]; 50 while (fgets(buffer, sizeof buffer, iob) != NULL) { 51 // NB: fgets does not discard the newline nor any carriage return 52 // character before that. 53 // 54 // Note that CR LF is the default end-of-line style for Windows. 55 // Furthermore, when the test_file (input data, which happens to 56 // be the nmf file) is initially created in a change list, the 57 // patch is sent to our try bots as text. This means that when 58 // the file arrives, it has CR LF endings instead of the original 59 // LF line endings. Since the expected or golden data is 60 // (manually) encoded in the HTML file's JavaScript, there will be 61 // a mismatch. After submission, the svn property svn:eol-style 62 // will be set to LF, so a clean check out should have LF and not 63 // CR LF endings, and the tests will pass without CR removal. 64 // However -- and there's always a however in long discourses -- 65 // if the nmf file is edited, say, because the test is being 66 // modified, and the modification is being done on a Windows 67 // machine, then it is likely that the editor used by the 68 // programmer will convert the file to CR LF endings. Which, 69 // unfortunatly, implies that the test will mysteriously fail 70 // again. 71 // 72 // To defend against such nonsense, we weaken the test slighty, 73 // and just strip the CR if it is present. 74 int len = strlen(buffer); 75 if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') { 76 buffer[len-2] = '\n'; 77 buffer[len-1] = '\0'; 78 } 79 str += buffer; 80 } 81 printf("file loaded: %s\n", str.c_str()); 82 fclose(iob); // closed desc 83 return; 84} 85 86class TestInstance : public pp::Instance { 87 public: 88 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {} 89 virtual ~TestInstance() {} 90 virtual void HandleMessage(const pp::Var& var_message) { 91 if (!var_message.is_string()) { 92 return; 93 } 94 if (var_message.AsString() != "hello") { 95 return; 96 } 97 pp::Var reply = pp::Var(str); 98 PostMessage(reply); 99 } 100}; 101 102class TestModule : public pp::Module { 103 public: 104 TestModule() : pp::Module() {} 105 virtual ~TestModule() {} 106 107 virtual pp::Instance* CreateInstance(PP_Instance instance) { 108 return new TestInstance(instance); 109 } 110}; 111 112namespace pp { 113Module* CreateModule() { 114 return new TestModule(); 115} 116} 117 118int main() { 119 load_manifest(&__nacl_irt_query); 120 return PpapiPluginMain(); 121} 122 123